Beepeen24 commited on
Commit
deffb42
·
verified ·
1 Parent(s): afa7e86

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +136 -198
app.py CHANGED
@@ -425,227 +425,165 @@ def generate_image(
425
 
426
  return images[0], gr.update(visible=True)
427
 
428
- # Description
429
  title = r"""
430
- <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
431
  """
432
 
433
  description = r"""
434
- <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>
435
- We are organizing a Spring Festival event with HuggingFace from 2.7 to 2.25, and you can now generate pictures of Spring Festival costumes. Happy Dragon Year 🐲 ! Share the joy with your family.<br>
436
- How to use:<br>
437
- 1. Upload an image with a face. For images with multiple faces, we will only detect the largest face. Ensure the face is not too small and is clearly visible without significant obstructions or blurring.
438
- 2. (Optional) You can upload another image as a reference for the face pose. If you don't, we will use the first detected face image to extract facial landmarks. If you use a cropped face at step 1, it is recommended to upload it to define a new face pose.
439
- 3. (Optional) You can select multiple ControlNet models to control the generation process. The default is to use the IdentityNet only. The ControlNet models include pose skeleton, canny, and depth. You can adjust the strength of each ControlNet model to control the generation process.
440
- 4. Enter a text prompt, as done in normal text-to-image models.
441
- 5. Click the <b>Submit</b> button to begin customization.
442
- 6. Share your customized photo with your friends and enjoy! 😊"""
443
-
444
- article = r"""
445
- ---
446
- 📝 **Citation**
447
- <br>
448
- If our work is helpful for your research or applications, please cite us via:
449
- ```bibtex
450
- @article{wang2024instantid,
451
- title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
452
- author={Wang, Qixun and Bai, Xu and Wang, Haofan and Qin, Zekui and Chen, Anthony},
453
- journal={arXiv preprint arXiv:2401.07519},
454
- year={2024}
455
- }
456
- ```
457
- 📧 **Contact**
458
- <br>
459
- If you have any questions, please feel free to open an issue or directly reach us out at <b>haofanwang.ai@gmail.com</b>.
460
- """
461
 
462
- tips = r"""
463
- ### Usage tips of InstantID
464
- 1. If you're not satisfied with the similarity, try increasing the weight of "IdentityNet Strength" and "Adapter Strength."
465
- 2. If you feel that the saturation is too high, first decrease the Adapter strength. If it remains too high, then decrease the IdentityNet strength.
466
- 3. If you find that text control is not as expected, decrease Adapter strength.
467
- 4. If you find that realistic style is not good enough, go for our Github repo and use a more realistic base model.
468
  """
469
 
470
  css = """
471
  .gradio-container {width: 85% !important}
 
472
  """
 
473
  with gr.Blocks(css=css) as demo:
474
- # description
475
  gr.Markdown(title)
476
- gr.Markdown(description)
477
-
478
  with gr.Row():
479
- with gr.Column():
480
- with gr.Row(equal_height=True):
481
- # upload face image
 
 
 
 
 
 
 
 
 
 
 
 
 
482
  face_file = gr.Image(
483
- label="Upload a photo of your face", type="filepath"
484
- )
485
- # optional: upload a reference pose image
486
- pose_file = gr.Image(
487
- label="Upload a reference pose image (Optional)",
488
  type="filepath",
 
489
  )
490
-
491
- # prompt
492
- prompt = gr.Textbox(
493
- label="Prompt",
494
- info="Give simple prompt is enough to achieve good face fidelity",
495
- placeholder="A photo of a person",
496
- value="",
497
- )
498
-
499
- submit = gr.Button("Submit", variant="primary")
500
- enable_LCM = gr.Checkbox(
501
- label="Enable Fast Inference with LCM", value=enable_lcm_arg,
502
- info="LCM speeds up the inference step, the trade-off is the quality of the generated image. It performs better with portrait face images rather than distant faces",
503
- )
504
- style = gr.Dropdown(
505
- label="Style template",
506
- choices=STYLE_NAMES,
507
- value=DEFAULT_STYLE_NAME,
508
- )
509
-
510
- # strength
511
- identitynet_strength_ratio = gr.Slider(
512
- label="IdentityNet strength (for fidelity)",
513
- minimum=0,
514
- maximum=1.5,
515
- step=0.05,
516
- value=0.80,
517
- )
518
- adapter_strength_ratio = gr.Slider(
519
- label="Image adapter strength (for detail)",
520
- minimum=0,
521
- maximum=1.5,
522
- step=0.05,
523
- value=0.80,
524
- )
525
- with gr.Accordion("Controlnet"):
526
- controlnet_selection = gr.CheckboxGroup(
527
- ["canny", "depth"], label="Controlnet", value=["depth"],
528
- info="Use pose for skeleton inference, canny for edge detection, and depth for depth map estimation. You can try all three to control the generation process"
529
- )
530
- # pose_strength = gr.Slider(
531
- # label="Pose strength",
532
- # minimum=0,
533
- # maximum=1.5,
534
- # step=0.05,
535
- # value=0.40,
536
- # )
537
- canny_strength = gr.Slider(
538
- label="Canny strength",
539
- minimum=0,
540
- maximum=1.5,
541
- step=0.05,
542
- value=0.40,
543
- )
544
- depth_strength = gr.Slider(
545
- label="Depth strength",
546
- minimum=0,
547
- maximum=1.5,
548
- step=0.05,
549
- value=0.40,
550
- )
551
- with gr.Accordion(open=False, label="Advanced Options"):
552
- negative_prompt = gr.Textbox(
553
- label="Negative Prompt",
554
- placeholder="low quality",
555
- 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, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
556
  )
557
- num_steps = gr.Slider(
558
- label="Number of sample steps",
559
- minimum=1,
560
- maximum=100,
561
- step=1,
562
- value=5 if enable_lcm_arg else 30,
 
 
563
  )
564
- guidance_scale = gr.Slider(
565
- label="Guidance scale",
566
- minimum=0.1,
567
- maximum=20.0,
568
- step=0.1,
569
- value=0.0 if enable_lcm_arg else 5.0,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
570
  )
571
- seed = gr.Slider(
572
- label="Seed",
573
- minimum=0,
574
- maximum=MAX_SEED,
575
- step=1,
576
- value=42,
577
- )
578
- schedulers = [
579
- "DEISMultistepScheduler",
580
- "HeunDiscreteScheduler",
581
- "EulerDiscreteScheduler",
582
- "DPMSolverMultistepScheduler",
583
- "DPMSolverMultistepScheduler-Karras",
584
- "DPMSolverMultistepScheduler-Karras-SDE",
585
- ]
586
- scheduler = gr.Dropdown(
587
- label="Schedulers",
588
- choices=schedulers,
589
- value="EulerDiscreteScheduler",
590
- )
591
- randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
592
- enhance_face_region = gr.Checkbox(label="Enhance non-face region", value=True)
593
 
594
  with gr.Column(scale=1):
595
- gallery = gr.Image(label="Generated Images")
596
- usage_tips = gr.Markdown(
597
- label="InstantID Usage Tips", value=tips, visible=False
 
 
 
598
  )
599
-
600
- submit.click(
601
- fn=remove_tips,
602
- outputs=usage_tips,
603
- ).then(
604
- fn=randomize_seed_fn,
605
- inputs=[seed, randomize_seed],
606
- outputs=seed,
607
- queue=False,
608
- api_name=False,
609
- ).then(
610
- fn=generate_image,
611
- inputs=[
612
- face_file,
613
- pose_file,
614
- prompt,
615
- negative_prompt,
616
- style,
617
- num_steps,
618
- identitynet_strength_ratio,
619
- adapter_strength_ratio,
620
- #pose_strength,
621
- canny_strength,
622
- depth_strength,
623
- controlnet_selection,
624
- guidance_scale,
625
- seed,
626
- scheduler,
627
- enable_LCM,
628
- enhance_face_region,
629
- ],
630
- outputs=[gallery, usage_tips],
631
- )
632
-
633
- enable_LCM.input(
634
- fn=toggle_lcm_ui,
635
- inputs=[enable_LCM],
636
- outputs=[num_steps, guidance_scale],
637
- queue=False,
638
- )
639
-
640
- gr.Examples(
641
- examples=get_example(),
642
- inputs=[face_file, pose_file, prompt, style, negative_prompt],
643
- fn=run_for_examples,
644
- outputs=[gallery, usage_tips],
645
- cache_examples=True,
 
 
 
 
646
  )
647
 
648
- gr.Markdown(article)
 
 
 
 
 
649
 
650
  demo.queue(api_open=False)
651
- demo.launch()
 
425
 
426
  return images[0], gr.update(visible=True)
427
 
428
+ # SIMPLIFIED PROFESSIONAL UI
429
  title = r"""
430
+ <h1 align="center">🎯 Pro AI Headshot Generator</h1>
431
  """
432
 
433
  description = r"""
434
+ <b>Transform any selfie into professional headshots in seconds</b><br>
435
+ Perfect for LinkedIn, corporate profiles, and professional portfolios.<br><br>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
436
 
437
+ <strong>🎯 How to get perfect headshots:</strong><br>
438
+ 1. <b>Upload</b> a clear photo of your face<br>
439
+ 2. <b>Choose</b> your preferred style<br>
440
+ 3. <b>Click Generate</b> and download your professional headshots<br><br>
441
+
442
+ <em>No technical knowledge needed - we handle all the complex AI settings automatically!</em>
443
  """
444
 
445
  css = """
446
  .gradio-container {width: 85% !important}
447
+ .instructions {background: #f0f8ff; padding: 15px; border-radius: 10px; border-left: 4px solid #3b82f6;}
448
  """
449
+
450
  with gr.Blocks(css=css) as demo:
 
451
  gr.Markdown(title)
452
+
 
453
  with gr.Row():
454
+ with gr.Column(scale=1):
455
+ # Instructions Box
456
+ with gr.Group():
457
+ gr.Markdown("""
458
+ <div class="instructions">
459
+ <h3>📸 Photo Tips for Best Results:</h3>
460
+ • Clear, well-lit face photo<br>
461
+ • Face should be visible and not too small<br>
462
+ • Avoid blurry or dark images<br>
463
+ • Single person in photo works best
464
+ </div>
465
+ """)
466
+
467
+ # Simple Upload Section
468
+ with gr.Group():
469
+ gr.Markdown("### 1️⃣ Upload Your Photo")
470
  face_file = gr.Image(
471
+ label="Upload your photo",
 
 
 
 
472
  type="filepath",
473
+ height=200
474
  )
475
+
476
+ # Style Selection
477
+ with gr.Group():
478
+ gr.Markdown("### 2️⃣ Choose Style")
479
+ style = gr.Dropdown(
480
+ label="Professional Style",
481
+ choices=STYLE_NAMES,
482
+ value="Professional",
483
+ info="Select the look you want for your headshots"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
484
  )
485
+
486
+ # Simple Settings (Hidden Advanced Options)
487
+ with gr.Accordion("⚙️ Advanced Settings (Optional)", open=False):
488
+ prompt = gr.Textbox(
489
+ label="Custom Description",
490
+ placeholder="e.g., 'professional business headshot'",
491
+ value="professional headshot",
492
+ info="Describe how you want to appear"
493
  )
494
+
495
+ with gr.Row():
496
+ identitynet_strength_ratio = gr.Slider(
497
+ label="Face Similarity",
498
+ minimum=0.5,
499
+ maximum=1.2,
500
+ step=0.05,
501
+ value=0.80,
502
+ )
503
+ adapter_strength_ratio = gr.Slider(
504
+ label="Detail Quality",
505
+ minimum=0.5,
506
+ maximum=1.2,
507
+ step=0.05,
508
+ value=0.80,
509
+ )
510
+
511
+ enable_LCM = gr.Checkbox(
512
+ label="Fast Generation Mode",
513
+ value=False,
514
+ info="Faster results with slightly lower quality"
515
  )
516
+
517
+ # Generate Button
518
+ submit = gr.Button("🚀 Generate Headshots", variant="primary", size="lg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
519
 
520
  with gr.Column(scale=1):
521
+ # Results Section
522
+ gr.Markdown("### 🎉 Your Professional Headshots")
523
+ gallery = gr.Image(
524
+ label="Generated Headshot",
525
+ height=400,
526
+ show_download_button=True
527
  )
528
+
529
+ # Success Message
530
+ success_msg = gr.Markdown("""
531
+ <div style='background: #d4edda; padding: 15px; border-radius: 10px; border-left: 4px solid #28a745; margin-top: 20px;'>
532
+ <h4>✅ Success!</h4>
533
+ Your professional headshot is ready! Click the download button above to save your image.
534
+ </div>
535
+ """, visible=False)
536
+
537
+ # Hidden technical parameters (keep functionality but hide from users)
538
+ negative_prompt = gr.Textbox(
539
+ 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, photo, anthropomorphic cat, monochrome, pet collar, gun, weapon, blue, 3d, drones, drone, buildings in background, green",
540
+ visible=False
541
+ )
542
+ num_steps = gr.Slider(value=30, visible=False)
543
+ guidance_scale = gr.Slider(value=5.0, visible=False)
544
+ seed = gr.Slider(value=42, visible=False)
545
+ scheduler = gr.Dropdown(value="EulerDiscreteScheduler", visible=False)
546
+ randomize_seed = gr.Checkbox(value=True, visible=False)
547
+ enhance_face_region = gr.Checkbox(value=True, visible=False)
548
+ controlnet_selection = gr.CheckboxGroup(value=["depth"], visible=False)
549
+ canny_strength = gr.Slider(value=0.4, visible=False)
550
+ depth_strength = gr.Slider(value=0.4, visible=False)
551
+
552
+ def show_success():
553
+ return gr.update(visible=True)
554
+
555
+ submit.click(
556
+ fn=generate_image,
557
+ inputs=[
558
+ face_file,
559
+ gr.update(visible=False), # pose_file hidden
560
+ prompt,
561
+ negative_prompt,
562
+ style,
563
+ num_steps,
564
+ identitynet_strength_ratio,
565
+ adapter_strength_ratio,
566
+ canny_strength,
567
+ depth_strength,
568
+ controlnet_selection,
569
+ guidance_scale,
570
+ seed,
571
+ scheduler,
572
+ enable_LCM,
573
+ enhance_face_region,
574
+ ],
575
+ outputs=[gallery, success_msg]
576
+ ).then(
577
+ fn=show_success,
578
+ outputs=success_msg
579
  )
580
 
581
+ enable_LCM.input(
582
+ fn=toggle_lcm_ui,
583
+ inputs=[enable_LCM],
584
+ outputs=[num_steps, guidance_scale],
585
+ queue=False,
586
+ )
587
 
588
  demo.queue(api_open=False)
589
+ demo.launch(share=True)