victorisgeek commited on
Commit
2772a3d
·
verified ·
1 Parent(s): 2e8faf2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -24
app.py CHANGED
@@ -1,32 +1,89 @@
1
- import subprocess
 
 
2
  import time
 
3
 
4
- # Install Gradio
5
- subprocess.run(["pip", "install", "gradio"])
 
 
 
 
 
6
 
7
- # Import Gradio
8
- import gradio as gr
9
- from gradio.themes.utils.theme_dropdown import create_theme_dropdown
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
 
11
  dropdown, js = create_theme_dropdown()
12
 
13
- with gr.Blocks(theme='victorisgeek/gray') as demo:
14
  with gr.Row(equal_height=True):
15
  with gr.Column(scale=10):
16
- pass # Placeholder for any additional code needed here
17
-
18
- # Define your face swapping function here
19
- def swapped_image(image):
20
- # Your face swapping logic here
21
- return image # Return the modified image
22
-
23
- # Create Gradio interface
24
- iface = gr.Interface(fn=swapped_image,
25
- inputs="image",
26
- outputs="image",
27
- title="Face Swapping",
28
- description="Upload an image to swap faces.")
29
-
30
- # Launch Gradio interface
31
- if __name__ == "__main__":
32
- iface.launch()
 
 
 
 
 
 
 
 
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)