Generate Images with Stable Diffusion on RunPod

Generate Images with Stable Diffusion on RunPod
💡
RunPod is hosting an AI art contest, find out more on our Discord in the #art-contest channel.

In this tutorial, you will learn how to generate images using Stable Diffusion, a powerful text-to-image model, on the RunPod platform.

CLI for project and image generation.

By following the step-by-step instructions, you'll set up the prerequisites, create a new project, and run a Python script to send requests to your endpoint and generate images based on text descriptions.

Prerequisites

Before getting started, ensure you have the following:

  • Network Volume: Create a Network Volume on the RunPod platform. Visit the RunPod Storage Console to set up your Network Volume. Ensure you have sufficient memory (at least 50 GB) to store your model and generated images.

RunPod CLI: Install the RunPod CLI by running the following command in your terminal:

wget -qO- cli.runpod.net | sudo bash

For full installation instructions, refer to the RunPod CLI documentation.

1. Creating a New Project

  1. Open your terminal and run the following command to create a new project:
    runpodctl project create
  2. When prompted, enter your project name, for example:
    photo-competition
  3. Select Stable Diffusion as the project type.
  4. Leave the field blank for the default model (stabilityai/sdxl-turbo) or enter the name of another text-to-image Hugging Face model. You can find more models in the text-to-image category.
  5. Keep the default values for CUDA and Python versions.
  6. Select your Network Volume when prompted and wait for your Pod to come online. This process may take a few minutes.
  7. Change to the project directory and start the development server:
    cd photo-competition && runpodctl project dev

Once you see the following output in your terminal, your Pod is ready, and you can proceed to the next step:

Connect to the API server at:
>  https://<YOUR_ENDPOINT_ID>-7270.proxy.runpod.net

Loading pipeline components...: 100%|██████████| 7/7 [00:04<00:00,  1.48it/s]
Synced venv to network volume
--- Starting Serverless Worker |  Version 1.6.2 ---
INFO   | Starting API server.
DEBUG  | Not deployed on RunPod serverless, pings will not be sent.

Make note of your <YOUR_ENDPOINT_ID> as you'll need it in the next step.

2. Running the Python Script

  1. Create a new Python script file, for example, script.py, and copy the following code into it:
import requests
import base64
import io
from PIL import Image

url = 'https://<YOUR_ENDPOINT_ID>.proxy.runpod.net/runsync'
headers = {
    'accept': 'application/json',
    'Content-Type': 'application/json'
}
data = {
    'input': {
        'prompt': 'Your description here'
    }
}

response = requests.post(url, headers=headers, json=data)

if response.status_code == 200:
    result = response.json()

    if result['status'] == 'COMPLETED':
        base64_image = result['output']

        # Remove the data URI prefix
        base64_string = base64_image.split(',')[1]

        # Decode the base64 string
        image_data = base64.b64decode(base64_string)

        # Create an image object from the decoded data
        image = Image.open(io.BytesIO(image_data))

        # Save the image as a PNG file
        image.save('output_image.png', 'PNG')

        print("Image saved as 'output_image.png'")

    else:
        print(f"Request status: {result['status']}")
else:
    print(f"Request failed with status code: {response.status_code}")
  1. Replace <YOUR_ENDPOINT_ID> with the endpoint ID you obtained in the previous step.
  2. Customize the prompt value in the data dictionary with your desired text description for the image generation.
  3. Save the Python script file as script.py.
  4. Run the Python script using the following command:
    python script.py
Puppy dog image generated from this tutorial.

The script will send a request to your RunPod endpoint, and if successful, it will generate an image based on your text description. The generated image will be saved as output_image.png in the same directory as your Python script.

Conclusion

Congratulations! You have successfully set up a RunPod project, created an endpoint, and generated images using Stable Diffusion.
By following this tutorial, you have learned how to harness the power of text-to-image models on the RunPod platform.

Consult the model card you are using to customize the parameters, such as temperature, strength, or other parameterizes to control the image output.

You can provide more inputs to the src/handler.py file, save it and the RunPod server will sync your changes.

💡
You can find out more and submit your art to our AI Art contest.

You can also dive deeper into the RunPod documentation and explore additional features and customization options to enhance your image generation workflow.