Model Demo
In this tutorial, we will run Stable Diffusion model to generate an image from text. Specifically, we want the model to generate a photo of an astronaut riding a horse on mars
. The image is stored on EverlyAI
file storage and it can be downloaded from web UI (opens in a new tab).
Step 1 Implement Code
The final folder structure is shown below.
- everlyai_entrypoint.sh
- stable_diffusion.py
- code.zip
everlyai_entrypoint.sh
is the entrypoint of the server. It contains code to install dependencies and start the server.
python3 -m pip install diffusers transformers accelerate scipy safetensors
python3 stable_diffusion.py
stable_diffusion.py
contains the entire image generation code, adapted from Huggingface model card (opens in a new tab).
Note that the image is saved inside directory, /everlyai/fs
. This will automatically store the file inside EverlyAI's persistent file storage.
import torch
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
model_id = "stabilityai/stable-diffusion-2"
# Use the Euler scheduler here instead
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("/everlyai/fs/astronaut_rides_horse.png")
Inside the project directory, my_awesome_project
, run the following command to generate the code.zip
file. And now
we are ready to start the project on EverlyAI.
zip code.zip everlyai_entrypoint.sh stable_diffusion.py
Step 2 Start Project
Go to EverlyAI project (opens in a new tab) page. Start the project with the following configuration.
Step 3 Verify Result
Wait until the job completes. We can now navigate to file storage (opens in a new tab) page. You should be able to see and download the image from there.