dprat0821 commited on
Commit
cc24143
·
verified ·
1 Parent(s): 038805f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +87 -0
app.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ from PIL import Image
5
+ import time
6
+ import traceback
7
+ from typing import Optional
8
+
9
+ # ---- Configuration ----
10
+ model_id: str = "runwayml/stable-diffusion-v1-5"
11
+ device: str = "cpu" # force CPU usage for compatibility
12
+
13
+ # ---- Load Model ----
14
+ image_generator_pipe: Optional[StableDiffusionPipeline] = None
15
+
16
+ try:
17
+ print(f"Loading Stable Diffusion pipeline ({model_id}) on CPU...")
18
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
19
+ image_generator_pipe = pipe.to(device)
20
+ print("Stable Diffusion pipeline loaded successfully.")
21
+ except Exception as e:
22
+ print(f"Failed to load Stable Diffusion model: {e}")
23
+ traceback.print_exc()
24
+
25
+ # ---- Core Image Generation Function ----
26
+ def generate_image_sd(prompt: str, negative_prompt: str, guidance_scale: float, num_inference_steps: int) -> Image.Image:
27
+ if not image_generator_pipe:
28
+ raise gr.Error("Stable Diffusion pipeline failed to load. Image generation unavailable.")
29
+
30
+ if not prompt.strip():
31
+ raise gr.Error("Prompt cannot be empty.")
32
+
33
+ print(f"Generating image with prompt: {prompt[:100]}...")
34
+ print(f"Negative prompt: {negative_prompt}")
35
+ print(f"Guidance scale: {guidance_scale}, Steps: {num_inference_steps}")
36
+
37
+ start_time = time.time()
38
+
39
+ try:
40
+ with torch.no_grad():
41
+ output = image_generator_pipe(
42
+ prompt=prompt,
43
+ negative_prompt=negative_prompt,
44
+ guidance_scale=guidance_scale,
45
+ num_inference_steps=num_inference_steps
46
+ )
47
+ image = output.images[0] if output.images else None
48
+
49
+ if not image:
50
+ raise RuntimeError("No image was returned from the generation pipeline.")
51
+
52
+ end_time = time.time()
53
+ print(f"Image generated in {end_time - start_time:.2f} seconds.")
54
+ return image
55
+
56
+ except Exception as e:
57
+ print(f"Error generating image: {e}")
58
+ traceback.print_exc()
59
+ raise gr.Error(f"Image generation failed: {e}")
60
+
61
+ # ---- Gradio UI ----
62
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
63
+ gr.Markdown("# Stable Diffusion Image Generator (CPU Mode)")
64
+
65
+ with gr.Row():
66
+ with gr.Column(scale=1):
67
+ prompt = gr.Textbox(label="Prompt", placeholder="A beautiful futuristic city skyline at night")
68
+ neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="blurry, distorted, watermark")
69
+ guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="Guidance Scale")
70
+ steps = gr.Slider(10, 50, value=25, step=1, label="Inference Steps")
71
+ generate_btn = gr.Button("Generate Image")
72
+
73
+ with gr.Column(scale=1):
74
+ output_image = gr.Image(label="Generated Image", type="pil")
75
+
76
+ generate_btn.click(
77
+ fn=generate_image_sd,
78
+ inputs=[prompt, neg_prompt, guidance, steps],
79
+ outputs=output_image
80
+ )
81
+
82
+ # ---- Launch ----
83
+ if __name__ == "__main__":
84
+ if not image_generator_pipe:
85
+ print("WARNING: Image generator pipeline is not available. UI will launch, but generation will fail.")
86
+ demo.launch(server_name="0.0.0.0", server_port=7860)
87
+