mirror of
https://github.com/linyqh/NarratoAI.git
synced 2026-05-01 06:08:16 +00:00
fix(documentary): normalize streamlit progress values
This commit is contained in:
parent
abc9db22e5
commit
e53156f4f2
1
.gitignore
vendored
1
.gitignore
vendored
@ -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
|
||||
|
||||
19
tests/test_generate_script_docu_unittest.py
Normal file
19
tests/test_generate_script_docu_unittest.py
Normal file
@ -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()
|
||||
@ -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("正在生成脚本..."):
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user