Spaces:
Sleeping
Sleeping
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
|
@@ -22,24 +21,7 @@ from huggingface_hub import snapshot_download
|
|
22 |
import subprocess
|
23 |
import sys
|
24 |
|
25 |
-
@spaces.GPU(duration=120)
|
26 |
-
def install_deformable_detr():
|
27 |
-
# 克隆 Deformable-DETR 仓库
|
28 |
-
if not os.path.exists('Deformable-DETR'):
|
29 |
-
subprocess.run(['git', 'clone', 'https://github.com/fundamentalvision/Deformable-DETR.git'])
|
30 |
-
|
31 |
-
# 安装依赖
|
32 |
-
subprocess.run(['pip', 'install', '-r', 'Deformable-DETR/requirements.txt'])
|
33 |
-
|
34 |
-
# 编译 CUDA 操作符
|
35 |
-
os.chdir('Deformable-DETR/models/ops')
|
36 |
-
subprocess.run(['sh', './make.sh'])
|
37 |
-
os.chdir('../../../..')
|
38 |
-
|
39 |
-
# 在应用启动时调用安装函数
|
40 |
-
install_deformable_detr()
|
41 |
|
42 |
-
@spaces.GPU(duration=120)
|
43 |
def download_liveportrait():
|
44 |
"""
|
45 |
Clone the LivePortrait repository and prepare its dependencies.
|
@@ -74,7 +56,9 @@ def download_liveportrait():
|
|
74 |
print("Failed to initialize LivePortrait:", e)
|
75 |
raise
|
76 |
|
77 |
-
|
|
|
|
|
78 |
def download_huggingface_resources():
|
79 |
"""
|
80 |
Download additional necessary resources from Hugging Face using the CLI.
|
@@ -101,7 +85,9 @@ def download_huggingface_resources():
|
|
101 |
print("General error in downloading resources:", e)
|
102 |
raise
|
103 |
|
104 |
-
|
|
|
|
|
105 |
def get_project_root():
|
106 |
"""Get the root directory of the current project."""
|
107 |
return os.path.abspath(os.path.dirname(__file__))
|
@@ -137,11 +123,11 @@ pipe_controlnet.enable_attention_slicing()
|
|
137 |
pipe_inpaint_controlnet.to(device)
|
138 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
139 |
|
140 |
-
|
141 |
def resize_to_multiple_of_64(width, height):
|
142 |
return (width // 64) * 64, (height // 64) * 64
|
143 |
|
144 |
-
|
145 |
def expand_mask(mask, kernel_size):
|
146 |
mask_array = np.array(mask)
|
147 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
@@ -150,7 +136,7 @@ def expand_mask(mask, kernel_size):
|
|
150 |
).astype(np.uint8) * 255
|
151 |
return Image.fromarray(expanded_mask_array)
|
152 |
|
153 |
-
|
154 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
155 |
"""
|
156 |
Detects the face in the input image and crops an enlarged square region around it.
|
@@ -179,7 +165,7 @@ def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
|
179 |
|
180 |
return resized_image
|
181 |
|
182 |
-
|
183 |
def spirit_animal_baseline(image_path, num_images = 4):
|
184 |
|
185 |
image = cv2.imread(image_path)
|
@@ -260,7 +246,7 @@ def spirit_animal_baseline(image_path, num_images = 4):
|
|
260 |
|
261 |
return prompt, generated_images
|
262 |
|
263 |
-
|
264 |
def spirit_animal_with_background(image_path, num_images = 4):
|
265 |
|
266 |
image = cv2.imread(image_path)
|
@@ -350,7 +336,6 @@ def spirit_animal_with_background(image_path, num_images = 4):
|
|
350 |
return prompt, generated_images
|
351 |
|
352 |
|
353 |
-
@spaces.GPU(duration=120)
|
354 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
355 |
|
356 |
image = cv2.imread(image_path)
|
@@ -471,7 +456,7 @@ def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
|
471 |
|
472 |
return formatted_prompts, generated_images
|
473 |
|
474 |
-
|
475 |
def wait_for_file(file_path, timeout=500):
|
476 |
"""
|
477 |
Wait for a file to be created, with a specified timeout.
|
@@ -488,7 +473,7 @@ def wait_for_file(file_path, timeout=500):
|
|
488 |
time.sleep(0.5) # Check every 0.5 seconds
|
489 |
return True
|
490 |
|
491 |
-
|
492 |
def generate_spirit_animal_video(driving_video_path):
|
493 |
os.chdir(".")
|
494 |
try:
|
@@ -594,19 +579,19 @@ def compress_video(input_path, output_path, target_size_mb):
|
|
594 |
@spaces.GPU(duration=120)
|
595 |
def process_video(video_file):
|
596 |
|
597 |
-
# 初始化 LivePortrait
|
598 |
-
try:
|
599 |
-
|
600 |
-
except Exception as e:
|
601 |
-
|
602 |
-
|
603 |
|
604 |
-
|
605 |
-
try:
|
606 |
-
|
607 |
-
except Exception as e:
|
608 |
-
|
609 |
-
|
610 |
|
611 |
compressed_path = "./uploaded_video_compressed.mp4"
|
612 |
compress_video(video_file, compressed_path, target_size_mb=1)
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import torch
|
3 |
import cv2
|
|
|
21 |
import subprocess
|
22 |
import sys
|
23 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
|
|
25 |
def download_liveportrait():
|
26 |
"""
|
27 |
Clone the LivePortrait repository and prepare its dependencies.
|
|
|
56 |
print("Failed to initialize LivePortrait:", e)
|
57 |
raise
|
58 |
|
59 |
+
download_liveportrait()
|
60 |
+
|
61 |
+
|
62 |
def download_huggingface_resources():
|
63 |
"""
|
64 |
Download additional necessary resources from Hugging Face using the CLI.
|
|
|
85 |
print("General error in downloading resources:", e)
|
86 |
raise
|
87 |
|
88 |
+
download_huggingface_resources():
|
89 |
+
|
90 |
+
|
91 |
def get_project_root():
|
92 |
"""Get the root directory of the current project."""
|
93 |
return os.path.abspath(os.path.dirname(__file__))
|
|
|
123 |
pipe_inpaint_controlnet.to(device)
|
124 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
125 |
|
126 |
+
|
127 |
def resize_to_multiple_of_64(width, height):
|
128 |
return (width // 64) * 64, (height // 64) * 64
|
129 |
|
130 |
+
|
131 |
def expand_mask(mask, kernel_size):
|
132 |
mask_array = np.array(mask)
|
133 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
|
|
136 |
).astype(np.uint8) * 255
|
137 |
return Image.fromarray(expanded_mask_array)
|
138 |
|
139 |
+
|
140 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
141 |
"""
|
142 |
Detects the face in the input image and crops an enlarged square region around it.
|
|
|
165 |
|
166 |
return resized_image
|
167 |
|
168 |
+
|
169 |
def spirit_animal_baseline(image_path, num_images = 4):
|
170 |
|
171 |
image = cv2.imread(image_path)
|
|
|
246 |
|
247 |
return prompt, generated_images
|
248 |
|
249 |
+
|
250 |
def spirit_animal_with_background(image_path, num_images = 4):
|
251 |
|
252 |
image = cv2.imread(image_path)
|
|
|
336 |
return prompt, generated_images
|
337 |
|
338 |
|
|
|
339 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
340 |
|
341 |
image = cv2.imread(image_path)
|
|
|
456 |
|
457 |
return formatted_prompts, generated_images
|
458 |
|
459 |
+
|
460 |
def wait_for_file(file_path, timeout=500):
|
461 |
"""
|
462 |
Wait for a file to be created, with a specified timeout.
|
|
|
473 |
time.sleep(0.5) # Check every 0.5 seconds
|
474 |
return True
|
475 |
|
476 |
+
|
477 |
def generate_spirit_animal_video(driving_video_path):
|
478 |
os.chdir(".")
|
479 |
try:
|
|
|
579 |
@spaces.GPU(duration=120)
|
580 |
def process_video(video_file):
|
581 |
|
582 |
+
# # 初始化 LivePortrait
|
583 |
+
# try:
|
584 |
+
# download_liveportrait()
|
585 |
+
# except Exception as e:
|
586 |
+
# print("Failed to initialize LivePortrait:", e)
|
587 |
+
# return gr.update(value=None, visible=False)
|
588 |
|
589 |
+
# # 下载 Hugging Face 资源
|
590 |
+
# try:
|
591 |
+
# download_huggingface_resources()
|
592 |
+
# except Exception as e:
|
593 |
+
# print("Failed to download Hugging Face resources:", e)
|
594 |
+
# return gr.update(value=None, visible=False)
|
595 |
|
596 |
compressed_path = "./uploaded_video_compressed.mp4"
|
597 |
compress_video(video_file, compressed_path, target_size_mb=1)
|