import gradio as gr import spaces import torch import cv2 import numpy as np from PIL import Image import os from models import load_model, process_video_character_replacement from utils import validate_inputs, save_uploaded_files, create_output_directory import tempfile # Global model variable model = None def initialize_model(): """Initialize the MoCha model""" global model if model is None: print("Loading MoCha model...") model = load_model() print("Model loaded successfully!") def video_character_replacement(reference_image, source_video): """ Main function to perform video character replacement using MoCha model. Args: reference_image: PIL Image or file path of the target character source_video: Uploaded video file of the original subject Returns: Processed video with character replacement """ try: # Initialize model if not already loaded if model is None: initialize_model() # Validate inputs if reference_image is None or source_video is None: return None, "Please provide both a reference image and a source video." # Validate and process inputs ref_img, video_path = validate_inputs(reference_image, source_video) # Create output directory output_dir = create_output_directory() # Process the video output_path = process_video_character_replacement( model, ref_img, video_path, output_dir ) if output_path and os.path.exists(output_path): return output_path, "Video character replacement completed successfully!" else: return None, "Error: Failed to process video. Please check your inputs." except Exception as e: return None, f"Error during processing: {str(e)}" def clear_cache(): """Clear temporary files and cache""" try: temp_dir = tempfile.gettempdir() for file in os.listdir(temp_dir): if file.startswith('video_char_replacement_'): try: os.remove(os.path.join(temp_dir, file)) except: pass return "Cache cleared successfully!" except Exception as e: return f"Error clearing cache: {str(e)}" # Custom CSS for better styling css = """ .container { max-width: 1200px; margin: auto; } .title { text-align: center; color: #2E86AB; font-size: 2.5em; margin-bottom: 10px; } .description { text-align: center; color: #666; font-size: 1.2em; margin-bottom: 30px; } .upload-area { border: 2px dashed #2E86AB; border-radius: 10px; padding: 20px; background-color: #f8f9fa; } .output-area { border: 2px solid #28a745; border-radius: 10px; padding: 20px; background-color: #f8fff8; } .status { font-weight: bold; color: #2E86AB; } .error { color: #dc3545; font-weight: bold; } .success { color: #28a745; font-weight: bold; } """ # Create the Gradio interface with gr.Blocks(css=css, title="Video Character Replacement") as demo: # Header gr.HTML("""
End-to-End Video Character Replacement using MoCha Model
Replace subjects in videos with reference images without structural guidance
1. Upload a clear reference image of the target character
2. Upload the source video containing the subject to replace
3. Click 'Replace Character' to process the video
4. Download the processed video with character replacement
Note: Processing large videos may take several minutes. Please be patient!