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.
The diffusers library from Hugging Face gives you Stable Diffusion in one line.
pip install diffusers transformers accelerate torch Pillow gradio
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
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
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.
Spotted a bug, broken code, or something that doesn't look right? Tell us what's off and we'll fix it.