Spaces:
Build error
Build error
Update: Refactored the app.py file to improve error handling and add comments.
Browse files
app.py
CHANGED
|
@@ -5,41 +5,50 @@ from PIL import Image
|
|
| 5 |
from io import BytesIO
|
| 6 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 7 |
|
| 8 |
-
# 加
|
| 9 |
model_id = "CompVis/stable-diffusion-v1-4"
|
| 10 |
controlnet_id = "lllyasviel/sd-controlnet-canny"
|
| 11 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_id))
|
| 12 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
-
# 定
|
| 15 |
def get_clip_interrogator_prompt(image):
|
| 16 |
url = "https://huggingface.co/spaces/fffiloni/CLIP-Interrogator-2/run/predict"
|
| 17 |
buffered = BytesIO()
|
| 18 |
image.save(buffered, format="JPEG")
|
| 19 |
img_str = buffered.getvalue()
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
def generate_image(control_image, additional_prompt):
|
| 25 |
-
# 使用CLIP-Interrogator-2
|
| 26 |
base_prompt = get_clip_interrogator_prompt(control_image)
|
|
|
|
|
|
|
| 27 |
|
| 28 |
-
#
|
| 29 |
combined_prompt = base_prompt + " " + additional_prompt
|
| 30 |
|
| 31 |
-
# 生成
|
| 32 |
-
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
-
# 定
|
| 36 |
interface = gr.Interface(
|
| 37 |
fn=generate_image,
|
| 38 |
inputs=[
|
| 39 |
-
gr.
|
| 40 |
-
gr.
|
| 41 |
],
|
| 42 |
-
outputs=gr.
|
| 43 |
title="Stable Diffusion with ControlNet and CLIP Interrogator 2",
|
| 44 |
description="Generate images using Stable Diffusion, ControlNet, and CLIP Interrogator 2."
|
| 45 |
)
|
|
|
|
| 5 |
from io import BytesIO
|
| 6 |
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
|
| 7 |
|
| 8 |
+
# 加載模型和ControlNet
|
| 9 |
model_id = "CompVis/stable-diffusion-v1-4"
|
| 10 |
controlnet_id = "lllyasviel/sd-controlnet-canny"
|
| 11 |
pipe = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_id))
|
| 12 |
pipe = pipe.to("cuda" if torch.cuda.is_available() else "cpu")
|
| 13 |
|
| 14 |
+
# 定義函數來調用CLIP-Interrogator-2
|
| 15 |
def get_clip_interrogator_prompt(image):
|
| 16 |
url = "https://huggingface.co/spaces/fffiloni/CLIP-Interrogator-2/run/predict"
|
| 17 |
buffered = BytesIO()
|
| 18 |
image.save(buffered, format="JPEG")
|
| 19 |
img_str = buffered.getvalue()
|
| 20 |
+
try:
|
| 21 |
+
response = requests.post(url, files={"file": img_str}, timeout=120)
|
| 22 |
+
response.raise_for_status() # 檢查請求是否成功
|
| 23 |
+
prompt = response.json()["data"][0]["caption"]
|
| 24 |
+
return prompt
|
| 25 |
+
except requests.exceptions.RequestException as e:
|
| 26 |
+
return f"Error: {str(e)}"
|
| 27 |
|
| 28 |
def generate_image(control_image, additional_prompt):
|
| 29 |
+
# 使用CLIP-Interrogator-2獲取圖片的提示詞
|
| 30 |
base_prompt = get_clip_interrogator_prompt(control_image)
|
| 31 |
+
if base_prompt.startswith("Error:"):
|
| 32 |
+
return base_prompt
|
| 33 |
|
| 34 |
+
# 結合用戶提供的附加提示詞
|
| 35 |
combined_prompt = base_prompt + " " + additional_prompt
|
| 36 |
|
| 37 |
+
# 生成圖像
|
| 38 |
+
try:
|
| 39 |
+
result = pipe(prompt=combined_prompt, control_image=control_image).images[0]
|
| 40 |
+
return result
|
| 41 |
+
except Exception as e:
|
| 42 |
+
return f"Error: {str(e)}"
|
| 43 |
|
| 44 |
+
# 定義Gradio接口
|
| 45 |
interface = gr.Interface(
|
| 46 |
fn=generate_image,
|
| 47 |
inputs=[
|
| 48 |
+
gr.components.Image(type="pil", label="Control Image"),
|
| 49 |
+
gr.components.Textbox(label="Additional Prompt")
|
| 50 |
],
|
| 51 |
+
outputs=gr.components.Image(type="pil", label="Generated Image"),
|
| 52 |
title="Stable Diffusion with ControlNet and CLIP Interrogator 2",
|
| 53 |
description="Generate images using Stable Diffusion, ControlNet, and CLIP Interrogator 2."
|
| 54 |
)
|