File size: 2,460 Bytes
06cd1c0 8bade78 06cd1c0 8bade78 06cd1c0 8bade78 06cd1c0 8bade78 06cd1c0 8bade78 06cd1c0 8bade78 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
import gradio as gr
import os
import sys
# YEH SABSE ZAROORI LINE HAI
# Hum 'roop' folder ko system path mein add kar rahe hain taaki Python use dhoondh sake.
sys.path.append('roop')
# AB HUM SAHI JAGAH SE IMPORT KAR RAHE HAIN
# 'roop' folder ke andar 'core.py' file se 'run' function ko import kar rahe hain.
# Yahi aapke Colab ke video wale code ka logic hai.
from roop.core import run as roop_run
# --- Gradio Function ---
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.")
# Gradio se mili files ke temporary paths
source_path = source_img.name
target_path = target_media.name
# Output file ka naam tay karna
output_filename = os.path.basename(target_path)
# Output folder banayein agar nahi hai
os.makedirs("output", exist_ok=True)
output_path = os.path.join("output", f"result_{output_filename}")
# Roop ko chalane ke liye arguments set karna
args = [
"run.py", # yeh bas ek placeholder hai, asli kaam roop_run() function karega
"--source", source_path,
"--target", target_path,
"--output", output_path,
"--execution-provider", "cpu", # Free tier ke liye 'cpu'
"-e" # Face enhancer on
]
sys.argv = args
print(f"Running roop with args: {sys.argv}")
# Asli face swap process yahan run hoga
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}")
# Result ko UI mein dikhana
return output_path
# --- Gradio UI ---
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 ko launch karein
app.launch() |