Spaces:
Running
on
Zero
Running
on
Zero
Use gr.Image instead of gr.Files
Browse files
app.py
CHANGED
@@ -64,26 +64,6 @@ def randomize_seed_fn(seed: int, randomize_seed: bool) -> int:
|
|
64 |
return seed
|
65 |
|
66 |
|
67 |
-
def swap_to_gallery(images):
|
68 |
-
return (
|
69 |
-
gr.update(value=images, visible=True),
|
70 |
-
gr.update(visible=True),
|
71 |
-
gr.update(visible=False),
|
72 |
-
)
|
73 |
-
|
74 |
-
|
75 |
-
def upload_example_to_gallery(images, prompt, style, negative_prompt):
|
76 |
-
return (
|
77 |
-
gr.update(value=images, visible=True),
|
78 |
-
gr.update(visible=True),
|
79 |
-
gr.update(visible=False),
|
80 |
-
)
|
81 |
-
|
82 |
-
|
83 |
-
def remove_back_to_files():
|
84 |
-
return gr.update(visible=False), gr.update(visible=False), gr.update(visible=True)
|
85 |
-
|
86 |
-
|
87 |
def remove_tips():
|
88 |
return gr.update(visible=False)
|
89 |
|
@@ -91,31 +71,31 @@ def remove_tips():
|
|
91 |
def get_example():
|
92 |
case = [
|
93 |
[
|
94 |
-
|
95 |
"a man",
|
96 |
"Snow",
|
97 |
"(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",
|
98 |
],
|
99 |
[
|
100 |
-
|
101 |
"a man",
|
102 |
"Mars",
|
103 |
"(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",
|
104 |
],
|
105 |
[
|
106 |
-
|
107 |
"a man",
|
108 |
"Jungle",
|
109 |
"(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",
|
110 |
],
|
111 |
[
|
112 |
-
|
113 |
"a man",
|
114 |
"Neon",
|
115 |
"(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",
|
116 |
],
|
117 |
[
|
118 |
-
|
119 |
"a man",
|
120 |
"Vibrant Color",
|
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",
|
@@ -124,8 +104,8 @@ def get_example():
|
|
124 |
return case
|
125 |
|
126 |
|
127 |
-
def run_for_examples(
|
128 |
-
return generate_image(
|
129 |
|
130 |
|
131 |
def convert_from_cv2_to_image(img: np.ndarray) -> Image:
|
@@ -202,10 +182,15 @@ def apply_style(style_name: str, positive: str, negative: str = "") -> tuple[str
|
|
202 |
return p.replace("{prompt}", positive), n + " " + negative
|
203 |
|
204 |
|
|
|
|
|
|
|
|
|
|
|
205 |
@spaces.GPU
|
206 |
def generate_image(
|
207 |
-
|
208 |
-
|
209 |
prompt,
|
210 |
negative_prompt,
|
211 |
style_name,
|
@@ -217,16 +202,13 @@ def generate_image(
|
|
217 |
seed,
|
218 |
progress=gr.Progress(track_tqdm=True),
|
219 |
):
|
220 |
-
if face_image is None:
|
221 |
-
raise gr.Error("Cannot find any input face image! Please upload the face image")
|
222 |
-
|
223 |
if prompt is None:
|
224 |
prompt = "a person"
|
225 |
|
226 |
# apply the style template
|
227 |
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
228 |
|
229 |
-
face_image = load_image(
|
230 |
face_image = resize_img(face_image)
|
231 |
face_image_cv2 = convert_from_image_to_cv2(face_image)
|
232 |
height, width, _ = face_image_cv2.shape
|
@@ -237,17 +219,14 @@ def generate_image(
|
|
237 |
if len(face_info) == 0:
|
238 |
raise gr.Error("Cannot find any face in the image! Please upload another person image")
|
239 |
|
240 |
-
face_info = sorted(
|
241 |
-
face_info,
|
242 |
-
key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1],
|
243 |
-
)[
|
244 |
-1
|
245 |
] # only use the maximum face
|
246 |
face_emb = face_info["embedding"]
|
247 |
face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
|
248 |
|
249 |
-
if
|
250 |
-
pose_image = load_image(
|
251 |
pose_image = resize_img(pose_image)
|
252 |
pose_image_cv2 = convert_from_image_to_cv2(pose_image)
|
253 |
|
@@ -290,7 +269,7 @@ def generate_image(
|
|
290 |
generator=generator,
|
291 |
).images
|
292 |
|
293 |
-
return images, gr.update(visible=True)
|
294 |
|
295 |
|
296 |
### Description
|
@@ -346,20 +325,10 @@ with gr.Blocks(css=css) as demo:
|
|
346 |
with gr.Row():
|
347 |
with gr.Column():
|
348 |
# upload face image
|
349 |
-
|
350 |
-
uploaded_faces = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)
|
351 |
-
with gr.Column(visible=False) as clear_button_face:
|
352 |
-
remove_and_reupload_faces = gr.ClearButton(
|
353 |
-
value="Remove and upload new ones", components=face_files, size="sm"
|
354 |
-
)
|
355 |
|
356 |
# optional: upload a reference pose image
|
357 |
-
|
358 |
-
uploaded_poses = gr.Gallery(label="Your images", visible=False, columns=1, rows=1, height=512)
|
359 |
-
with gr.Column(visible=False) as clear_button_pose:
|
360 |
-
remove_and_reupload_poses = gr.ClearButton(
|
361 |
-
value="Remove and upload new ones", components=pose_files, size="sm"
|
362 |
-
)
|
363 |
|
364 |
# prompt
|
365 |
prompt = gr.Textbox(
|
@@ -420,32 +389,14 @@ with gr.Blocks(css=css) as demo:
|
|
420 |
enhance_face_region = gr.Checkbox(label="Enhance non-face region", value=True)
|
421 |
|
422 |
with gr.Column():
|
423 |
-
|
424 |
usage_tips = gr.Markdown(label="Usage tips of InstantID", value=tips, visible=False)
|
425 |
|
426 |
-
face_files.upload(
|
427 |
-
fn=swap_to_gallery,
|
428 |
-
inputs=face_files,
|
429 |
-
outputs=[uploaded_faces, clear_button_face, face_files],
|
430 |
-
)
|
431 |
-
pose_files.upload(
|
432 |
-
fn=swap_to_gallery,
|
433 |
-
inputs=pose_files,
|
434 |
-
outputs=[uploaded_poses, clear_button_pose, pose_files],
|
435 |
-
)
|
436 |
-
|
437 |
-
remove_and_reupload_faces.click(
|
438 |
-
fn=remove_back_to_files,
|
439 |
-
outputs=[uploaded_faces, clear_button_face, face_files],
|
440 |
-
)
|
441 |
-
remove_and_reupload_poses.click(
|
442 |
-
fn=remove_back_to_files,
|
443 |
-
outputs=[uploaded_poses, clear_button_pose, pose_files],
|
444 |
-
)
|
445 |
-
|
446 |
submit.click(
|
447 |
fn=remove_tips,
|
448 |
outputs=usage_tips,
|
|
|
|
|
449 |
).then(
|
450 |
fn=randomize_seed_fn,
|
451 |
inputs=[seed, randomize_seed],
|
@@ -453,10 +404,15 @@ with gr.Blocks(css=css) as demo:
|
|
453 |
queue=False,
|
454 |
api_name=False,
|
455 |
).then(
|
|
|
|
|
|
|
|
|
|
|
456 |
fn=generate_image,
|
457 |
inputs=[
|
458 |
-
|
459 |
-
|
460 |
prompt,
|
461 |
negative_prompt,
|
462 |
style,
|
@@ -467,16 +423,15 @@ with gr.Blocks(css=css) as demo:
|
|
467 |
guidance_scale,
|
468 |
seed,
|
469 |
],
|
470 |
-
outputs=[
|
471 |
)
|
472 |
|
473 |
gr.Examples(
|
474 |
examples=get_example(),
|
475 |
-
inputs=[
|
476 |
-
|
477 |
-
fn=
|
478 |
-
|
479 |
-
cache_examples=True,
|
480 |
)
|
481 |
|
482 |
gr.Markdown(article)
|
|
|
64 |
return seed
|
65 |
|
66 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
def remove_tips():
|
68 |
return gr.update(visible=False)
|
69 |
|
|
|
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",
|
|
|
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:
|
|
|
182 |
return p.replace("{prompt}", positive), n + " " + negative
|
183 |
|
184 |
|
185 |
+
def check_input_image(face_image):
|
186 |
+
if face_image is None:
|
187 |
+
raise gr.Error("Cannot find any input face image! Please upload the face image")
|
188 |
+
|
189 |
+
|
190 |
@spaces.GPU
|
191 |
def generate_image(
|
192 |
+
face_image_path,
|
193 |
+
pose_image_path,
|
194 |
prompt,
|
195 |
negative_prompt,
|
196 |
style_name,
|
|
|
202 |
seed,
|
203 |
progress=gr.Progress(track_tqdm=True),
|
204 |
):
|
|
|
|
|
|
|
205 |
if prompt is None:
|
206 |
prompt = "a person"
|
207 |
|
208 |
# apply the style template
|
209 |
prompt, negative_prompt = apply_style(style_name, prompt, negative_prompt)
|
210 |
|
211 |
+
face_image = load_image(face_image_path)
|
212 |
face_image = resize_img(face_image)
|
213 |
face_image_cv2 = convert_from_image_to_cv2(face_image)
|
214 |
height, width, _ = face_image_cv2.shape
|
|
|
219 |
if len(face_info) == 0:
|
220 |
raise gr.Error("Cannot find any face in the image! Please upload another person image")
|
221 |
|
222 |
+
face_info = sorted(face_info, key=lambda x: (x["bbox"][2] - x["bbox"][0]) * x["bbox"][3] - x["bbox"][1])[
|
|
|
|
|
|
|
223 |
-1
|
224 |
] # only use the maximum face
|
225 |
face_emb = face_info["embedding"]
|
226 |
face_kps = draw_kps(convert_from_cv2_to_image(face_image_cv2), face_info["kps"])
|
227 |
|
228 |
+
if pose_image_path is not None:
|
229 |
+
pose_image = load_image(pose_image_path)
|
230 |
pose_image = resize_img(pose_image)
|
231 |
pose_image_cv2 = convert_from_image_to_cv2(pose_image)
|
232 |
|
|
|
269 |
generator=generator,
|
270 |
).images
|
271 |
|
272 |
+
return images[0], gr.update(visible=True)
|
273 |
|
274 |
|
275 |
### Description
|
|
|
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(
|
|
|
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],
|
|
|
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,
|
|
|
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=False,
|
|
|
435 |
)
|
436 |
|
437 |
gr.Markdown(article)
|