Update app.py
Browse files
app.py
CHANGED
@@ -5,81 +5,59 @@ import sys
|
|
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"result_{output_filename}")
|
19 |
|
20 |
-
args = [
|
21 |
-
"run.py",
|
22 |
-
"--source", source_path,
|
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 |
-
raise gr.Error(f"Face swap fail hua.
|
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 |
-
# ---
|
45 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
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 |
-
fn=face_swap,
|
75 |
-
inputs=[source_face, target_image],
|
76 |
-
outputs=result_image
|
77 |
-
)
|
78 |
-
|
79 |
-
submit_btn_video.click(
|
80 |
-
fn=face_swap,
|
81 |
-
inputs=[source_face, target_video],
|
82 |
-
outputs=result_video
|
83 |
-
)
|
84 |
|
85 |
app.launch()
|
|
|
5 |
sys.path.append('roop')
|
6 |
from roop.core import run as roop_run
|
7 |
|
8 |
+
def face_swap_logic(source_img, target_media):
|
|
|
9 |
if source_img is None or target_media is None:
|
10 |
raise gr.Error("Dono file upload karein.")
|
11 |
|
12 |
source_path = source_img
|
13 |
target_path = target_media
|
14 |
|
15 |
+
# Check karein ki target video hai ya image
|
16 |
+
is_video = target_path.lower().endswith(('.mp4', '.mov', '.avi'))
|
17 |
+
|
18 |
output_filename = os.path.basename(target_path)
|
19 |
os.makedirs("output", exist_ok=True)
|
20 |
output_path = os.path.join("output", f"result_{output_filename}")
|
21 |
|
22 |
+
args = [ "run.py", "--source", source_path, "--target", target_path, "--output", output_path, "--execution-provider", "cpu" ]
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
sys.argv = args
|
24 |
|
|
|
|
|
25 |
try:
|
26 |
roop_run()
|
27 |
+
except SystemExit: pass
|
|
|
28 |
except Exception as e:
|
29 |
+
raise gr.Error(f"Face swap fail hua. Roop ke andar error: {str(e)}")
|
30 |
|
31 |
+
# Ab hum behtar tarike se check karenge
|
32 |
if not os.path.exists(output_path):
|
33 |
+
if is_video:
|
34 |
+
# Video ke liye alag error message
|
35 |
+
raise gr.Error("Video processing fail hua. Free server par time ya memory (RAM) kam pad gayi. Please chota video (5-10 sec) try karein, ya GPU server use karein.")
|
36 |
+
else:
|
37 |
+
# Image ke liye alag error message
|
38 |
+
raise gr.Error("Image processing fail hua. Aisa tab hota hai jab image mein chehra nahi milta.")
|
39 |
|
|
|
40 |
return output_path
|
41 |
|
42 |
+
# --- UI Code (Bilkul Same, Koi Change Nahi) ---
|
43 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
44 |
gr.Markdown("# 🎭 Roop Face Swap AI")
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
with gr.Tabs():
|
|
|
46 |
with gr.TabItem("🖼️ Swap on Image"):
|
47 |
+
with gr.Row():
|
48 |
+
source_face_img = gr.Image(label="Source Face", type="filepath")
|
49 |
+
target_image = gr.Image(label="Target Image", type="filepath")
|
50 |
+
submit_btn_image = gr.Button("Swap Face on Image")
|
51 |
+
result_image = gr.Image(label="Result Image")
|
52 |
|
|
|
53 |
with gr.TabItem("🎬 Swap on Video"):
|
54 |
+
with gr.Row():
|
55 |
+
source_face_vid = gr.Image(label="Source Face", type="filepath")
|
56 |
+
target_video = gr.Video(label="Target Video")
|
57 |
+
submit_btn_video = gr.Button("Swap Face on Video")
|
58 |
+
result_video = gr.Video(label="Result Video")
|
59 |
+
|
60 |
+
submit_btn_image.click(fn=face_swap_logic, inputs=[source_face_img, target_image], outputs=result_image)
|
61 |
+
submit_btn_video.click(fn=face_swap_logic, inputs=[source_face_vid, target_video], outputs=result_video)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
app.launch()
|