Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,75 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import os
|
| 3 |
import gradio as gr
|
| 4 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
#
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
return output_path
|
| 20 |
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
)
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import os
|
| 3 |
+
import sys
|
| 4 |
+
|
| 5 |
+
# YEH SABSE ZAROORI LINE HAI
|
| 6 |
+
# Hum 'roop' folder ko system path mein add kar rahe hain taaki Python use dhoondh sake.
|
| 7 |
+
sys.path.append('roop')
|
| 8 |
+
|
| 9 |
+
# AB HUM SAHI JAGAH SE IMPORT KAR RAHE HAIN
|
| 10 |
+
# 'roop' folder ke andar 'core.py' file se 'run' function ko import kar rahe hain.
|
| 11 |
+
# Yahi aapke Colab ke video wale code ka logic hai.
|
| 12 |
+
from roop.core import run as roop_run
|
| 13 |
|
| 14 |
+
# --- Gradio Function ---
|
| 15 |
+
def face_swap(source_img, target_media):
|
| 16 |
+
if source_img is None or target_media is None:
|
| 17 |
+
raise gr.Error("Please upload both source image and target media.")
|
| 18 |
|
| 19 |
+
# Gradio se mili files ke temporary paths
|
| 20 |
+
source_path = source_img.name
|
| 21 |
+
target_path = target_media.name
|
| 22 |
+
|
| 23 |
+
# Output file ka naam tay karna
|
| 24 |
+
output_filename = os.path.basename(target_path)
|
| 25 |
+
# Output folder banayein agar nahi hai
|
| 26 |
+
os.makedirs("output", exist_ok=True)
|
| 27 |
+
output_path = os.path.join("output", f"result_{output_filename}")
|
| 28 |
|
| 29 |
+
# Roop ko chalane ke liye arguments set karna
|
| 30 |
+
args = [
|
| 31 |
+
"run.py", # yeh bas ek placeholder hai, asli kaam roop_run() function karega
|
| 32 |
+
"--source", source_path,
|
| 33 |
+
"--target", target_path,
|
| 34 |
+
"--output", output_path,
|
| 35 |
+
"--execution-provider", "cpu", # Free tier ke liye 'cpu'
|
| 36 |
+
"-e" # Face enhancer on
|
| 37 |
+
]
|
| 38 |
+
sys.argv = args
|
| 39 |
+
|
| 40 |
+
print(f"Running roop with args: {sys.argv}")
|
| 41 |
|
| 42 |
+
# Asli face swap process yahan run hoga
|
| 43 |
+
try:
|
| 44 |
+
roop_run()
|
| 45 |
+
except Exception as e:
|
| 46 |
+
raise gr.Error(f"Face swap failed. Error: {str(e)}")
|
| 47 |
+
|
| 48 |
+
print(f"Processing complete. Result at: {output_path}")
|
| 49 |
+
|
| 50 |
+
# Result ko UI mein dikhana
|
| 51 |
return output_path
|
| 52 |
|
| 53 |
+
# --- Gradio UI ---
|
| 54 |
+
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
| 55 |
+
gr.Markdown("# 🎭 Roop Face Swap AI")
|
| 56 |
+
gr.Markdown("Upload a source face and a target image/video, then click 'Swap Face'.")
|
| 57 |
+
|
| 58 |
+
with gr.Row():
|
| 59 |
+
source_image = gr.Image(label="Source Face (Single Face)", type="filepath")
|
| 60 |
+
target_media = gr.File(label="Target Image or Video", type="filepath")
|
| 61 |
+
|
| 62 |
+
submit_btn = gr.Button("Swap Face", variant="primary")
|
| 63 |
+
result_output = gr.File(label="Result", interactive=False)
|
| 64 |
+
|
| 65 |
+
submit_btn.click(
|
| 66 |
+
fn=face_swap,
|
| 67 |
+
inputs=[source_image, target_media],
|
| 68 |
+
outputs=result_output
|
| 69 |
+
)
|
| 70 |
+
|
| 71 |
+
gr.Markdown("---")
|
| 72 |
+
gr.Markdown("Created with help from AI. Make sure to use this tool responsibly.")
|
| 73 |
+
|
| 74 |
+
# App ko launch karein
|
| 75 |
+
app.launch()
|