Update app.py
Browse files
app.py
CHANGED
@@ -1,85 +1,97 @@
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import sys
|
|
|
|
|
|
|
4 |
|
|
|
5 |
sys.path.append('roop')
|
6 |
from roop.core import run as roop_run
|
7 |
|
8 |
-
|
9 |
-
def face_swap(source_img, target_media, progress=gr.Progress(track_tqdm=True)):
|
10 |
-
if source_img is None or target_media is None:
|
11 |
-
raise gr.Error("Dono file upload karein.")
|
12 |
-
|
13 |
-
source_path = source_img
|
14 |
-
target_path = target_media
|
15 |
-
|
16 |
output_filename = os.path.basename(target_path)
|
17 |
os.makedirs("output", exist_ok=True)
|
18 |
-
output_path = os.path.join("output", f"
|
19 |
|
20 |
args = [
|
21 |
-
"run.py",
|
22 |
-
"--
|
23 |
-
"--target", target_path,
|
24 |
-
"--output", output_path,
|
25 |
-
"--execution-provider", "cpu",
|
26 |
]
|
27 |
sys.argv = args
|
28 |
-
|
29 |
print(f"Running roop with args: {sys.argv}")
|
30 |
|
31 |
try:
|
32 |
roop_run()
|
33 |
-
except SystemExit:
|
34 |
-
pass
|
35 |
except Exception as e:
|
36 |
-
|
|
|
37 |
|
38 |
if not os.path.exists(output_path):
|
39 |
-
|
40 |
-
|
41 |
-
print(f"Process poora hua. Result yahan hai: {output_path}")
|
42 |
return output_path
|
43 |
|
44 |
-
# --- Gradio UI (
|
45 |
-
|
|
|
46 |
gr.Markdown("# 🎭 Roop Face Swap AI")
|
47 |
-
|
48 |
-
# Step 1: Source Face (Yeh dono tabs ke liye common rahega)
|
49 |
-
gr.Markdown("### Step 1: Upload Source Face")
|
50 |
-
source_face = gr.Image(label="Source Face (Jiska Chehra Lagana Hai)", type="filepath")
|
51 |
-
|
52 |
-
# Step 2: Alag-alag Tabs banana (Image aur Video ke liye)
|
53 |
with gr.Tabs():
|
54 |
-
# TAB 1: IMAGE KE LIYE
|
55 |
with gr.TabItem("🖼️ Swap on Image"):
|
56 |
-
gr.
|
57 |
-
|
58 |
-
|
59 |
-
gr.
|
60 |
-
result_image = gr.Image(label="Result Image"
|
61 |
-
|
62 |
-
# TAB 2: VIDEO KE LIYE
|
63 |
with gr.TabItem("🎬 Swap on Video"):
|
64 |
-
gr.
|
65 |
-
|
66 |
-
|
67 |
-
gr.
|
68 |
-
result_video = gr.Video(label="Result Video"
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
submit_btn_image.click(
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import os
|
3 |
import sys
|
4 |
+
import base64
|
5 |
+
import uuid
|
6 |
+
from fastapi import FastAPI
|
7 |
|
8 |
+
# --- Purana Code (Koi Change Nahi) ---
|
9 |
sys.path.append('roop')
|
10 |
from roop.core import run as roop_run
|
11 |
|
12 |
+
def face_swap_logic(source_path, target_path):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
output_filename = os.path.basename(target_path)
|
14 |
os.makedirs("output", exist_ok=True)
|
15 |
+
output_path = os.path.join("output", f"api_result_{output_filename}")
|
16 |
|
17 |
args = [
|
18 |
+
"run.py", "--source", source_path, "--target", target_path,
|
19 |
+
"--output", output_path, "--execution-provider", "cpu",
|
|
|
|
|
|
|
20 |
]
|
21 |
sys.argv = args
|
|
|
22 |
print(f"Running roop with args: {sys.argv}")
|
23 |
|
24 |
try:
|
25 |
roop_run()
|
26 |
+
except SystemExit: pass
|
|
|
27 |
except Exception as e:
|
28 |
+
print(f"Error during face swap: {e}")
|
29 |
+
return None
|
30 |
|
31 |
if not os.path.exists(output_path):
|
32 |
+
return None
|
33 |
+
|
|
|
34 |
return output_path
|
35 |
|
36 |
+
# --- Gradio UI (Yeh Web Users ke liye hai) ---
|
37 |
+
# Isse hum 'ui_app' naam denge
|
38 |
+
with gr.Blocks(theme=gr.themes.Soft()) as ui_app:
|
39 |
gr.Markdown("# 🎭 Roop Face Swap AI")
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
with gr.Tabs():
|
|
|
41 |
with gr.TabItem("🖼️ Swap on Image"):
|
42 |
+
with gr.Row():
|
43 |
+
source_face_img = gr.Image(label="Source Face", type="filepath")
|
44 |
+
target_image = gr.Image(label="Target Image", type="filepath")
|
45 |
+
submit_btn_image = gr.Button("Swap Face on Image")
|
46 |
+
result_image = gr.Image(label="Result Image")
|
|
|
|
|
47 |
with gr.TabItem("🎬 Swap on Video"):
|
48 |
+
with gr.Row():
|
49 |
+
source_face_vid = gr.Image(label="Source Face", type="filepath")
|
50 |
+
target_video = gr.Video(label="Target Video")
|
51 |
+
submit_btn_video = gr.Button("Swap Face on Video")
|
52 |
+
result_video = gr.Video(label="Result Video")
|
53 |
+
|
54 |
+
def web_ui_swap(source, target):
|
55 |
+
return face_swap_logic(source, target)
|
56 |
+
|
57 |
+
submit_btn_image.click(fn=web_ui_swap, inputs=[source_face_img, target_image], outputs=result_image)
|
58 |
+
submit_btn_video.click(fn=web_ui_swap, inputs=[source_face_vid, target_video], outputs=result_video)
|
59 |
+
|
60 |
+
# --- Naya API Code (Yeh Aapke Android App ke liye hai) ---
|
61 |
+
# Hum ek FastAPI app banayenge
|
62 |
+
app = FastAPI()
|
63 |
+
|
64 |
+
def save_base64_to_file(b64_string, extension):
|
65 |
+
filename = f"/tmp/{uuid.uuid4().hex}.{extension}"
|
66 |
+
with open(filename, "wb") as f:
|
67 |
+
f.write(base64.b64decode(b64_string))
|
68 |
+
return filename
|
69 |
+
|
70 |
+
@app.post("/api/swap-face")
|
71 |
+
async def api_face_swap(payload: dict):
|
72 |
+
source_b64 = payload.get("source_face_b64")
|
73 |
+
target_b64 = payload.get("target_media_b64")
|
74 |
+
target_ext = payload.get("target_ext", "jpg")
|
75 |
+
|
76 |
+
if not source_b64 or not target_b64:
|
77 |
+
return {"error": "Source ya Target file nahi mili."}
|
78 |
+
|
79 |
+
source_file_path = save_base64_to_file(source_b64, "jpg")
|
80 |
+
target_file_path = save_base64_to_file(target_b64, target_ext)
|
81 |
+
|
82 |
+
result_path = face_swap_logic(source_file_path, target_file_path)
|
83 |
+
|
84 |
+
if result_path:
|
85 |
+
with open(result_path, "rb") as f:
|
86 |
+
result_b64 = base64.b64encode(f.read()).decode("utf-8")
|
87 |
+
|
88 |
+
os.remove(source_file_path)
|
89 |
+
os.remove(target_file_path)
|
90 |
+
os.remove(result_path)
|
91 |
+
|
92 |
+
return {"result_b64": result_b64}
|
93 |
+
else:
|
94 |
+
return {"error": "Face swap fail hua."}
|
95 |
+
|
96 |
+
# Gradio UI aur FastAPI API, dono ko ek saath mount karein
|
97 |
+
app = gr.mount_app(app, ui_app, path="/")
|