Spaces:
Paused
Paused
Update app.py
Browse files
app.py
CHANGED
|
@@ -69,6 +69,8 @@ print(f"๐ Base directory: {BASE_DIR}")
|
|
| 69 |
print(f"๐ Temp directory: {TEMP_DIR}")
|
| 70 |
print(f"๐ค Output directory: {OUTPUT_DIR}")
|
| 71 |
print(f"๐ฏ Assets directory: {ASSETS_DIR}")
|
|
|
|
|
|
|
| 72 |
try:
|
| 73 |
from SwitcherAI.utilities import set_temp_directory, get_temp_directory_info
|
| 74 |
|
|
@@ -89,6 +91,7 @@ try:
|
|
| 89 |
except ImportError as e:
|
| 90 |
print(f"โ ๏ธ Could not import SwitcherAI utilities: {e}")
|
| 91 |
print("๐ Using default temp directory behavior")
|
|
|
|
| 92 |
# Download required model files
|
| 93 |
def download_required_models():
|
| 94 |
"""Download required model files if not present"""
|
|
@@ -219,6 +222,56 @@ def safe_copy_file(source, destination):
|
|
| 219 |
print(f"โ Copy error: {e}")
|
| 220 |
return False
|
| 221 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
def resize_video(input_path, output_path, fps=30):
|
| 223 |
"""Resize/process video with fallback"""
|
| 224 |
if not MOVIEPY_AVAILABLE:
|
|
@@ -265,6 +318,16 @@ def cleanup_temp_files():
|
|
| 265 |
except Exception as e:
|
| 266 |
print(f"โ ๏ธ Cleanup error: {e}")
|
| 267 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 268 |
def create_batch_zip():
|
| 269 |
"""Create zip file of all output files"""
|
| 270 |
try:
|
|
@@ -423,7 +486,7 @@ def run_batch_processing(source_image, frame_processor, face_analyser_direction,
|
|
| 423 |
video_files = list(CONVERT_DIR.glob("*.mp4")) + list(CONVERT_DIR.glob("*.avi")) + list(CONVERT_DIR.glob("*.mov"))
|
| 424 |
|
| 425 |
if not video_files:
|
| 426 |
-
yield None, f"๐ No video files found in
|
| 427 |
return
|
| 428 |
|
| 429 |
temp_source = TEMP_DIR / 'source-image.jpg'
|
|
@@ -432,7 +495,7 @@ def run_batch_processing(source_image, frame_processor, face_analyser_direction,
|
|
| 432 |
return
|
| 433 |
|
| 434 |
source_name = Path(source_image).stem
|
| 435 |
-
cli_output = f"๐ Processing {len(video_files)} videos\n๐ฏ Source: {source_name}\n\n"
|
| 436 |
yield None, cli_output
|
| 437 |
|
| 438 |
successful = 0
|
|
@@ -586,6 +649,7 @@ def cancel_processing():
|
|
| 586 |
def reset_interface():
|
| 587 |
"""Reset interface to defaults"""
|
| 588 |
cleanup_temp_files()
|
|
|
|
| 589 |
return (
|
| 590 |
None, # source_image
|
| 591 |
None, # target_video
|
|
@@ -622,6 +686,33 @@ def handle_action_button(button_text, *inputs):
|
|
| 622 |
else:
|
| 623 |
return inputs + ("", button_text)
|
| 624 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 625 |
# Initialize GPU detection
|
| 626 |
AVAILABLE_GPUS = get_available_gpus()
|
| 627 |
print(f"๐ฅ๏ธ Available GPUs: {AVAILABLE_GPUS}")
|
|
@@ -661,11 +752,25 @@ def create_interface():
|
|
| 661 |
file_count="single"
|
| 662 |
)
|
| 663 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 664 |
target_video = gr.File(
|
| 665 |
label="Target Video (Video to modify)",
|
| 666 |
file_types=["video"],
|
| 667 |
file_count="single"
|
| 668 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 669 |
|
| 670 |
with gr.Group(elem_classes="control-panel"):
|
| 671 |
gr.HTML('<div class="section-header">๐ฎ Controls</div>')
|
|
@@ -766,11 +871,6 @@ def create_interface():
|
|
| 766 |
skip_audio = gr.Checkbox(label="๐ Skip Audio", value=False)
|
| 767 |
|
| 768 |
with gr.Column():
|
| 769 |
-
use_folder_mode = gr.Checkbox(
|
| 770 |
-
label="๐ Batch Mode (Convert folder)",
|
| 771 |
-
value=False
|
| 772 |
-
)
|
| 773 |
-
|
| 774 |
keep_fps = gr.Checkbox(label="๐ฌ Keep Original FPS", value=True)
|
| 775 |
|
| 776 |
# Event handlers
|
|
@@ -781,11 +881,17 @@ def create_interface():
|
|
| 781 |
)
|
| 782 |
|
| 783 |
use_folder_mode.change(
|
| 784 |
-
|
| 785 |
inputs=[use_folder_mode],
|
| 786 |
outputs=[target_video]
|
| 787 |
)
|
| 788 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
start_button.click(
|
| 790 |
handle_processing,
|
| 791 |
inputs=[
|
|
|
|
| 69 |
print(f"๐ Temp directory: {TEMP_DIR}")
|
| 70 |
print(f"๐ค Output directory: {OUTPUT_DIR}")
|
| 71 |
print(f"๐ฏ Assets directory: {ASSETS_DIR}")
|
| 72 |
+
print(f"๐ Convert directory: {CONVERT_DIR}")
|
| 73 |
+
|
| 74 |
try:
|
| 75 |
from SwitcherAI.utilities import set_temp_directory, get_temp_directory_info
|
| 76 |
|
|
|
|
| 91 |
except ImportError as e:
|
| 92 |
print(f"โ ๏ธ Could not import SwitcherAI utilities: {e}")
|
| 93 |
print("๐ Using default temp directory behavior")
|
| 94 |
+
|
| 95 |
# Download required model files
|
| 96 |
def download_required_models():
|
| 97 |
"""Download required model files if not present"""
|
|
|
|
| 222 |
print(f"โ Copy error: {e}")
|
| 223 |
return False
|
| 224 |
|
| 225 |
+
def handle_batch_file_upload(files):
|
| 226 |
+
"""Handle multiple file uploads for batch mode"""
|
| 227 |
+
if not files:
|
| 228 |
+
return "๐ No files uploaded"
|
| 229 |
+
|
| 230 |
+
# Clear existing files in convert directory
|
| 231 |
+
for existing_file in CONVERT_DIR.glob("*"):
|
| 232 |
+
if existing_file.is_file():
|
| 233 |
+
existing_file.unlink()
|
| 234 |
+
|
| 235 |
+
uploaded_count = 0
|
| 236 |
+
failed_count = 0
|
| 237 |
+
|
| 238 |
+
for file in files:
|
| 239 |
+
try:
|
| 240 |
+
if file is None:
|
| 241 |
+
continue
|
| 242 |
+
|
| 243 |
+
# Get the original filename
|
| 244 |
+
original_name = Path(file.name).name if hasattr(file, 'name') else f"video_{uploaded_count}.mp4"
|
| 245 |
+
|
| 246 |
+
# Copy file to convert directory
|
| 247 |
+
dest_path = CONVERT_DIR / original_name
|
| 248 |
+
shutil.copy2(file, dest_path)
|
| 249 |
+
|
| 250 |
+
if dest_path.exists() and dest_path.stat().st_size > 0:
|
| 251 |
+
file_size = dest_path.stat().st_size / (1024 * 1024) # MB
|
| 252 |
+
print(f"โ
Uploaded: {original_name} ({file_size:.1f}MB)")
|
| 253 |
+
uploaded_count += 1
|
| 254 |
+
else:
|
| 255 |
+
print(f"โ Failed to upload: {original_name}")
|
| 256 |
+
failed_count += 1
|
| 257 |
+
|
| 258 |
+
except Exception as e:
|
| 259 |
+
print(f"โ Error uploading file: {e}")
|
| 260 |
+
failed_count += 1
|
| 261 |
+
|
| 262 |
+
status_msg = f"๐ฆ Batch Upload Complete:\nโ
Uploaded: {uploaded_count} files\n"
|
| 263 |
+
if failed_count > 0:
|
| 264 |
+
status_msg += f"โ Failed: {failed_count} files\n"
|
| 265 |
+
|
| 266 |
+
# List uploaded files
|
| 267 |
+
uploaded_files = [f.name for f in CONVERT_DIR.glob("*.mp4")] + [f.name for f in CONVERT_DIR.glob("*.avi")] + [f.name for f in CONVERT_DIR.glob("*.mov")]
|
| 268 |
+
if uploaded_files:
|
| 269 |
+
status_msg += f"๐ Files ready for processing:\n" + "\n".join([f" โข {f}" for f in uploaded_files[:10]])
|
| 270 |
+
if len(uploaded_files) > 10:
|
| 271 |
+
status_msg += f"\n ... and {len(uploaded_files) - 10} more"
|
| 272 |
+
|
| 273 |
+
return status_msg
|
| 274 |
+
|
| 275 |
def resize_video(input_path, output_path, fps=30):
|
| 276 |
"""Resize/process video with fallback"""
|
| 277 |
if not MOVIEPY_AVAILABLE:
|
|
|
|
| 318 |
except Exception as e:
|
| 319 |
print(f"โ ๏ธ Cleanup error: {e}")
|
| 320 |
|
| 321 |
+
def cleanup_convert_files():
|
| 322 |
+
"""Clean up convert directory files"""
|
| 323 |
+
try:
|
| 324 |
+
for file in CONVERT_DIR.glob("*"):
|
| 325 |
+
if file.is_file():
|
| 326 |
+
file.unlink()
|
| 327 |
+
print("๐งน Convert directory cleaned")
|
| 328 |
+
except Exception as e:
|
| 329 |
+
print(f"โ ๏ธ Convert cleanup error: {e}")
|
| 330 |
+
|
| 331 |
def create_batch_zip():
|
| 332 |
"""Create zip file of all output files"""
|
| 333 |
try:
|
|
|
|
| 486 |
video_files = list(CONVERT_DIR.glob("*.mp4")) + list(CONVERT_DIR.glob("*.avi")) + list(CONVERT_DIR.glob("*.mov"))
|
| 487 |
|
| 488 |
if not video_files:
|
| 489 |
+
yield None, f"๐ No video files found in Convert folder.\nPlease upload videos using the file input above."
|
| 490 |
return
|
| 491 |
|
| 492 |
temp_source = TEMP_DIR / 'source-image.jpg'
|
|
|
|
| 495 |
return
|
| 496 |
|
| 497 |
source_name = Path(source_image).stem
|
| 498 |
+
cli_output = f"๐ Processing {len(video_files)} videos in batch mode\n๐ฏ Source: {source_name}\n\n"
|
| 499 |
yield None, cli_output
|
| 500 |
|
| 501 |
successful = 0
|
|
|
|
| 649 |
def reset_interface():
|
| 650 |
"""Reset interface to defaults"""
|
| 651 |
cleanup_temp_files()
|
| 652 |
+
cleanup_convert_files()
|
| 653 |
return (
|
| 654 |
None, # source_image
|
| 655 |
None, # target_video
|
|
|
|
| 686 |
else:
|
| 687 |
return inputs + ("", button_text)
|
| 688 |
|
| 689 |
+
def toggle_batch_mode(use_folder_mode):
|
| 690 |
+
"""Handle batch mode toggle"""
|
| 691 |
+
if use_folder_mode:
|
| 692 |
+
return gr.update(
|
| 693 |
+
label="๐ Target Videos (Drag multiple files here)",
|
| 694 |
+
file_count="multiple",
|
| 695 |
+
file_types=["video"]
|
| 696 |
+
)
|
| 697 |
+
else:
|
| 698 |
+
return gr.update(
|
| 699 |
+
label="Target Video (Video to modify)",
|
| 700 |
+
file_count="single",
|
| 701 |
+
file_types=["video"]
|
| 702 |
+
)
|
| 703 |
+
|
| 704 |
+
def handle_file_upload(files, use_folder_mode):
|
| 705 |
+
"""Handle file uploads - single or multiple"""
|
| 706 |
+
if use_folder_mode and files:
|
| 707 |
+
# Handle batch upload
|
| 708 |
+
status = handle_batch_file_upload(files)
|
| 709 |
+
return status
|
| 710 |
+
elif not use_folder_mode and files:
|
| 711 |
+
# Single file mode - just return status
|
| 712 |
+
return f"โ
Single video uploaded: {Path(files.name).name if hasattr(files, 'name') else 'video file'}"
|
| 713 |
+
else:
|
| 714 |
+
return "๐ No files uploaded"
|
| 715 |
+
|
| 716 |
# Initialize GPU detection
|
| 717 |
AVAILABLE_GPUS = get_available_gpus()
|
| 718 |
print(f"๐ฅ๏ธ Available GPUs: {AVAILABLE_GPUS}")
|
|
|
|
| 752 |
file_count="single"
|
| 753 |
)
|
| 754 |
|
| 755 |
+
# Batch mode toggle
|
| 756 |
+
use_folder_mode = gr.Checkbox(
|
| 757 |
+
label="๐ Batch Mode (Process multiple videos)",
|
| 758 |
+
value=False
|
| 759 |
+
)
|
| 760 |
+
|
| 761 |
target_video = gr.File(
|
| 762 |
label="Target Video (Video to modify)",
|
| 763 |
file_types=["video"],
|
| 764 |
file_count="single"
|
| 765 |
)
|
| 766 |
+
|
| 767 |
+
# Upload status display
|
| 768 |
+
upload_status = gr.Textbox(
|
| 769 |
+
label="Upload Status",
|
| 770 |
+
value="Ready to upload files...",
|
| 771 |
+
interactive=False,
|
| 772 |
+
lines=3
|
| 773 |
+
)
|
| 774 |
|
| 775 |
with gr.Group(elem_classes="control-panel"):
|
| 776 |
gr.HTML('<div class="section-header">๐ฎ Controls</div>')
|
|
|
|
| 871 |
skip_audio = gr.Checkbox(label="๐ Skip Audio", value=False)
|
| 872 |
|
| 873 |
with gr.Column():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 874 |
keep_fps = gr.Checkbox(label="๐ฌ Keep Original FPS", value=True)
|
| 875 |
|
| 876 |
# Event handlers
|
|
|
|
| 881 |
)
|
| 882 |
|
| 883 |
use_folder_mode.change(
|
| 884 |
+
toggle_batch_mode,
|
| 885 |
inputs=[use_folder_mode],
|
| 886 |
outputs=[target_video]
|
| 887 |
)
|
| 888 |
|
| 889 |
+
target_video.upload(
|
| 890 |
+
handle_file_upload,
|
| 891 |
+
inputs=[target_video, use_folder_mode],
|
| 892 |
+
outputs=[upload_status]
|
| 893 |
+
)
|
| 894 |
+
|
| 895 |
start_button.click(
|
| 896 |
handle_processing,
|
| 897 |
inputs=[
|