
Stable Diffusion is a text-to-image diffusion model developed by Stability AI. It is capable of generating high-quality, photorealistic images from text descriptions. Unlike other text-to-image models, Stable Diffusion generates consistent photos even when the input text description is complex or open-ended.
In this guide, you'll set up the Stable Diffusion environment and query the model using a web user interface. Then, you'll create a REST API to generate responses from the model and access the API through a Jupyter Notebook.
Before you begin:
Deploy a new Ubuntu 22.04 A100 Vultr Cloud GPU Server with at least:
Create a non-root user with sudo rights and switch to the account.
The Stable Diffusion requires some dependency packages to work. Install the packages using the following command:
You can run the Stable Diffusion model in a web interface. Follow the steps below to download an automatic script that installs all the necessary packages. Then, load the model:
Create a new sd directory and navigate to it.
Download the Stable Diffusion webui.sh file.
Add execute permissions to the webui.sh file.
Allow port 7860 through the firewall.
Run the webui.sh script to download the model and run the web interface.
Output:
Visit the URL below. Replace PUBLIC_IP_ADDRESS with the public IP address of your GPU instance.
Type the following queries and review the output:
A cute white cat sitting next to a computer keyboard
Output:
Taj Mahal during sunset, photo realistic, high quality
Output:
The bentoml library provides support for deploying and serving the Stable Diffusion model through an API. Follow the steps below to create and run an API:
Use pip to install the required libraries.
Navigate to the sd directory you created earlier.
Create a new fetch_sd.py file.
Enter the following information into the fetch_sd.py file.
Create a new service.py file.
Enter the following information into the service.py file. The following script loads a BentoML service that uses the Stable Diffusion model to convert text to image.
Save and close the file.
Create a new sdargs.py file.
Enter the following information into the sdargs.py file. The following script defines an SDArgs Pydantic model that allows extra fields while inputting data. The script handles data validation in the application.
Create a service.yaml file
Enter the following information into the file.
Save and close the file
Run the fetch_sd.py file to pull the image from Hugging Face. This file allows the bentoml library to download the Stable Diffusion image and make it available locally. The command takes around 10 minutes to complete.
Output:
List the models.
Allow port 3000 through the firewall.
Run the bentoml service.
Save and close the file.
After setting up a Stable Diffusion API in the previous section, you can now run a Python script to access the API using a Jupyter Notebook. Follow the steps below:
Invoke a Jupypter lab service and retrieve your access token.
Allow port 8890 through the firewall.
Access the Jupyter Lab on a web browser. Replace YOUR_SERVER_IP with the public IP address of the GPU instance.
Click Python 3 ipykernel under Notebook and paste the following Python code. The following script accesses the REST API to infer the Stable diffusion model. The script also provides a text prompt to the model with extra values like height and width to generate a response.
Output:
In this guide, you've used the Stable Diffusion model to generate images using text inputs. You've run the model's functionalities using a web interface and later used a Jupyter Notebook to access the REST API.
0 Comments
Be the first to comment and share your perspective with the community.