import numpy as np import gradio as gr import roop.globals from roop.core import ( start, decode_execution_providers, suggest_max_memory, suggest_execution_threads, ) from roop.processors.frame.core import get_frame_processors_modules from roop.utilities import normalize_output_path import os from PIL import Image import time def swap_face(source_file, target_file, doFaceEnhancer, video_quality): source_path = "input.jpg" target_path = "target.mp4" # Save source image source_image = Image.fromarray(source_file) source_image.save(source_path) # Copy the target video to the working directory os.rename(target_file, target_path) print("source_path: ", source_path) print("target_path: ", target_path) roop.globals.video_quality = video_quality roop.globals.source_path = source_path roop.globals.target_path = target_path output_path = "output.mp4" roop.globals.output_path = normalize_output_path( roop.globals.source_path, roop.globals.target_path, output_path ) if doFaceEnhancer: roop.globals.frame_processors = ["face_swapper", "face_enhancer"] else: roop.globals.frame_processors = ["face_swapper"] roop.globals.headless = True roop.globals.keep_fps = True roop.globals.keep_audio = True roop.globals.keep_frames = False roop.globals.many_faces = False roop.globals.video_encoder = "libx264" roop.globals.video_quality = 10 roop.globals.max_memory = suggest_max_memory() roop.globals.execution_providers = decode_execution_providers(["cuda"]) roop.globals.execution_threads = suggest_execution_threads() print( "start process", roop.globals.source_path, roop.globals.target_path, roop.globals.output_path, ) for frame_processor in get_frame_processors_modules( roop.globals.frame_processors ): if not frame_processor.pre_check(): return start() return output_path # Custom CSS for neon style with animated online indicator custom_css = """ :root { --neon-primary: #00f3ff; --neon-secondary: #ff00ff; --neon-accent: #00ff87; --neon-warning: #ffcc00; --dark-bg: #0a0a1a; --dark-panel: #13132b; --darker-panel: #0c0c1f; --text-primary: #ffffff; --text-secondary: #a0a0c0; } body { background: var(--dark-bg) !important; color: var(--text-primary) !important; font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif !important; } .gr-block { background: var(--dark-panel) !important; border-radius: 12px !important; border: 1px solid rgba(0, 243, 255, 0.2) !important; box-shadow: 0 0 15px rgba(0, 243, 255, 0.1) !important; } .gr-box { border-color: rgba(0, 243, 255, 0.3) !important; color: var(--text-primary) !important; background: rgba(10, 10, 26, 0.7) !important; } h1, h2, h3, h4, label, .gr-label { color: var(--text-primary) !important; text-shadow: 0 0 5px rgba(0, 243, 255, 0.5); } .gr-button { background: linear-gradient(45deg, var(--neon-primary), var(--neon-secondary)) !important; color: black !important; border: none !important; border-radius: 8px !important; padding: 12px 28px !important; font-weight: 600 !important; text-transform: uppercase !important; letter-spacing: 1px !important; box-shadow: 0 0 10px var(--neon-primary), 0 0 20px rgba(0, 243, 255, 0.3) !important; transition: all 0.3s ease !important; width: 100% !important; margin: 10px 0 !important; } .gr-button:not(:disabled):hover { transform: translateY(-2px); box-shadow: 0 0 15px var(--neon-primary), 0 0 30px rgba(0, 243, 255, 0.5) !important; } .gr-button:disabled { background: #4b5563 !important; box-shadow: none !important; } .status-indicator { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 10px; background-color: var(--neon-accent); box-shadow: 0 0 0 0 rgba(0, 255, 135, 0.7); animation: pulse 2s infinite; } @keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(0, 255, 135, 0.7); } 70% { box-shadow: 0 0 0 10px rgba(0, 255, 135, 0); } 100% { box-shadow: 0 0 0 0 rgba(0, 255, 135, 0); } } .status-text { color: var(--text-secondary); font-size: 0.9rem; display: flex; align-items: center; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem; padding: 1.5rem; background: linear-gradient(90deg, rgba(0,243,255,0.1) 0%, rgba(255,0,255,0.1) 100%); border-radius: 12px; border: 1px solid rgba(0, 243, 255, 0.3); box-shadow: 0 0 20px rgba(0, 243, 255, 0.2); } .title-section h1 { margin-bottom: 0.25rem; font-weight: 800; background: linear-gradient(45deg, var(--neon-primary), var(--neon-secondary)); -webkit-background-clip: text; -webkit-text-fill-color: transparent; text-shadow: 0 0 10px rgba(0, 243, 255, 0.5); } .title-section p { color: var(--text-secondary); margin: 0; } .instructions { background: linear-gradient(90deg, rgba(0,243,255,0.05) 0%, rgba(255,0,255,0.05) 100%) !important; padding: 1.5rem !important; margin-bottom: 2rem !important; border: 1px solid rgba(0, 243, 255, 0.2) !important; } .instructions h3 { margin-top: 0; margin-bottom: 0.75rem; color: var(--neon-primary) !important; } .instructions ul { margin-bottom: 0; padding-left: 1.5rem; } .instructions li { margin-bottom: 0.5rem; color: var(--text-secondary); } .instructions li:last-child { margin-bottom: 0; } .footer { text-align: center; margin-top: 2rem; padding-top: 1.5rem; border-top: 1px solid rgba(0, 243, 255, 0.2); color: var(--text-secondary); font-size: 0.875rem; } .image-container { border: 2px solid rgba(0, 243, 255, 0.3); border-radius: 12px; padding: 8px; background: rgba(0, 0, 0, 0.2); margin-bottom: 1.5rem; box-shadow: 0 0 15px rgba(0, 243, 255, 0.1); } .image-container .gr-label { background: rgba(0, 243, 255, 0.1); padding: 8px 12px; border-radius: 8px; margin-bottom: 10px; display: inline-block; } .control-panel { background: linear-gradient(90deg, rgba(0,243,255,0.08) 0%, rgba(255,0,255,0.08) 100%) !important; padding: 1.5rem !important; border: 1px solid rgba(0, 243, 255, 0.3) !important; border-radius: 12px !important; margin-bottom: 1.5rem !important; } .status-panel { background: var(--darker-panel) !important; padding: 1.5rem !important; border: 1px solid rgba(0, 255, 135, 0.3) !important; border-radius: 12px !important; box-shadow: 0 0 15px rgba(0, 255, 135, 0.1) !important; } .status-header { display: flex; align-items: center; margin-bottom: 1rem; padding-bottom: 0.5rem; border-bottom: 1px solid rgba(0, 255, 135, 0.2); } .status-content { min-height: 100px; } .output-highlight { border: 2px solid var(--neon-accent) !important; box-shadow: 0 0 20px rgba(0, 255, 135, 0.3) !important; } .upload-text { color: var(--text-secondary); text-align: center; padding: 20px; } .progress-bar { height: 6px; background: rgba(0, 243, 255, 0.2); border-radius: 3px; margin: 10px 0; overflow: hidden; } .progress-fill { height: 100%; background: linear-gradient(90deg, var(--neon-primary), var(--neon-accent)); border-radius: 3px; width: 0%; transition: width 0.3s ease; } .control-item { margin-bottom: 1rem; padding: 1rem; background: rgba(0, 0, 0, 0.2); border-radius: 8px; border: 1px solid rgba(0, 243, 255, 0.1); } .control-item:last-child { margin-bottom: 0; } .download-btn { background: linear-gradient(45deg, var(--neon-accent), #00cc70) !important; margin-top: 15px !important; } .video-container { border: 2px solid rgba(0, 243, 255, 0.3); border-radius: 12px; padding: 8px; background: rgba(0, 0, 0, 0.2); margin-bottom: 1.5rem; box-shadow: 0 0 15px rgba(0, 243, 255, 0.1); } """ # Gradio UI with neon style theme with gr.Blocks( css=custom_css, title="Neon Face Swap AI", theme=gr.themes.Default(primary_hue="cyan", secondary_hue="pink") ) as demo: # Header section with title and animated status indicator with gr.Row(elem_classes="header"): with gr.Column(scale=3): with gr.Row(elem_classes="title-section"): gr.Markdown(""" # 🌌 AI FACE SWAPPER ### Next-Generation AI Face Swapping Technology """) with gr.Column(scale=1): with gr.Row(): gr.Markdown("""