Freedom Sale
Independence Day Special — Unlock the AI Path 70% off our most popular AI course · Limited time offer
--Days
--Hrs
--Min
--Sec
Claim Your Discount
✦ Intermediate ⏱ 45 min

🎨 Build a Text-to-Image App with Stable Diffusion

🎯 What You'll Build

A local image generator that turns any text prompt into a high-quality image using Stable Diffusion — runs free on your own machine, no API key needed.

📋 What You'll Need

1

Install dependencies

The diffusers library from Hugging Face gives you Stable Diffusion in one line.

pip install diffusers transformers accelerate torch Pillow gradio
2

Generate your first image

Load the Stable Diffusion pipeline and pass it a prompt. The model downloads automatically on first run (~4 GB).

from diffusers import StableDiffusionPipeline
import torch

# Load model — downloads once, cached locally after
pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float32   # use float16 if you have a GPU
)

prompt = "a futuristic city skyline at sunset, cyberpunk style, highly detailed"
image = pipe(prompt, num_inference_steps=30).images[0]
image.save("output.png")
print("Image saved as output.png")
Downloading model weights... (first run only)
Image saved as output.png
3

Build a Gradio web UI

Wrap the pipeline in a Gradio interface so you can type prompts in a browser.

import gradio as gr
from diffusers import StableDiffusionPipeline
import torch

pipe = StableDiffusionPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float32
)

def generate(prompt, steps, guidance):
    image = pipe(prompt, num_inference_steps=int(steps), guidance_scale=guidance).images[0]
    return image

demo = gr.Interface(
    fn=generate,
    inputs=[
        gr.Textbox(label="Prompt", placeholder="a red panda in a bamboo forest, studio lighting"),
        gr.Slider(10, 50, value=30, step=1, label="Inference Steps"),
        gr.Slider(1, 20, value=7.5, label="Guidance Scale"),
    ],
    outputs=gr.Image(label="Generated Image"),
    title="🎨 Free AI Image Generator",
    description="Runs locally — no API key, no cost.",
)

demo.launch()
Running on local URL: http://127.0.0.1:7860
💡 Tip: Use a negative prompt to improve quality: pass negative_prompt="blurry, low quality, text, watermark" to the pipeline call.

🎉 You Did It!

You now have a fully local AI image generator with a web UI. Upgrade to SDXL (stabilityai/stable-diffusion-xl-base-1.0) for higher-resolution results.

Found something wrong?

Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.