kevalfst commited on
Commit
16588c5
·
verified ·
1 Parent(s): 65843da

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -0
app.py ADDED
@@ -0,0 +1,44 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from diffusers import StableDiffusionPipeline
3
+ import gradio as gr
4
+
5
+ # Use GPU if available
6
+ device = "cuda" if torch.cuda.is_available() else "cpu"
7
+
8
+ # Load Stable Diffusion v1.5 from Hugging Face
9
+ pipe = StableDiffusionPipeline.from_pretrained(
10
+ "stabilityai/stable-diffusion-v1-5",
11
+ torch_dtype=torch.float16 if device == "cuda" else torch.float32,
12
+ revision="fp16" if device == "cuda" else None,
13
+ use_safetensors=True
14
+ )
15
+ pipe = pipe.to(device)
16
+
17
+ # Inference function
18
+ def generate(prompt, guidance, steps, width, height):
19
+ image = pipe(prompt=prompt, guidance_scale=guidance, num_inference_steps=steps, height=height, width=width).images[0]
20
+ return image
21
+
22
+ # Gradio UI
23
+ title = "🎨 Offline Text-to-Image Generator (Stable Diffusion v1.5)"
24
+ description = "Generate images from text prompts using a fully self-hosted Stable Diffusion model."
25
+
26
+ with gr.Blocks() as demo:
27
+ gr.Markdown(f"# {title}")
28
+ gr.Markdown(description)
29
+
30
+ with gr.Row():
31
+ with gr.Column():
32
+ prompt = gr.Textbox(label="Enter your prompt", placeholder="A steampunk dragon flying over a futuristic city")
33
+ guidance = gr.Slider(1, 20, value=7.5, step=0.5, label="Guidance Scale")
34
+ steps = gr.Slider(10, 100, value=30, step=5, label="Inference Steps")
35
+ width = gr.Slider(256, 768, value=512, step=64, label="Image Width")
36
+ height = gr.Slider(256, 768, value=512, step=64, label="Image Height")
37
+ submit = gr.Button("Generate Image")
38
+
39
+ with gr.Column():
40
+ output = gr.Image(label="Generated Image")
41
+
42
+ submit.click(fn=generate, inputs=[prompt, guidance, steps, width, height], outputs=output)
43
+
44
+ demo.launch()