Skip to main content

Stable Diffusion Google Colab FastAPI Server

Note: This is a pretty hacky server / client interface for generic Stable Diffusion pipelines using diffusers. It's not made for production use and hasn't really been optimized completely.

Installation

From pip:

pip install sd-server

From source:

git clone git@github.com:shyamsn97/stable-diffusion-server.git
cd stable-diffusion-server/
python setup.py install

Stable Diffusion Server + Client Interface -- Simple Usage

Here we create a simple server hosting the standard StableDiffusionPipeline. The client takes in **kwargs that should be the arguments passed into the __call__ function from the pipeline. For instance, the StableDiffusionPipeline __call__ method can be found: https://github.com/huggingface/diffusers/blob/main/src/diffusers/pipelines/stable_diffusion/pipeline_stable_diffusion.py#L185. host and port can be specified in the start method for generic hosting.

from sd_server import DiffusersServer, DiffusersClient
from diffusers import StableDiffusionPipeline

STABLE_DIFFUSION_PATH = "runwayml/stable-diffusion-v1-5" # this can be a local path
pipeline_kwargs = {
    "revision":"fp16",
    "torch_dtype":torch.float16
}
device = torch.device('cuda')

server = DiffusersServer.create(
    pretrained_path = STABLE_DIFFUSION_PATH,
    pipeline_cls = StableDiffusionPipeline,
    pipeline_kwargs = pipeline_kwargs,
    enable_attention_slicing = True,
    device = device
)
url = server.start(host="127.0.0.1", port=8000) # url -- either remote or local

# on another host / terminal
client = DiffusersClient(url)

responses = client(prompt='a photo of an astronaut riding a horse on mars', num_images_per_prompt=4) # this should return a list of images

Serving from Google Colab

Using the server in Google Colab requires an ngrok account, and needs your ngrok auth token https://dashboard.ngrok.com/get-started/your-authtoken to be passed to the server. Note: Ngrok basically opens a tunnel from a dev machine to the cloud, which means its not really that secure, and should be used at your own risk. Read more here: https://stackoverflow.com/questions/36552950/is-ngrok-safe-to-use-or-can-it-be-compromised

NGROK_AUTH_KEY = "your auth key from https://dashboard.ngrok.com/get-started/your-authtoken"

... # imports from above

server = DiffusersServer.create(
    pretrained_path = STABLE_DIFFUSION_PATH,
    pipeline_cls = StableDiffusionPipeline,
    pipeline_kwargs = pipeline_kwargs,
    enable_attention_slicing = True,
    device = device
)

url = server.start(ngrok_auth_token=NGROK_AUTH_KEY)

# on another host
client = DiffusersClient(url)

responses = client(prompt='a photo of an astronaut riding a horse on mars', num_images_per_prompt=4) # this should return a list of images

Metadata

Release files for sd-server 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for sd-server 0.1.0
File Size Uploaded Provenance Actions
sd-server-0.1.0.tar.gz 5.3 kB Without provenance Details for sd-server-0.1.0.tar.gz

Built distributions (wheels)

Table of built distributions (wheels) for sd-server 0.1.0
File Interpreter ABI Platform Actions
sd_server-0.1.0-py3.9.egg Legacy Egg format - - Details for sd_server-0.1.0-py3.9.egg
sd_server-0.1.0-py3-none-any.whl Python 3 none any Details for sd_server-0.1.0-py3-none-any.whl

Total release size: 20.2 kB

Release files / sd-server-0.1.0.tar.gz

Download URL sd-server-0.1.0.tar.gz
Size 5.3 kB
Tags Source
SHA-256 checksum
How to use checksums
d6a62d204249268f1b123a8a3b9b14bc90e67d92afe6ba1e5c180d21beb4b48a
BLAKE2b-256 checksum
How to use checksums
b4961ecf459dfc020301594a916f36c2aa486a8a655018734b51ea94f0cf478b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / sd_server-0.1.0-py3.9.egg

Download URL sd_server-0.1.0-py3.9.egg
Size 9.0 kB
Tags Egg
SHA-256 checksum
How to use checksums
3de39ee01a7e04863485488b3d865e9a458c88f4e17bfefea4e1139dc92df8c5
BLAKE2b-256 checksum
How to use checksums
7f5b377339ff5ceafd40b77bc699bf3b3dc285c7fb6c455edd0b753e99ccbb96
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Release files / sd_server-0.1.0-py3-none-any.whl

Download URL sd_server-0.1.0-py3-none-any.whl
Size 5.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fdc8780177c2292a6660a592656d386cde19ad112510e05a29c5a02f5806c707
BLAKE2b-256 checksum
How to use checksums
26109def98289c355a69fa8d7a4ceac153df60b1290886776136d4e6ebef06d6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.15

Security for sd-server 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

3 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page