Ii
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
3 |
-
import argparse
|
4 |
-
import tempfile # Temporary file handling
|
5 |
import os
|
6 |
import requests
|
7 |
|
8 |
-
#
|
9 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
10 |
model_path = "./inswapper_128.onnx"
|
11 |
|
@@ -23,74 +21,72 @@ def download_model():
|
|
23 |
else:
|
24 |
print("Model already exists.")
|
25 |
|
26 |
-
# Download the model
|
27 |
download_model()
|
28 |
|
29 |
-
# Argument parser
|
30 |
-
parser = argparse.ArgumentParser(description='Refacer')
|
31 |
-
parser.add_argument("--max_num_faces", type=int, help="Max number of faces on UI", default=5)
|
32 |
-
parser.add_argument("--force_cpu", help="Force CPU mode", default=False, action="store_true")
|
33 |
-
parser.add_argument("--share_gradio", help="Share Gradio", default=False, action="store_true")
|
34 |
-
parser.add_argument("--server_name", type=str, help="Server IP address", default="127.0.0.1")
|
35 |
-
parser.add_argument("--server_port", type=int, help="Server port", default=7860)
|
36 |
-
parser.add_argument("--colab_performance", help="Use in colab for better performance", default=False, action="store_true")
|
37 |
-
args = parser.parse_args()
|
38 |
-
|
39 |
# Initialize the Refacer class
|
40 |
-
refacer = Refacer(force_cpu=
|
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 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
94 |
|
95 |
# Launch the Gradio app
|
96 |
-
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
from refacer import Refacer
|
|
|
|
|
3 |
import os
|
4 |
import requests
|
5 |
|
6 |
+
# Model download URL and path
|
7 |
model_url = "https://huggingface.co/ofter/4x-UltraSharp/resolve/main/inswapper_128.onnx"
|
8 |
model_path = "./inswapper_128.onnx"
|
9 |
|
|
|
21 |
else:
|
22 |
print("Model already exists.")
|
23 |
|
24 |
+
# Download the model
|
25 |
download_model()
|
26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
27 |
# Initialize the Refacer class
|
28 |
+
refacer = Refacer(force_cpu=True) # Use CPU for simplicity
|
29 |
+
|
30 |
+
# Function to process the video
|
31 |
+
def reface_video(video_path, *faces):
|
32 |
+
try:
|
33 |
+
# Prepare face data
|
34 |
+
face_data = [
|
35 |
+
{
|
36 |
+
"origin": face["origin"],
|
37 |
+
"destination": face["destination"],
|
38 |
+
"threshold": face.get("threshold", 0.2),
|
39 |
+
}
|
40 |
+
for face in faces if face.get("origin") and face.get("destination")
|
41 |
+
]
|
42 |
+
|
43 |
+
# Process the video
|
44 |
+
print("Processing video...")
|
45 |
+
refaced_video_path = refacer.reface(video_path, face_data)
|
46 |
+
|
47 |
+
# Return only the refaced video
|
48 |
+
return refaced_video_path
|
49 |
+
except Exception as e:
|
50 |
+
return f"Error processing video: {e}"
|
51 |
+
|
52 |
+
# Build Gradio UI
|
53 |
+
def build_ui():
|
54 |
+
num_faces = 5 # Maximum number of faces
|
55 |
+
|
56 |
+
with gr.Blocks() as demo:
|
57 |
+
with gr.Row():
|
58 |
+
gr.Markdown("# Refacer: AI-Powered Video Face Replacement")
|
59 |
+
|
60 |
+
with gr.Row():
|
61 |
+
input_video = gr.Video(label="Upload a video", type="filepath")
|
62 |
+
output_video = gr.Video(label="Refaced video", interactive=False)
|
63 |
+
|
64 |
+
# Create inputs for multiple faces
|
65 |
+
faces = []
|
66 |
+
for i in range(num_faces):
|
67 |
+
with gr.Tab(f"Face #{i+1}"):
|
68 |
+
with gr.Row():
|
69 |
+
origin = gr.Image(label="Origin Face", type="filepath")
|
70 |
+
destination = gr.Image(label="Destination Face", type="filepath")
|
71 |
+
threshold = gr.Slider(
|
72 |
+
label="Threshold", minimum=0.0, maximum=1.0, value=0.2
|
73 |
+
)
|
74 |
+
faces.append({"origin": origin, "destination": destination, "threshold": threshold})
|
75 |
+
|
76 |
+
with gr.Row():
|
77 |
+
submit_button = gr.Button("Reface Video")
|
78 |
+
|
79 |
+
# Connect inputs and outputs
|
80 |
+
inputs = [input_video] + [face["origin"] for face in faces] + [
|
81 |
+
face["destination"] for face in faces
|
82 |
+
] + [face["threshold"] for face in faces]
|
83 |
+
submit_button.click(
|
84 |
+
fn=reface_video, inputs=inputs, outputs=[output_video]
|
85 |
+
)
|
86 |
+
|
87 |
+
return demo
|
88 |
|
89 |
# Launch the Gradio app
|
90 |
+
if __name__ == "__main__":
|
91 |
+
ui = build_ui()
|
92 |
+
ui.queue().launch(server_name="0.0.0.0", server_port=7860, debug=True)
|