Spaces:
Paused
Paused
ResearcherXman commited on
Commit ·
3c49f9a
1
Parent(s): 4912220
update
Browse filesFiles changed (2)
- app.py+195-4
- requirements.txtAdded+14
1234567import gradio as grdef greet(name):return "Hello " + name + "!!"iface = gr.Interface(fn=greet, inputs="text", outputs="text")iface.launch()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198import osimport cv2import mathimport randomimport numpy as npfrom PIL import Imagefrom diffusers.utils import load_imageimport gradio as gr# global variableMAX_SEED = np.iinfo(np.int32).maxdef randomize_seed_fn(seed: int, randomize_seed: bool) -> int:if randomize_seed:seed = random.randint(0, MAX_SEED)return seeddef swap_to_gallery(images):return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)def upload_example_to_gallery(images, prompt, style, negative_prompt):return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)def remove_back_to_files():return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)def remove_tips():return gr.update(visible=False)def generate_image(face_image, pose_image, prompt, negative_prompt, num_steps, identitynet_strength_ratio, adapter_strength_ratio, num_outputs, guidance_scale, seed, progress=gr.Progress(track_tqdm=True)):if face_image is None:raise gr.Error(f"Cannot find any input face image! Please upload the face image")face_image = load_image(face_image[0])return [face_image], gr.update(visible=True)### Descriptiontitle = r"""<h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>"""description = r"""<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>How to use:<br>1. Upload a person image or cropped face image. For multiple person images, we will only detect the biggest face. Make sure face is in good condition and not significantly blocked or blurred.2. (Optionally) upload another person image as reference pose. If not uploaded, we will use the first person image to extract landmarks.3. Enter a text prompt as normal text-to-image model.4. Click the <b>Submit</b> button to start customizing.5. Share your customizd photo with your friends, enjoy😊!"""article = r"""---📝 **Citation**<br>If our work is helpful for your research or applications, please cite us via:```bibtex@misc{wang2024instantid,title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},author={Qixun Wang and Xu Bai and Haofan Wang and Zekui Qin and Anthony Chen},year={2024},eprint={2401.07519},archivePrefix={arXiv},primaryClass={cs.CV}}```📧 **Contact**<br>If you have any questions, please feel free to open an issue or directly reach us out at <b>haofanwang.ai@gmail.com</b>."""tips = r"""### Usage tips of InstantID1. If you're not satisfied with the similarity, scroll down to "Advanced Options" and increase the weight of "IdentityNet Strength" and "Adapter Strength".2. If you feel that the saturation is too high, first decrease the Adapter strength. If it is still too high, then decrease the IdentityNet strength.3. If you find that text control is not as expected, decrease Adapter strength."""css = '''.gradio-container {width: 85% !important}'''with gr.Blocks(css=css) as demo:# descriptiongr.Markdown(title)gr.Markdown(description)with gr.Row():with gr.Column():# upload face imageface_files = gr.Files(label="Upload a photo of your face",file_types=["image"])uploaded_faces = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)with gr.Column(visible=False) as clear_button_face:remove_and_reupload_faces = gr.ClearButton(value="Remove and upload new ones", components=face_files, size="sm")# optional: upload a reference pose imagepose_files = gr.Files(label="Upload a reference pose image (optional)",file_types=["image"])uploaded_poses = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)with gr.Column(visible=False) as clear_button_pose:remove_and_reupload_poses = gr.ClearButton(value="Remove and upload new ones", components=pose_files, size="sm")# promptprompt = gr.Textbox(label="Prompt",info="Give simple prompt is enough to achieve good face fedility",placeholder="A photo of a man/woman")submit = gr.Button("Submit")with gr.Accordion(open=False, label="Advanced Options"):negative_prompt = gr.Textbox(label="Negative Prompt",placeholder="low quality",value="nsfw, lowres, bad anatomy, bad hands, text, error, missing fingers, extra digit, fewer digits, cropped, worst quality, low quality, normal quality, jpeg artifacts, signature, watermark, username, blurry",)num_steps = gr.Slider(label="Number of sample steps",minimum=20,maximum=100,step=1,value=30,)identitynet_strength_ratio = gr.Slider(label="IdentityNet strength",minimum=0,maximum=1.5,step=0.05,value=0.65,)adapter_strength_ratio = gr.Slider(label="Image adapter strength",minimum=0,maximum=1,step=0.05,value=0.30,)num_outputs = gr.Slider(label="Number of output images",minimum=1,maximum=4,step=1,value=2,)guidance_scale = gr.Slider(label="Guidance scale",minimum=0.1,maximum=10.0,step=0.1,value=5,)seed = gr.Slider(label="Seed",minimum=0,maximum=MAX_SEED,step=1,value=42,)randomize_seed = gr.Checkbox(label="Randomize seed", value=True)with gr.Column():gallery = gr.Gallery(label="Generated Images")usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)face_files.upload(fn=swap_to_gallery, inputs=face_files, outputs=[uploaded_faces, clear_button_face, face_files])pose_files.upload(fn=swap_to_gallery, inputs=pose_files, outputs=[uploaded_poses, clear_button_pose, pose_files])remove_and_reupload_faces.click(fn=remove_back_to_files, outputs=[uploaded_faces, clear_button_face, face_files])remove_and_reupload_poses.click(fn=remove_back_to_files, outputs=[uploaded_poses, clear_button_pose, pose_files])submit.click(fn=remove_tips,outputs=usage_tips,).then(fn=randomize_seed_fn,inputs=[seed, randomize_seed],outputs=seed,queue=False,api_name=False,).then(fn=generate_image,inputs=[face_files, pose_files, prompt, negative_prompt, num_steps, identitynet_strength_ratio, adapter_strength_ratio, num_outputs, guidance_scale, seed],outputs=[gallery, usage_tips])gr.Markdown(article)demo.launch()
1234567891011121314diffusers==0.25.0torch==2.0.0torchvision==0.15.1transformers==4.36.2acceleratesafetensorseinopsonnxruntime-gpuspaces==0.19.4omegaconfpefthuggingface-hub==0.20.2opencv-pythoninsightfaceapp.py+195-4
requirements.txtAdded+14