Python
added file deletion detection
DeepBeepMeep2824d1cccdac
diff --git a/shared/gradio/audio_gallery.py b/shared/gradio/audio_gallery.pyindex a38e5eb..1fd9a7f 100644--- a/shared/gradio/audio_gallery.py+++ b/shared/gradio/audio_gallery.py@@ -320,11 +320,12 @@ AG.tryInstall();audio_infos = self._process_audio_paths(paths)if not audio_infos:- return None, self._create_gallery_html([], 0), paths_json, 0, ""+ return None, self._create_gallery_html([], 0), paths_json, -1, ""new_index = int(click_value)if 0 <= new_index < len(audio_infos):selected_path = audio_infos[new_index]["path"]+ if not os.path.exists(selected_path): selected_path = Nonereturn (selected_path,self._create_gallery_html(audio_infos, new_index),@@ -397,19 +398,25 @@ AG.tryInstall();secs = int(seconds % 60)return f"{mins}:{secs:02d}"- def _get_file_info(self, audio_path):+ def _get_file_info(self, audio_path, not_found=False):"""Get file information: basename, date/time, duration."""p = Path(audio_path)basename = p.name- # Get modification time- mtime = os.path.getmtime(audio_path)- dt = datetime.fromtimestamp(mtime)- date_str = dt.strftime("%Y-%m-%d")- time_str = dt.strftime("%H:%M:%S")+ if not_found:+ mtime = ""+ date_str = "Deleted"+ time_str = "00:00:00"+ duration = "0:00"+ else:+ # Get modification time+ mtime = os.path.getmtime(audio_path)+ dt = datetime.fromtimestamp(mtime)+ date_str = dt.strftime("%Y-%m-%d")+ time_str = dt.strftime("%H:%M:%S")- # Get duration- duration = self._get_audio_duration(audio_path)+ # Get duration+ duration = self._get_audio_duration(audio_path)return {"basename": basename,@@ -633,6 +640,8 @@ AG.tryInstall();try:if os.path.exists(path):audio_infos.append(self._get_file_info(path))+ else:+ audio_infos.append(self._get_file_info(path, True))except Exception:continueaudio_infos = audio_infos[: self.max_thumbnails]@@ -645,12 +654,13 @@ AG.tryInstall();audio_infos = self._process_audio_paths(paths)if not audio_infos:- return None, self._create_gallery_html([], 0), paths_json, 0, ""+ return None, self._create_gallery_html([], 0), paths_json, -1, ""selected_idx = _get_selected_idx(audio_infos, selected_idx)- selected_path = audio_infos[selected_idx]["path"]+ selected_path = audio_infos[selected_idx]["path"]+ if not os.path.exists(selected_path): selected_path = Nonegallery_html_content = self._create_gallery_html(audio_infos, selected_idx)return selected_path, gallery_html_content, paths_json, selected_idx, ""except Exception:- return None, self._create_gallery_html([], 0), paths_json, 0, ""+ return None, self._create_gallery_html([], 0), paths_json, -1, ""diff --git a/wgp.py b/wgp.pyindex 75ea01f..c567a7f 100644--- a/wgp.py+++ b/wgp.py@@ -37,7 +37,7 @@ from shared.utils.utils import convert_tensor_to_image, save_image, get_video_infrom shared.utils.utils import calculate_new_dimensions, get_outpainting_frame_location, get_outpainting_full_area_dimensionsfrom shared.utils.utils import has_video_file_extension, has_image_file_extension, has_audio_file_extensionfrom shared.utils.audio_video import extract_audio_tracks, combine_video_with_audio_tracks, combine_and_concatenate_video_with_audio_tracks, cleanup_temp_audio_files, save_video, save_image-from shared.utils.audio_video import save_image_metadata, read_image_metadata+from shared.utils.audio_video import save_image_metadata, read_image_metadata, extract_audio_track_to_wav, write_wav_filefrom shared.utils.audio_metadata import save_audio_metadata, read_audio_metadatafrom shared.utils.video_metadata import save_video_metadatafrom shared.match_archi import match_nvidia_architecture@@ -2786,6 +2786,8 @@ if not "embed_source_images" in server_config: server_config["embed_source_imageif not "enable_4k_resolutions" in server_config: server_config["enable_4k_resolutions"]= 0if not "max_reserved_loras" in server_config: server_config["max_reserved_loras"]= -1if not "image_output_codec" in server_config: server_config["image_output_codec"]= "jpeg_95"+if not "audio_output_codec" in server_config: server_config["audio_output_codec"]= "aac_128"+if not "audio_stand_alone_output_codec" in server_config: server_config["audio_stand_alone_output_codec"]= "wav"if not "rife_version" in server_config: server_config["rife_version"] = "v4"preload_model_policy = server_config.get("preload_model_policy", [])@@ -3714,6 +3716,8 @@ def refresh_gallery(state): #, msggen = get_gen_info(state)# gen["last_msg"] = msg+ clear_deleted_files(state, False)+ clear_deleted_files(state, True)file_list = gen.get("file_list", None)choice = gen.get("selected",0)audio_file_list = gen.get("audio_file_list", None)@@ -3860,6 +3864,8 @@ def get_file_list(state, input_file_list, audio_files = False):if input_file_list != None:for file_path in input_file_list:if isinstance(file_path, tuple): file_path = file_path[0]+ if not os.path.isfile(file_path):+ continuefile_settings, _, _ = get_settings_from_file(state, file_path, False, False, False)file_list.append(file_path)file_settings_list.append(file_settings)@@ -3963,6 +3969,7 @@ def select_video(state, current_gallery_tab, input_file_list, file_selected, audis_audio = Falseis_image = Falseis_video = False+ is_deleted = Falseif len(files) > 0:if len(settings_list) <= choice:pass@@ -3974,24 +3981,25 @@ def select_video(state, current_gallery_tab, input_file_list, file_selected, audmisc_labels = []pp_values= []pp_labels = []- extension = os.path.splitext(file_name)[-1]- if has_audio_file_extension(file_name):+ nb_audio_tracks = 0++ if not os.path.isfile(file_name):+ is_deleted = True+ configs = None+ elif has_audio_file_extension(file_name):is_audio = Truewidth, height = 0, 0frames_count = fps = 1- nb_audio_tracks = 0elif not has_video_file_extension(file_name):img = Image.open(file_name)width, height = img.sizeis_image = Trueframes_count = fps = 1- nb_audio_tracks = 0else:fps, width, height, frames_count = get_video_info(file_name)- is_image = False+ is_video = Truenb_audio_tracks = extract_audio_tracks(file_name,query_only = True)- is_video = not (is_image or is_audio)if configs != None:video_model_name = configs.get("type", "Unknown model")if "-" in video_model_name: video_model_name = video_model_name[video_model_name.find("-")+2:]@@ -4022,14 +4030,15 @@ def select_video(state, current_gallery_tab, input_file_list, file_selected, audif configs == None or not "seed" in configs:values += misc_valueslabels += misc_labels- video_creation_date = str(get_file_creation_date(file_name))++ video_creation_date = "Deleted" if is_deleted else str(get_file_creation_date(file_name))if "." in video_creation_date: video_creation_date = video_creation_date[:video_creation_date.rfind(".")]if is_audio:passelif is_image:values += [f"{width}x{height}"]labels += ["Resolution"]- else:+ elif is_video:values += [f"{width}x{height}", f"{frames_count} frames (duration={frames_count/fps:.1f} s, fps={round(fps)})"]labels += ["Resolution", "Frames"]if nb_audio_tracks > 0:@@ -4281,7 +4290,7 @@ def select_video(state, current_gallery_tab, input_file_list, file_selected, audelse:html_content = get_default_video_info()visible= len(files) > 0- return choice if source=="video" else gr.update(), html_content, gr.update(visible=visible and is_video) , gr.update(visible=visible and is_image), gr.update(visible=visible and is_audio), gr.update(visible=visible and is_video) , gr.update(visible=visible and is_video)+ return choice if source=="video" else gr.update(), html_content, gr.update(visible=visible and is_video) , gr.update(visible=visible and is_image), gr.update(visible=visible and is_audio), gr.update(visible=visible and is_deleted and source=="video"), gr.update(visible=visible and is_deleted and source=="audio"), gr.update(visible=visible and is_video) , gr.update(visible=visible and is_video)def convert_image(image):@@ -5288,63 +5297,6 @@ def slice_audio_window(audio_path, start_frame, num_frames, fps, output_dir, sufdata = data.Treturn data, sample_rate-def extract_audio_track_to_wav(video_path, output_dir, suffix=""):- if not video_path:- return None- try:- video_path = os.fspath(video_path)- except TypeError:- return None- try:- import ffmpeg- except Exception:- return None- output_dir = output_dir or os.path.dirname(video_path) or "."- output_path = get_available_filename(output_dir, video_path, suffix=suffix, force_extension=".wav")- try:- (- ffmpeg.input(video_path)- .output(output_path, **{"map": "0:a:0", "acodec": "pcm_s16le"})- .overwrite_output()- .run(quiet=True)- )- except Exception:- return None- return output_path---def write_wav_file(file_path, audio_data, sampling_rate):- if audio_data is None:- return- import numpy as np- import soundfile as sf-- if torch.is_tensor(audio_data):- audio_data = audio_data.detach()- if audio_data.is_cuda:- audio_data = audio_data.cpu()- if audio_data.dtype not in (torch.float32, torch.float64):- audio_data = audio_data.float()- audio = audio_data.numpy()- else:- audio = np.asarray(audio_data)- if audio.ndim > 1 and audio.shape[0] == 1:- audio = audio.squeeze(0)- if audio.ndim == 2 and audio.shape[0] in (1, 2) and audio.shape[1] > audio.shape[0]:- audio = audio.T- if audio.ndim == 0:- audio = audio.reshape(1)- if audio.ndim == 2 and audio.shape[1] == 1:- audio = audio[:, 0]- if not np.issubdtype(audio.dtype, np.floating):- audio = audio.astype(np.float32)-- sampling_rate = int(sampling_rate)- audio = np.ascontiguousarray(audio)- try:- sf.write(file_path, audio, sampling_rate)- except sf.LibsndfileError:- sf.write(file_path, audio, sampling_rate, format="WAV", subtype="FLOAT")def custom_preprocess_video_with_mask(model_handler, base_model_type, pre_video_guide, video_guide, video_mask, height, width, max_frames, start_frame, fit_canvas, fit_crop, target_fps, block_size, expand_scale, video_prompt_type):pad_frames = 0@@ -7862,6 +7814,25 @@ def remux_audio(state, input_file_list, choice, PP_MMAudio_setting, PP_MMAudio_pin_progress = gen.get("in_progress", False)return "edit_remux", get_unique_id() if not in_progress else gr.update(), get_unique_id() if in_progress else gr.update()+def clear_deleted_files(state, audio_files):+ gen = get_gen_info(state)+ if audio_files:+ file_list_name = "audio_file_list"+ file_settings_name = "audio_file_settings_list"+ else:+ file_list_name = "file_list"+ file_settings_name = "file_settings_list"+ with lock:+ file_list = gen.get(file_list_name, [])+ file_settings_list = gen.get(file_settings_name, [])+ new_file_list = []+ new_file_settings_list = []+ for file_path, file_settings in zip(file_list, file_settings_list):+ if os.path.isfile(file_path):+ new_file_list.append(file_path)+ new_file_settings_list.append(file_settings)+ file_list[:]=new_file_list+ file_settings_list[:]=new_file_settings_listdef eject_video_from_gallery(state, input_file_list, choice):gen = get_gen_info(state)@@ -10200,11 +10171,15 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonvideo_info_to_audio_guide2_btn = gr.Button("To Audio Source 2", min_width= 1, size ="sm", visible = any_audio_guide2 )video_info_to_audio_source_btn = gr.Button("To Custom Audio", min_width= 1, size ="sm", visible = any_audio_source )video_info_eject_audio_btn = gr.Button("Eject Audio File", min_width= 1, size ="sm")+ with gr.Row(**default_visibility_false) as deleted_audio_buttons_row:+ video_info_eject_deleted_audio_btn = gr.Button("Eject Deleted File", min_width= 1, size ="sm")with gr.Row(**default_visibility_false) as video_buttons_row:video_info_extract_settings_btn = gr.Button("Extract Settings", min_width= 1, size ="sm")video_info_to_video_source_btn = gr.Button("To Video Source", min_width= 1, size ="sm", visible = any_video_source)video_info_to_control_video_btn = gr.Button("To Control Video", min_width= 1, size ="sm", visible = any_control_video )video_info_eject_video_btn = gr.Button("Eject Video", min_width= 1, size ="sm")+ with gr.Row(**default_visibility_false) as deleted_video_buttons_row:+ video_info_eject_deleted_video_btn = gr.Button("Eject Deleted File", min_width= 1, size ="sm")with gr.Row(**default_visibility_false) as image_buttons_row:video_info_extract_image_settings_btn = gr.Button("Extract Settings", min_width= 1, size ="sm")video_info_to_start_image_btn = gr.Button("To Start Image", size ="sm", min_width= 1, visible = any_start_image )@@ -10292,8 +10267,8 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonapg_col, audio_prompt_type_sources, audio_prompt_type_remux, audio_prompt_type_remux_row, force_fps_col,video_guide_outpainting_col,video_guide_outpainting_top, video_guide_outpainting_bottom, video_guide_outpainting_left, video_guide_outpainting_right,video_guide_outpainting_checkbox, video_guide_outpainting_row, show_advanced, video_info_to_control_video_btn, video_info_to_video_source_btn, sample_solver_row,- video_buttons_row, image_buttons_row, video_postprocessing_tab, audio_remuxing_tab, PP_MMAudio_col, PP_MMAudio_setting, PP_MMAudio_row, PP_custom_audio_row,- audio_buttons_row, video_info_extract_audio_settings_btn, video_info_to_audio_guide_btn, video_info_to_audio_guide2_btn, video_info_to_audio_source_btn, video_info_eject_audio_btn,+ video_buttons_row, deleted_video_buttons_row, image_buttons_row, video_postprocessing_tab, audio_remuxing_tab, PP_MMAudio_col, PP_MMAudio_setting, PP_MMAudio_row, PP_custom_audio_row,+ audio_buttons_row, deleted_audio_buttons_row, video_info_extract_audio_settings_btn, video_info_to_audio_guide_btn, video_info_to_audio_guide2_btn, video_info_to_audio_source_btn, video_info_eject_audio_btn,video_info_to_start_image_btn, video_info_to_end_image_btn, video_info_to_reference_image_btn, video_info_to_image_guide_btn, video_info_to_image_mask_btn,NAG_col, remove_background_sound , speakers_locations_row, embedded_guidance_row, guidance_phases_row, guidance_row, resolution_group, cfg_free_guidance_col, control_net_weights_row, guide_selection_row, image_mode_tabs,min_frames_if_references_col, motion_amplitude_col, video_prompt_type_alignment, prompt_enhancer_btn, tab_inpaint, tab_t2v, resolution_row, loras_tab, post_processing_tab, temperature_row, top_k_row, number_frames_row, negative_prompt_row, chatter_row] +\@@ -10311,9 +10286,9 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonresolution.change(fn=record_last_resolution, inputs=[state, resolution])video_info_add_videos_btn.click(fn=add_videos_to_gallery, inputs =[state, output, last_choice, audio_files_paths, audio_file_selected, files_to_load], outputs = [gallery_tabs, current_gallery_tab, output, audio_files_paths, audio_file_selected, audio_gallery_refresh_trigger, files_to_load, video_info_tabs, gallery_source] ).then(- fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gallery_source], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")+ fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gallery_source], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, deleted_video_buttons_row, deleted_audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")gallery_tabs.select(fn=set_gallery_tab, inputs=[state], outputs=[current_gallery_tab, gallery_source]).then(- fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gallery_source], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")+ fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gallery_source], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, deleted_video_buttons_row, deleted_audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")gr.on(triggers=[video_length.release, force_fps.change, video_guide.change, video_source.change], fn=refresh_video_length_label, inputs=[state, video_length, force_fps, video_guide, video_source] , outputs = video_length, trigger_mode="always_last", show_progress="hidden" )guidance_phases.change(fn=change_guidance_phases, inputs= [state, guidance_phases], outputs =[model_switch_phase, guidance_phases_row, switch_threshold, switch_threshold2, guidance2_scale, guidance3_scale ])audio_prompt_type_remux.change(fn=refresh_audio_prompt_type_remux, inputs=[state, audio_prompt_type, audio_prompt_type_remux], outputs=[audio_prompt_type])@@ -10338,9 +10313,9 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonvideo_guide_outpainting_checkbox.input(fn=refresh_video_guide_outpainting_row, inputs=[video_guide_outpainting_checkbox, video_guide_outpainting], outputs= [video_guide_outpainting_row,video_guide_outpainting])show_advanced.change(fn=switch_advanced, inputs=[state, show_advanced, lset_name], outputs=[advanced_row, preset_buttons_rows, refresh_lora_btn, refresh2_row ,lset_name]).then(fn=switch_prompt_type, inputs = [state, wizard_prompt_activated_var, wizard_variables_var, prompt, wizard_prompt, *prompt_vars], outputs = [wizard_prompt_activated_var, wizard_variables_var, prompt, wizard_prompt, prompt_column_advanced, prompt_column_wizard, prompt_column_wizard_vars, *prompt_vars])- gr.on( triggers=[output.change, output.select],fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gr.State("video")], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")+ gr.on( triggers=[output.change, output.select],fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gr.State("video")], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, deleted_video_buttons_row, deleted_audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")# gr.on( triggers=[output.change, output.select], fn=select_video, inputs=[state, output, last_choice, audio_files_paths, audio_file_selected, gr.State("video")], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")- audio_file_selected.change(fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gr.State("audio")], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")+ audio_file_selected.change(fn=select_video, inputs=[state, current_gallery_tab, output, last_choice, audio_files_paths, audio_file_selected, gr.State("audio")], outputs=[last_choice, video_info, video_buttons_row, image_buttons_row, audio_buttons_row, deleted_video_buttons_row, deleted_audio_buttons_row, video_postprocessing_tab, audio_remuxing_tab], show_progress="hidden")preview_trigger.change(refresh_preview, inputs= [state], outputs= [preview], show_progress="hidden")PP_MMAudio_setting.change(fn = lambda value : [gr.update(visible = value == 1), gr.update(visible = value == 0)] , inputs = [PP_MMAudio_setting], outputs = [PP_MMAudio_row, PP_custom_audio_row] )@@ -10445,7 +10420,7 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Non)set_save_form_event(save_form_trigger.change)- gr.on(triggers=[video_info_eject_video_btn.click, video_info_eject_video2_btn.click, video_info_eject_video3_btn.click, video_info_eject_image_btn.click], fn=eject_video_from_gallery, inputs =[state, output, last_choice], outputs = [output, video_info, video_buttons_row] )+ gr.on(triggers=[video_info_eject_video_btn.click, video_info_eject_video2_btn.click, video_info_eject_video3_btn.click, video_info_eject_deleted_video_btn.click, video_info_eject_image_btn.click], fn=eject_video_from_gallery, inputs =[state, output, last_choice], outputs = [output, video_info, video_buttons_row] )video_info_to_control_video_btn.click(fn=video_to_control_video, inputs =[state, output, last_choice], outputs = [video_guide] )video_info_to_video_source_btn.click(fn=video_to_source_video, inputs =[state, output, last_choice], outputs = [video_source] )video_info_to_start_image_btn.click(fn=image_to_ref_image_add, inputs =[state, output, last_choice, image_start, gr.State("Start Image")], outputs = [image_start] )@@ -10454,7 +10429,7 @@ def generate_video_tab(update_form = False, state_dict = None, ui_defaults = Nonvideo_info_to_image_mask_btn.click(fn=image_to_ref_image_set, inputs =[state, output, last_choice, image_mask, gr.State("Image Mask")], outputs = [image_mask] )video_info_to_reference_image_btn.click(fn=image_to_ref_image_add, inputs =[state, output, last_choice, image_refs, gr.State("Ref Image")], outputs = [image_refs] )- video_info_eject_audio_btn.click(fn=eject_audio_from_gallery, inputs =[state, audio_files_paths, audio_file_selected], outputs = [audio_files_paths, audio_file_selected, audio_gallery_refresh_trigger, video_info, audio_buttons_row] )+ gr.on(triggers=[video_info_eject_audio_btn.click, video_info_eject_deleted_audio_btn.click], fn=eject_audio_from_gallery, inputs =[state, audio_files_paths, audio_file_selected], outputs = [audio_files_paths, audio_file_selected, audio_gallery_refresh_trigger, video_info, audio_buttons_row] )video_info_to_audio_guide_btn.click(fn=audio_to_source_set, inputs =[state, audio_files_paths, audio_file_selected, gr.State("Audio Source")], outputs = [audio_guide] )video_info_to_audio_guide2_btn.click(fn=audio_to_source_set, inputs =[state, audio_files_paths, audio_file_selected, gr.State("Audio Source 2")], outputs = [audio_guide] )video_info_to_audio_source_btn.click(fn=audio_to_source_set, inputs =[state, audio_files_paths, audio_file_selected, gr.State("Custom Audio")], outputs = [audio_source] )