Spaces:
Paused
Paused
Commit ·
041b694
1
Parent(s): fffaf19
Update app.py
Browse files123456789101112131415161718192021222324253 unmodified lines293031323334354 unmodified lines40414243444546474849505152535455565758596017 unmodified lines7879808182838411 unmodified lines969798991001011021031041051061071081091101111121131143 unmodified lines118119120121122123124125126127128129130import gradio as grimport subprocess as spimport osos.makedirs("./output", exist_ok=True)def run(*args):source, target, *rest_args = argsif not os.path.exists(source):return "Source file does not exist"if not os.path.exists(target):return "Target file does not exist"filename = os.path.basename(target)output = f"./output/{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} "3 unmodified linescmd += 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]4 unmodified linescmd += " --keep-fps"if keep_temp:cmd += " --keep-temp"try:print("Started...", cmd)sp.run(cmd, shell=True, capture_output=True, text=True).stdoutreturn outputexcept Exception as e:return f"An error occurred: {str(e)}"def clear_output(output):if os.path.exists(output):os.remove(output)return "Output file deleted"else:return "Output file does not exist"def get_theme() -> gr.Theme:return gr.themes.Soft(17 unmodified lineslabel = 'FRAME PROCESSORS',value = ['face_swapper'] # Default value)with gr.Box():with gr.Column(scale=3):face_analyser_direction_dropdown = gr.Dropdown(11 unmodified lineschoices = ['none'] + ['male', 'female'],value = 'none')with gr.Tab("Image: "):source_image = gr.Image(type="filepath", label="SOURCE IMAGE")target_image = gr.Image(type="filepath", label="TARGET IMAGE")image_button = gr.Button("START")clear_button = gr.Button("CLEAR")image_output = gr.Image(label="OUTPUT")image_button.click(run,inputs=[source_image, target_image, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown],outputs=image_output)clear_button.click(clear_output, inputs=[image_output])with gr.Tab("Video: "):source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")3 unmodified lineskeep_fps = gr.Checkbox(label="KEEP FPS")keep_temp = gr.Checkbox(label="KEEP TEMP")video_button = gr.Button("START")clear_video_button = gr.Button("CLEAR")video_output = gr.Video(label="OUTPUT")video_button.click(run,inputs=[source_image_video, target_video, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown, skip_audio, keep_fps, keep_temp],outputs=video_output)clear_video_button.click(clear_output, inputs=[video_output])ui.launch(debug=True)1234567891011121314151617181920212223242526273 unmodified lines313233343536374 unmodified lines4243444546474849505152535455565758596061626364656667686970717217 unmodified lines9091929394959611 unmodified lines1081091101111121131141151161171181191201211221231241251261271283 unmodified lines132133134135136137138139140141142143144145import gradio as grimport subprocess as spimport osimport uuidos.makedirs("./output", exist_ok=True)def run(*args):source, target, unique_id, *rest_args = argsif not os.path.exists(source):return "Source file does not exist"if not os.path.exists(target):return "Target file does not exist"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} "3 unmodified linescmd += 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]4 unmodified linescmd += " --keep-fps"if keep_temp:cmd += " --keep-temp"try:print("Started...", cmd)output_text = sp.run(cmd, shell=True, capture_output=True, text=True).stdoutprint(output_text)return outputexcept 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 get_theme() -> gr.Theme:return gr.themes.Soft(17 unmodified lineslabel = 'FRAME PROCESSORS',value = ['face_swapper'] # Default value)with gr.Box():with gr.Column(scale=3):face_analyser_direction_dropdown = gr.Dropdown(11 unmodified lineschoices = ['none'] + ['male', 'female'],value = 'none')unique_id = gr.Textbox(value=str(uuid.uuid4()), visible=False)with gr.Tab("Image: "):source_image = gr.Image(type="filepath", label="SOURCE IMAGE")target_image = gr.Image(type="filepath", label="TARGET IMAGE")image_button = gr.Button("START")clear_button = gr.ClearButton(value="CLEAR")image_output = gr.Image(label="OUTPUT")clear_button.add(image_output)image_button.click(run,inputs=[source_image, target_image, unique_id, frame_processor_checkbox, face_analyser_direction_dropdown, face_analyser_age_dropdown, face_analyser_gender_dropdown],outputs=image_output)clear_button.click(fn=clear_output, inputs=unique_id)with gr.Tab("Video: "):source_image_video = gr.Image(type="filepath", label="SOURCE IMAGE")3 unmodified lineskeep_fps = gr.Checkbox(label="KEEP FPS")keep_temp = gr.Checkbox(label="KEEP TEMP")video_button = gr.Button("START")clear_video_button = gr.ClearButton(value="CLEAR")video_output = gr.Video(label="OUTPUT")clear_video_button.add(video_output)video_button.click(run,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],outputs=video_output)clear_video_button.click(fn=clear_output, inputs=unique_id)ui.launch(debug=True)
app.py+39-24