# import gradio as gr import subprocess as sp import os import uuid import time import shutil os.makedirs("./output", exist_ok=True) def run(*args): source, target, unique_id, *rest_args = args if not os.path.exists(source): return "Source file does not exist" if not os.path.exists(target): return "Target file does not exist" remove_old_directories("./output", num_minutes=2) filename = os.path.basename(target) os.makedirs(f"./output/{unique_id}",exist_ok=True) output = f"./output/{unique_id}/{filename}" frame_processor = rest_args[0] selected_frame_processors = ' '.join(frame_processor) face_analyser_direction = rest_args[1] face_analyser_age = rest_args[2] face_analyser_gender = rest_args[3] cmd = ( f"python run.py --execution-providers cpu -s {source} -t {target} -o {output} " f"--frame-processors {selected_frame_processors} " f"--face-analyser-direction {face_analyser_direction} " ) if face_analyser_age != 'none': cmd += f" --face-analyser-age {face_analyser_age}" if face_analyser_gender != 'none': cmd += f" --face-analyser-gender {face_analyser_gender}" if len(rest_args) > 4: skip_audio = rest_args[4] keep_fps = rest_args[5] keep_temp = rest_args[6] if skip_audio: cmd += " --skip-audio" if keep_fps: cmd += " --keep-fps" if keep_temp: cmd += " --keep-temp" try: print("Started...", cmd) output_text = sp.run(cmd, shell=True, capture_output=True, text=True).stdout print(output_text) return output except Exception as e: return f"An error occurred: {str(e)}" def clear_output(unique_id): try: output_path = f"./output/{unique_id}" if os.path.exists(output_path): print("Trying to delete ") for filename in os.listdir(output_path): file_path = os.path.join(output_path, filename) if os.path.isfile(file_path): os.remove(file_path) print(f"Output files in {output_path} are deleted") return "Output files for unique_id deleted" else: print(f"Output files in {output_path} does not exist") return "Output directory for (output_path} does not exist" except Exception as e: return f"An error occurred: {str(e)}" def remove_old_directories(directory, num_minutes=10): now = time.time() for r, d, f in os.walk(directory): for dir_name in d: dir_path = os.path.join(r, dir_name) timestamp = os.path.getmtime(dir_path) age_minutes = (now - timestamp) / 60 # Convert to minutes if age_minutes >= num_minutes: try: print("Removing", dir_path) shutil.rmtree(dir_path) print("Directory removed:", dir_path) except Exception as e: print(e) pass def get_theme() -> gr.Theme: return gr.themes.Soft( primary_hue = gr.themes.colors.red, secondary_hue = gr.themes.colors.gray, font = gr.themes.GoogleFont('Inter') ).set( background_fill_primary = '*neutral_50', block_label_text_size = '*text_sm', block_title_text_size = '*text_sm' ) with gr.Blocks(theme=get_theme(), title="DeepFakeAI 1.0.0") as ui: with gr.Box(): gr.HTML('