Giter VIP home page Giter VIP logo

stable-diffusion-api's Introduction

Unofficial Fastapi implementation Stable-Diffusion API

UNOFFICIAL, Stable-Diffusion api using FastAPI

Samples

Text2Image-01 Text2Image-02
Image2Image-01 Image2Image-02
Inpaint-01 Inpaint-02

Features

  • long-prompt-weighting support
  • text2image
  • image2image
  • inpaints
  • negative-prompt
  • celery async task (check celery_task branch)
  • original ckpt format support
  • object storage support
  • stable-diffusion 2.0 support
  • token size checker
  • JAX/Flax pipeline

Requirements

API

fastapi[all]==0.80.0
fastapi-restful==0.4.3
fastapi-health==0.4.0
service-streamer==0.1.2
pydantic==1.9.2
diffusers==0.3.0
transformers==4.19.2
scipy
ftfy

Frontend

streamlit==1.12.2
requests==2.27.1 
requests-toolbelt==0.9.1 
pydantic==1.8.2
streamlit-drawable-canvas==0.9.2

API

/text2image

create image from input prompt

inputs:

- prompt(str): text prompt
- num_images(int): number of images
- guidance_scale(float): guidance scale for stable-diffusion
- height(int): image height
- width(int): image width
- seed(int): generator seed

outputs:

- prompt(str): input text prompt
- task_id(str): uuid4 hex string
- image_urls(str): generated images url

/image2image

create image from input image

inputs:

- prompt(str): text prompt
- init_image(imagefile): init image for i2i task
- num_images(int): number of images
- guidance_scale(float): guidance scale for stable-diffusion
- seed(int): generator seed

outputs:

- prompt(str): input text prompt
- task_id(str): uuid4 hex string
- image_urls(str): generated images url

Environment variable

# env setting is in 
>> ./core/settings/settings.py
Name Default Desc
MODEL_ID CompVis/stable-diffusion-v1-4 huggingface repo id or model path
ENABLE_ATTENTION_SLICING True Enable sliced attention computation.
CUDA_DEVICE "cuda" target cuda device
CUDA_DEVICES [0] visible cuda device
MB_BATCH_SIZE 1 Micro Batch: MAX Batch size
MB_TIMEOUT 120 Micro Batch: timeout sec
HUGGINGFACE_TOKEN None huggingface access token
IMAGESERVER_URL None result image base url
SAVE_DIR static result image save dir
CORS_ALLOW_ORIGINS [*] cross origin resource sharing setting for FastAPI

RUN from code (API)

1. install python Requirements

pip install -r requirements.txt

2. downlaod and caching huggingface model

python huggingface_model_download.py
# check stable-diffusion model in huggingface cache dir 
[[ -d ~/.cache/huggingface/diffusers/models--CompVis--stable-diffusion-v1-4 ]] && echo "exist"
>> exist

3. update settings.py in ./core/settings/settings.py

# example
class ModelSetting(BaseSettings):
    MODEL_ID: str = "CompVis/stable-diffusion-v1-4" # huggingface repo id
    ENABLE_ATTENTION_SLICING: bool = True
...
class Settings(
    ...
):
    HUGGINGFACE_TOKEN: str = "YOUR HUGGINGFACE ACCESS TOKEN"
    IMAGESERVER_URL: str  = "http://localhost:3000/images"
    SAVE_DIR: str = 'static'
    ...

4. RUN API from code

bash docker/api/start.sh

RUN from code (frontend)

1. install python Requirements

pip install \
    streamlit==1.12.2 \
    requests==2.27.1 \
    requests-toolbelt==0.9.1 \
    pydantic==1.8.2 \
    streamlit-drawable-canvas==0.9.2

2. RUN streamlit frontend

streamlit run inpaint.py

RUN using Docker (docker-compose)

1. Image Build

docker-compose build

3. update docker-compose.yaml file in repo root

version: "3.7"
services:
  api:
    ...
    volumes:
      # mount huggingface model cache dir path to container root user home dir
      - /model:/model  # if you load pretraind model 
      - ...
    environment:
      ...
      MODEL_ID: "CompVis/stable-diffusion-v1-4" 
      HUGGINGFACE_TOKEN: {YOUR HUGGINGFACE ACCESS TOKEN}
      ...

    deploy:
      ...
  frontend:
    ...

4. Container RUN

docker-compose up -d 
# or API only
docker-compsoe up -d  api
# or frontend only
docker-compsoe up -d  frontend

References

stable-diffusion-api's People

Contributors

rapidrabbit76 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

stable-diffusion-api's Issues

Facing Connection Error While Running API

I've followed these steps and running from my personal system

RUN from code (frontend)

  1. install python Requirements
    pip install
    streamlit==1.12.2
    requests==2.27.1
    requests-toolbelt==0.9.1
    pydantic==1.8.2
    streamlit-drawable-canvas==0.9.2
  2. RUN streamlit frontend
    streamlit run inpaint.py

But im getting below error

ConnectionError: HTTPConnectionPool(host='localhost', port=3000): Max retries exceeded with url: /%5Cinpaint (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001A5C72EEA70>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))

Screenshot (74)

Generate images simultaneously

Is it possible to generate images at the same time? Currently it works with two requests at the same time, but is it possible to generate two images simultaneously without waiting for a queue?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.