From-scratch implementation of a Stable Diffusion model, divided into two main parts: the model_package
for the model implementation and packaging, and the serving_api
for deployment of the model via a FastAPI interface.
stable-diffusion/
│
├── model_package/
│ ├── sd_model/
│ │ ├── config/
│ │ ├── data/
│ │ ├── model/
│ │ │ ├── clip.py
│ │ │ ├── sampler.py
│ │ │ ├── unet_diffusion.py
│ │ │ └── vae.py
│ │ ├── util/
│ │ ├── finetuning.py
│ │ └── predict.py
│ ├── tests/
│ ├── requirements/
│ ├── setup.py
│ └── tox.ini
│
└── serving_api/
├── app/
│ ├── api.py
│ ├── config.py
│ ├── main.py
│ ├── schemas/
│ └── tests/
├── Procfile
├── requirements.txt
└── tox.ini
Model can be deployed via CI/CD pipeline implemented in Jenkinsfile (currently its Railway deployment). If one wants to test API locally:
- Navigate to the
serving_api
directory:
cd ../serving_api
- Start the FastAPI server:
uvicorn app.main:app --host 0.0.0.0 --port 8001
- The API will be accessible at
http://localhost:8001
.
POST /api/v1/generate
: Generate an image from a text prompt.
Example payload:
{
"inputs": [
{
"config": {
"mode": "text_to_image",
"prompt": "A beautiful painting of a sunset over a mountain range",
"uncond_prompt": "",
"strength": 0.75,
"do_cfg": True,
"cfg_scale": 7.5,
"num_inference_steps": 50,
"seed": 42,
"device": "cuda",
"idle_device": "cpu"
}
}
]
}
Configuration files for the model are located in the model_package/sd_model/config
directory. Adjust the base_config.yaml
file as needed for your setup.
This project is licensed under the MIT License - see the LICENSE file for details.