import gradio as gr import roop.globals import roop.core import roop.processors.frame.core import os import shutil # Ensure roop thinks it's headless so it doesn't try to launch its own GUI roop.globals.headless = True roop.globals.execution_providers = ['cpu'] # Default to CPU, can be changed roop.globals.execution_threads = 1 roop.globals.frame_processors = ['face_swapper'] roop.globals.keep_fps = True roop.globals.keep_frames = False roop.globals.skip_audio = False roop.globals.many_faces = False roop.globals.reference_face_position = 0 roop.globals.reference_frame_number = 0 roop.globals.similar_face_distance = 0.85 roop.globals.temp_frame_format = 'png' roop.globals.temp_frame_quality = 100 roop.globals.output_video_encoder = 'libx264' roop.globals.output_video_quality = 35 roop.globals.max_memory = None def swap_face(source_file, target_file, enable_enhancer): if source_file is None or target_file is None: return None source_path = source_file if isinstance(source_file, str) else source_file.name target_path = target_file if isinstance(target_file, str) else target_file.name # Force strict check: Allow images ONLY. target_name, target_ext = os.path.splitext(os.path.basename(target_path)) if target_ext.lower() not in ['.jpg', '.jpeg', '.png', '.bmp', '.webp']: raise gr.Error("Video processing is disabled on this Space due to high CPU usage. Please use Images only.") return None output_ext = '.png' output_path = os.path.join(os.getcwd(), f"output_{target_name}{output_ext}") print(f"Source: {source_path}") print(f"Target: {target_path}") print(f"Output: {output_path}") print(f"Enhancer Enabled: {enable_enhancer}") # Set globals roop.globals.source_path = source_path roop.globals.target_path = target_path roop.globals.output_path = output_path roop.globals.execution_providers = ['cpu'] # Configure processors processors = ['face_swapper'] if enable_enhancer: processors.append('face_enhancer') roop.globals.frame_processors = processors # CRITICAL FIX: Clear the cache of frame processors so it reloads based on the new list roop.processors.frame.core.FRAME_PROCESSORS_MODULES = [] # Use correct provider name to avoid ONNX Runtime warnings roop.globals.execution_providers = ['CPUExecutionProvider'] try: roop.core.start() if os.path.exists(output_path): return output_path else: print("Error: Output file not created.") return None except Exception as e: print(f"Error during processing: {e}") return None # Define the interface with gr.Blocks(title="Roop Web UI") as demo: # Custom Banner for AI Guruji gr.HTML("""
If multiple users are using this space (3-4 users), generation might be slow.
Duplicate this Space for faster generation without waiting!
❤️ Please Like this Space if you find it useful! ❤️