|
import gradio as gr |
|
import os |
|
import sys |
|
|
|
|
|
sys.path.append('roop') |
|
|
|
|
|
from roop.core import run as roop_run |
|
|
|
|
|
def face_swap(source_img, target_media): |
|
if source_img is None or target_media is None: |
|
raise gr.Error("Please upload both source image and target media.") |
|
|
|
|
|
|
|
|
|
source_path = source_img |
|
target_path = target_media |
|
|
|
|
|
|
|
output_filename = os.path.basename(target_path) |
|
os.makedirs("output", exist_ok=True) |
|
output_path = os.path.join("output", f"result_{output_filename}") |
|
|
|
|
|
args = [ |
|
"run.py", |
|
"--source", source_path, |
|
"--target", target_path, |
|
"--output", output_path, |
|
"--execution-provider", "cpu", |
|
"-e" |
|
] |
|
sys.argv = args |
|
|
|
print(f"Running roop with args: {sys.argv}") |
|
|
|
|
|
try: |
|
roop_run() |
|
except Exception as e: |
|
|
|
raise gr.Error(f"Face swap failed. Error: {str(e)}") |
|
|
|
print(f"Processing complete. Result at: {output_path}") |
|
|
|
|
|
return output_path |
|
|
|
|
|
with gr.Blocks(theme=gr.themes.Soft()) as app: |
|
gr.Markdown("# 🎭 Roop Face Swap AI") |
|
gr.Markdown("Upload a source face and a target image/video, then click 'Swap Face'.") |
|
|
|
with gr.Row(): |
|
source_image = gr.Image(label="Source Face (Single Face)", type="filepath") |
|
target_media = gr.File(label="Target Image or Video", type="filepath") |
|
|
|
submit_btn = gr.Button("Swap Face", variant="primary") |
|
result_output = gr.File(label="Result", interactive=False) |
|
|
|
submit_btn.click( |
|
fn=face_swap, |
|
inputs=[source_image, target_media], |
|
outputs=result_output |
|
) |
|
|
|
gr.Markdown("---") |
|
gr.Markdown("Created with help from AI. Make sure to use this tool responsibly.") |
|
|
|
app.launch() |