Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/main/diffusers_doc/en/bentoml.ipynb
Views: 2542
BentoML Integration Guide
BentoML is an open-source framework designed for building, shipping, and scaling AI applications. It allows users to easily package and serve diffusion models for production, ensuring reliable and efficient deployments. It features out-of-the-box operational management tools like monitoring and tracing, and facilitates the deployment to various cloud platforms with ease. BentoML's distributed architecture and the separation of API server logic from model inference logic enable efficient scaling of deployments, even with budget constraints. As a result, integrating it with Diffusers provides a valuable tool for real-world deployments.
This tutorial demonstrates how to integrate BentoML with Diffusers.
Prerequisites
Install Diffusers.
Install BentoML by running
pip install bentoml
. For more information, see the BentoML documentation.
Import a diffusion model
First, you need to prepare the model. BentoML has its own Model Store for model management. Create a download_model.py
file as below to import a diffusion model into BentoML's Model Store:
This code snippet downloads the Stable Diffusion 2.1 model (using it's repo id stabilityai/stable-diffusion-2-1
) from the Hugging Face Hub (or use the cached download files if the model is already downloaded) and imports it into the BentoML Model Store with the name sd2.1
.
For models already fine-tuned and stored on disk, you can provide the path instead of the repo id.
You can view the model in the Model Store:
Turn a diffusion model into a RESTful service with BentoML
Once the diffusion model is in BentoML's Model Store, you can implement a text-to-image service with it. The Stable Diffusion model accepts various arguments in addition to the required prompt to guide the image generation process. To validate these input arguments, use BentoML's pydantic integration. Create a sdargs.py
file with an example pydantic model:
This pydantic model requires a string field prompt
and three optional fields: height
, width
, and negative_prompt
, each with corresponding types. The extra = "allow"
line supports adding additional fields not defined in the SDArgs
class. In a real-world scenario, you may define all the desired fields and not allow extra ones.
Next, create a BentoML Service file that defines a Stable Diffusion service:
Save the file as service.py
, and spin up a BentoML Service endpoint using:
An HTTP server with /txt2img
endpoint that accepts a JSON dictionary should be up at port 3000. Go to http://127.0.0.1:3000 in your web browser to access the Swagger UI.
You can also test the text-to-image generation using curl
and write the returned image to output.jpg
.
Package a BentoML Service for cloud deployment
To deploy a BentoML Service, you need to pack it into a BentoML Bento, a file archive with all the source code, models, data files, and dependencies. This can be done by providing a bentofile.yaml
file as follows:
The bentofile.yaml
file contains Bento build options, such as package dependencies and Docker options.
Then you build a Bento using:
The output looks like:
You can create a Docker image based on the Bento by running the following command and deploy it to a cloud provider.
If you want an end-to-end solution for deploying and managing models, you can push the Bento to Yatai or BentoCloud for a distributed deployment.
For more information about BentoML's integration with Diffusers, see the BentoML Diffusers Guide.