Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import cv2
|
@@ -23,7 +22,6 @@ import subprocess
|
|
23 |
import sys
|
24 |
|
25 |
|
26 |
-
@spaces.GPU(duration=120)
|
27 |
def download_liveportrait():
|
28 |
"""
|
29 |
Clone the LivePortrait repository and prepare its dependencies.
|
@@ -59,7 +57,7 @@ def download_liveportrait():
|
|
59 |
raise
|
60 |
download_liveportrait()
|
61 |
|
62 |
-
|
63 |
def download_huggingface_resources():
|
64 |
"""
|
65 |
Download additional necessary resources from Hugging Face using the CLI.
|
@@ -89,7 +87,6 @@ def download_huggingface_resources():
|
|
89 |
download_huggingface_resources()
|
90 |
|
91 |
|
92 |
-
@spaces.GPU(duration=120)
|
93 |
def get_project_root():
|
94 |
"""Get the root directory of the current project."""
|
95 |
return os.path.abspath(os.path.dirname(__file__))
|
@@ -126,12 +123,10 @@ pipe_inpaint_controlnet.to(device)
|
|
126 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
127 |
|
128 |
|
129 |
-
@spaces.GPU(duration=120)
|
130 |
def resize_to_multiple_of_64(width, height):
|
131 |
return (width // 64) * 64, (height // 64) * 64
|
132 |
|
133 |
|
134 |
-
@spaces.GPU(duration=120)
|
135 |
def expand_mask(mask, kernel_size):
|
136 |
mask_array = np.array(mask)
|
137 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
@@ -141,7 +136,6 @@ def expand_mask(mask, kernel_size):
|
|
141 |
return Image.fromarray(expanded_mask_array)
|
142 |
|
143 |
|
144 |
-
@spaces.GPU(duration=120)
|
145 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
146 |
"""
|
147 |
Detects the face in the input image and crops an enlarged square region around it.
|
@@ -171,7 +165,6 @@ def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
|
171 |
return resized_image
|
172 |
|
173 |
|
174 |
-
@spaces.GPU(duration=120)
|
175 |
def spirit_animal_baseline(image_path, num_images = 4):
|
176 |
|
177 |
image = cv2.imread(image_path)
|
@@ -253,7 +246,6 @@ def spirit_animal_baseline(image_path, num_images = 4):
|
|
253 |
return prompt, generated_images
|
254 |
|
255 |
|
256 |
-
@spaces.GPU(duration=120)
|
257 |
def spirit_animal_with_background(image_path, num_images = 4):
|
258 |
|
259 |
image = cv2.imread(image_path)
|
@@ -343,7 +335,6 @@ def spirit_animal_with_background(image_path, num_images = 4):
|
|
343 |
return prompt, generated_images
|
344 |
|
345 |
|
346 |
-
@spaces.GPU(duration=120)
|
347 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
348 |
|
349 |
image = cv2.imread(image_path)
|
@@ -465,7 +456,6 @@ def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
|
465 |
return formatted_prompts, generated_images
|
466 |
|
467 |
|
468 |
-
@spaces.GPU(duration=120)
|
469 |
def wait_for_file(file_path, timeout=500):
|
470 |
"""
|
471 |
Wait for a file to be created, with a specified timeout.
|
@@ -483,7 +473,6 @@ def wait_for_file(file_path, timeout=500):
|
|
483 |
return True
|
484 |
|
485 |
|
486 |
-
@spaces.GPU(duration=120)
|
487 |
def generate_spirit_animal_video(driving_video_path):
|
488 |
os.chdir(".")
|
489 |
try:
|
@@ -542,7 +531,6 @@ def generate_spirit_animal_video(driving_video_path):
|
|
542 |
return None
|
543 |
|
544 |
|
545 |
-
@spaces.GPU(duration=120)
|
546 |
def generate_spirit_animal(image, animal_type, background):
|
547 |
if animal_type == "Single Animal":
|
548 |
if background == "Preserve Background":
|
@@ -557,7 +545,6 @@ def generate_spirit_animal(image, animal_type, background):
|
|
557 |
return prompt, generated_images
|
558 |
|
559 |
|
560 |
-
@spaces.GPU(duration=120)
|
561 |
def compress_video(input_path, output_path, target_size_mb):
|
562 |
target_size_bytes = target_size_mb * 1024 * 1024
|
563 |
temp_output = "./temp_compressed.mp4"
|
@@ -587,7 +574,6 @@ def compress_video(input_path, output_path, target_size_mb):
|
|
587 |
shutil.move(temp_output, output_path)
|
588 |
|
589 |
|
590 |
-
@spaces.GPU(duration=120)
|
591 |
def process_video(video_file):
|
592 |
|
593 |
# # εε§ε LivePortrait
|
@@ -613,7 +599,7 @@ def process_video(video_file):
|
|
613 |
generate_spirit_animal_video(compressed_path)
|
614 |
|
615 |
# Wait until the output video is generated
|
616 |
-
timeout =
|
617 |
if not wait_for_file(output_video_path, timeout=timeout):
|
618 |
print("Timeout occurred while waiting for video generation.")
|
619 |
return gr.update(value=None, visible=False) # Hide output if failed
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import cv2
|
|
|
22 |
import sys
|
23 |
|
24 |
|
|
|
25 |
def download_liveportrait():
|
26 |
"""
|
27 |
Clone the LivePortrait repository and prepare its dependencies.
|
|
|
57 |
raise
|
58 |
download_liveportrait()
|
59 |
|
60 |
+
|
61 |
def download_huggingface_resources():
|
62 |
"""
|
63 |
Download additional necessary resources from Hugging Face using the CLI.
|
|
|
87 |
download_huggingface_resources()
|
88 |
|
89 |
|
|
|
90 |
def get_project_root():
|
91 |
"""Get the root directory of the current project."""
|
92 |
return os.path.abspath(os.path.dirname(__file__))
|
|
|
123 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
124 |
|
125 |
|
|
|
126 |
def resize_to_multiple_of_64(width, height):
|
127 |
return (width // 64) * 64, (height // 64) * 64
|
128 |
|
129 |
|
|
|
130 |
def expand_mask(mask, kernel_size):
|
131 |
mask_array = np.array(mask)
|
132 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
|
|
136 |
return Image.fromarray(expanded_mask_array)
|
137 |
|
138 |
|
|
|
139 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
140 |
"""
|
141 |
Detects the face in the input image and crops an enlarged square region around it.
|
|
|
165 |
return resized_image
|
166 |
|
167 |
|
|
|
168 |
def spirit_animal_baseline(image_path, num_images = 4):
|
169 |
|
170 |
image = cv2.imread(image_path)
|
|
|
246 |
return prompt, generated_images
|
247 |
|
248 |
|
|
|
249 |
def spirit_animal_with_background(image_path, num_images = 4):
|
250 |
|
251 |
image = cv2.imread(image_path)
|
|
|
335 |
return prompt, generated_images
|
336 |
|
337 |
|
|
|
338 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
339 |
|
340 |
image = cv2.imread(image_path)
|
|
|
456 |
return formatted_prompts, generated_images
|
457 |
|
458 |
|
|
|
459 |
def wait_for_file(file_path, timeout=500):
|
460 |
"""
|
461 |
Wait for a file to be created, with a specified timeout.
|
|
|
473 |
return True
|
474 |
|
475 |
|
|
|
476 |
def generate_spirit_animal_video(driving_video_path):
|
477 |
os.chdir(".")
|
478 |
try:
|
|
|
531 |
return None
|
532 |
|
533 |
|
|
|
534 |
def generate_spirit_animal(image, animal_type, background):
|
535 |
if animal_type == "Single Animal":
|
536 |
if background == "Preserve Background":
|
|
|
545 |
return prompt, generated_images
|
546 |
|
547 |
|
|
|
548 |
def compress_video(input_path, output_path, target_size_mb):
|
549 |
target_size_bytes = target_size_mb * 1024 * 1024
|
550 |
temp_output = "./temp_compressed.mp4"
|
|
|
574 |
shutil.move(temp_output, output_path)
|
575 |
|
576 |
|
|
|
577 |
def process_video(video_file):
|
578 |
|
579 |
# # εε§ε LivePortrait
|
|
|
599 |
generate_spirit_animal_video(compressed_path)
|
600 |
|
601 |
# Wait until the output video is generated
|
602 |
+
timeout = 1000 # Timeout in seconds
|
603 |
if not wait_for_file(output_video_path, timeout=timeout):
|
604 |
print("Timeout occurred while waiting for video generation.")
|
605 |
return gr.update(value=None, visible=False) # Hide output if failed
|