dprat0821 commited on
Commit
545bd19
·
verified ·
1 Parent(s): d6d0e65

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +51 -0
app.py ADDED
@@ -0,0 +1,51 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from diffusers import StableDiffusionPipeline
4
+ from PIL import Image
5
+ import traceback
6
+ from typing import Optional
7
+
8
+ model_id: str = "runwayml/stable-diffusion-v1-5"
9
+ device: str = "cpu" # force CPU usage for compatibility
10
+
11
+ image_generator_pipe: Optional[StableDiffusionPipeline] = None
12
+ pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float32)
13
+ image_generator_pipe = pipe.to(device)
14
+
15
+ def generate_image_sd(prompt: str, negative_prompt: str, guidance_scale: float, num_inference_steps: int) -> Image.Image:
16
+ with torch.no_grad():
17
+ output = image_generator_pipe(
18
+ prompt=prompt,
19
+ negative_prompt=negative_prompt,
20
+ guidance_scale=guidance_scale,
21
+ num_inference_steps=num_inference_steps
22
+ )
23
+ image = output.images[0] if output.images else None
24
+
25
+ if not image:
26
+ raise RuntimeError("No image was returned from the generation pipeline.")
27
+ return image
28
+
29
+
30
+ with gr.Blocks(theme=gr.themes.Soft()) as demo:
31
+ with gr.Row():
32
+ with gr.Column(scale=1):
33
+ prompt = gr.Textbox(label="Prompt", placeholder="A beautiful futuristic city skyline at night")
34
+ neg_prompt = gr.Textbox(label="Negative Prompt", placeholder="blurry, distorted, watermark")
35
+ guidance = gr.Slider(1.0, 15.0, value=7.5, step=0.5, label="Guidance Scale")
36
+ steps = gr.Slider(10, 50, value=25, step=1, label="Inference Steps")
37
+ generate_btn = gr.Button("Generate Image")
38
+
39
+ with gr.Column(scale=1):
40
+ output_image = gr.Image(label="Generated Image", type="pil")
41
+
42
+ generate_btn.click(
43
+ fn=generate_image_sd,
44
+ inputs=[prompt, neg_prompt, guidance, steps],
45
+ outputs=output_image
46
+ )
47
+
48
+ if __name__ == "__main__":
49
+ if not image_generator_pipe:
50
+ print("WARNING: Image generator pipeline is not available. UI will launch, but generation will fail.")
51
+ demo.launch(server_name="0.0.0.0", server_port=7860)