dprat0821 commited on
Commit
e8c7aaa
·
verified ·
1 Parent(s): 99c697a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -60
app.py CHANGED
@@ -1,65 +1,86 @@
1
  import gradio as gr
2
  import torch
3
- import numpy as np
4
- import cv2
5
  from PIL import Image
6
- from diffusers import (
7
- StableDiffusionControlNetPipeline,
8
- ControlNetModel,
9
- UniPCMultistepScheduler
10
- )
11
-
12
- # Load ControlNet model (Canny)
13
- controlnet = ControlNetModel.from_pretrained(
14
- "lllyasviel/sd-controlnet-canny", torch_dtype=torch.float32
15
- )
16
-
17
- # Load Stable Diffusion pipeline with ControlNet
18
- pipe = StableDiffusionControlNetPipeline.from_pretrained(
19
- "runwayml/stable-diffusion-v1-5",
20
- controlnet=controlnet,
21
- torch_dtype=torch.float32
22
- )
23
-
24
- # Set the scheduler
25
- pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
26
-
27
- # Move the pipeline to the appropriate device
28
- pipe.to("cuda" if torch.cuda.is_available() else "cpu")
29
-
30
- def process_and_generate(image, prompt, num_inference_steps, guidance_scale):
31
- # Convert PIL image to numpy array
32
- image = np.array(image)
33
-
34
- # Apply Canny edge detection
35
- image = cv2.Canny(image, 100, 200)
36
- image = image[:, :, None]
37
- image = np.concatenate([image, image, image], axis=2)
38
- canny_image = Image.fromarray(image)
39
-
40
- # Generate image using the pipeline
41
- result = pipe(
42
- prompt=prompt,
43
- image=canny_image,
44
- num_inference_steps=num_inference_steps,
45
- guidance_scale=guidance_scale,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
46
  )
47
 
48
- return result.images[0]
49
-
50
- # Define the Gradio interface
51
- iface = gr.Interface(
52
- fn=process_and_generate,
53
- inputs=[
54
- gr.Image(type="pil", label="Input Image"),
55
- gr.Textbox(label="Prompt"),
56
- gr.Slider(1, 100, value=50, step=1, label="Number of Inference Steps"),
57
- gr.Slider(0.1, 10.0, value=7.5, step=0.1, label="Guidance Scale"),
58
- ],
59
- outputs=gr.Image(type="pil", label="Generated Image"),
60
- title="🧠 Stable Diffusion with ControlNet (Canny)",
61
- description="Upload an image and enter a prompt. The system uses Canny edge detection to guide Stable Diffusion generation.",
62
- )
63
-
64
- # Launch the app
65
- iface.launch()
 
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)