From 31fb97f761020195a3783d7e0da313373c809cfb Mon Sep 17 00:00:00 2001 From: zhifu gao Date: Wed, 2 Dec 2026 14:01:49 +0800 Subject: [PATCH] Fix Gradio audio clipping for empty selections and AI timestamps Signed-off-by: LauraGPT --- funclip/launch.py | 25 ++++++++------ tests/test_gradio_runtime_compat.py | 52 +++++++++++++++++++++++------ 2 files changed, 56 insertions(+), 21 deletions(-) diff --git a/funclip/launch.py b/funclip/launch.py index 80b0973..4f521e9 100644 --- a/funclip/launch.py +++ b/funclip/launch.py @@ -123,6 +123,17 @@ def mix_recog_speaker(video_input, audio_input, hotwords, output_dir): srt_file = save_text_to_file(res_srt, 'srt', output_dir) return res_text, res_srt, None, audio_state, text_file, srt_file + def clip_audio(dest_text, speaker, start_ost, end_ost, state, output_dir, ranges_ms=None): + samples = None + if ranges_ms is not None: + rate = state['audio_input'][0] + samples = [[start * rate // 1000, end * rate // 1000] + for start, end in ranges_ms if start < end] + audio, message, subtitles = audio_clipper.clip( + dest_text, start_ost, end_ost, state, dest_spk=speaker, output_dir=output_dir, + timestamp_list=samples, require_match=True) + return None, audio, message, subtitles + def mix_clip(dest_text, video_spk_input, start_ost, end_ost, video_state, audio_state, output_dir): output_dir = output_dir.strip() if not len(output_dir): @@ -134,9 +145,7 @@ def mix_clip(dest_text, video_spk_input, start_ost, end_ost, video_state, audio_ dest_text, start_ost, end_ost, video_state, dest_spk=video_spk_input, output_dir=output_dir) return clip_video_file, None, message, clip_srt if audio_state is not None: - (sr, res_audio), message, clip_srt = audio_clipper.clip( - dest_text, start_ost, end_ost, audio_state, dest_spk=video_spk_input, output_dir=output_dir) - return None, (sr, res_audio), message, clip_srt + return clip_audio(dest_text, video_spk_input, start_ost, end_ost, audio_state, output_dir) def video_clip_addsub(dest_text, video_spk_input, start_ost, end_ost, state, output_dir, font_size, font_color): output_dir = output_dir.strip() @@ -185,10 +194,7 @@ def AI_clip(LLM_res, dest_text, video_spk_input, start_ost, end_ost, video_state dest_spk=video_spk_input, output_dir=output_dir, timestamp_list=timestamp_list, add_sub=False) return clip_video_file, None, message, clip_srt if audio_state is not None: - (sr, res_audio), message, clip_srt = audio_clipper.clip( - dest_text, start_ost, end_ost, audio_state, - dest_spk=video_spk_input, output_dir=output_dir, timestamp_list=timestamp_list, add_sub=False) - return None, (sr, res_audio), message, clip_srt + return clip_audio(dest_text, video_spk_input, start_ost, end_ost, audio_state, output_dir, timestamp_list) def AI_clip_subti(LLM_res, dest_text, video_spk_input, start_ost, end_ost, video_state, audio_state, output_dir): timestamp_list = extract_timestamps(LLM_res) @@ -203,10 +209,7 @@ def AI_clip_subti(LLM_res, dest_text, video_spk_input, start_ost, end_ost, video dest_spk=video_spk_input, output_dir=output_dir, timestamp_list=timestamp_list, add_sub=True) return clip_video_file, None, message, clip_srt if audio_state is not None: - (sr, res_audio), message, clip_srt = audio_clipper.clip( - dest_text, start_ost, end_ost, audio_state, - dest_spk=video_spk_input, output_dir=output_dir, timestamp_list=timestamp_list, add_sub=True) - return None, (sr, res_audio), message, clip_srt + return clip_audio(dest_text, video_spk_input, start_ost, end_ost, audio_state, output_dir, timestamp_list) # gradio interface theme = gr.Theme.load("funclip/utils/theme.json") diff --git a/tests/test_gradio_runtime_compat.py b/tests/test_gradio_runtime_compat.py index 60318a9..47a7349 100644 --- a/tests/test_gradio_runtime_compat.py +++ b/tests/test_gradio_runtime_compat.py @@ -94,6 +94,20 @@ def test_launcher_does_not_patch_dependencies_or_retry_with_public_share(): @pytest.mark.parametrize("options, expected_sizes", [([], [3, 1, 1]), (["--no-examples"], [])]) def test_local_upload_and_optional_demo_gallery(monkeypatch, options, expected_sizes): + _, config, gallery_sizes = _load_launcher(monkeypatch, options) + assert gallery_sizes == expected_sizes + labels = {c["id"]: c["props"].get("label") or c["props"].get("value") for c in config["components"]} + assert any("Video Input" in str(label) for label in labels.values()) + assert any("Audio Input" in str(label) for label in labels.values()) + for button, output in ((" | ASR", "Recognition Result"), (" | Clip", "Video Clipped")): + assert any( + any(event == "click" and str(labels.get(i)).endswith(button) for i, event in dep["targets"]) + and any(output in str(labels[i]) for i in dep["outputs"]) + for dep in config["dependencies"] + ) + + +def _load_launcher(monkeypatch, options): monkeypatch.setenv("GRADIO_ANALYTICS_ENABLED", "False") import gradio as gr @@ -125,15 +139,33 @@ def launch(blocks, **kwargs): monkeypatch.chdir(ROOT) monkeypatch.syspath_prepend(str(LAUNCH_PATH.parent)) monkeypatch.setattr(sys, "argv", [str(LAUNCH_PATH), *options]) - runpy.run_path(str(LAUNCH_PATH), run_name="__main__") - assert gallery_sizes == expected_sizes + namespace = runpy.run_path(str(LAUNCH_PATH), run_name="__main__") config, = configs - labels = {c["id"]: c["props"].get("label") or c["props"].get("value") for c in config["components"]} - assert any("Video Input" in str(label) for label in labels.values()) - assert any("Audio Input" in str(label) for label in labels.values()) - for button, output in ((" | ASR", "Recognition Result"), (" | Clip", "Video Clipped")): - assert any( - any(event == "click" and str(labels.get(i)).endswith(button) for i, event in dep["targets"]) - and any(output in str(labels[i]) for i in dep["outputs"]) - for dep in config["dependencies"] + return namespace, config, gallery_sizes + + +@pytest.mark.parametrize("callback", ["mix_clip", "AI_clip", "AI_clip_subti"]) +def test_audio_callbacks_clear_empty_results_and_use_sample_indices(monkeypatch, callback): + ns, _, _ = _load_launcher(monkeypatch, ["--no-examples"]) + state = {"audio_input": (16000, object())} + selected = (16000, object()) + calls = [] + + def clip(text, start, end, audio_state, dest_spk=None, output_dir=None, + timestamp_list=None, *, require_match=False): + assert require_match and audio_state is state + assert (start, end, dest_spk, output_dir) == (0, 100, "", None) + calls.append(timestamp_list) + if timestamp_list == [] or (timestamp_list is None and not text): + return None, "No audio selected", "" + return selected, "Selected", "SRT" + + ns["audio_clipper"].clip.side_effect = clip + for valid in (True, False): + args = ["text" if valid else "", "", 0, 100, None, state, ""] + if callback != "mix_clip": + args.insert(0, "[00:00:01,000-00:00:02,000]" if valid else "no time range") + assert ns[callback](*args) == ( + (None, selected, "Selected", "SRT") if valid else (None, None, "No audio selected", "") ) + assert calls == ([None, None] if callback == "mix_clip" else [[[16000, 32000]], []])