Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,3 +1,4 @@
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import cv2
|
@@ -22,6 +23,7 @@ import subprocess
|
|
22 |
import sys
|
23 |
|
24 |
|
|
|
25 |
def download_liveportrait():
|
26 |
"""
|
27 |
Clone the LivePortrait repository and prepare its dependencies.
|
@@ -57,6 +59,7 @@ def download_liveportrait():
|
|
57 |
raise
|
58 |
download_liveportrait()
|
59 |
|
|
|
60 |
def download_huggingface_resources():
|
61 |
"""
|
62 |
Download additional necessary resources from Hugging Face using the CLI.
|
@@ -85,6 +88,8 @@ def download_huggingface_resources():
|
|
85 |
|
86 |
download_huggingface_resources()
|
87 |
|
|
|
|
|
88 |
def get_project_root():
|
89 |
"""Get the root directory of the current project."""
|
90 |
return os.path.abspath(os.path.dirname(__file__))
|
@@ -121,10 +126,12 @@ pipe_inpaint_controlnet.to(device)
|
|
121 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
122 |
|
123 |
|
|
|
124 |
def resize_to_multiple_of_64(width, height):
|
125 |
return (width // 64) * 64, (height // 64) * 64
|
126 |
|
127 |
|
|
|
128 |
def expand_mask(mask, kernel_size):
|
129 |
mask_array = np.array(mask)
|
130 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
@@ -134,6 +141,7 @@ def expand_mask(mask, kernel_size):
|
|
134 |
return Image.fromarray(expanded_mask_array)
|
135 |
|
136 |
|
|
|
137 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
138 |
"""
|
139 |
Detects the face in the input image and crops an enlarged square region around it.
|
@@ -163,6 +171,7 @@ def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
|
163 |
return resized_image
|
164 |
|
165 |
|
|
|
166 |
def spirit_animal_baseline(image_path, num_images = 4):
|
167 |
|
168 |
image = cv2.imread(image_path)
|
@@ -244,6 +253,7 @@ def spirit_animal_baseline(image_path, num_images = 4):
|
|
244 |
return prompt, generated_images
|
245 |
|
246 |
|
|
|
247 |
def spirit_animal_with_background(image_path, num_images = 4):
|
248 |
|
249 |
image = cv2.imread(image_path)
|
@@ -333,6 +343,7 @@ def spirit_animal_with_background(image_path, num_images = 4):
|
|
333 |
return prompt, generated_images
|
334 |
|
335 |
|
|
|
336 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
337 |
|
338 |
image = cv2.imread(image_path)
|
@@ -454,6 +465,7 @@ def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
|
454 |
return formatted_prompts, generated_images
|
455 |
|
456 |
|
|
|
457 |
def wait_for_file(file_path, timeout=500):
|
458 |
"""
|
459 |
Wait for a file to be created, with a specified timeout.
|
@@ -471,6 +483,7 @@ def wait_for_file(file_path, timeout=500):
|
|
471 |
return True
|
472 |
|
473 |
|
|
|
474 |
def generate_spirit_animal_video(driving_video_path):
|
475 |
os.chdir(".")
|
476 |
try:
|
@@ -529,6 +542,7 @@ def generate_spirit_animal_video(driving_video_path):
|
|
529 |
return None
|
530 |
|
531 |
|
|
|
532 |
def generate_spirit_animal(image, animal_type, background):
|
533 |
if animal_type == "Single Animal":
|
534 |
if background == "Preserve Background":
|
@@ -543,6 +557,7 @@ def generate_spirit_animal(image, animal_type, background):
|
|
543 |
return prompt, generated_images
|
544 |
|
545 |
|
|
|
546 |
def compress_video(input_path, output_path, target_size_mb):
|
547 |
target_size_bytes = target_size_mb * 1024 * 1024
|
548 |
temp_output = "./temp_compressed.mp4"
|
@@ -572,6 +587,7 @@ def compress_video(input_path, output_path, target_size_mb):
|
|
572 |
shutil.move(temp_output, output_path)
|
573 |
|
574 |
|
|
|
575 |
def process_video(video_file):
|
576 |
|
577 |
# # εε§ε LivePortrait
|
|
|
1 |
+
import spaces
|
2 |
import gradio as gr
|
3 |
import torch
|
4 |
import cv2
|
|
|
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 |
raise
|
60 |
download_liveportrait()
|
61 |
|
62 |
+
@spaces.GPU(duration=120)
|
63 |
def download_huggingface_resources():
|
64 |
"""
|
65 |
Download additional necessary resources from Hugging Face using the CLI.
|
|
|
88 |
|
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 |
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 |
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 |
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 |
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 |
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 |
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 |
return True
|
484 |
|
485 |
|
486 |
+
@spaces.GPU(duration=120)
|
487 |
def generate_spirit_animal_video(driving_video_path):
|
488 |
os.chdir(".")
|
489 |
try:
|
|
|
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 |
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 |
shutil.move(temp_output, output_path)
|
588 |
|
589 |
|
590 |
+
@spaces.GPU(duration=120)
|
591 |
def process_video(video_file):
|
592 |
|
593 |
# # εε§ε LivePortrait
|