victorisgeek commited on
Commit
9245934
·
verified ·
1 Parent(s): 43f8d6f

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +84 -0
app.py ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ with gr.Blocks() as demo:
60
+ with gr.Row():
61
+ gr.Markdown("# Refacer")
62
+ with gr.Row():
63
+ video = gr.Video(label="Original video", format="mp4")
64
+ video2 = gr.Video(label="Refaced video", interactive=False, format="mp4")
65
+
66
+ for i in range(num_faces):
67
+ with gr.Tab(f"Face #{i + 1}"):
68
+ with gr.Row():
69
+ origin.append(gr.Image(label="Face to replace"))
70
+ destination.append(gr.Image(label="Destination face"))
71
+ with gr.Row():
72
+ thresholds.append(gr.Slider(label="Threshold", minimum=0.0, maximum=1.0, value=0.2))
73
+
74
+ with gr.Row():
75
+ button = gr.Button("Reface", variant="primary")
76
+
77
+ button.click(fn=run, inputs=[video] + origin + destination + thresholds, outputs=[video2])
78
+
79
+ # Start initialization in a separate thread to prevent blocking
80
+ init_thread = threading.Thread(target=initialize_refacer)
81
+ init_thread.start()
82
+
83
+ # Launch demo
84
+ demo.queue().launch(show_error=True, share=True, server_name="0.0.0.0", server_port=7860)