Docker images, code, buildspecs, and guidance provided as proof of concept only and for educational purposes only.
This repository demonstrates a proof of concept (PoC) of running Stable Diffusion Models on AWS Lambda, showcasing the potential of serverless architecture in handling text-to-image and image-to-image generative AI tasks. This PoC is primarily educational and experimental, aiming to bridge the gap between theory and practical implementation.
Utilizing the quantization advancements from the GGML Project and the ported diffusion models in stable-diffusion.cpp, this PoC enables the deployment of generative AI models on AWS Lambda. By employing models optimized for 256x256 or 128x128 image generation like MiniSD and stable-diffusion-nano, we significantly reduce image generation time and computational resources. The XBRZ library further allows efficient image upscaling up to 7x, making this serverless setup a scalable solution for image generation tasks.
Before diving in, ensure you have:
- Python 3.10+ and pip
- AWS CDK
- Docker
- An AWS account with necessary credentials
- Clone this repository.
- Navigate to the
stablediffusion_lambdafolder:cd stablediffusion-on-lambda/stablediffusion_lambda
- Clone this repository
- CD into this repository and then into the stablediffusion_lambda folder
cd stablediffusion-on-lambda/stablediffusion_lambda
- Set up a virtual environment, activate it, and install dependencies:
python3 -m venv .venv
source .venv/bin/activate
pip3 install -r requirements.txt
- Deploy the architecture
cdk deploy
Upon successful deployment, you'll receive a URL to your FastAPI endpoint documentation, where you can interact with the deployed model.
Interact with your endpoint using the provided Lambda Function URL. If you'd like to use a different model, go to the dockerfile in the stablediffusion_cpp_docker folder and modify the FOUNDATIONMODEL to your model url, and IMAGE_DIMENSION to match the default image dimension of the model you're using. This repo recommends 128x128 or 256x256 models, as 512x512 models and up take significant time and may have to tweak the steps value to fit within the 15 minute runtime of the Lambda function.
Generates an image based on the provided parameters.
mode(str, default: "txt2img"): Generation mode (txt2imgorimg2img).threads(int, default: -1): Number of threads to use during computation.model(str, default:MODELPATH): Path to model.init_img(str, optional): S3 location of the input image, required byimg2img.prompt(str): The prompt to render.negative_prompt(str, default: ""): The negative prompt.cfg_scale(float, default: 7.0): Unconditional guidance scale.strength(float, default: 0.75): Strength for noising/unnoising.scale(int, default: 4): How large you wish to scale the image, default 4x.sampling_method(str, default: "euler_a"): Sampling method.steps(int, default: 10): Number of sample steps.rng(str, default: "cuda"): RNG.seed(int, default: -1): RNG seed.schedule(str, default: "discrete"): Denoiser sigma schedule.verbose(bool, default: False): Print extra info.
curl -X POST "https://your-api-url/execute" \
-d "mode=txt2img&prompt=Your Prompt Here"
Uploads a file to the specified S3 bucket.
Parameters: file (file): The file to be uploaded. Example Request:
curl -X POST "https://your-api-url/upload" \
-F "[email protected]"
Checks the health of the API.
Example Request:
curl "https://your-api-url/healthcheck"
The /execute endpoint returns a presigned URL to the generated image in the S3 bucket:
{
"presigned_url": "https://your-s3-bucket.s3.amazonaws.com/generated-image.png?AWSAccessKeyId=..."
}
The /upload endpoint returns the unique filename of the uploaded file in the S3 bucket:
{
"unique_filename": "unique-filename.jpg"
}
The /healthcheck endpoint returns a status indicating the health of the API:
{
"status": "OK"
}
This project is under the Apache 2 license. Please check the licenses for XBRZ and any models you use. Contributions are welcome; see CONTRIBUTION.md for details.
