heatingma commited on
Commit
417ab93
·
verified ·
1 Parent(s): 46088e2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +117 -116
app.py CHANGED
@@ -1,117 +1,118 @@
1
- import os
2
- import time
3
- import shutil
4
- import gradio as gr
5
-
6
-
7
- ROOP_DEFAULT_PATH = "media/roop_default.png"
8
- ROOP_OUTPUT_VIDEO_PATH = "media/roop_output.mp4"
9
-
10
-
11
- def _handle_roop_solve(
12
- video_path: str,
13
- img_path: str
14
- ):
15
- if video_path is None:
16
- raise gr.Error("Please upload source video!")
17
- if img_path is None:
18
- raise gr.Error("Please upload target image!")
19
-
20
- start_time = time.time()
21
- command = f"python run.py -t {video_path} -s {img_path} -o {ROOP_OUTPUT_VIDEO_PATH}"
22
- os.system(command)
23
- solved_time = time.time() - start_time
24
- message = "Successfully solve the GED problem, using time ({:.3f}s).".format(solved_time)
25
-
26
- return message, ROOP_OUTPUT_VIDEO_PATH
27
-
28
-
29
- def handle_roop(
30
- video_path: str,
31
- img_path: str
32
- ):
33
- try:
34
- message = _handle_roop_solve(
35
- video_path=video_path,
36
- img_path=img_path
37
- )
38
- return message
39
- except Exception as e:
40
- message = str(e)
41
- return message, ROOP_OUTPUT_VIDEO_PATH
42
-
43
-
44
- def handle_roop_clear():
45
- shutil.copy(
46
- src=ROOP_DEFAULT_PATH,
47
- dst=ROOP_OUTPUT_VIDEO_PATH
48
- )
49
-
50
- message = "successfully clear the files!"
51
- return message, ROOP_OUTPUT_VIDEO_PATH
52
-
53
-
54
- with gr.Blocks() as ged_page:
55
-
56
- gr.Markdown(
57
- '''
58
- This space displays the solution to the Graph Edit Distance problem.
59
- ## How to use this Space?
60
- - Upload two '.gexf' files.
61
- - The images of the GED problem and solution will be shown after you click the solve button.
62
- - Click the 'clear' button to clear all the files.
63
- ## Examples
64
- - You can get the test examples from our [GED Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/Graph-Edit-Distance)
65
- '''
66
- )
67
-
68
- with gr.Row(variant="panel"):
69
- with gr.Column(scale=2):
70
- with gr.Row():
71
- upload_video = gr.Video(
72
- label="Upload .mp4 Vide0",
73
- format="mp4",
74
- )
75
- upload_img = gr.Image(
76
- label="Upload .png or .jpg File",
77
- type="filepath",
78
- min_width=40,
79
- )
80
- info = gr.Textbox(
81
- value="",
82
- label="Log",
83
- scale=4,
84
- )
85
- with gr.Row():
86
- with gr.Column(scale=1, min_width=100):
87
- solve_button = gr.Button(
88
- value="Solve",
89
- variant="primary",
90
- scale=1
91
- )
92
- with gr.Column(scale=1, min_width=100):
93
- clear_button = gr.Button(
94
- "Clear",
95
- variant="secondary",
96
- scale=1
97
- )
98
- with gr.Column(scale=8):
99
- pass
100
- with gr.Column(scale=2):
101
- output_video = gr.Video(height=405, width=720)
102
-
103
- solve_button.click(
104
- handle_roop,
105
- [upload_video, upload_img],
106
- outputs=[info, output_video]
107
- )
108
-
109
- clear_button.click(
110
- handle_roop_clear,
111
- inputs=None,
112
- outputs=[info, output_video]
113
- )
114
-
115
-
116
- if __name__ == "__main__":
 
117
  ged_page.launch(debug = True)
 
1
+ import os
2
+ import time
3
+ import shutil
4
+ import gradio as gr
5
+
6
+
7
+ ROOP_DEFAULT_PATH = "media/roop_default.png"
8
+ ROOP_OUTPUT_VIDEO_PATH = "media/roop_output.mp4"
9
+
10
+
11
+ def _handle_roop_solve(
12
+ video_path: str,
13
+ img_path: str
14
+ ):
15
+ if video_path is None:
16
+ raise gr.Error("Please upload source video!")
17
+ if img_path is None:
18
+ raise gr.Error("Please upload target image!")
19
+
20
+ start_time = time.time()
21
+ command = f"python run.py -t {video_path} -s {img_path} -o {ROOP_OUTPUT_VIDEO_PATH}"
22
+ os.system(command)
23
+ solved_time = time.time() - start_time
24
+ message = "Successfully solve the GED problem, using time ({:.3f}s).".format(solved_time)
25
+
26
+ return message, ROOP_OUTPUT_VIDEO_PATH
27
+
28
+
29
+ def handle_roop(
30
+ video_path: str,
31
+ img_path: str
32
+ ):
33
+ try:
34
+ message = _handle_roop_solve(
35
+ video_path=video_path,
36
+ img_path=img_path
37
+ )
38
+ return message
39
+ except Exception as e:
40
+ message = str(e)
41
+ return message, ROOP_OUTPUT_VIDEO_PATH
42
+
43
+
44
+ def handle_roop_clear():
45
+ shutil.copy(
46
+ src=ROOP_DEFAULT_PATH,
47
+ dst=ROOP_OUTPUT_VIDEO_PATH
48
+ )
49
+
50
+ message = "successfully clear the files!"
51
+ return message, ROOP_OUTPUT_VIDEO_PATH
52
+
53
+
54
+ with gr.Blocks() as ged_page:
55
+
56
+ gr.Markdown(
57
+ '''
58
+ This space displays how to perform face swapping.
59
+ ## How to use this Space?
60
+ - Upload a video, preferably with a duration of less than 5 seconds.
61
+ - Upload a photo of the person you wish to swap with.
62
+ - You will receive the result of the face swap after 5-10 minutes.
63
+ - Click the 'clear' button to clear all the files.
64
+ ## Examples
65
+ - You can get the test examples from our [Roop Dataset Repo.](https://huggingface.co/datasets/SJTU-TES/Roop)
66
+ '''
67
+ )
68
+
69
+ with gr.Row(variant="panel"):
70
+ with gr.Column(scale=2):
71
+ with gr.Row():
72
+ upload_video = gr.Video(
73
+ label="Upload .mp4 Vide0",
74
+ format="mp4",
75
+ )
76
+ upload_img = gr.Image(
77
+ label="Upload .png or .jpg File",
78
+ type="filepath",
79
+ min_width=40,
80
+ )
81
+ info = gr.Textbox(
82
+ value="",
83
+ label="Log",
84
+ scale=4,
85
+ )
86
+ with gr.Row():
87
+ with gr.Column(scale=1, min_width=100):
88
+ solve_button = gr.Button(
89
+ value="Solve",
90
+ variant="primary",
91
+ scale=1
92
+ )
93
+ with gr.Column(scale=1, min_width=100):
94
+ clear_button = gr.Button(
95
+ "Clear",
96
+ variant="secondary",
97
+ scale=1
98
+ )
99
+ with gr.Column(scale=8):
100
+ pass
101
+ with gr.Column(scale=2):
102
+ output_video = gr.Video(height=405, width=720)
103
+
104
+ solve_button.click(
105
+ handle_roop,
106
+ [upload_video, upload_img],
107
+ outputs=[info, output_video]
108
+ )
109
+
110
+ clear_button.click(
111
+ handle_roop_clear,
112
+ inputs=None,
113
+ outputs=[info, output_video]
114
+ )
115
+
116
+
117
+ if __name__ == "__main__":
118
  ged_page.launch(debug = True)