Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import cv2
|
|
@@ -21,6 +22,7 @@ from huggingface_hub import snapshot_download
|
|
| 21 |
import subprocess
|
| 22 |
import sys
|
| 23 |
|
|
|
|
| 24 |
def download_liveportrait():
|
| 25 |
"""
|
| 26 |
Clone the LivePortrait repository and prepare its dependencies.
|
|
@@ -55,6 +57,7 @@ def download_liveportrait():
|
|
| 55 |
print("Failed to initialize LivePortrait:", e)
|
| 56 |
raise
|
| 57 |
|
|
|
|
| 58 |
def download_huggingface_resources():
|
| 59 |
"""
|
| 60 |
Download additional necessary resources from Hugging Face using the CLI.
|
|
@@ -81,6 +84,7 @@ def download_huggingface_resources():
|
|
| 81 |
print("General error in downloading resources:", e)
|
| 82 |
raise
|
| 83 |
|
|
|
|
| 84 |
def get_project_root():
|
| 85 |
"""Get the root directory of the current project."""
|
| 86 |
return os.path.abspath(os.path.dirname(__file__))
|
|
@@ -116,9 +120,11 @@ pipe_controlnet.enable_attention_slicing()
|
|
| 116 |
pipe_inpaint_controlnet.to(device)
|
| 117 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
| 118 |
|
|
|
|
| 119 |
def resize_to_multiple_of_64(width, height):
|
| 120 |
return (width // 64) * 64, (height // 64) * 64
|
| 121 |
|
|
|
|
| 122 |
def expand_mask(mask, kernel_size):
|
| 123 |
mask_array = np.array(mask)
|
| 124 |
structuring_element = np.ones((kernel_size, kernel_size), dtype=np.uint8)
|
|
@@ -127,6 +133,7 @@ def expand_mask(mask, kernel_size):
|
|
| 127 |
).astype(np.uint8) * 255
|
| 128 |
return Image.fromarray(expanded_mask_array)
|
| 129 |
|
|
|
|
| 130 |
def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
| 131 |
"""
|
| 132 |
Detects the face in the input image and crops an enlarged square region around it.
|
|
@@ -155,6 +162,7 @@ def crop_face_to_square(image_rgb, padding_ratio=0.2):
|
|
| 155 |
|
| 156 |
return resized_image
|
| 157 |
|
|
|
|
| 158 |
def spirit_animal_baseline(image_path, num_images = 4):
|
| 159 |
|
| 160 |
image = cv2.imread(image_path)
|
|
@@ -235,6 +243,7 @@ def spirit_animal_baseline(image_path, num_images = 4):
|
|
| 235 |
|
| 236 |
return prompt, generated_images
|
| 237 |
|
|
|
|
| 238 |
def spirit_animal_with_background(image_path, num_images = 4):
|
| 239 |
|
| 240 |
image = cv2.imread(image_path)
|
|
@@ -323,6 +332,8 @@ def spirit_animal_with_background(image_path, num_images = 4):
|
|
| 323 |
|
| 324 |
return prompt, generated_images
|
| 325 |
|
|
|
|
|
|
|
| 326 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
| 327 |
|
| 328 |
image = cv2.imread(image_path)
|
|
@@ -443,6 +454,7 @@ def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
|
| 443 |
|
| 444 |
return formatted_prompts, generated_images
|
| 445 |
|
|
|
|
| 446 |
def wait_for_file(file_path, timeout=500):
|
| 447 |
"""
|
| 448 |
Wait for a file to be created, with a specified timeout.
|
|
@@ -459,6 +471,7 @@ def wait_for_file(file_path, timeout=500):
|
|
| 459 |
time.sleep(0.5) # Check every 0.5 seconds
|
| 460 |
return True
|
| 461 |
|
|
|
|
| 462 |
def generate_spirit_animal_video(driving_video_path):
|
| 463 |
os.chdir(".")
|
| 464 |
try:
|
|
@@ -516,6 +529,8 @@ def generate_spirit_animal_video(driving_video_path):
|
|
| 516 |
print(f"Error occurred: {e}")
|
| 517 |
return None
|
| 518 |
|
|
|
|
|
|
|
| 519 |
def generate_spirit_animal(image, animal_type, background):
|
| 520 |
if animal_type == "Single Animal":
|
| 521 |
if background == "Preserve Background":
|
|
@@ -529,6 +544,8 @@ def generate_spirit_animal(image, animal_type, background):
|
|
| 529 |
prompt, generated_images = generate_multiple_animals(image, keep_background=False)
|
| 530 |
return prompt, generated_images
|
| 531 |
|
|
|
|
|
|
|
| 532 |
def compress_video(input_path, output_path, target_size_mb):
|
| 533 |
target_size_bytes = target_size_mb * 1024 * 1024
|
| 534 |
temp_output = "./temp_compressed.mp4"
|
|
@@ -557,6 +574,7 @@ def compress_video(input_path, output_path, target_size_mb):
|
|
| 557 |
else:
|
| 558 |
shutil.move(temp_output, output_path)
|
| 559 |
|
|
|
|
| 560 |
def process_video(video_file):
|
| 561 |
|
| 562 |
# εε§ε LivePortrait
|
|
|
|
| 1 |
+
import spaces
|
| 2 |
import gradio as gr
|
| 3 |
import torch
|
| 4 |
import cv2
|
|
|
|
| 22 |
import subprocess
|
| 23 |
import sys
|
| 24 |
|
| 25 |
+
@spaces.GPU(duration=120)
|
| 26 |
def download_liveportrait():
|
| 27 |
"""
|
| 28 |
Clone the LivePortrait repository and prepare its dependencies.
|
|
|
|
| 57 |
print("Failed to initialize LivePortrait:", e)
|
| 58 |
raise
|
| 59 |
|
| 60 |
+
@spaces.GPU(duration=120)
|
| 61 |
def download_huggingface_resources():
|
| 62 |
"""
|
| 63 |
Download additional necessary resources from Hugging Face using the CLI.
|
|
|
|
| 84 |
print("General error in downloading resources:", e)
|
| 85 |
raise
|
| 86 |
|
| 87 |
+
@spaces.GPU(duration=120)
|
| 88 |
def get_project_root():
|
| 89 |
"""Get the root directory of the current project."""
|
| 90 |
return os.path.abspath(os.path.dirname(__file__))
|
|
|
|
| 120 |
pipe_inpaint_controlnet.to(device)
|
| 121 |
pipe_inpaint_controlnet.enable_attention_slicing()
|
| 122 |
|
| 123 |
+
@spaces.GPU(duration=120)
|
| 124 |
def resize_to_multiple_of_64(width, height):
|
| 125 |
return (width // 64) * 64, (height // 64) * 64
|
| 126 |
|
| 127 |
+
@spaces.GPU(duration=120)
|
| 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)
|
|
|
|
| 133 |
).astype(np.uint8) * 255
|
| 134 |
return Image.fromarray(expanded_mask_array)
|
| 135 |
|
| 136 |
+
@spaces.GPU(duration=120)
|
| 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.
|
|
|
|
| 162 |
|
| 163 |
return resized_image
|
| 164 |
|
| 165 |
+
@spaces.GPU(duration=120)
|
| 166 |
def spirit_animal_baseline(image_path, num_images = 4):
|
| 167 |
|
| 168 |
image = cv2.imread(image_path)
|
|
|
|
| 243 |
|
| 244 |
return prompt, generated_images
|
| 245 |
|
| 246 |
+
@spaces.GPU(duration=120)
|
| 247 |
def spirit_animal_with_background(image_path, num_images = 4):
|
| 248 |
|
| 249 |
image = cv2.imread(image_path)
|
|
|
|
| 332 |
|
| 333 |
return prompt, generated_images
|
| 334 |
|
| 335 |
+
|
| 336 |
+
@spaces.GPU(duration=120)
|
| 337 |
def generate_multiple_animals(image_path, keep_background=True, num_images = 4):
|
| 338 |
|
| 339 |
image = cv2.imread(image_path)
|
|
|
|
| 454 |
|
| 455 |
return formatted_prompts, generated_images
|
| 456 |
|
| 457 |
+
@spaces.GPU(duration=120)
|
| 458 |
def wait_for_file(file_path, timeout=500):
|
| 459 |
"""
|
| 460 |
Wait for a file to be created, with a specified timeout.
|
|
|
|
| 471 |
time.sleep(0.5) # Check every 0.5 seconds
|
| 472 |
return True
|
| 473 |
|
| 474 |
+
@spaces.GPU(duration=120)
|
| 475 |
def generate_spirit_animal_video(driving_video_path):
|
| 476 |
os.chdir(".")
|
| 477 |
try:
|
|
|
|
| 529 |
print(f"Error occurred: {e}")
|
| 530 |
return None
|
| 531 |
|
| 532 |
+
|
| 533 |
+
@spaces.GPU(duration=120)
|
| 534 |
def generate_spirit_animal(image, animal_type, background):
|
| 535 |
if animal_type == "Single Animal":
|
| 536 |
if background == "Preserve Background":
|
|
|
|
| 544 |
prompt, generated_images = generate_multiple_animals(image, keep_background=False)
|
| 545 |
return prompt, generated_images
|
| 546 |
|
| 547 |
+
|
| 548 |
+
@spaces.GPU(duration=120)
|
| 549 |
def compress_video(input_path, output_path, target_size_mb):
|
| 550 |
target_size_bytes = target_size_mb * 1024 * 1024
|
| 551 |
temp_output = "./temp_compressed.mp4"
|
|
|
|
| 574 |
else:
|
| 575 |
shutil.move(temp_output, output_path)
|
| 576 |
|
| 577 |
+
@spaces.GPU(duration=120)
|
| 578 |
def process_video(video_file):
|
| 579 |
|
| 580 |
# εε§ε LivePortrait
|