Spaces:
Build error
Build error
Refactor: Improve error handling and add comments in app.py
Browse files
app.py
CHANGED
|
@@ -1,6 +1,5 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
-
from io import BytesIO
|
| 4 |
from PIL import Image
|
| 5 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 6 |
|
|
@@ -12,21 +11,30 @@ pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=Co
|
|
| 12 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
def generate_image(control_image, additional_prompt, reference_image1, reference_image2):
|
|
|
|
|
|
|
|
|
|
| 15 |
try:
|
| 16 |
# 第一步:使用 canny 模型生成初始图像
|
| 17 |
initial_image = pipe(prompt=additional_prompt, control_image=control_image).images[0]
|
|
|
|
|
|
|
| 18 |
except Exception as e:
|
| 19 |
return f"Error in Step 1: {str(e)}"
|
| 20 |
|
| 21 |
try:
|
| 22 |
# 第二步:使用第一张参考图像进行 reference_adain+attn 生成
|
| 23 |
step2_image = pipe(prompt=additional_prompt, control_image=reference_image1).images[0]
|
|
|
|
|
|
|
| 24 |
except Exception as e:
|
| 25 |
return f"Error in Step 2: {str(e)}"
|
| 26 |
|
| 27 |
try:
|
| 28 |
# 第三步:使用第二张参考图像进行 reference_adain+attn 生成
|
| 29 |
final_image = pipe(prompt=additional_prompt, control_image=reference_image2).images[0]
|
|
|
|
|
|
|
| 30 |
return final_image
|
| 31 |
except Exception as e:
|
| 32 |
return f"Error in Step 3: {str(e)}"
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 5 |
|
|
|
|
| 11 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 12 |
|
| 13 |
def generate_image(control_image, additional_prompt, reference_image1, reference_image2):
|
| 14 |
+
if control_image is None or reference_image1 is None or reference_image2 is None:
|
| 15 |
+
return "Error: One or more input images are None."
|
| 16 |
+
|
| 17 |
try:
|
| 18 |
# 第一步:使用 canny 模型生成初始图像
|
| 19 |
initial_image = pipe(prompt=additional_prompt, control_image=control_image).images[0]
|
| 20 |
+
if initial_image is None:
|
| 21 |
+
return "Error in Step 1: Generated image is None."
|
| 22 |
except Exception as e:
|
| 23 |
return f"Error in Step 1: {str(e)}"
|
| 24 |
|
| 25 |
try:
|
| 26 |
# 第二步:使用第一张参考图像进行 reference_adain+attn 生成
|
| 27 |
step2_image = pipe(prompt=additional_prompt, control_image=reference_image1).images[0]
|
| 28 |
+
if step2_image is None:
|
| 29 |
+
return "Error in Step 2: Generated image is None."
|
| 30 |
except Exception as e:
|
| 31 |
return f"Error in Step 2: {str(e)}"
|
| 32 |
|
| 33 |
try:
|
| 34 |
# 第三步:使用第二张参考图像进行 reference_adain+attn 生成
|
| 35 |
final_image = pipe(prompt=additional_prompt, control_image=reference_image2).images[0]
|
| 36 |
+
if final_image is None:
|
| 37 |
+
return "Error in Step 3: Generated image is None."
|
| 38 |
return final_image
|
| 39 |
except Exception as e:
|
| 40 |
return f"Error in Step 3: {str(e)}"
|