deeme commited on
Commit
bd12566
·
verified ·
1 Parent(s): 985a663

Upload main.py

Browse files
Files changed (1) hide show
  1. main.py +52 -27
main.py CHANGED
@@ -10,7 +10,16 @@ import retrying
10
  from PIL import Image
11
 
12
  @retrying.retry(stop_max_attempt_number=2, wait_fixed=1000)
13
- def generate_image(image, inference_steps, guidance_scale, image_size, prompt, model_choice):
 
 
 
 
 
 
 
 
 
14
  image_size = {
15
  '1:1': '1024x1024',
16
  '1:2': '1024x2048',
@@ -44,6 +53,10 @@ def generate_image(image, inference_steps, guidance_scale, image_size, prompt, m
44
  # 如果选择了图生图模型但没有提供图像,可以返回一个错误消息或使用默认图像
45
  return Image.new('RGB', (512, 512), color = 'white') # 创建一个白色的默认图像
46
 
 
 
 
 
47
  url = f"https://api.siliconflow.cn/v1/{model_choice}"
48
 
49
  with requests.post(url, data=json.dumps(data), headers=headers) as response:
@@ -53,40 +66,52 @@ def generate_image(image, inference_steps, guidance_scale, image_size, prompt, m
53
  img = Image.open(io.BytesIO(image_response.content))
54
  return img
55
 
56
- def dosomething(image, inference_steps, guidance_scale, image_size, prompt, model_choice):
57
- return generate_image(image, inference_steps, guidance_scale, image_size, prompt, model_choice)
58
 
59
  if __name__ == "__main__":
60
  dotenv.load_dotenv()
61
 
62
  with gr.Blocks() as demo:
63
  gr.Markdown("# 文生图、图生图")
64
-
 
65
  with gr.Row():
66
  inference_steps = gr.Number(minimum=1, maximum=100, value=20, label='推理步数')
67
- guidance_scale = gr.Slider(minimum=0, maximum=100, value=7.5, step=0.1, label='引导比例')
68
  image_size = gr.Dropdown(['1:1', '1:2', '3:2', '3:4', '16:9', '9:16'], label='生成图片比例', value='1:1')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
69
 
70
  with gr.Row():
71
- prompt_input = gr.Textbox(label="输入prompt", lines=3, placeholder="在此输入你的prompt...")
72
- with gr.Column():
73
- model_choice = gr.Dropdown([
74
- 'stabilityai/stable-diffusion-3-medium/text-to-image',
75
- 'stabilityai/stable-diffusion-xl-base-1.0/text-to-image',
76
- 'stabilityai/stable-diffusion-2-1/text-to-image',
77
- 'stabilityai/sd-turbo/text-to-image',
78
- 'stabilityai/sdxl-turbo/text-to-image',
79
- 'ByteDance/SDXL-Lightning/text-to-image',
80
- 'stabilityai/stable-diffusion-xl-base-1.0/image-to-image',
81
- 'stabilityai/stable-diffusion-2-1/image-to-image',
82
- 'stabilityai/SDXL-Lightning/image-to-image',
83
- 'InstantX/InstantID/image-to-image',
84
- 'TencentARC/PhotoMaker/image-to-image'
85
- ],
86
- label='选择模型', value='stabilityai/stable-diffusion-xl-base-1.0/image-to-image')
87
- with gr.Row():
88
- clear_btn = gr.Button("清空")
89
- generate_btn = gr.Button("生成图片")
90
 
91
  with gr.Row():
92
  image_input = gr.Image(label='选择原图 (仅用于图生图)', type='pil')
@@ -94,14 +119,14 @@ if __name__ == "__main__":
94
 
95
  generate_btn.click(
96
  fn=dosomething,
97
- inputs=[image_input, inference_steps, guidance_scale, image_size, prompt_input, model_choice],
98
  outputs=output_image
99
  )
100
 
101
  clear_btn.click(
102
- fn=lambda: (None, 20, 7.5, "1:1", "", "stable-diffusion-xl-base-1.0/image-to-image", None),
103
  inputs=None,
104
- outputs=[image_input, inference_steps, guidance_scale, image_size, prompt_input, model_choice, output_image]
105
  )
106
 
107
  demo.launch(server_name='0.0.0.0', server_port=7861)
 
10
  from PIL import Image
11
 
12
  @retrying.retry(stop_max_attempt_number=2, wait_fixed=1000)
13
+ def generate_image(image, inference_steps, guidance_scale, image_size, prompt, model_choice, style_name):
14
+ # 检查模型名称是否包含SDXL-Lightning
15
+ if "SDXL-Lightning" in model_choice:
16
+ inference_steps = 4
17
+ guidance_scale = 1
18
+
19
+ if "turbo" in model_choice:
20
+ inference_steps = 6
21
+ guidance_scale = 1
22
+
23
  image_size = {
24
  '1:1': '1024x1024',
25
  '1:2': '1024x2048',
 
53
  # 如果选择了图生图模型但没有提供图像,可以返回一个错误消息或使用默认图像
54
  return Image.new('RGB', (512, 512), color = 'white') # 创建一个白色的默认图像
55
 
56
+ if "PhotoMaker" in model_choice:
57
+ data["style_name"] = style_name
58
+ data["style_strengh_radio"] = 20
59
+
60
  url = f"https://api.siliconflow.cn/v1/{model_choice}"
61
 
62
  with requests.post(url, data=json.dumps(data), headers=headers) as response:
 
66
  img = Image.open(io.BytesIO(image_response.content))
67
  return img
68
 
69
+ def dosomething(image, inference_steps, guidance_scale, image_size, prompt, model_choice, style_name):
70
+ return generate_image(image, inference_steps, guidance_scale, image_size, prompt, model_choice, style_name)
71
 
72
  if __name__ == "__main__":
73
  dotenv.load_dotenv()
74
 
75
  with gr.Blocks() as demo:
76
  gr.Markdown("# 文生图、图生图")
77
+
78
+ guidance_scale = gr.Slider(minimum=0, maximum=100, value=7.5, step=0.1, label='引导比例')
79
  with gr.Row():
80
  inference_steps = gr.Number(minimum=1, maximum=100, value=20, label='推理步数')
 
81
  image_size = gr.Dropdown(['1:1', '1:2', '3:2', '3:4', '16:9', '9:16'], label='生成图片比例', value='1:1')
82
+ style_name = gr.Dropdown([
83
+ 'Cinematic',
84
+ 'Comic book',
85
+ 'Disney Character',
86
+ 'Digital Art',
87
+ 'Photographic (Default)',
88
+ 'Fantasy Art',
89
+ 'Neopunk',
90
+ 'Enhance',
91
+ 'Lowpoly',
92
+ 'Line art',
93
+ '(No style)'
94
+ ], label='PhotoMaker风格', value='Photographic (Default)')
95
+
96
+ model_choice = gr.Dropdown([
97
+ 'stabilityai/stable-diffusion-3-medium/text-to-image',
98
+ 'stabilityai/stable-diffusion-xl-base-1.0/text-to-image',
99
+ 'stabilityai/stable-diffusion-2-1/text-to-image',
100
+ 'stabilityai/sd-turbo/text-to-image',
101
+ 'stabilityai/sdxl-turbo/text-to-image',
102
+ 'ByteDance/SDXL-Lightning/text-to-image',
103
+ 'stabilityai/stable-diffusion-xl-base-1.0/image-to-image',
104
+ 'stabilityai/stable-diffusion-2-1/image-to-image',
105
+ 'ByteDance/SDXL-Lightning/image-to-image',
106
+ 'TencentARC/PhotoMaker/image-to-image',
107
+ ],label='选择模型', value='stabilityai/stable-diffusion-xl-base-1.0/image-to-image')
108
+
109
+ prompt_input = gr.Textbox(label="输入prompt", lines=3, value="Transform all objects in the scene into a highly detailed and realistic anime style. Ensure that all characters have perfectly proportioned features including complete and natural-looking hands and fingers, and symmetrical, well-defined facial features with no distortions or anomalies. All objects should be rendered with vibrant and colorful details, smooth shading, and dynamic compositions. The style should resemble the works of Studio Ghibli or Makoto Shinkai, with meticulous attention to detail in every aspect, including backgrounds, clothing, and accessories. The overall image should be cohesive, with a harmonious blend of all elements.", placeholder="在此输入你的prompt...")
110
 
111
  with gr.Row():
112
+ #with gr.Column():
113
+ clear_btn = gr.Button("清空")
114
+ generate_btn = gr.Button("生成图片")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
116
  with gr.Row():
117
  image_input = gr.Image(label='选择原图 (仅用于图生图)', type='pil')
 
119
 
120
  generate_btn.click(
121
  fn=dosomething,
122
+ inputs=[image_input, inference_steps, guidance_scale, image_size, prompt_input, model_choice, style_name],
123
  outputs=output_image
124
  )
125
 
126
  clear_btn.click(
127
+ fn=lambda: (None, 20, 5, "1:1", "a half-body portrait of a man img wearing the sunglasses in Iron man suit, best quality", "TencentARC/PhotoMaker/image-to-image", "Photographic (Default)", None),
128
  inputs=None,
129
+ outputs=[image_input, inference_steps, guidance_scale, image_size, prompt_input, model_choice, style_name, output_image]
130
  )
131
 
132
  demo.launch(server_name='0.0.0.0', server_port=7861)