Spaces:
Build error
Build error
Refactor: Update app.py to use diffusers_xl_canny_full and lllyasviel/sd-controlnet-canny models for multi-step generation
Browse files
app.py
CHANGED
|
@@ -87,7 +87,10 @@
|
|
| 87 |
|
| 88 |
import gradio as gr
|
| 89 |
import torch
|
| 90 |
-
from diffusers import
|
|
|
|
|
|
|
|
|
|
| 91 |
import logging
|
| 92 |
from PIL import Image
|
| 93 |
|
|
@@ -95,24 +98,44 @@ from PIL import Image
|
|
| 95 |
logging.basicConfig(level=logging.DEBUG)
|
| 96 |
|
| 97 |
# 加载模型
|
| 98 |
-
model_id = "runwayml/stable-diffusion-v1-5"
|
|
|
|
| 99 |
device = "cpu"
|
| 100 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
pipe = pipe.to(device)
|
| 102 |
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
| 106 |
|
| 107 |
-
def generate_image(prompt):
|
| 108 |
try:
|
| 109 |
logging.debug("Starting image generation process...")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
| 113 |
with torch.no_grad():
|
| 114 |
-
generated_images = pipe(
|
| 115 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 116 |
if not generated_images or not isinstance(generated_images[0], Image.Image):
|
| 117 |
error_message = "Error: Generated image is not valid."
|
| 118 |
logging.error(error_message)
|
|
@@ -121,22 +144,21 @@ def generate_image(prompt):
|
|
| 121 |
final_image = generated_images[0]
|
| 122 |
logging.debug("Image generation process completed successfully.")
|
| 123 |
return final_image
|
| 124 |
-
except TimeoutError as te:
|
| 125 |
-
error_message = f"Error: Image generation timed out. {str(te)}"
|
| 126 |
-
logging.error(error_message)
|
| 127 |
-
return error_message
|
| 128 |
except Exception as e:
|
| 129 |
error_message = f"Error: {str(e)}"
|
| 130 |
logging.error(error_message)
|
| 131 |
return error_message
|
| 132 |
|
| 133 |
-
# 定义Gradio接口
|
| 134 |
interface = gr.Interface(
|
| 135 |
fn=generate_image,
|
| 136 |
-
inputs=
|
|
|
|
|
|
|
|
|
|
| 137 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
| 138 |
-
title="
|
| 139 |
-
description="Generate images using a
|
| 140 |
)
|
| 141 |
|
| 142 |
if __name__ == "__main__":
|
|
|
|
| 87 |
|
| 88 |
import gradio as gr
|
| 89 |
import torch
|
| 90 |
+
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 91 |
+
from diffusers.utils import load_image
|
| 92 |
+
import cv2
|
| 93 |
+
import numpy as np
|
| 94 |
import logging
|
| 95 |
from PIL import Image
|
| 96 |
|
|
|
|
| 98 |
logging.basicConfig(level=logging.DEBUG)
|
| 99 |
|
| 100 |
# 加载模型
|
| 101 |
+
model_id = "runwayml/stable-diffusion-v1-5"
|
| 102 |
+
controlnet_id = "lllyasviel/sd-controlnet-canny"
|
| 103 |
device = "cpu"
|
| 104 |
+
|
| 105 |
+
# 加载 ControlNet 模型
|
| 106 |
+
controlnet = ControlNetModel.from_pretrained(controlnet_id, torch_dtype=torch.float32)
|
| 107 |
+
|
| 108 |
+
# 加载 Stable Diffusion 管道
|
| 109 |
+
pipe = StableDiffusionControlNetPipeline.from_pretrained(
|
| 110 |
+
model_id,
|
| 111 |
+
controlnet=controlnet,
|
| 112 |
+
use_safetensors=True,
|
| 113 |
+
torch_dtype=torch.float32
|
| 114 |
+
)
|
| 115 |
pipe = pipe.to(device)
|
| 116 |
|
| 117 |
+
def canny_edge_detection(image):
|
| 118 |
+
image = np.array(image)
|
| 119 |
+
edges = cv2.Canny(image, 100, 200)
|
| 120 |
+
return Image.fromarray(edges)
|
| 121 |
|
| 122 |
+
def generate_image(prompt, input_image):
|
| 123 |
try:
|
| 124 |
logging.debug("Starting image generation process...")
|
| 125 |
|
| 126 |
+
# 进行 Canny 边缘检测
|
| 127 |
+
canny_image = canny_edge_detection(input_image)
|
| 128 |
+
|
| 129 |
+
# 使用 ControlNet 和 Stable Diffusion 模型生成图像
|
| 130 |
with torch.no_grad():
|
| 131 |
+
generated_images = pipe(
|
| 132 |
+
prompt=prompt,
|
| 133 |
+
image=canny_image,
|
| 134 |
+
height=500,
|
| 135 |
+
width=750,
|
| 136 |
+
num_inference_steps=30
|
| 137 |
+
).images
|
| 138 |
+
|
| 139 |
if not generated_images or not isinstance(generated_images[0], Image.Image):
|
| 140 |
error_message = "Error: Generated image is not valid."
|
| 141 |
logging.error(error_message)
|
|
|
|
| 144 |
final_image = generated_images[0]
|
| 145 |
logging.debug("Image generation process completed successfully.")
|
| 146 |
return final_image
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
except Exception as e:
|
| 148 |
error_message = f"Error: {str(e)}"
|
| 149 |
logging.error(error_message)
|
| 150 |
return error_message
|
| 151 |
|
| 152 |
+
# 定义 Gradio 接口
|
| 153 |
interface = gr.Interface(
|
| 154 |
fn=generate_image,
|
| 155 |
+
inputs=[
|
| 156 |
+
gr.components.Textbox(label="Prompt"),
|
| 157 |
+
gr.components.Image(type="pil", label="Input Image")
|
| 158 |
+
],
|
| 159 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
| 160 |
+
title="Stable Diffusion with ControlNet Canny",
|
| 161 |
+
description="Generate images using a prompt and an input image with Stable Diffusion and ControlNet Canny on CPU."
|
| 162 |
)
|
| 163 |
|
| 164 |
if __name__ == "__main__":
|