{"id":21501179,"url":"https://github.com/rapidrabbit76/stable-diffusion-api","last_synced_at":"2025-07-15T22:32:17.675Z","repository":{"id":63538493,"uuid":"536225330","full_name":"rapidrabbit76/stable-diffusion-API","owner":"rapidrabbit76","description":"Unofficial Fastapi implementation of Stable-Diffusion API","archived":false,"fork":false,"pushed_at":"2022-12-09T11:50:59.000Z","size":6152,"stargazers_count":47,"open_issues_count":1,"forks_count":10,"subscribers_count":3,"default_branch":"main","last_synced_at":"2023-04-24T16:41:37.791Z","etag":null,"topics":["fastapi","generative-model","image2image","python","pytorch","stable-diffusion","text2image","unofficial-api"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/rapidrabbit76.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-09-13T16:56:51.000Z","updated_at":"2023-04-24T14:56:50.000Z","dependencies_parsed_at":"2023-01-26T06:01:29.699Z","dependency_job_id":null,"html_url":"https://github.com/rapidrabbit76/stable-diffusion-API","commit_stats":null,"previous_names":[],"tags_count":null,"template":null,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidrabbit76%2Fstable-diffusion-API","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidrabbit76%2Fstable-diffusion-API/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidrabbit76%2Fstable-diffusion-API/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rapidrabbit76%2Fstable-diffusion-API/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rapidrabbit76","download_url":"https://codeload.github.com/rapidrabbit76/stable-diffusion-API/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":226077516,"owners_count":17570163,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["fastapi","generative-model","image2image","python","pytorch","stable-diffusion","text2image","unofficial-api"],"created_at":"2024-11-23T17:49:47.805Z","updated_at":"2024-11-23T17:49:48.410Z","avatar_url":"https://github.com/rapidrabbit76.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Unofficial Fastapi implementation Stable-Diffusion API\n\nUNOFFICIAL, [Stable-Diffusion](https://github.com/CompVis/stable-diffusion) api using FastAPI\n\n# Samples\n\n|           Text2Image-01            |           Text2Image-02            |\n| :--------------------------------: | :--------------------------------: |\n| ![](./src/image/text2image/1.png)  | ![](./src/image/text2image/2.png)  |\n|           Image2Image-01           |           Image2Image-02           |\n| ![](./src/image/image2image/1.png) | ![](./src/image/image2image/2.png) |\n|             Inpaint-01             |             Inpaint-02             |\n|   ![](./src/image/inpaint/0.png)   |   ![](./src/image/inpaint/0.png)   |\n|   ![](./src/image/inpaint/1.png)   |   ![](./src/image/inpaint/2.png)   |\n\n# Features\n- [x] long-prompt-weighting support\n- [x] text2image \n- [x] image2image\n- [x] inpaints\n- [x] negative-prompt\n- [x] celery async task (check celery_task [branch](https://github.com/rapidrabbit76/stable-diffusion-API/tree/celery_task))\n- [x] original ```ckpt``` format support\n- [ ] object storage support \n- [ ] stable-diffusion 2.0 support\n- [ ] token size checker\n- [ ] JAX/Flax pipeline \n\n# Requirements\n\n\n## API\n\n```txt\nfastapi[all]==0.80.0\nfastapi-restful==0.4.3\nfastapi-health==0.4.0\nservice-streamer==0.1.2\npydantic==1.9.2\ndiffusers==0.3.0\ntransformers==4.19.2\nscipy\nftfy\n```\n\n## Frontend\n```txt\nstreamlit==1.12.2\nrequests==2.27.1 \nrequests-toolbelt==0.9.1 \npydantic==1.8.2\nstreamlit-drawable-canvas==0.9.2\n```\n\n\n# API\n\n\n## /text2image\ncreate image from input prompt\n\ninputs:\n\n    - prompt(str): text prompt\n    - num_images(int): number of images\n    - guidance_scale(float): guidance scale for stable-diffusion\n    - height(int): image height\n    - width(int): image width\n    - seed(int): generator seed\n\noutputs:\n\n    - prompt(str): input text prompt\n    - task_id(str): uuid4 hex string\n    - image_urls(str): generated images url\n\n\n## /image2image\ncreate image from input image\n\ninputs:\n\n    - prompt(str): text prompt\n    - init_image(imagefile): init image for i2i task\n    - num_images(int): number of images\n    - guidance_scale(float): guidance scale for stable-diffusion\n    - seed(int): generator seed\n\noutputs:\n\n    - prompt(str): input text prompt\n    - task_id(str): uuid4 hex string\n    - image_urls(str): generated images url\n\n\n\n# Environment variable\n\n\n```bash\n# env setting is in \n\u003e\u003e ./core/settings/settings.py\n```\n\n| Name                     | Default                       | Desc                                                                                                                                                                    |\n| ------------------------ | ----------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------- |\n| MODEL_ID                 | CompVis/stable-diffusion-v1-4 | huggingface repo id or model path                                                                                                                                       |\n| ENABLE_ATTENTION_SLICING | True                          | [Enable sliced attention computation.](https://huggingface.co/docs/diffusers/api/pipelines/stable_diffusion#diffusers.StableDiffusionPipeline.enable_attention_slicing) |\n| CUDA_DEVICE              | \"cuda\"                        | target cuda device                                                                                                                                                      |\n| CUDA_DEVICES             | [0]                           | visible cuda device                                                                                                                                                     |\n| MB_BATCH_SIZE            | 1                             | Micro Batch: MAX Batch size                                                                                                                                             |\n| MB_TIMEOUT               | 120                           | Micro Batch: timeout sec                                                                                                                                                |\n| HUGGINGFACE_TOKEN        | None                          | huggingface access token                                                                                                                                                |\n| IMAGESERVER_URL          | None                          | result image base url                                                                                                                                                   |\n| SAVE_DIR                 | static                        | result image save dir                                                                                                                                                   |\n| CORS_ALLOW_ORIGINS       | [*]                           | cross origin resource sharing setting for FastAPI                                                                                                                       |\n\n# RUN from code (API)\n\n## 1. install python Requirements\n```bash\npip install -r requirements.txt\n```\n\n## 2. downlaod and caching huggingface model\n```bash\npython huggingface_model_download.py\n# check stable-diffusion model in huggingface cache dir \n[[ -d ~/.cache/huggingface/diffusers/models--CompVis--stable-diffusion-v1-4 ]] \u0026\u0026 echo \"exist\"\n\u003e\u003e exist\n```\n\n## 3. update settings.py in ./core/settings/settings.py\n```python\n# example\nclass ModelSetting(BaseSettings):\n    MODEL_ID: str = \"CompVis/stable-diffusion-v1-4\" # huggingface repo id\n    ENABLE_ATTENTION_SLICING: bool = True\n...\nclass Settings(\n    ...\n):\n    HUGGINGFACE_TOKEN: str = \"YOUR HUGGINGFACE ACCESS TOKEN\"\n    IMAGESERVER_URL: str  = \"http://localhost:3000/images\"\n    SAVE_DIR: str = 'static'\n    ...\n```\n\n## 4. RUN API from code\n```bash\nbash docker/api/start.sh\n```\n\n# RUN from code (frontend)\n\n## 1. install python Requirements\n```bash\npip install \\\n    streamlit==1.12.2 \\\n    requests==2.27.1 \\\n    requests-toolbelt==0.9.1 \\\n    pydantic==1.8.2 \\\n    streamlit-drawable-canvas==0.9.2\n```\n\n## 2. RUN streamlit frontend\n```bash\nstreamlit run inpaint.py\n```\n\n\n# RUN using Docker (docker-compose)\n\n## 1. Image Build \n```bash\ndocker-compose build\n```\n\n## 3. update docker-compose.yaml file in repo root\n```yaml\nversion: \"3.7\"\nservices:\n  api:\n    ...\n    volumes:\n      # mount huggingface model cache dir path to container root user home dir\n      - /model:/model  # if you load pretraind model \n      - ...\n    environment:\n      ...\n      MODEL_ID: \"CompVis/stable-diffusion-v1-4\" \n      HUGGINGFACE_TOKEN: {YOUR HUGGINGFACE ACCESS TOKEN}\n      ...\n\n    deploy:\n      ...\n  frontend:\n    ...\n```\n\n## 4. Container RUN\n```bash\ndocker-compose up -d \n# or API only\ndocker-compsoe up -d  api\n# or frontend only\ndocker-compsoe up -d  frontend\n```\n\n\n\n\n## References\n- [CompVis/stable-diffusion](https://github.com/CompVis/stable-diffusion)\n- [huggingface, stable-diffusion](https://huggingface.co/CompVis)\n- [teamhide/fastapi-boilerplate](https://github.com/teamhide/fastapi-boilerplate)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frapidrabbit76%2Fstable-diffusion-api","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frapidrabbit76%2Fstable-diffusion-api","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frapidrabbit76%2Fstable-diffusion-api/lists"}