Spaces:
Running
Running
Commit
•
be712f7
1
Parent(s):
67650ab
Upload folder using huggingface_hub
Browse files
app.py
CHANGED
@@ -1,167 +0,0 @@
|
|
1 |
-
import gradio as gr
|
2 |
-
import subprocess as sp
|
3 |
-
import os
|
4 |
-
import uuid
|
5 |
-
import time
|
6 |
-
import shutil
|
7 |
-
|
8 |
-
os.makedirs("./output", exist_ok=True)
|
9 |
-
|
10 |
-
def run(*args):
|
11 |
-
source, target, unique_id, *rest_args = args
|
12 |
-
if not os.path.exists(source):
|
13 |
-
return "Source file does not exist"
|
14 |
-
if not os.path.exists(target):
|
15 |
-
return "Target file does not exist"
|
16 |
-
remove_old_directories("./output", num_minutes=60)
|
17 |
-
filename = os.path.basename(target)
|
18 |
-
os.makedirs(f"./output/{unique_id}",exist_ok=True)
|
19 |
-
output = f"./output/{unique_id}/{filename}"
|
20 |
-
frame_processor = rest_args[0]
|
21 |
-
selected_frame_processors = ' '.join(frame_processor)
|
22 |
-
|
23 |
-
face_analyser_direction = rest_args[1]
|
24 |
-
face_recognition = rest_args[2]
|
25 |
-
face_analyser_gender = rest_args[3]
|
26 |
-
|
27 |
-
cmd = (
|
28 |
-
f"python run.py --execution-providers cpu -s {source} -t {target} -o {output} "
|
29 |
-
f"--frame-processors {selected_frame_processors} "
|
30 |
-
f"--face-analyser-direction {face_analyser_direction} "
|
31 |
-
)
|
32 |
-
if face_recognition != 'none':
|
33 |
-
cmd += f"--face-recognition {face_recognition} "
|
34 |
-
if face_analyser_gender != 'none':
|
35 |
-
cmd += f"--face-analyser-gender {face_analyser_gender} "
|
36 |
-
|
37 |
-
if len(rest_args) > 4:
|
38 |
-
skip_audio = rest_args[4]
|
39 |
-
keep_fps = rest_args[5]
|
40 |
-
keep_temp = rest_args[6]
|
41 |
-
if skip_audio:
|
42 |
-
cmd += "--skip-audio "
|
43 |
-
if keep_fps:
|
44 |
-
cmd += "--keep-fps "
|
45 |
-
if keep_temp:
|
46 |
-
cmd += "--keep-temp "
|
47 |
-
|
48 |
-
try:
|
49 |
-
print("Started...", cmd)
|
50 |
-
output_text = sp.run(cmd, shell=True, capture_output=True, text=True).stdout
|
51 |
-
print(output_text)
|
52 |
-
return output
|
53 |
-
except Exception as e:
|
54 |
-
return f"An error occurred: {str(e)}"
|
55 |
-
|
56 |
-
def clear_output(unique_id):
|
57 |
-
try:
|
58 |
-
output_path = f"./output/{unique_id}"
|
59 |
-
if os.path.exists(output_path):
|
60 |
-
print("Trying to delete ")
|
61 |
-
for filename in os.listdir(output_path):
|
62 |
-
file_path = os.path.join(output_path, filename)
|
63 |
-
if os.path.isfile(file_path):
|
64 |
-
os.remove(file_path)
|
65 |
-
print(f"Output files in {output_path} are deleted")
|
66 |
-
return "Output files for unique_id deleted"
|
67 |
-
else:
|
68 |
-
print(f"Output files in {output_path} does not exist")
|
69 |
-
return "Output directory for (output_path} does not exist"
|
70 |
-
except Exception as e:
|
71 |
-
return f"An error occurred: {str(e)}"
|
72 |
-
|
73 |
-
def remove_old_directories(directory, num_minutes=60):
|
74 |
-
now = time.time()
|
75 |
-
|
76 |
-
for r, d, f in os.walk(directory):
|
77 |
-
for dir_name in d:
|
78 |
-
dir_path = os.path.join(r, dir_name)
|
79 |
-
timestamp = os.path.getmtime(dir_path)
|
80 |
-
age_minutes = (now - timestamp) / 60 # Convert to minutes
|
81 |
-
|
82 |
-
if age_minutes >= num_minutes:
|
83 |
-
try:
|
84 |
-
print("Removing", dir_path)
|
85 |
-
shutil.rmtree(dir_path)
|
86 |
-
print("Directory removed:", dir_path)
|
87 |
-
except Exception as e:
|
88 |
-
print(e)
|
89 |
-
pass
|
90 |
-
|
91 |
-
|
92 |
-
def get_theme() -> gr.Theme:
|
93 |
-
return gr.themes.Soft(
|
94 |
-
primary_hue = gr.themes.colors.red,
|
95 |
-
secondary_hue = gr.themes.colors.gray,
|
96 |
-
font = gr.themes.GoogleFont('Inter')
|
97 |
-
).set(
|
98 |
-
background_fill_primary = '*neutral_50',
|
99 |
-
block_label_text_size = '*text_sm',
|
100 |
-
block_title_text_size = '*text_sm'
|
101 |
-
)
|
102 |
-
|
103 |
-
with gr.Blocks(theme=get_theme(), title="DeepFakeAI 1.0.0") as ui:
|
104 |
-
with gr.Box():
|
105 |
-
gr.HTML('<center><a href="https://codegenius.me">DeepFakeAI 1.0.1</a></center>')
|
106 |
-
|
107 |
-
with gr.Box():
|
108 |
-
with gr.Column(scale=3):
|
109 |
-
frame_processor_checkbox = gr.CheckboxGroup(
|
110 |
-
choices = ['face_swapper', 'face_enhancer', 'frame_enhancer'],
|
111 |
-
label = 'FRAME PROCESSORS',
|
112 |
-
value = ['face_swapper'] # Default value
|
113 |
-
)
|
114 |
-
|
115 |
-
|
116 |
-
with gr.Box():
|
117 |
-
with gr.Column(scale=3):
|
118 |
-
face_analyser_direction_dropdown = gr.Dropdown(
|
119 |
-
label = 'FACE ANALYSER DIRECTION',
|
120 |
-
choices = ['left-right', 'right-left', 'top-bottom', 'bottom-top', 'small-large', 'large-small'],
|
121 |
-
value = 'left-right'
|
122 |
-
)
|
123 |
-
face_analyser_age_dropdown = gr.Dropdown(
|
124 |
-
label = 'FACE RECOGNITION',
|
125 |
-
choices = ['none'] + ['reference', 'many'],
|
126 |
-
value = 'reference'
|
127 |
-
)
|
128 |
-
face_analyser_gender_dropdown = gr.Dropdown(
|
129 |
-
label = 'FACE ANALYSER GENDER',
|
130 |
-
choices = ['none'] + ['male', 'female'],
|
131 |
-
value = 'none'
|
132 |
-
)
|
133 |
-
unique_id = gr.Textbox(value=str(uuid.uuid4()), visible=False)
|
134 |
-
with gr.Tab("Image: "):
|
135 |
-
source_image = gr.Image(type="filepath", label="SOURCE IMAGE")
|
136 |
-
target_image = gr.Image(type="filepath", label="TARGET IMAGE")
|
137 |
-
image_button = gr.Button("START")
|
138 |
-
clear_button = gr.ClearButton(value="CLEAR")
|
139 |
-
image_output = gr.Image(label="OUTPUT")
|
140 |
-
clear_button.add(image_output)
|
141 |
-
|
142 |
-
image_button.click(
|
143 |
-
run,
|
144 |
-
inputs=[source_image, target_image, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown],
|
145 |
-
outputs=image_output
|
146 |
-
)
|
147 |
-
clear_button.click(fn=clear_output, inputs=unique_id)
|
148 |
-
|
149 |
-
with gr.Tab("Video: "):
|
150 |
-
source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")
|
151 |
-
target_video = gr.Video(label="TARGET VIDEO")
|
152 |
-
with gr.Box():
|
153 |
-
skip_audio = gr.Checkbox(label="SKIP AUDIO")
|
154 |
-
keep_fps = gr.Checkbox(label="KEEP FPS")
|
155 |
-
keep_temp = gr.Checkbox(label="KEEP TEMP")
|
156 |
-
video_button = gr.Button("START")
|
157 |
-
clear_video_button = gr.ClearButton(value="CLEAR")
|
158 |
-
video_output = gr.Video(label="OUTPUT")
|
159 |
-
clear_video_button.add(video_output)
|
160 |
-
video_button.click(
|
161 |
-
run,
|
162 |
-
inputs=[source_image_video, target_video, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],
|
163 |
-
outputs=video_output
|
164 |
-
)
|
165 |
-
clear_video_button.click(fn=clear_output, inputs=unique_id)
|
166 |
-
|
167 |
-
ui.launch(debug=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|