LPDoctor commited on
Commit
6fa1851
·
verified ·
1 Parent(s): f3831bd

隐藏huggingface上的gradio UI,当后端服务启用

Browse files
Files changed (1) hide show
  1. app.py +204 -203
app.py CHANGED
@@ -68,44 +68,44 @@ def remove_tips():
68
  return gr.update(visible=False)
69
 
70
 
71
- def get_example():
72
- case = [
73
- [
74
- "./examples/yann-lecun_resize.jpg",
75
- "a man",
76
- "Snow",
77
- "(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",
78
- ],
79
- [
80
- "./examples/musk_resize.jpeg",
81
- "a man",
82
- "Mars",
83
- "(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",
84
- ],
85
- [
86
- "./examples/sam_resize.png",
87
- "a man",
88
- "Jungle",
89
- "(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, gree",
90
- ],
91
- [
92
- "./examples/schmidhuber_resize.png",
93
- "a man",
94
- "Neon",
95
- "(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",
96
- ],
97
- [
98
- "./examples/kaifu_resize.png",
99
- "a man",
100
- "Vibrant Color",
101
- "(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",
102
- ],
103
- ]
104
- return case
105
-
106
-
107
- def run_for_examples(face_file, prompt, style, negative_prompt):
108
- return generate_image(face_file, None, prompt, negative_prompt, style, True, 30, 0.8, 0.8, 5, 42)
109
 
110
 
111
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
@@ -240,6 +240,7 @@ def generate_image(
240
 
241
  width, height = face_kps.size
242
 
 
243
  if enhance_face_region:
244
  control_mask = np.zeros([height, width, 3])
245
  x1, y1, x2, y2 = face_info["bbox"]
@@ -251,8 +252,8 @@ def generate_image(
251
 
252
  generator = torch.Generator(device=device).manual_seed(seed)
253
 
254
- print("Start inference...")
255
- print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
256
 
257
  pipe.set_ip_adapter_scale(adapter_strength_ratio)
258
  images = pipe(
@@ -272,169 +273,169 @@ def generate_image(
272
  return images[0], gr.update(visible=True)
273
 
274
 
275
- ### Description
276
- title = r"""
277
- <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
278
- """
279
-
280
- description = r"""
281
- <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: Zero-shot Identity-Preserving Generation in Seconds</b></a>.<br>
282
-
283
- How to use:<br>
284
- 1. Upload a person image. For multiple person images, we will only detect the biggest face. Make sure face is not too small and not significantly blocked or blurred.
285
- 2. (Optionally) upload another person image as reference pose. If not uploaded, we will use the first person image to extract landmarks. If you use a cropped face at step1, it is recommeneded to upload it to extract a new pose.
286
- 3. Enter a text prompt as done in normal text-to-image models.
287
- 4. Click the <b>Submit</b> button to start customizing.
288
- 5. Share your customizd photo with your friends, enjoy😊!
289
- """
290
-
291
- article = r"""
292
- ---
293
- 📝 **Citation**
294
- <br>
295
- If our work is helpful for your research or applications, please cite us via:
296
- ```bibtex
297
- @article{wang2024instantid,
298
- title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
299
- author={Wang, Qixun and Bai, Xu and Wang, Haofan and Qin, Zekui and Chen, Anthony},
300
- journal={arXiv preprint arXiv:2401.07519},
301
- year={2024}
302
- }
303
- ```
304
- 📧 **Contact**
305
- <br>
306
- If you have any questions, please feel free to open an issue or directly reach us out at <b>[email protected]</b>.
307
- """
308
-
309
- tips = r"""
310
- ### Usage tips of InstantID
311
- 1. If you're unsatisfied with the similarity, increase the weight of controlnet_conditioning_scale (IdentityNet) and ip_adapter_scale (Adapter).
312
- 2. If the generated image is over-saturated, decrease the ip_adapter_scale. If not work, decrease controlnet_conditioning_scale.
313
- 3. If text control is not as expected, decrease ip_adapter_scale.
314
- 4. Find a good base model always makes a difference.
315
- """
316
-
317
- css = """
318
- .gradio-container {width: 85% !important}
319
- """
320
- with gr.Blocks(css=css) as demo:
321
- # description
322
- gr.Markdown(title)
323
- gr.Markdown(description)
324
-
325
- with gr.Row():
326
- with gr.Column():
327
- # upload face image
328
- face_file = gr.Image(label="Upload a photo of your face", type="filepath")
329
-
330
- # optional: upload a reference pose image
331
- pose_file = gr.Image(label="Upload a reference pose image (optional)", type="filepath")
332
-
333
- # prompt
334
- prompt = gr.Textbox(
335
- label="Prompt",
336
- info="Give simple prompt is enough to achieve good face fedility",
337
- placeholder="A photo of a person",
338
- value="",
339
- )
340
-
341
- submit = gr.Button("Submit", variant="primary")
342
-
343
- style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
344
-
345
- # strength
346
- identitynet_strength_ratio = gr.Slider(
347
- label="IdentityNet strength (for fedility)",
348
- minimum=0,
349
- maximum=1.5,
350
- step=0.05,
351
- value=0.80,
352
- )
353
- adapter_strength_ratio = gr.Slider(
354
- label="Image adapter strength (for detail)",
355
- minimum=0,
356
- maximum=1.5,
357
- step=0.05,
358
- value=0.80,
359
- )
360
-
361
- with gr.Accordion(open=False, label="Advanced Options"):
362
- negative_prompt = gr.Textbox(
363
- label="Negative Prompt",
364
- placeholder="low quality",
365
- value="(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, nudity,naked, bikini, skimpy, scanty, bare skin, lingerie, swimsuit, exposed, see-through, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
366
- )
367
- num_steps = gr.Slider(
368
- label="Number of sample steps",
369
- minimum=20,
370
- maximum=100,
371
- step=1,
372
- value=30,
373
- )
374
- guidance_scale = gr.Slider(
375
- label="Guidance scale",
376
- minimum=0.1,
377
- maximum=10.0,
378
- step=0.1,
379
- value=5,
380
- )
381
- seed = gr.Slider(
382
- label="Seed",
383
- minimum=0,
384
- maximum=MAX_SEED,
385
- step=1,
386
- value=42,
387
- )
388
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
389
- enhance_face_region = gr.Checkbox(label="Enhance non-face region", value=True)
390
-
391
- with gr.Column():
392
- output_image = gr.Image(label="Generated Image")
393
- usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips, visible=False)
394
-
395
- submit.click(
396
- fn=remove_tips,
397
- outputs=usage_tips,
398
- queue=False,
399
- api_name=False,
400
- ).then(
401
- fn=randomize_seed_fn,
402
- inputs=[seed, randomize_seed],
403
- outputs=seed,
404
- queue=False,
405
- api_name=False,
406
- ).then(
407
- fn=check_input_image,
408
- inputs=face_file,
409
- queue=False,
410
- api_name=False,
411
- ).success(
412
- fn=generate_image,
413
- inputs=[
414
- face_file,
415
- pose_file,
416
- prompt,
417
- negative_prompt,
418
- style,
419
- enhance_face_region,
420
- num_steps,
421
- identitynet_strength_ratio,
422
- adapter_strength_ratio,
423
- guidance_scale,
424
- seed,
425
- ],
426
- outputs=[output_image, usage_tips],
427
- )
428
-
429
- gr.Examples(
430
- examples=get_example(),
431
- inputs=[face_file, prompt, style, negative_prompt],
432
- outputs=[output_image, usage_tips],
433
- fn=run_for_examples,
434
- cache_examples=True,
435
- )
436
-
437
- gr.Markdown(article)
438
 
439
  demo.queue(api_open=False)
440
  demo.launch()
 
68
  return gr.update(visible=False)
69
 
70
 
71
+ # def get_example():
72
+ # case = [
73
+ # [
74
+ # "./examples/yann-lecun_resize.jpg",
75
+ # "a man",
76
+ # "Snow",
77
+ # "(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",
78
+ # ],
79
+ # [
80
+ # "./examples/musk_resize.jpeg",
81
+ # "a man",
82
+ # "Mars",
83
+ # "(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",
84
+ # ],
85
+ # [
86
+ # "./examples/sam_resize.png",
87
+ # "a man",
88
+ # "Jungle",
89
+ # "(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, gree",
90
+ # ],
91
+ # [
92
+ # "./examples/schmidhuber_resize.png",
93
+ # "a man",
94
+ # "Neon",
95
+ # "(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",
96
+ # ],
97
+ # [
98
+ # "./examples/kaifu_resize.png",
99
+ # "a man",
100
+ # "Vibrant Color",
101
+ # "(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",
102
+ # ],
103
+ # ]
104
+ # return case
105
+
106
+
107
+ # def run_for_examples(face_file, prompt, style, negative_prompt):
108
+ # return generate_image(face_file, None, prompt, negative_prompt, style, True, 30, 0.8, 0.8, 5, 42)
109
 
110
 
111
  def convert_from_cv2_to_image(img: np.ndarray) -> Image:
 
240
 
241
  width, height = face_kps.size
242
 
243
+ # 面部增强
244
  if enhance_face_region:
245
  control_mask = np.zeros([height, width, 3])
246
  x1, y1, x2, y2 = face_info["bbox"]
 
252
 
253
  generator = torch.Generator(device=device).manual_seed(seed)
254
 
255
+ # print("Start inference...")
256
+ # print(f"[Debug] Prompt: {prompt}, \n[Debug] Neg Prompt: {negative_prompt}")
257
 
258
  pipe.set_ip_adapter_scale(adapter_strength_ratio)
259
  images = pipe(
 
273
  return images[0], gr.update(visible=True)
274
 
275
 
276
+ # ### Description
277
+ # title = r"""
278
+ # <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
279
+ # """
280
+
281
+ # description = r"""
282
+ # <b>Official 🤗 Gradio demo</b> for <a href='https://github.com/InstantID/InstantID' target='_blank'><b>InstantID: Zero-shot Identity-Preserving Generation in Seconds</b></a>.<br>
283
+
284
+ # How to use:<br>
285
+ # 1. Upload a person image. For multiple person images, we will only detect the biggest face. Make sure face is not too small and not significantly blocked or blurred.
286
+ # 2. (Optionally) upload another person image as reference pose. If not uploaded, we will use the first person image to extract landmarks. If you use a cropped face at step1, it is recommeneded to upload it to extract a new pose.
287
+ # 3. Enter a text prompt as done in normal text-to-image models.
288
+ # 4. Click the <b>Submit</b> button to start customizing.
289
+ # 5. Share your customizd photo with your friends, enjoy😊!
290
+ # """
291
+
292
+ # article = r"""
293
+ # ---
294
+ # 📝 **Citation**
295
+ # <br>
296
+ # If our work is helpful for your research or applications, please cite us via:
297
+ # ```bibtex
298
+ # @article{wang2024instantid,
299
+ # title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
300
+ # author={Wang, Qixun and Bai, Xu and Wang, Haofan and Qin, Zekui and Chen, Anthony},
301
+ # journal={arXiv preprint arXiv:2401.07519},
302
+ # year={2024}
303
+ # }
304
+ # ```
305
+ # 📧 **Contact**
306
+ # <br>
307
+ # If you have any questions, please feel free to open an issue or directly reach us out at <b>[email protected]</b>.
308
+ # """
309
+
310
+ # tips = r"""
311
+ # ### Usage tips of InstantID
312
+ # 1. If you're unsatisfied with the similarity, increase the weight of controlnet_conditioning_scale (IdentityNet) and ip_adapter_scale (Adapter).
313
+ # 2. If the generated image is over-saturated, decrease the ip_adapter_scale. If not work, decrease controlnet_conditioning_scale.
314
+ # 3. If text control is not as expected, decrease ip_adapter_scale.
315
+ # 4. Find a good base model always makes a difference.
316
+ # """
317
+
318
+ # css = """
319
+ # .gradio-container {width: 85% !important}
320
+ # """
321
+ # with gr.Blocks(css=css) as demo:
322
+ # # description
323
+ # gr.Markdown(title)
324
+ # gr.Markdown(description)
325
+
326
+ # with gr.Row():
327
+ # with gr.Column():
328
+ # # upload face image
329
+ # face_file = gr.Image(label="Upload a photo of your face", type="filepath")
330
+
331
+ # # optional: upload a reference pose image
332
+ # pose_file = gr.Image(label="Upload a reference pose image (optional)", type="filepath")
333
+
334
+ # # prompt
335
+ # prompt = gr.Textbox(
336
+ # label="Prompt",
337
+ # info="Give simple prompt is enough to achieve good face fedility",
338
+ # placeholder="A photo of a person",
339
+ # value="",
340
+ # )
341
+
342
+ # submit = gr.Button("Submit", variant="primary")
343
+
344
+ # style = gr.Dropdown(label="Style template", choices=STYLE_NAMES, value=DEFAULT_STYLE_NAME)
345
+
346
+ # # strength
347
+ # identitynet_strength_ratio = gr.Slider(
348
+ # label="IdentityNet strength (for fedility)",
349
+ # minimum=0,
350
+ # maximum=1.5,
351
+ # step=0.05,
352
+ # value=0.80,
353
+ # )
354
+ # adapter_strength_ratio = gr.Slider(
355
+ # label="Image adapter strength (for detail)",
356
+ # minimum=0,
357
+ # maximum=1.5,
358
+ # step=0.05,
359
+ # value=0.80,
360
+ # )
361
+
362
+ # with gr.Accordion(open=False, label="Advanced Options"):
363
+ # negative_prompt = gr.Textbox(
364
+ # label="Negative Prompt",
365
+ # placeholder="low quality",
366
+ # value="(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, nudity,naked, bikini, skimpy, scanty, bare skin, lingerie, swimsuit, exposed, see-through, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
367
+ # )
368
+ # num_steps = gr.Slider(
369
+ # label="Number of sample steps",
370
+ # minimum=20,
371
+ # maximum=100,
372
+ # step=1,
373
+ # value=30,
374
+ # )
375
+ # guidance_scale = gr.Slider(
376
+ # label="Guidance scale",
377
+ # minimum=0.1,
378
+ # maximum=10.0,
379
+ # step=0.1,
380
+ # value=5,
381
+ # )
382
+ # seed = gr.Slider(
383
+ # label="Seed",
384
+ # minimum=0,
385
+ # maximum=MAX_SEED,
386
+ # step=1,
387
+ # value=42,
388
+ # )
389
+ # randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
390
+ # enhance_face_region = gr.Checkbox(label="Enhance non-face region", value=True)
391
+
392
+ # with gr.Column():
393
+ # output_image = gr.Image(label="Generated Image")
394
+ # usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips, visible=False)
395
+
396
+ # submit.click(
397
+ # fn=remove_tips,
398
+ # outputs=usage_tips,
399
+ # queue=False,
400
+ # api_name=False,
401
+ # ).then(
402
+ # fn=randomize_seed_fn,
403
+ # inputs=[seed, randomize_seed],
404
+ # outputs=seed,
405
+ # queue=False,
406
+ # api_name=False,
407
+ # ).then(
408
+ # fn=check_input_image,
409
+ # inputs=face_file,
410
+ # queue=False,
411
+ # api_name=False,
412
+ # ).success(
413
+ # fn=generate_image,
414
+ # inputs=[
415
+ # face_file,
416
+ # pose_file,
417
+ # prompt,
418
+ # negative_prompt,
419
+ # style,
420
+ # enhance_face_region,
421
+ # num_steps,
422
+ # identitynet_strength_ratio,
423
+ # adapter_strength_ratio,
424
+ # guidance_scale,
425
+ # seed,
426
+ # ],
427
+ # outputs=[output_image, usage_tips],
428
+ # )
429
+
430
+ # gr.Examples(
431
+ # examples=get_example(),
432
+ # inputs=[face_file, prompt, style, negative_prompt],
433
+ # outputs=[output_image, usage_tips],
434
+ # fn=run_for_examples,
435
+ # cache_examples=True,
436
+ # )
437
+
438
+ # gr.Markdown(article)
439
 
440
  demo.queue(api_open=False)
441
  demo.launch()