Edison commited on
Commit
68ccd12
·
1 Parent(s): 80427d7

Update: Update app.py to use diffusers_xl_canny_full and lllyasviel/sd-controlnet-canny models for multi-step generation

Browse files
Files changed (1) hide show
  1. app.py +34 -19
app.py CHANGED
@@ -1,17 +1,22 @@
1
  import gradio as gr
2
  import torch
3
- import requests
4
- 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()
@@ -19,38 +24,48 @@ def get_clip_interrogator_prompt(image):
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
  )
55
 
56
  if __name__ == "__main__":
 
1
  import gradio as gr
2
  import torch
 
 
3
  from io import BytesIO
4
+ from PIL import Image
5
+ import requests
6
  from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
7
 
8
  # 加載模型和ControlNet
9
  model_id = "CompVis/stable-diffusion-v1-4"
10
+ controlnet_canny_id = "diffusers_xl_canny_full"
11
+ controlnet_adain_attn_id = "lllyasviel/sd-controlnet-canny"
 
12
 
13
+ pipe_canny = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_canny_id))
14
+ pipe_adain_attn = StableDiffusionControlNetPipeline.from_pretrained(model_id, controlnet=ControlNetModel.from_pretrained(controlnet_adain_attn_id))
15
+
16
+ pipe_canny = pipe_canny.to("cuda" if torch.cuda.is_available() else "cpu")
17
+ pipe_adain_attn = pipe_adain_attn.to("cuda" if torch.cuda.is_available() else "cpu")
18
+
19
+ # 定义函数来调用CLIP-Interrogator-2
20
  def get_clip_interrogator_prompt(image):
21
  url = "https://huggingface.co/spaces/fffiloni/CLIP-Interrogator-2/run/predict"
22
  buffered = BytesIO()
 
24
  img_str = buffered.getvalue()
25
  try:
26
  response = requests.post(url, files={"file": img_str}, timeout=120)
27
+ response.raise_for_status() # 检查请求是否成功
28
  prompt = response.json()["data"][0]["caption"]
29
  return prompt
30
  except requests.exceptions.RequestException as e:
31
  return f"Error: {str(e)}"
32
 
33
+ def generate_image(control_image, additional_prompt, reference_image1, reference_image2):
34
+ # 第一步:使用 diffusers_xl_canny_full 模型生成初始图像
35
  base_prompt = get_clip_interrogator_prompt(control_image)
36
  if base_prompt.startswith("Error:"):
37
  return base_prompt
 
 
38
  combined_prompt = base_prompt + " " + additional_prompt
39
+ try:
40
+ initial_image = pipe_canny(prompt=combined_prompt, control_image=control_image).images[0]
41
+ except Exception as e:
42
+ return f"Error in Step 1: {str(e)}"
43
 
44
+ # 第二步:使用第一张参考图像进行 reference_adain+attn 生成
45
  try:
46
+ step2_image = pipe_adain_attn(prompt=combined_prompt, control_image=reference_image1).images[0]
 
47
  except Exception as e:
48
+ return f"Error in Step 2: {str(e)}"
49
+
50
+ # 第三步:使用第二张参考图像进行 reference_adain+attn 生成
51
+ try:
52
+ final_image = pipe_adain_attn(prompt=combined_prompt, control_image=reference_image2).images[0]
53
+ return final_image
54
+ except Exception as e:
55
+ return f"Error in Step 3: {str(e)}"
56
 
57
+ # 定义Gradio接口
58
  interface = gr.Interface(
59
  fn=generate_image,
60
  inputs=[
61
  gr.components.Image(type="pil", label="Control Image"),
62
+ gr.components.Textbox(label="Additional Prompt"),
63
+ gr.components.Image(type="pil", label="Reference Image 1"),
64
+ gr.components.Image(type="pil", label="Reference Image 2")
65
  ],
66
  outputs=gr.components.Image(type="pil", label="Generated Image"),
67
+ title="Stable Diffusion with Multi-Step ControlNet",
68
+ description="Generate images using a multi-step process with Stable Diffusion, ControlNet, and CLIP Interrogator."
69
  )
70
 
71
  if __name__ == "__main__":