Metadata-Version: 2.1
Name: onediffusion
Version: 0.0.1
Summary: Onediffusion: REST API server for running any diffusion models - Stable Diffusion, Anything, ControlNet, Lora, Custom
Project-URL: Documentation, https://github.com/BentoML/OneDiffusion#readme
Project-URL: Issues, https://github.com/BentoML/OneDiffusion/issues
Project-URL: Source, https://github.com/BentoML/OneDiffusion
Author-email: Shenyang Zhao <larme@bentoml.com>, BentoML Team <contact@bentoml.com>
License-Expression: Apache-2.0
License-File: LICENSE.txt
Keywords: AI,BentoML,Diffusers,Generative AI,Lora,MLOps,Model Deployment,Model Serving,PyTorch,Stable Diffusion
Classifier: Development Status :: 4 - Beta
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Programming Language :: Python :: Implementation :: PyPy
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Libraries
Requires-Python: >=3.8
Requires-Dist: accelerate
Requires-Dist: attrs>=23.1.0
Requires-Dist: bentoml[io]>=1.1.2
Requires-Dist: cattrs>=23.1.0
Requires-Dist: diffusers[torch]>=0.19.3
Requires-Dist: huggingface-hub
Requires-Dist: inflection
Requires-Dist: orjson
Requires-Dist: pydantic
Requires-Dist: safetensors
Requires-Dist: tabulate[widechars]>=0.9.0
Requires-Dist: transformers
Description-Content-Type: text/markdown

# OneDiffusion

OneDiffusion is an open-source one-stop shop for facilitating the deployment of any diffusion models in production. It caters specifically to the needs of diffusion models, supporting both pretrained and fine-tuned diffusion models with LoRA adapters.

Key features include:

- 🌐 **Broad compatibility**: Support both pretrained and LoRA-adapted diffusion models, providing flexibility in choosing and deploying the appropriate model for various image generation tasks. It currently supports Stable Diffusion (v1.4, v1.5 and v2.0) and Stable Diffusion XL (v1.0) models.
- 💪 **Optimized performance and scalability**: Apply the best in class optimizations for serving diffusion models on your behalf.
- ⌛️ **Dynamic LoRA adapter loading**: Dynamically load and unload LoRA adapters on every request, providing greater adaptability and ensuring the models remain responsive to changing inputs and conditions.
- 🍱 **First-class support for BentoML**: Seamless integration with the [BentoML](https://github.com/bentoml/BentoML) ecosystem, allowing you to build Bentos and push them to [BentoCloud](https://www.bentoml.com/cloud) or [Yatai](https://github.com/bentoml/Yatai). 

OneDiffusion is designed for AI application developers who require a robust and flexible platform for deploying diffusion models in production. The platform offers tools and features to fine-tune, serve, deploy, and monitor these models effectively, streamlining the end-to-end workflow for diffusion model deployment.

## Get started

### Prerequisites

You have installed Python 3.8 (or later) and `pip`.

### Install OneDiffusion

Install OneDiffusion by using `pip` as follows:

```
pip install onediffusion
```

To verify the installation, run:

```
$ onediffusion -h

Usage: onediffusion [OPTIONS] COMMAND [ARGS]...

 ██████╗ ███╗   ██╗███████╗██████╗ ██╗███████╗███████╗██╗   ██╗███████╗██╗ ██████╗ ███╗   ██╗
██╔═══██╗████╗  ██║██╔════╝██╔══██╗██║██╔════╝██╔════╝██║   ██║██╔════╝██║██╔═══██╗████╗  ██║
██║   ██║██╔██╗ ██║█████╗  ██║  ██║██║█████╗  █████╗  ██║   ██║███████╗██║██║   ██║██╔██╗ ██║
██║   ██║██║╚██╗██║██╔══╝  ██║  ██║██║██╔══╝  ██╔══╝  ██║   ██║╚════██║██║██║   ██║██║╚██╗██║
╚██████╔╝██║ ╚████║███████╗██████╔╝██║██║     ██║     ╚██████╔╝███████║██║╚██████╔╝██║ ╚████║
 ╚═════╝ ╚═╝  ╚═══╝╚══════╝╚═════╝ ╚═╝╚═╝     ╚═╝      ╚═════╝ ╚══════╝╚═╝ ╚═════╝ ╚═╝  ╚═══╝
 
          An open platform for operating diffusion models in production.
          Fine-tune, serve, deploy, and monitor any diffusion models with ease.


Options:
  -v, --version  Show the version and exit.
  -h, --help     Show this message and exit.

Commands:
  download  Setup diffusion model interactively.
  start     Start any diffusion models as a REST server.
```

## Start a diffusion server

OneDiffusion allows you to quickly spin up any diffusion models. To start a server, run:

```bash
onediffusion start stable-diffusion
```

This starts a server at http://0.0.0.0:3000/. You can interact with it by visiting the web UI or send a request via `curl`.

```bash
curl -X 'POST' \
  'http://0.0.0.0:3000/text2img' \
  -H 'accept: image/jpeg' \
  -H 'Content-Type: application/json' \
  --output output.jpg \
  -d '{
  "prompt": "a bento box",
  "negative_prompt": null,
  "height": 768,
  "width": 768,
  "num_inference_steps": 50,
  "guidance_scale": 7.5,
  "eta": 0
}'
```

By default, OneDiffusion uses `stabilityai/stable-diffusion-2` to start the server. To use a specific model version, add the `--model-id` option as below:

```bash
onediffusion start stable-diffusion --model-id runwayml/stable-diffusion-v1-5
```

OneDiffusion downloads the models to the BentoML local Model Store if they have not been registered before. To view your models, install BentoML first with `pip install bentoml` and then run:

```
$ bentoml models list

Tag                                                                                         Module                              Size        Creation Time
pt-sd-stabilityai--stable-diffusion-2:1e128c8891e52218b74cde8f26dbfc701cb99d79              bentoml.diffusers                   4.81 GiB    2023-08-16 17:52:33
pt-sdxl-stabilityai--stable-diffusion-xl-base-1.0:bf714989e22c57ddc1c453bf74dab4521acb81d8  bentoml.diffusers                   13.24 GiB   2023-08-16 16:09:01
```

### Start a Stable Diffusion XL server

OneDiffusion also supports running Stable Diffusion XL 1.0, the most advanced development in the Stable Diffusion text-to-image suite of models launched by Stability AI. To start an XL server, simply run:

```
onediffusion start stable-diffusion-xl
```

It downloads the model automatically if it does not exist locally. Options such as `--model-id` are also supported. For more information, run `onediffusion start stable-diffusion-xl --help`. 

Similarly, visit http://0.0.0.0:3000/ or send a request via `curl` to interact with the XL server. Example prompt:

```
{
  "prompt": "the scene is a picturesque environment with beautiful flowers and trees. In the center, there is a small cat. The cat is shown with its chin being scratched. It is crouched down peacefully. The cat's eyes are filled with excitement and satisfaction as it uses its small paws to hold onto the food, emitting a content purring sound.",
  "negative_prompt": null,
  "height": 1024,
  "width": 1024,
  "num_inference_steps": 50,
  "guidance_scale": 7.5,
  "eta": 0,
}
```

Example output:

[sdxl-cat](/example-images/sdxl-cat.jpeg)

### Add LoRA weights

Low-Rank Adaptation (LoRA) is a training method to fine-tune models without the need to retrain all parameters. You can add LoRA weights to your diffusion models for specific data needs.

Add the `--lora-weights` option as below:

```
onediffusion start stable-diffusion-xl --lora-weights "/path/to/lora-weights.safetensors"
```

Use the same prompt in the previous section but with LoRA for pixel art:

[sdxl-cat-with-pixel-lora](/example-images/sdxl-cat-with-pixel-lora.jpeg)

## Download a model

If you want to download a diffusion model without starting a server, use the `onediffusion download` command. For example:

```
onediffusion download stable-diffusion --model-id "CompVis/stable-diffusion-v1-4"
```

## Create a BentoML Runner

You can create a BentoML Runner with the `diffusers_runners.create_runner()` function, which downloads the model specified automatically if it does not exist locally.

```python
import bentoml

# Create a Runner for a Stable Diffusion model
runner = bentoml.diffusers_simple.stable_diffusion.create_runner("CompVis/stable-diffusion-v1-4")

# Create a Runner for a Stable Diffusion XL model
runner_xl = bentoml.diffusers_simple.stable_diffusion_xl.create_runner("stabilityai/stable-diffusion-xl-base-1.0")
```

You can then wrap the Runner into a BentoML Service. See the [BentoML documentation](https://docs.bentoml.com/en/latest/concepts/service.html) for more details.
