visionary-ai / app.py
kevalfst's picture
Update app.py
8f45be3 verified
raw
history blame
1.7 kB
import torch
from diffusers import StableDiffusionPipeline
import gradio as gr
# Use GPU if available
device = "cuda" if torch.cuda.is_available() else "cpu"
# Load Stable Diffusion v1.5 from Hugging Face
pipe = StableDiffusionPipeline.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5",
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
revision="fp16" if device == "cuda" else None,
use_safetensors=True
)
pipe = pipe.to(device)
# Inference function
def generate(prompt, guidance, steps, width, height):
image = pipe(prompt=prompt, guidance_scale=guidance, num_inference_steps=steps, height=height, width=width).images[0]
return image
# Gradio UI
title = "🎨 Offline Text-to-Image Generator (Stable Diffusion v1.5)"
description = "Generate images from text prompts using a fully self-hosted Stable Diffusion model."
with gr.Blocks() as demo:
gr.Markdown(f"# {title}")
gr.Markdown(description)
with gr.Row():
with gr.Column():
prompt = gr.Textbox(label="Enter your prompt", placeholder="A steampunk dragon flying over a futuristic city")
guidance = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
steps = gr.Slider(10, 100, value=30, step=5, label="Inference Steps")
width = gr.Slider(256, 768, value=512, step=64, label="Image Width")
height = gr.Slider(256, 768, value=512, step=64, label="Image Height")
submit = gr.Button("Generate Image")
with gr.Column():
output = gr.Image(label="Generated Image")
submit.click(fn=generate, inputs=[prompt, guidance, steps, width, height], outputs=output)
demo.launch()