ResearcherXman commited on
Commit
3c49f9a
β€’
1 Parent(s): 4912220
Files changed (2) hide show
  1. app.py +195 -4
  2. requirements.txt +14 -0
app.py CHANGED
@@ -1,7 +1,198 @@
 
 
 
 
 
 
 
 
 
1
  import gradio as gr
2
 
3
- def greet(name):
4
- return "Hello " + name + "!!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
7
- iface.launch()
 
 
1
+ import os
2
+ import cv2
3
+ import math
4
+ import random
5
+ import numpy as np
6
+ from PIL import Image
7
+
8
+ from diffusers.utils import load_image
9
+
10
  import gradio as gr
11
 
12
+ # global variable
13
+ MAX_SEED = np.iinfo(np.int32).max
14
+
15
+ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
16
+ if randomize_seed:
17
+ seed = random.randint(0, MAX_SEED)
18
+ return seed
19
+
20
+ def swap_to_gallery(images):
21
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
22
+
23
+ def upload_example_to_gallery(images, prompt, style, negative_prompt):
24
+ return gr.update(value=images, visible=True), gr.update(visible=True), gr.update(visible=False)
25
+
26
+ def remove_back_to_files():
27
+ return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
28
+
29
+ def remove_tips():
30
+ return gr.update(visible=False)
31
+
32
+ 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)):
33
+
34
+ if face_image is None:
35
+ raise gr.Error(f"Cannot find any input face image! Please upload the face image")
36
+
37
+ face_image = load_image(face_image[0])
38
+
39
+ return [face_image], gr.update(visible=True)
40
+
41
+ ### Description
42
+ title = r"""
43
+ <h1 align="center">InstantID: Zero-shot Identity-Preserving Generation in Seconds</h1>
44
+ """
45
+
46
+ description = r"""
47
+ <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>
48
+
49
+ How to use:<br>
50
+ 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.
51
+ 2. (Optionally) upload another person image as reference pose. If not uploaded, we will use the first person image to extract landmarks.
52
+ 3. Enter a text prompt as normal text-to-image model.
53
+ 4. Click the <b>Submit</b> button to start customizing.
54
+ 5. Share your customizd photo with your friends, enjoy😊!
55
+ """
56
+
57
+ article = r"""
58
+ ---
59
+ πŸ“ **Citation**
60
+ <br>
61
+ If our work is helpful for your research or applications, please cite us via:
62
+ ```bibtex
63
+ @misc{wang2024instantid,
64
+ title={InstantID: Zero-shot Identity-Preserving Generation in Seconds},
65
+ author={Qixun Wang and Xu Bai and Haofan Wang and Zekui Qin and Anthony Chen},
66
+ year={2024},
67
+ eprint={2401.07519},
68
+ archivePrefix={arXiv},
69
+ primaryClass={cs.CV}
70
+ }
71
+ ```
72
+
73
+ πŸ“§ **Contact**
74
+ <br>
75
+ If you have any questions, please feel free to open an issue or directly reach us out at <b>haofanwang.ai@gmail.com</b>.
76
+ """
77
+
78
+ tips = r"""
79
+ ### Usage tips of InstantID
80
+ 1. If you're not satisfied with the similarity, scroll down to "Advanced Options" and increase the weight of "IdentityNet Strength" and "Adapter Strength".
81
+ 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.
82
+ 3. If you find that text control is not as expected, decrease Adapter strength.
83
+ """
84
+
85
+ css = '''
86
+ .gradio-container {width: 85% !important}
87
+ '''
88
+ with gr.Blocks(css=css) as demo:
89
+
90
+ # description
91
+ gr.Markdown(title)
92
+ gr.Markdown(description)
93
+
94
+ with gr.Row():
95
+ with gr.Column():
96
+
97
+ # upload face image
98
+ face_files = gr.Files(
99
+ label="Upload a photo of your face",
100
+ file_types=["image"]
101
+ )
102
+ uploaded_faces = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)
103
+ with gr.Column(visible=False) as clear_button_face:
104
+ remove_and_reupload_faces = gr.ClearButton(value="Remove and upload new ones", components=face_files, size="sm")
105
+
106
+ # optional: upload a reference pose image
107
+ pose_files = gr.Files(
108
+ label="Upload a reference pose image (optional)",
109
+ file_types=["image"]
110
+ )
111
+ uploaded_poses = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)
112
+ with gr.Column(visible=False) as clear_button_pose:
113
+ remove_and_reupload_poses = gr.ClearButton(value="Remove and upload new ones", components=pose_files, size="sm")
114
+
115
+ # prompt
116
+ prompt = gr.Textbox(label="Prompt",
117
+ info="Give simple prompt is enough to achieve good face fedility",
118
+ placeholder="A photo of a man/woman")
119
+ submit = gr.Button("Submit")
120
+
121
+ with gr.Accordion(open=False, label="Advanced Options"):
122
+ negative_prompt = gr.Textbox(
123
+ label="Negative Prompt",
124
+ placeholder="low quality",
125
+ 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",
126
+ )
127
+ num_steps = gr.Slider(
128
+ label="Number of sample steps",
129
+ minimum=20,
130
+ maximum=100,
131
+ step=1,
132
+ value=30,
133
+ )
134
+ identitynet_strength_ratio = gr.Slider(
135
+ label="IdentityNet strength",
136
+ minimum=0,
137
+ maximum=1.5,
138
+ step=0.05,
139
+ value=0.65,
140
+ )
141
+ adapter_strength_ratio = gr.Slider(
142
+ label="Image adapter strength",
143
+ minimum=0,
144
+ maximum=1,
145
+ step=0.05,
146
+ value=0.30,
147
+ )
148
+ num_outputs = gr.Slider(
149
+ label="Number of output images",
150
+ minimum=1,
151
+ maximum=4,
152
+ step=1,
153
+ value=2,
154
+ )
155
+ guidance_scale = gr.Slider(
156
+ label="Guidance scale",
157
+ minimum=0.1,
158
+ maximum=10.0,
159
+ step=0.1,
160
+ value=5,
161
+ )
162
+ seed = gr.Slider(
163
+ label="Seed",
164
+ minimum=0,
165
+ maximum=MAX_SEED,
166
+ step=1,
167
+ value=42,
168
+ )
169
+ randomize_seed = gr.Checkbox(label="Randomize seed", value=True)
170
+
171
+ with gr.Column():
172
+ gallery = gr.Gallery(label="Generated Images")
173
+ usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips ,visible=False)
174
+
175
+ face_files.upload(fn=swap_to_gallery, inputs=face_files, outputs=[uploaded_faces, clear_button_face, face_files])
176
+ pose_files.upload(fn=swap_to_gallery, inputs=pose_files, outputs=[uploaded_poses, clear_button_pose, pose_files])
177
+
178
+ remove_and_reupload_faces.click(fn=remove_back_to_files, outputs=[uploaded_faces, clear_button_face, face_files])
179
+ remove_and_reupload_poses.click(fn=remove_back_to_files, outputs=[uploaded_poses, clear_button_pose, pose_files])
180
+
181
+ submit.click(
182
+ fn=remove_tips,
183
+ outputs=usage_tips,
184
+ ).then(
185
+ fn=randomize_seed_fn,
186
+ inputs=[seed, randomize_seed],
187
+ outputs=seed,
188
+ queue=False,
189
+ api_name=False,
190
+ ).then(
191
+ fn=generate_image,
192
+ inputs=[face_files, pose_files, prompt, negative_prompt, num_steps, identitynet_strength_ratio, adapter_strength_ratio, num_outputs, guidance_scale, seed],
193
+ outputs=[gallery, usage_tips]
194
+ )
195
 
196
+ gr.Markdown(article)
197
+
198
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1,14 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ diffusers==0.25.0
2
+ torch==2.0.0
3
+ torchvision==0.15.1
4
+ transformers==4.36.2
5
+ accelerate
6
+ safetensors
7
+ einops
8
+ onnxruntime-gpu
9
+ spaces==0.19.4
10
+ omegaconf
11
+ peft
12
+ huggingface-hub==0.20.2
13
+ opencv-python
14
+ insightface