How to run Stable Diffusion on RunPod
Quick answer
Run Stable Diffusion on
RunPod by creating a serverless endpoint or pod with a GPU, then invoking it via the runpod Python SDK or REST API. Use the runpod.Endpoint class to send prompts and receive generated images programmatically.PREREQUISITES
Python 3.8+RunPod API keypip install runpod
Setup
Install the runpod Python package and set your API key as an environment variable. You also need to create a Stable Diffusion pod or endpoint on the RunPod platform with GPU support.
pip install runpod
export RUNPOD_API_KEY=os.environ["RUNPOD_API_KEY"] output
$ pip install runpod Collecting runpod Downloading runpod-1.0.0-py3-none-any.whl (10 kB) Installing collected packages: runpod Successfully installed runpod-1.0.0 $ export RUNPOD_API_KEY="your_runpod_api_key_here"
Step by step
Use the runpod Python SDK to invoke your Stable Diffusion endpoint. Replace YOUR_ENDPOINT_ID with your actual RunPod endpoint ID. The example sends a prompt and receives a generated image URL.
import os
import runpod
runpod.api_key = os.environ["RUNPOD_API_KEY"]
# Replace with your actual RunPod Stable Diffusion endpoint ID
endpoint = runpod.Endpoint("YOUR_ENDPOINT_ID")
input_data = {
"prompt": "A fantasy landscape with mountains and a river, digital art",
"num_inference_steps": 50,
"guidance_scale": 7.5
}
result = endpoint.run_sync({"input": input_data})
print("Generated image URL:", result["output"]) output
Generated image URL: https://runpod-public.s3.amazonaws.com/generated-images/abc123.png
Common variations
You can use asynchronous calls with endpoint.run_async() for non-blocking requests. Adjust parameters like num_inference_steps and guidance_scale to control image quality and style. You can also switch to different Stable Diffusion versions by selecting the appropriate pod on RunPod.
import asyncio
async def generate_image():
runpod.api_key = os.environ["RUNPOD_API_KEY"]
endpoint = runpod.Endpoint("YOUR_ENDPOINT_ID")
input_data = {
"prompt": "A futuristic cityscape at sunset",
"num_inference_steps": 30,
"guidance_scale": 8.0
}
result = await endpoint.run_async({"input": input_data})
print("Generated image URL:", result["output"])
asyncio.run(generate_image()) output
Generated image URL: https://runpod-public.s3.amazonaws.com/generated-images/def456.png
Troubleshooting
- If you get authentication errors, verify your
RUNPOD_API_KEYenvironment variable is set correctly. - If the endpoint ID is invalid, double-check your RunPod dashboard for the correct Stable Diffusion pod or endpoint ID.
- For slow responses, ensure your pod has GPU resources allocated and is running.
Key Takeaways
- Use the official
runpodPython SDK with your API key to interact with Stable Diffusion pods. - Set up a GPU-enabled Stable Diffusion pod on RunPod before invoking it programmatically.
- Adjust inference parameters like
num_inference_stepsandguidance_scaleto customize image output. - Use asynchronous calls for non-blocking image generation in Python.
- Check environment variables and endpoint IDs carefully to avoid common errors.