XuDongZhou commited on
Commit
2792f5f
·
verified ·
1 Parent(s): 6d4afa8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -21
app.py CHANGED
@@ -2,7 +2,7 @@ import cv2, os, math
2
  import torch
3
  import random
4
  import numpy as np
5
-
6
  import spaces
7
 
8
  import PIL
@@ -25,7 +25,7 @@ from insightface.app import FaceAnalysis
25
  from pipeline_controlnet_xs_sd_xl_instantid import StableDiffusionXLInstantIDXSPipeline, UNetControlNetXSModel
26
 
27
  from utils.controlnet_xs import ControlNetXSAdapter
28
- from style import styles
29
 
30
 
31
  import gradio as gr
@@ -39,7 +39,10 @@ hf_hub_download(repo_id="RED-AIGC/InstantID-XS", filename="image_proj.bin", loca
39
  MAX_SEED = np.iinfo(np.int32).max
40
  device = "cuda" if torch.cuda.is_available() else "cpu"
41
  weight_dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
42
- STYLE_NAMES = list(styles.keys())
 
 
 
43
  DEFAULT_STYLE_NAME = "Ordinary"
44
 
45
  base_model = 'frankjoshua/realvisxlV40_v40Bakedvae'
@@ -117,29 +120,50 @@ def get_example():
117
  "./examples/1.jpg",
118
  None,
119
  "Ordinary",
120
- "a woman",
121
- "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
122
  ],
123
  [
124
  "./examples/1.jpg",
125
  "./examples/pose/pose1.jpg",
126
  "Hanfu",
127
- "a woman",
128
- "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
129
  ],
130
  [
131
  "./examples/2.jpg",
132
  "./examples/pose/pose2.png",
133
  "ZangZu",
134
- "a woman",
135
- "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
136
  ],
137
  [
138
  "./examples/3.png",
139
  "./examples/pose/pose3.png",
140
  "QingQiu",
141
- "a woman",
142
- "(lowres, low quality, worst quality:1.2), (text:1.2), watermark, (frame:1.2), deformed, ugly, deformed eyes, blur, out of focus, blurry, deformed cat, deformed, photo, anthropomorphic cat, monochrome, photo, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
143
  ],
144
  ]
145
  return case
@@ -204,11 +228,12 @@ def resize_img(input_image,max_side=1280,min_side=1024,size=None,pad_to_max_side
204
  input_image = Image.fromarray(res)
205
  return input_image
206
 
207
- def apply_style(style_name: str, positive: str, negative: str = "") -> Tuple[str, str]:
208
- p, n = styles.get(style_name, styles[DEFAULT_STYLE_NAME])
209
- return p.replace("{prompt}", positive), n + ' ' + negative
 
210
 
211
- def run_for_examples(face_file, pose_file, style, prompt, negative_prompt, ):
212
  return generate_image(
213
  face_file,
214
  pose_file,
@@ -242,11 +267,6 @@ def generate_image(
242
  if face_image_path is None:
243
  raise gr.Error(f"Cannot find any input face image! Please upload the face image")
244
 
245
- if prompt is None:
246
- prompt = "a person"
247
-
248
- # apply the style template
249
- prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
250
 
251
  face_image = load_image(face_image_path)
252
  face_image = resize_img(face_image, max_side=max_side)
@@ -267,6 +287,12 @@ def generate_image(
267
 
268
  face_emb = torch.from_numpy(face_info.normed_embedding)
269
  face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
 
 
 
 
 
 
270
  if pose_image_path is not None:
271
  pose_image = load_image(pose_image_path)
272
  pose_image = resize_img(pose_image, max_side=max_side)
@@ -342,6 +368,7 @@ with gr.Blocks(css=css) as demo:
342
 
343
  style = gr.Dropdown(
344
  label="Style",
 
345
  choices=STYLE_NAMES,
346
  value=DEFAULT_STYLE_NAME
347
  )
@@ -432,7 +459,7 @@ with gr.Blocks(css=css) as demo:
432
 
433
  gr.Examples(
434
  examples=get_example(),
435
- inputs=[face_file, pose_file, style, prompt, negative_prompt],
436
  fn=run_for_examples,
437
  outputs=[gallery, usage_tips],
438
  cache_examples=True,
 
2
  import torch
3
  import random
4
  import numpy as np
5
+ import json
6
  import spaces
7
 
8
  import PIL
 
25
  from pipeline_controlnet_xs_sd_xl_instantid import StableDiffusionXLInstantIDXSPipeline, UNetControlNetXSModel
26
 
27
  from utils.controlnet_xs import ControlNetXSAdapter
28
+
29
 
30
 
31
  import gradio as gr
 
39
  MAX_SEED = np.iinfo(np.int32).max
40
  device = "cuda" if torch.cuda.is_available() else "cpu"
41
  weight_dtype = torch.float16 if str(device).__contains__("cuda") else torch.float32
42
+
43
+ with open('./style.json') as f:
44
+ style_lib = json.load(f)
45
+ STYLE_NAMES = list(style_lib.keys())
46
  DEFAULT_STYLE_NAME = "Ordinary"
47
 
48
  base_model = 'frankjoshua/realvisxlV40_v40Bakedvae'
 
120
  "./examples/1.jpg",
121
  None,
122
  "Ordinary",
123
+ ""
 
124
  ],
125
  [
126
  "./examples/1.jpg",
127
  "./examples/pose/pose1.jpg",
128
  "Hanfu",
129
+ ""
 
130
  ],
131
  [
132
  "./examples/2.jpg",
133
  "./examples/pose/pose2.png",
134
  "ZangZu",
135
+ ""
 
136
  ],
137
  [
138
  "./examples/3.png",
139
  "./examples/pose/pose3.png",
140
  "QingQiu",
141
+ "",
142
+ ],
143
+ [
144
+ "./examples/4.png",
145
+ "./examples/pose/pose2.png",
146
+ "(No style)",
147
+ "A man in suit",
148
+ ],
149
+
150
+ [
151
+ "./examples/5.jpeg",
152
+ "./examples/pose/pose3.png",
153
+ "(No style)",
154
+ "Girl in white wedding dress",
155
+ ],
156
+ [
157
+ "./examples/6.jpg",
158
+ "./examples/pose/pose4.jpeg",
159
+ "ZangZu",
160
+ "",
161
+ ],
162
+ [
163
+ "./examples/7.jpeg",
164
+ "./examples/pose/pose3.png",
165
+ "ZangZu",
166
+ "",
167
  ],
168
  ]
169
  return case
 
228
  input_image = Image.fromarray(res)
229
  return input_image
230
 
231
+ def apply_style(style_params, positive: str, negative: str = ""):
232
+ p = style_params["prompt"].replace("{prompt}", positive)
233
+ n = style_params["negative_prompt"] + ' ' + negative
234
+ return p, n
235
 
236
+ def run_for_examples(face_file, pose_file, style, prompt, negative_prompt="", ):
237
  return generate_image(
238
  face_file,
239
  pose_file,
 
267
  if face_image_path is None:
268
  raise gr.Error(f"Cannot find any input face image! Please upload the face image")
269
 
 
 
 
 
 
270
 
271
  face_image = load_image(face_image_path)
272
  face_image = resize_img(face_image, max_side=max_side)
 
287
 
288
  face_emb = torch.from_numpy(face_info.normed_embedding)
289
  face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
290
+
291
+ style_params = style_lib[style_name][face_info["gender"]]
292
+ if prompt is None:
293
+ prompt = "a person"
294
+ prompt, negative_prompt = apply_style(style_params, prompt, negative_prompt)
295
+
296
  if pose_image_path is not None:
297
  pose_image = load_image(pose_image_path)
298
  pose_image = resize_img(pose_image, max_side=max_side)
 
368
 
369
  style = gr.Dropdown(
370
  label="Style",
371
+ info="If you want to generate images completely according to your own prompt, please choose '(No style)'",
372
  choices=STYLE_NAMES,
373
  value=DEFAULT_STYLE_NAME
374
  )
 
459
 
460
  gr.Examples(
461
  examples=get_example(),
462
+ inputs=[face_file, pose_file, style, prompt],
463
  fn=run_for_examples,
464
  outputs=[gallery, usage_tips],
465
  cache_examples=True,