Inovative commited on
Commit
efb1507
·
verified ·
1 Parent(s): fe38bc4

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+
2
+ # Install Gradio and dependencies
3
+
4
+ # --- Imports ---
5
+ from diffusers import StableDiffusionPipeline
6
+ import torch
7
+ import gradio as gr
8
+ import uuid
9
+
10
+ # --- Configuration ---
11
+ class CGF:
12
+ device = "cuda" if torch.cuda.is_available() else "cpu"
13
+ seed = 42
14
+ generator = torch.Generator(device=device).manual_seed(seed)
15
+ image_gen_model_id = "my_text_to_image_saved_sd_model"
16
+ image_gen_steps = 50
17
+ image_gen_guidance_scale = 10
18
+ image_gen_size = (768, 768)
19
+
20
+ # --- Load Model ---
21
+ pipe = StableDiffusionPipeline.from_pretrained(
22
+ CGF.image_gen_model_id,
23
+ torch_dtype=torch.float16,
24
+ use_safetensors=True,
25
+ revision="fp16",
26
+ ).to(CGF.device)
27
+
28
+ # --- Generation Function ---
29
+ def generate_image(prompt):
30
+ result = pipe(
31
+ prompt,
32
+ num_inference_steps=CGF.image_gen_steps,
33
+ guidance_scale=CGF.image_gen_guidance_scale,
34
+ generator=CGF.generator,
35
+ ).images[0]
36
+
37
+ filename = f"{prompt.replace(' ', '_')}_{uuid.uuid4().hex[:6]}.png"
38
+ result.save(filename)
39
+ return result, filename
40
+
41
+ # --- Gradio UI ---
42
+ examples = [
43
+ "A majestic mountain landscape during sunrise",
44
+ "A futuristic cyberpunk city at night with neon lights",
45
+ "A photorealistic portrait of a happy Indian woman in a green saree",
46
+ "A fantasy dragon flying over a castle at sunset",
47
+ "A cat astronaut floating in space",
48
+ ]
49
+
50
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
51
+ gr.Markdown("## 🎨 Text-to-Image Generator with Stable Diffusion 2.1")
52
+ gr.Markdown("Type your prompt or select an example below to generate high-quality images.")
53
+
54
+ with gr.Row():
55
+ prompt = gr.Textbox(label="Enter your prompt", placeholder="e.g. A girl sitting on a hill in a summer breeze 🌼")
56
+ generate_btn = gr.Button("🚀 Generate Image")
57
+
58
+ with gr.Row():
59
+ with gr.Column():
60
+ gallery = gr.Image(label="Generated Image", type="filepath", interactive=False)
61
+ download = gr.File(label="Download Image")
62
+
63
+ with gr.Column():
64
+ gr.Examples(examples=examples, inputs=prompt)
65
+
66
+ generate_btn.click(fn=generate_image, inputs=prompt, outputs=[gallery, download])
67
+ # Launch Gradio app with a public link
68
+
69
+ # --- Launch App ---
70
+ demo.launch(share=True)
71
+
72
+