Edison commited on
Commit
6a7f883
1 Parent(s): b101645

Refactor: Improve error handling and add image loading from URLs in app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -25
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 load_image_from_url(url):
16
- try:
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(control_image_path, additional_prompt, reference_image1_path, reference_image2_path):
24
- control_image = load_image_from_url(control_image_path["path"])
25
- reference_image1 = load_image_from_url(reference_image1_path["path"])
26
- reference_image2 = load_image_from_url(reference_image2_path["path"])
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 is None:
35
- return "Error in Step 1: Generated image is None."
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 is None:
43
- return "Error in Step 2: Generated image is None."
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 is None:
51
- return "Error in Step 3: Generated image is None."
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.Textbox(label="Control Image URL"),
61
  gr.components.Textbox(label="Additional Prompt"),
62
- gr.components.Textbox(label="Reference Image 1 URL"),
63
- gr.components.Textbox(label="Reference Image 2 URL")
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",