Edison commited on
Commit
5087b82
·
1 Parent(s): 04c6f43

Update: Refactored the app.py file to improve error handling and add comments.

Browse files
Files changed (1) hide show
  1. app.py +23 -14
app.py CHANGED
@@ -5,41 +5,50 @@ from PIL import Image
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
- response = requests.post(url, files={"file": img_str})
21
- prompt = response.json()["data"][0]["caption"]
22
- return prompt
 
 
 
 
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
- result = pipe(prompt=combined_prompt, control_image=control_image).images[0]
33
- return result
 
 
 
34
 
35
- # 定义Gradio接口
36
  interface = gr.Interface(
37
  fn=generate_image,
38
  inputs=[
39
- gr.inputs.Image(type="pil", label="Control Image"),
40
- gr.inputs.Textbox(label="Additional Prompt")
41
  ],
42
- outputs=gr.outputs.Image(type="pil", label="Generated Image"),
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
  )