Update app.py
Browse files
app.py
CHANGED
@@ -2,58 +2,56 @@ import gradio as gr
|
|
2 |
import os
|
3 |
import sys
|
4 |
|
5 |
-
# roop folder ko system path mein add karna
|
6 |
sys.path.append('roop')
|
7 |
-
|
8 |
-
# Sahi jagah se function import karna
|
9 |
from roop.core import run as roop_run
|
10 |
|
11 |
-
# --- Gradio Function ---
|
12 |
def face_swap(source_img, target_media):
|
13 |
if source_img is None or target_media is None:
|
14 |
-
raise gr.Error("
|
15 |
|
16 |
source_path = source_img
|
17 |
target_path = target_media
|
18 |
|
19 |
-
# Output file ka naam tay karna
|
20 |
output_filename = os.path.basename(target_path)
|
21 |
os.makedirs("output", exist_ok=True)
|
22 |
output_path = os.path.join("output", f"result_{output_filename}")
|
23 |
|
24 |
-
# =====
|
25 |
-
#
|
26 |
-
# NEW: Face enhancer ke liye poora command
|
27 |
args = [
|
28 |
"run.py",
|
29 |
"--source", source_path,
|
30 |
"--target", target_path,
|
31 |
"--output", output_path,
|
32 |
"--execution-provider", "cpu",
|
33 |
-
# Face Enhancer ko chalu karne ka sahi tarika:
|
34 |
-
"--frame-processor", "face_swapper", "face_enhancer"
|
35 |
]
|
36 |
-
#
|
37 |
sys.argv = args
|
38 |
|
39 |
print(f"Running roop with args: {sys.argv}")
|
40 |
|
41 |
try:
|
42 |
roop_run()
|
|
|
|
|
|
|
43 |
except Exception as e:
|
44 |
-
raise gr.Error(f"Face swap
|
45 |
|
46 |
-
|
|
|
|
|
|
|
|
|
47 |
|
|
|
48 |
return output_path
|
49 |
|
50 |
-
# --- Gradio UI ---
|
51 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
52 |
-
gr.Markdown("# 🎭
|
53 |
-
gr.Markdown("Upload a source face and a target image/video, then click 'Swap Face'.")
|
54 |
|
55 |
with gr.Row():
|
56 |
-
source_image = gr.Image(label="Source Face
|
57 |
target_media = gr.File(label="Target Image or Video", type="filepath")
|
58 |
|
59 |
submit_btn = gr.Button("Swap Face", variant="primary")
|
@@ -64,8 +62,5 @@ with gr.Blocks(theme=gr.themes.Soft()) as app:
|
|
64 |
inputs=[source_image, target_media],
|
65 |
outputs=result_output
|
66 |
)
|
67 |
-
|
68 |
-
gr.Markdown("---")
|
69 |
-
gr.Markdown("Created with help from AI. Make sure to use this tool responsibly.")
|
70 |
|
71 |
app.launch()
|
|
|
2 |
import os
|
3 |
import sys
|
4 |
|
|
|
5 |
sys.path.append('roop')
|
|
|
|
|
6 |
from roop.core import run as roop_run
|
7 |
|
|
|
8 |
def face_swap(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 |
output_filename = os.path.basename(target_path)
|
16 |
os.makedirs("output", exist_ok=True)
|
17 |
output_path = os.path.join("output", f"result_{output_filename}")
|
18 |
|
19 |
+
# ===== KOI EXTRA LOGIC NAHI, SIMPLE SWAP =====
|
20 |
+
# Face Enhancer wala argument hata diya hai.
|
|
|
21 |
args = [
|
22 |
"run.py",
|
23 |
"--source", source_path,
|
24 |
"--target", target_path,
|
25 |
"--output", output_path,
|
26 |
"--execution-provider", "cpu",
|
|
|
|
|
27 |
]
|
28 |
+
# ============================================
|
29 |
sys.argv = args
|
30 |
|
31 |
print(f"Running roop with args: {sys.argv}")
|
32 |
|
33 |
try:
|
34 |
roop_run()
|
35 |
+
except SystemExit:
|
36 |
+
# Roop argparse error par SystemExit deta hai, use pass kar rahe hain.
|
37 |
+
pass
|
38 |
except Exception as e:
|
39 |
+
raise gr.Error(f"Face swap fail hua. Error: {str(e)}")
|
40 |
|
41 |
+
# Check karein ki output file bani hai ya nahi
|
42 |
+
if not os.path.exists(output_path):
|
43 |
+
# Agar file nahi bani to user ko batayein ki model download ho raha hai
|
44 |
+
# Pehli baar run hone mein 5-10 min lag sakte hain.
|
45 |
+
raise gr.Error("Processing ho raha hai... Pehli baar run hone mein 10-15 minute lag sakte hain kyunki model download ho raha hai. Please sabr rakhein aur thodi der baad dobara try karein.")
|
46 |
|
47 |
+
print(f"Process poora hua. Result yahan hai: {output_path}")
|
48 |
return output_path
|
49 |
|
|
|
50 |
with gr.Blocks(theme=gr.themes.Soft()) as app:
|
51 |
+
gr.Markdown("# 🎭 Simple Face Swap (Colab Port)")
|
|
|
52 |
|
53 |
with gr.Row():
|
54 |
+
source_image = gr.Image(label="Source Face", type="filepath")
|
55 |
target_media = gr.File(label="Target Image or Video", type="filepath")
|
56 |
|
57 |
submit_btn = gr.Button("Swap Face", variant="primary")
|
|
|
62 |
inputs=[source_image, target_media],
|
63 |
outputs=result_output
|
64 |
)
|
|
|
|
|
|
|
65 |
|
66 |
app.launch()
|