Spaces:
Sleeping
Sleeping
Refactor: Improve error handling and add image loading from URLs in app.py
Browse files
app.py
CHANGED
@@ -1,8 +1,6 @@
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from PIL import Image
|
4 |
-
from io import BytesIO
|
5 |
-
import requests
|
6 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
7 |
|
8 |
# 加载模型和ControlNet
|
@@ -12,43 +10,35 @@ controlnet_id = "lllyasviel/sd-controlnet-canny"
|
|
12 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_id))
|
13 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
14 |
|
15 |
-
def
|
16 |
-
|
17 |
-
response = requests.get(url)
|
18 |
-
response.raise_for_status()
|
19 |
-
return Image.open(BytesIO(response.content)).convert("RGB")
|
20 |
-
except Exception as e:
|
21 |
-
return None
|
22 |
|
23 |
-
def generate_image(
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
if control_image is None or reference_image1 is None or reference_image2 is None:
|
29 |
-
return "Error: One or more input images could not be loaded."
|
30 |
|
31 |
try:
|
32 |
# 第一步:使用 canny 模型生成初始图像
|
33 |
initial_image = pipe(prompt=additional_prompt, control_image=control_image).images[0]
|
34 |
-
if initial_image
|
35 |
-
return "Error in Step 1: Generated image is
|
36 |
except Exception as e:
|
37 |
return f"Error in Step 1: {str(e)}"
|
38 |
|
39 |
try:
|
40 |
# 第二步:使用第一张参考图像进行 reference_adain+attn 生成
|
41 |
step2_image = pipe(prompt=additional_prompt, control_image=reference_image1).images[0]
|
42 |
-
if step2_image
|
43 |
-
return "Error in Step 2: Generated image is
|
44 |
except Exception as e:
|
45 |
return f"Error in Step 2: {str(e)}"
|
46 |
|
47 |
try:
|
48 |
# 第三步:使用第二张参考图像进行 reference_adain+attn 生成
|
49 |
final_image = pipe(prompt=additional_prompt, control_image=reference_image2).images[0]
|
50 |
-
if final_image
|
51 |
-
return "Error in Step 3: Generated image is
|
52 |
return final_image
|
53 |
except Exception as e:
|
54 |
return f"Error in Step 3: {str(e)}"
|
@@ -57,10 +47,10 @@ def generate_image(control_image_path, additional_prompt, reference_image1_path,
|
|
57 |
interface = gr.Interface(
|
58 |
fn=generate_image,
|
59 |
inputs=[
|
60 |
-
gr.components.
|
61 |
gr.components.Textbox(label="Additional Prompt"),
|
62 |
-
gr.components.
|
63 |
-
gr.components.
|
64 |
],
|
65 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
66 |
title="Stable Diffusion with Multi-Step ControlNet",
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
from PIL import Image
|
|
|
|
|
4 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
5 |
|
6 |
# 加载模型和ControlNet
|
|
|
10 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_id))
|
11 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
12 |
|
13 |
+
def is_valid_pil_image(image):
|
14 |
+
return isinstance(image, Image.Image)
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
+
def generate_image(control_image, additional_prompt, reference_image1, reference_image2):
|
17 |
+
# 检查输入图像是否为有效的 PIL 图像
|
18 |
+
if not all(is_valid_pil_image(img) for img in [control_image, reference_image1, reference_image2]):
|
19 |
+
return "Error: One or more input images are not valid PIL images."
|
|
|
|
|
|
|
20 |
|
21 |
try:
|
22 |
# 第一步:使用 canny 模型生成初始图像
|
23 |
initial_image = pipe(prompt=additional_prompt, control_image=control_image).images[0]
|
24 |
+
if not is_valid_pil_image(initial_image):
|
25 |
+
return "Error in Step 1: Generated image is not a valid PIL image."
|
26 |
except Exception as e:
|
27 |
return f"Error in Step 1: {str(e)}"
|
28 |
|
29 |
try:
|
30 |
# 第二步:使用第一张参考图像进行 reference_adain+attn 生成
|
31 |
step2_image = pipe(prompt=additional_prompt, control_image=reference_image1).images[0]
|
32 |
+
if not is_valid_pil_image(step2_image):
|
33 |
+
return "Error in Step 2: Generated image is not a valid PIL image."
|
34 |
except Exception as e:
|
35 |
return f"Error in Step 2: {str(e)}"
|
36 |
|
37 |
try:
|
38 |
# 第三步:使用第二张参考图像进行 reference_adain+attn 生成
|
39 |
final_image = pipe(prompt=additional_prompt, control_image=reference_image2).images[0]
|
40 |
+
if not is_valid_pil_image(final_image):
|
41 |
+
return "Error in Step 3: Generated image is not a valid PIL image."
|
42 |
return final_image
|
43 |
except Exception as e:
|
44 |
return f"Error in Step 3: {str(e)}"
|
|
|
47 |
interface = gr.Interface(
|
48 |
fn=generate_image,
|
49 |
inputs=[
|
50 |
+
gr.components.Image(type="pil", label="Control Image"),
|
51 |
gr.components.Textbox(label="Additional Prompt"),
|
52 |
+
gr.components.Image(type="pil", label="Reference Image 1"),
|
53 |
+
gr.components.Image(type="pil", label="Reference Image 2")
|
54 |
],
|
55 |
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
56 |
title="Stable Diffusion with Multi-Step ControlNet",
|