Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -1,32 +1,89 @@
|
|
1 |
-
import
|
|
|
|
|
2 |
import time
|
|
|
3 |
|
4 |
-
#
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
#
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
dropdown, js = create_theme_dropdown()
|
12 |
|
13 |
-
with gr.Blocks(theme='
|
14 |
with gr.Row(equal_height=True):
|
15 |
with gr.Column(scale=10):
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from refacer import Refacer
|
3 |
+
import ngrok
|
4 |
import time
|
5 |
+
import threading
|
6 |
|
7 |
+
# Initialize Refacer
|
8 |
+
def initialize_refacer():
|
9 |
+
global refacer
|
10 |
+
print("💊Initializing Refacer...")
|
11 |
+
start_time = time.time()
|
12 |
+
refacer = Refacer(force_cpu=False, colab_performance=False)
|
13 |
+
print(f"💚Refacer initialized in {time.time() - start_time:.2f} seconds")
|
14 |
|
15 |
+
# Ngrok connection
|
16 |
+
def connect(token, port, options):
|
17 |
+
account = None
|
18 |
+
if token:
|
19 |
+
if ':' in token:
|
20 |
+
# token = authtoken:username:password
|
21 |
+
token, username, password = token.split(':', 2)
|
22 |
+
account = f"{username}:{password}"
|
23 |
+
|
24 |
+
if not options.get('authtoken_from_env'):
|
25 |
+
options['authtoken'] = token
|
26 |
+
if account:
|
27 |
+
options['basic_auth'] = account
|
28 |
+
|
29 |
+
try:
|
30 |
+
public_url = ngrok.connect(f"127.0.0.1:{port}", **options).url()
|
31 |
+
print(f'ngrok connected to localhost:{port}! URL: {public_url}')
|
32 |
+
except Exception as e:
|
33 |
+
print(f'ngrok connection aborted: {e}')
|
34 |
+
|
35 |
+
# Run reface
|
36 |
+
def run(*vars):
|
37 |
+
video_path = vars[0]
|
38 |
+
origins = vars[1:(num_faces + 1)]
|
39 |
+
destinations = vars[(num_faces + 1):(num_faces * 2) + 1]
|
40 |
+
thresholds = vars[(num_faces * 2) + 1:]
|
41 |
+
|
42 |
+
faces = []
|
43 |
+
for k in range(num_faces):
|
44 |
+
if origins[k] and destinations[k]:
|
45 |
+
faces.append({
|
46 |
+
'origin': origins[k],
|
47 |
+
'destination': destinations[k],
|
48 |
+
'threshold': thresholds[k]
|
49 |
+
})
|
50 |
+
|
51 |
+
return refacer.reface(video_path, faces)
|
52 |
+
|
53 |
+
# UI setup
|
54 |
+
origin = []
|
55 |
+
destination = []
|
56 |
+
thresholds = []
|
57 |
+
num_faces = 5
|
58 |
+
|
59 |
+
from gradio.themes.utils.theme_dropdown import create_theme_dropdown # noqa: F401
|
60 |
|
61 |
dropdown, js = create_theme_dropdown()
|
62 |
|
63 |
+
with gr.Blocks(theme='HaleyCH/HaleyCH_Theme') as demo:
|
64 |
with gr.Row(equal_height=True):
|
65 |
with gr.Column(scale=10):
|
66 |
+
gr.Markdown("# 🧸Refacer")
|
67 |
+
with gr.Row():
|
68 |
+
video = gr.Video(label="👑Original video", format="mp4")
|
69 |
+
video2 = gr.Video(label="💎Refaced video", interactive=False, format="mp4")
|
70 |
+
|
71 |
+
for i in range(num_faces):
|
72 |
+
with gr.Tab(f"Face #{i + 1}"):
|
73 |
+
with gr.Row():
|
74 |
+
origin.append(gr.Image(label="🧲Face to replace"))
|
75 |
+
destination.append(gr.Image(label="📥Destination face"))
|
76 |
+
with gr.Row():
|
77 |
+
thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
|
78 |
+
|
79 |
+
with gr.Row():
|
80 |
+
button = gr.Button("💽Reface", variant="primary")
|
81 |
+
|
82 |
+
button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
|
83 |
+
|
84 |
+
# Start initialization in a separate thread to prevent blocking
|
85 |
+
init_thread = threading.Thread(target=initialize_refacer)
|
86 |
+
init_thread.start()
|
87 |
+
|
88 |
+
# Launch demo
|
89 |
+
demo.queue().launch(show_error=True, share=True, server_name="0.0.0.0", server_port=7860)
|