Spaces:
Build error
Build error
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,161 +7,273 @@ import shutil
|
|
| 7 |
|
| 8 |
os.makedirs("./output", exist_ok=True)
|
| 9 |
|
|
|
|
| 10 |
def run(*args):
|
| 11 |
source, target, unique_id, *rest_args = args
|
| 12 |
-
|
|
|
|
| 13 |
return "Source file does not exist"
|
| 14 |
-
|
|
|
|
| 15 |
return "Target file does not exist"
|
|
|
|
| 16 |
remove_old_directories("./output", num_minutes=60)
|
|
|
|
| 17 |
filename = os.path.basename(target)
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
frame_processor = rest_args[0]
|
| 21 |
-
selected_frame_processors =
|
| 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
|
| 29 |
-
f
|
| 30 |
-
f
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
)
|
| 32 |
-
|
|
|
|
| 33 |
cmd += f"--face-recognition {face_recognition} "
|
| 34 |
-
|
|
|
|
| 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 |
-
|
| 51 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 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
|
| 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 |
-
|
|
|
|
| 93 |
return gr.themes.Soft(
|
| 94 |
-
primary_hue
|
| 95 |
-
secondary_hue
|
| 96 |
-
font
|
| 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.Group():
|
| 105 |
-
gr.HTML('<center><a href="https://codegenius.me">DeepFakeAI 1.0.1</a></center>')
|
| 106 |
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
)
|
| 114 |
|
| 115 |
-
|
| 116 |
-
with gr.Group():
|
| 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 |
-
|
| 139 |
-
image_output = gr.Image(
|
| 140 |
-
|
|
|
|
|
|
|
|
|
|
| 141 |
|
| 142 |
image_button.click(
|
| 143 |
-
run,
|
| 144 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 145 |
outputs=image_output
|
| 146 |
)
|
| 147 |
-
clear_button.click(fn=clear_output, inputs=unique_id)
|
| 148 |
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
with gr.Group():
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 156 |
video_button = gr.Button("START")
|
| 157 |
-
|
| 158 |
-
video_output = gr.Video(
|
| 159 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
video_button.click(
|
| 161 |
-
run,
|
| 162 |
-
inputs=[
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
outputs=video_output
|
| 164 |
)
|
| 165 |
-
clear_video_button.click(fn=clear_output, inputs=unique_id)
|
| 166 |
|
| 167 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
os.makedirs("./output", exist_ok=True)
|
| 9 |
|
| 10 |
+
|
| 11 |
def run(*args):
|
| 12 |
source, target, unique_id, *rest_args = args
|
| 13 |
+
|
| 14 |
+
if not source or not os.path.exists(source):
|
| 15 |
return "Source file does not exist"
|
| 16 |
+
|
| 17 |
+
if not target or not os.path.exists(target):
|
| 18 |
return "Target file does not exist"
|
| 19 |
+
|
| 20 |
remove_old_directories("./output", num_minutes=60)
|
| 21 |
+
|
| 22 |
filename = os.path.basename(target)
|
| 23 |
+
output_dir = f"./output/{unique_id}"
|
| 24 |
+
|
| 25 |
+
os.makedirs(output_dir, exist_ok=True)
|
| 26 |
+
|
| 27 |
+
output = f"{output_dir}/{filename}"
|
| 28 |
+
|
| 29 |
frame_processor = rest_args[0]
|
| 30 |
+
selected_frame_processors = " ".join(frame_processor)
|
| 31 |
|
| 32 |
face_analyser_direction = rest_args[1]
|
| 33 |
face_recognition = rest_args[2]
|
| 34 |
face_analyser_gender = rest_args[3]
|
| 35 |
|
| 36 |
cmd = (
|
| 37 |
+
f'python run.py '
|
| 38 |
+
f'--execution-providers cpu '
|
| 39 |
+
f'-s "{source}" '
|
| 40 |
+
f'-t "{target}" '
|
| 41 |
+
f'-o "{output}" '
|
| 42 |
+
f'--frame-processors {selected_frame_processors} '
|
| 43 |
+
f'--face-analyser-direction {face_analyser_direction} '
|
| 44 |
)
|
| 45 |
+
|
| 46 |
+
if face_recognition != "none":
|
| 47 |
cmd += f"--face-recognition {face_recognition} "
|
| 48 |
+
|
| 49 |
+
if face_analyser_gender != "none":
|
| 50 |
cmd += f"--face-analyser-gender {face_analyser_gender} "
|
| 51 |
|
| 52 |
if len(rest_args) > 4:
|
| 53 |
skip_audio = rest_args[4]
|
| 54 |
keep_fps = rest_args[5]
|
| 55 |
keep_temp = rest_args[6]
|
| 56 |
+
|
| 57 |
if skip_audio:
|
| 58 |
cmd += "--skip-audio "
|
| 59 |
+
|
| 60 |
if keep_fps:
|
| 61 |
cmd += "--keep-fps "
|
| 62 |
+
|
| 63 |
if keep_temp:
|
| 64 |
cmd += "--keep-temp "
|
| 65 |
|
| 66 |
try:
|
| 67 |
print("Started...", cmd)
|
| 68 |
+
|
| 69 |
+
result = sp.run(
|
| 70 |
+
cmd,
|
| 71 |
+
shell=True,
|
| 72 |
+
capture_output=True,
|
| 73 |
+
text=True
|
| 74 |
+
)
|
| 75 |
+
|
| 76 |
+
print(result.stdout)
|
| 77 |
+
|
| 78 |
+
if result.stderr:
|
| 79 |
+
print(result.stderr)
|
| 80 |
+
|
| 81 |
return output
|
| 82 |
+
|
| 83 |
except Exception as e:
|
| 84 |
return f"An error occurred: {str(e)}"
|
| 85 |
|
| 86 |
+
|
| 87 |
def clear_output(unique_id):
|
| 88 |
try:
|
| 89 |
output_path = f"./output/{unique_id}"
|
| 90 |
+
|
| 91 |
if os.path.exists(output_path):
|
| 92 |
+
shutil.rmtree(output_path)
|
| 93 |
+
print(f"Deleted {output_path}")
|
| 94 |
+
return "Output deleted"
|
| 95 |
+
|
| 96 |
+
return "Output directory does not exist"
|
| 97 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
return f"An error occurred: {str(e)}"
|
| 100 |
|
| 101 |
+
|
| 102 |
def remove_old_directories(directory, num_minutes=60):
|
| 103 |
now = time.time()
|
| 104 |
|
| 105 |
for r, d, f in os.walk(directory):
|
| 106 |
for dir_name in d:
|
| 107 |
dir_path = os.path.join(r, dir_name)
|
| 108 |
+
|
| 109 |
timestamp = os.path.getmtime(dir_path)
|
| 110 |
+
age_minutes = (now - timestamp) / 60
|
| 111 |
+
|
| 112 |
if age_minutes >= num_minutes:
|
| 113 |
try:
|
| 114 |
print("Removing", dir_path)
|
| 115 |
shutil.rmtree(dir_path)
|
| 116 |
print("Directory removed:", dir_path)
|
| 117 |
+
|
| 118 |
except Exception as e:
|
| 119 |
print(e)
|
|
|
|
|
|
|
| 120 |
|
| 121 |
+
|
| 122 |
+
def get_theme():
|
| 123 |
return gr.themes.Soft(
|
| 124 |
+
primary_hue=gr.themes.colors.red,
|
| 125 |
+
secondary_hue=gr.themes.colors.gray,
|
| 126 |
+
font=gr.themes.GoogleFont("Inter")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
)
|
| 128 |
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
+
with gr.Blocks(
|
| 131 |
+
theme=get_theme(),
|
| 132 |
+
title="DeepFakeAI 1.0.0"
|
| 133 |
+
) as ui:
|
| 134 |
+
|
| 135 |
+
gr.HTML(
|
| 136 |
+
'<center><h1>DeepFakeAI 1.0.1</h1></center>'
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
frame_processor_checkbox = gr.CheckboxGroup(
|
| 140 |
+
choices=[
|
| 141 |
+
"face_swapper",
|
| 142 |
+
"face_enhancer",
|
| 143 |
+
"frame_enhancer"
|
| 144 |
+
],
|
| 145 |
+
label="FRAME PROCESSORS",
|
| 146 |
+
value=["face_swapper"]
|
| 147 |
+
)
|
| 148 |
+
|
| 149 |
+
face_analyser_direction_dropdown = gr.Dropdown(
|
| 150 |
+
label="FACE ANALYSER DIRECTION",
|
| 151 |
+
choices=[
|
| 152 |
+
"left-right",
|
| 153 |
+
"right-left",
|
| 154 |
+
"top-bottom",
|
| 155 |
+
"bottom-top",
|
| 156 |
+
"small-large",
|
| 157 |
+
"large-small"
|
| 158 |
+
],
|
| 159 |
+
value="left-right"
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
face_analyser_age_dropdown = gr.Dropdown(
|
| 163 |
+
label="FACE RECOGNITION",
|
| 164 |
+
choices=["none", "reference", "many"],
|
| 165 |
+
value="reference"
|
| 166 |
+
)
|
| 167 |
+
|
| 168 |
+
face_analyser_gender_dropdown = gr.Dropdown(
|
| 169 |
+
label="FACE ANALYSER GENDER",
|
| 170 |
+
choices=["none", "male", "female"],
|
| 171 |
+
value="none"
|
| 172 |
+
)
|
| 173 |
+
|
| 174 |
+
unique_id = gr.Textbox(
|
| 175 |
+
value=str(uuid.uuid4()),
|
| 176 |
+
visible=False
|
| 177 |
+
)
|
| 178 |
+
|
| 179 |
+
with gr.Tab("Image"):
|
| 180 |
+
|
| 181 |
+
source_image = gr.Image(
|
| 182 |
+
type="filepath",
|
| 183 |
+
label="SOURCE IMAGE"
|
| 184 |
+
)
|
| 185 |
+
|
| 186 |
+
target_image = gr.Image(
|
| 187 |
+
type="filepath",
|
| 188 |
+
label="TARGET IMAGE"
|
| 189 |
)
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
image_button = gr.Button("START")
|
| 192 |
+
|
| 193 |
+
image_output = gr.Image(
|
| 194 |
+
label="OUTPUT"
|
| 195 |
+
)
|
| 196 |
+
|
| 197 |
+
clear_button = gr.Button("CLEAR")
|
| 198 |
|
| 199 |
image_button.click(
|
| 200 |
+
fn=run,
|
| 201 |
+
inputs=[
|
| 202 |
+
source_image,
|
| 203 |
+
target_image,
|
| 204 |
+
unique_id,
|
| 205 |
+
frame_processor_checkbox,
|
| 206 |
+
face_analyser_direction_dropdown,
|
| 207 |
+
face_analyser_age_dropdown,
|
| 208 |
+
face_analyser_gender_dropdown
|
| 209 |
+
],
|
| 210 |
outputs=image_output
|
| 211 |
)
|
|
|
|
| 212 |
|
| 213 |
+
clear_button.click(
|
| 214 |
+
fn=clear_output,
|
| 215 |
+
inputs=unique_id
|
| 216 |
+
)
|
| 217 |
+
|
| 218 |
+
with gr.Tab("Video"):
|
| 219 |
+
|
| 220 |
+
source_image_video = gr.Image(
|
| 221 |
+
type="filepath",
|
| 222 |
+
label="SOURCE IMAGE"
|
| 223 |
+
)
|
| 224 |
+
|
| 225 |
+
target_video = gr.Video(
|
| 226 |
+
label="TARGET VIDEO"
|
| 227 |
+
)
|
| 228 |
+
|
| 229 |
with gr.Group():
|
| 230 |
+
|
| 231 |
+
skip_audio = gr.Checkbox(
|
| 232 |
+
label="SKIP AUDIO"
|
| 233 |
+
)
|
| 234 |
+
|
| 235 |
+
keep_fps = gr.Checkbox(
|
| 236 |
+
label="KEEP FPS"
|
| 237 |
+
)
|
| 238 |
+
|
| 239 |
+
keep_temp = gr.Checkbox(
|
| 240 |
+
label="KEEP TEMP"
|
| 241 |
+
)
|
| 242 |
+
|
| 243 |
video_button = gr.Button("START")
|
| 244 |
+
|
| 245 |
+
video_output = gr.Video(
|
| 246 |
+
label="OUTPUT"
|
| 247 |
+
)
|
| 248 |
+
|
| 249 |
+
clear_video_button = gr.Button("CLEAR")
|
| 250 |
+
|
| 251 |
video_button.click(
|
| 252 |
+
fn=run,
|
| 253 |
+
inputs=[
|
| 254 |
+
source_image_video,
|
| 255 |
+
target_video,
|
| 256 |
+
unique_id,
|
| 257 |
+
frame_processor_checkbox,
|
| 258 |
+
face_analyser_direction_dropdown,
|
| 259 |
+
face_analyser_age_dropdown,
|
| 260 |
+
face_analyser_gender_dropdown,
|
| 261 |
+
skip_audio,
|
| 262 |
+
keep_fps,
|
| 263 |
+
keep_temp
|
| 264 |
+
],
|
| 265 |
outputs=video_output
|
| 266 |
)
|
|
|
|
| 267 |
|
| 268 |
+
clear_video_button.click(
|
| 269 |
+
fn=clear_output,
|
| 270 |
+
inputs=unique_id
|
| 271 |
+
)
|
| 272 |
+
|
| 273 |
+
ui.queue()
|
| 274 |
+
|
| 275 |
+
ui.launch(
|
| 276 |
+
server_name="0.0.0.0",
|
| 277 |
+
server_port=7860,
|
| 278 |
+
debug=True
|
| 279 |
+
)
|