Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
| 3 |
-
from diffusers import DDIMScheduler, EulerAncestralDiscreteScheduler
|
| 4 |
import torch
|
| 5 |
import numpy as np
|
| 6 |
import cv2
|
|
@@ -12,11 +11,9 @@ device = "cuda"
|
|
| 12 |
precision = torch.float16
|
| 13 |
|
| 14 |
|
| 15 |
-
eulera_scheduler = EulerAncestralDiscreteScheduler.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", subfolder="scheduler")
|
| 16 |
-
|
| 17 |
# 🏗️ Load ControlNet model for Canny edge detection
|
| 18 |
controlnet = ControlNetModel.from_pretrained(
|
| 19 |
-
"
|
| 20 |
torch_dtype=precision
|
| 21 |
)
|
| 22 |
|
|
@@ -28,22 +25,22 @@ pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
|
|
| 28 |
controlnet=controlnet,
|
| 29 |
vae=vae,
|
| 30 |
torch_dtype=precision,
|
| 31 |
-
scheduler=eulera_scheduler,
|
| 32 |
)
|
| 33 |
|
|
|
|
| 34 |
pipe.to(device)
|
| 35 |
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
|
| 39 |
# 📸 Edge detection function using OpenCV (Canny)
|
| 40 |
@spaces.GPU
|
| 41 |
def apply_canny(image, low_threshold, high_threshold):
|
| 42 |
-
image = np.array(image
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
return Image.fromarray(
|
| 47 |
|
| 48 |
# 🎨 Image generation function
|
| 49 |
@spaces.GPU
|
|
@@ -57,6 +54,7 @@ def generate_image(prompt, input_image, low_threshold, high_threshold, strength,
|
|
| 57 |
image=edge_detected,
|
| 58 |
num_inference_steps=30,
|
| 59 |
guidance_scale=guidance,
|
|
|
|
| 60 |
strength=strength
|
| 61 |
).images[0]
|
| 62 |
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from diffusers import ControlNetModel, StableDiffusionXLControlNetPipeline, AutoencoderKL
|
|
|
|
| 3 |
import torch
|
| 4 |
import numpy as np
|
| 5 |
import cv2
|
|
|
|
| 11 |
precision = torch.float16
|
| 12 |
|
| 13 |
|
|
|
|
|
|
|
| 14 |
# 🏗️ Load ControlNet model for Canny edge detection
|
| 15 |
controlnet = ControlNetModel.from_pretrained(
|
| 16 |
+
"diffusers/controlnet-canny-sdxl-1.0",
|
| 17 |
torch_dtype=precision
|
| 18 |
)
|
| 19 |
|
|
|
|
| 25 |
controlnet=controlnet,
|
| 26 |
vae=vae,
|
| 27 |
torch_dtype=precision,
|
|
|
|
| 28 |
)
|
| 29 |
|
| 30 |
+
pipe.enable_model_cpu_offload()
|
| 31 |
pipe.to(device)
|
| 32 |
|
| 33 |
+
## to move in the function
|
| 34 |
+
controlnet_conditioning_scale = 0.5 # recommended for good generalization
|
| 35 |
|
| 36 |
# 📸 Edge detection function using OpenCV (Canny)
|
| 37 |
@spaces.GPU
|
| 38 |
def apply_canny(image, low_threshold, high_threshold):
|
| 39 |
+
image = np.array(image)
|
| 40 |
+
image = cv2.Canny(image, low_threshold, high_threshold)
|
| 41 |
+
image = image[:, :, None]
|
| 42 |
+
image = np.concatenate([image, image, image], axis=2)
|
| 43 |
+
return Image.fromarray(image)
|
| 44 |
|
| 45 |
# 🎨 Image generation function
|
| 46 |
@spaces.GPU
|
|
|
|
| 54 |
image=edge_detected,
|
| 55 |
num_inference_steps=30,
|
| 56 |
guidance_scale=guidance,
|
| 57 |
+
controlnet_conditioning_scale=controlnet_conditioning_scale,
|
| 58 |
strength=strength
|
| 59 |
).images[0]
|
| 60 |
|