Update app.py
Browse files
app.py
CHANGED
|
@@ -3,21 +3,30 @@ import torch
|
|
| 3 |
import numpy as np
|
| 4 |
import cv2
|
| 5 |
from PIL import Image
|
| 6 |
-
from diffusers import
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
# Load ControlNet model
|
| 9 |
controlnet = ControlNetModel.from_pretrained(
|
| 10 |
"lllyasviel/sd-controlnet-canny", torch_dtype=torch.float32
|
| 11 |
)
|
| 12 |
|
| 13 |
# Load Stable Diffusion pipeline with ControlNet
|
| 14 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 15 |
-
"runwayml/stable-diffusion-v1-5",
|
|
|
|
|
|
|
| 16 |
)
|
| 17 |
|
| 18 |
# Set the scheduler
|
| 19 |
pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
|
| 20 |
|
|
|
|
|
|
|
|
|
|
| 21 |
def process_and_generate(image, prompt, num_inference_steps, guidance_scale):
|
| 22 |
# Convert PIL image to numpy array
|
| 23 |
image = np.array(image)
|
|
@@ -29,14 +38,14 @@ def process_and_generate(image, prompt, num_inference_steps, guidance_scale):
|
|
| 29 |
canny_image = Image.fromarray(image)
|
| 30 |
|
| 31 |
# Generate image using the pipeline
|
| 32 |
-
|
| 33 |
-
prompt,
|
| 34 |
-
canny_image,
|
| 35 |
num_inference_steps=num_inference_steps,
|
| 36 |
guidance_scale=guidance_scale,
|
| 37 |
-
)
|
| 38 |
|
| 39 |
-
return
|
| 40 |
|
| 41 |
# Define the Gradio interface
|
| 42 |
iface = gr.Interface(
|
|
@@ -48,9 +57,9 @@ iface = gr.Interface(
|
|
| 48 |
gr.Slider(0.1, 10.0, value=7.5, step=0.1, label="Guidance Scale"),
|
| 49 |
],
|
| 50 |
outputs=gr.Image(type="pil", label="Generated Image"),
|
| 51 |
-
title="Stable Diffusion with ControlNet",
|
| 52 |
-
description="
|
| 53 |
)
|
| 54 |
|
| 55 |
-
# Launch the
|
| 56 |
-
iface.launch()
|
|
|
|
| 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)
|
|
|
|
| 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(
|
|
|
|
| 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()
|