From e53156f4f25d07770a07aad8d9d0b539acbe1c6c Mon Sep 17 00:00:00 2001 From: linyq Date: Fri, 3 Apr 2026 12:57:24 +0800 Subject: [PATCH] fix(documentary): normalize streamlit progress values --- .gitignore | 1 + tests/test_generate_script_docu_unittest.py | 19 +++++++++++++++++++ webui/tools/generate_script_docu.py | 18 ++++++++++++++++-- 3 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/test_generate_script_docu_unittest.py diff --git a/.gitignore b/.gitignore index 4032202..a1c25bd 100644 --- a/.gitignore +++ b/.gitignore @@ -50,3 +50,4 @@ tests/* !tests/test_video_processor_documentary_unittest.py !tests/test_script_service_documentary_unittest.py !tests/test_generate_narration_script_documentary_unittest.py +!tests/test_generate_script_docu_unittest.py diff --git a/tests/test_generate_script_docu_unittest.py b/tests/test_generate_script_docu_unittest.py new file mode 100644 index 0000000..6431ae5 --- /dev/null +++ b/tests/test_generate_script_docu_unittest.py @@ -0,0 +1,19 @@ +import unittest + +from webui.tools.generate_script_docu import _normalize_progress_value + + +class GenerateScriptDocuProgressTests(unittest.TestCase): + def test_normalize_progress_rounds_percentage_float_to_valid_streamlit_int(self): + self.assertEqual(43, _normalize_progress_value(43.125)) + + def test_normalize_progress_converts_ratio_float_to_percentage_int(self): + self.assertEqual(43, _normalize_progress_value(0.43125)) + + def test_normalize_progress_clamps_out_of_range_values(self): + self.assertEqual(0, _normalize_progress_value(-5)) + self.assertEqual(100, _normalize_progress_value(101)) + + +if __name__ == "__main__": + unittest.main() diff --git a/webui/tools/generate_script_docu.py b/webui/tools/generate_script_docu.py index 77c322d..b366156 100644 --- a/webui/tools/generate_script_docu.py +++ b/webui/tools/generate_script_docu.py @@ -11,6 +11,19 @@ from app.config import config from app.services.documentary.frame_analysis_service import DocumentaryFrameAnalysisService +def _normalize_progress_value(progress: float | int) -> int: + """Normalize mixed progress inputs to Streamlit's 0-100 integer range.""" + try: + value = float(progress) + except (TypeError, ValueError): + return 0 + + if 0.0 <= value <= 1.0: + value *= 100 + + return max(0, min(100, int(round(value)))) + + def generate_script_docu(params): """ 生成纪录片视频脚本。 @@ -21,11 +34,12 @@ def generate_script_docu(params): status_text = st.empty() def update_progress(progress: float, message: str = ""): - progress_bar.progress(progress) + normalized_progress = _normalize_progress_value(progress) + progress_bar.progress(normalized_progress) if message: status_text.text(f"🎬 {message}") else: - status_text.text(f"📊 进度: {progress}%") + status_text.text(f"📊 进度: {normalized_progress}%") try: with st.spinner("正在生成脚本..."):