Skip to content

利用 AI 大模型和自动化工作流,根据主题或关键词一键生成高清短视频。Generate HD short videos from a topic or keyword with an automated AI workflow.

Add CI and targeted quality fixes (#1031)

010101:)a760e353211e
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
new file mode 100644
index 00000000..3a4703cd
--- /dev/null
+++ b/.github/workflows/ci.yml
@@ -0,0 +1,35 @@
+name: CI
+
+on:
+ pull_request:
+ push:
+ branches:
+ - main
+
+jobs:
+ tests:
+ name: Python tests
+ runs-on: ubuntu-latest
+
+ steps:
+ - name: Check out repository
+ uses: actions/checkout@v4
+
+ - name: Set up Python
+ uses: actions/setup-python@v5
+ with:
+ python-version: "3.11"
+
+ - name: Install uv
+ uses: astral-sh/setup-uv@v6
+ with:
+ enable-cache: true
+
+ - name: Install dependencies
+ run: uv sync --frozen
+
+ - name: Compile Python sources
+ run: uv run python -m compileall app cli.py main.py webui test
+
+ - name: Run deterministic unit tests
+ run: uv run python -X utf8 -m unittest test.services.test_state test.services.test_task test.services.test_schema test.services.test_webui_i18n
diff --git a/app/models/schema.py b/app/models/schema.py
index 967aabb6..7b5ee915 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -35,13 +35,13 @@ class VideoAspect(str, Enum):
square = "1:1"
def to_resolution(self):
- if self == VideoAspect.landscape.value:
+ if self == VideoAspect.landscape:
return 1920, 1080
- elif self == VideoAspect.portrait.value:
+ elif self == VideoAspect.portrait:
return 1080, 1920
- elif self == VideoAspect.square.value:
+ elif self == VideoAspect.square:
return 1080, 1080
- return 1080, 1920
+ raise ValueError(f"unsupported video aspect: {self}")
class _Config:
diff --git a/app/services/state.py b/app/services/state.py
index 3e6b98d4..c34e9f83 100644
--- a/app/services/state.py
+++ b/app/services/state.py
@@ -1,4 +1,6 @@
import ast
+import copy
+import threading
from abc import ABC, abstractmethod
from app.config import config
@@ -24,12 +26,14 @@ class BaseState(ABC):
class MemoryState(BaseState):
def __init__(self):
self._tasks = {}
+ self._lock = threading.RLock()
def get_all_tasks(self, page: int, page_size: int):
start = (page - 1) * page_size
end = start + page_size
- tasks = list(self._tasks.values())
- total = len(tasks)
+ with self._lock:
+ tasks = [copy.deepcopy(task) for task in self._tasks.values()]
+ total = len(tasks)
return tasks[start:end], total
def update_task(
@@ -43,23 +47,36 @@ class MemoryState(BaseState):
if progress > 100:
progress = 100
- self._tasks[task_id] = {
- "task_id": task_id,
- "state": state,
- "progress": progress,
- **kwargs,
- }
+ with self._lock:
+ self._tasks[task_id] = {
+ "task_id": task_id,
+ "state": state,
+ "progress": progress,
+ **kwargs,
+ }
def get_task(self, task_id: str):
- return self._tasks.get(task_id, None)
+ with self._lock:
+ task = self._tasks.get(task_id, None)
+ return copy.deepcopy(task) if task is not None else None
def delete_task(self, task_id: str):
- if task_id in self._tasks:
- del self._tasks[task_id]
+ with self._lock:
+ self._tasks.pop(task_id, None)
# Redis state management
class RedisState(BaseState):
+ """
+ Redis-backed task state.
+
+ Trust boundary: Redis is expected to be private to this application. Task
+ values are written by MoneyPrinterTurbo and converted back from strings for
+ compatibility with existing state records. Do not expose this Redis database
+ to untrusted writers without replacing deserialization with a stricter
+ schema-based format.
+ """
+
def __init__(self, host="localhost", port=6379, db=0, password=None):
import redis
@@ -135,8 +152,12 @@ class RedisState(BaseState):
@staticmethod
def _convert_to_original_type(value):
"""
- Convert the value from byte string to its original data type.
- You can extend this method to handle other data types as needed.
+ Convert values written by this application back to common Python types.
+
+ This compatibility parser assumes Redis is inside the application's
+ trust boundary. If Redis can be written by untrusted clients, task state
+ should move to a strict JSON/schema parser instead of open-ended literal
+ conversion.
"""
value_str = value.decode("utf-8")
diff --git a/app/services/task.py b/app/services/task.py
index a87be8f0..324a57f3 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -10,7 +10,7 @@ from app.models import const
from app.models.schema import VideoConcatMode, VideoParams
from app.services import llm, material, subtitle, video, voice, upload_post
from app.services import state as sm
-from app.utils import utils
+from app.utils import file_security, utils
def generate_script(task_id, params):
@@ -78,6 +78,45 @@ def save_script_data(task_id, video_script, video_terms, params):
f.write(utils.to_json(script_data))
+def resolve_custom_audio_file(task_id: str, custom_audio_file: str | None) -> str:
+ requested_file = (custom_audio_file or "").strip()
+ if not requested_file:
+ return ""
+
+ task_dir = utils.task_dir(task_id)
+ try:
+ return file_security.resolve_path_within_directory(
+ task_dir,
+ requested_file,
+ )
+ except ValueError as exc:
+ task_dir_error = exc
+
+ server_audio_file = path.realpath(
+ requested_file
+ if path.isabs(requested_file)
+ else path.join(utils.root_dir(), requested_file)
+ )
+ if not path.isabs(requested_file):
+ project_root = path.realpath(utils.root_dir())
+ try:
+ if path.commonpath([project_root, server_audio_file]) != project_root:
+ raise ValueError(
+ "relative custom audio paths must stay within the project directory"
+ )
+ except ValueError as exc:
+ raise ValueError(
+ "custom audio file must be task-local or an existing server-side file"
+ ) from exc
+
+ if not path.isfile(server_audio_file):
+ raise ValueError(
+ "custom audio file does not exist or is not a file"
+ ) from task_dir_error
+
+ return server_audio_file
+
+
def generate_audio(task_id, params, video_script):
'''
Generate audio for the video script.
@@ -92,14 +131,21 @@ def generate_audio(task_id, params, video_script):
logger.info("\n\n## generating audio")
# /audio 和 /subtitle 请求模型不包含 custom_audio_file,
# 这里统一做兼容读取,避免直调接口时抛属性错误。
- custom_audio_file = getattr(params, "custom_audio_file", None)
- if not custom_audio_file or not os.path.exists(custom_audio_file):
- if custom_audio_file:
- logger.warning(
- f"custom audio file not found: {custom_audio_file}, using TTS to generate audio."
- )
- else:
- logger.info("no custom audio file provided, using TTS to generate audio.")
+ requested_custom_audio_file = getattr(params, "custom_audio_file", None)
+ try:
+ custom_audio_file = resolve_custom_audio_file(
+ task_id, requested_custom_audio_file
+ )
+ except ValueError as exc:
+ logger.error(
+ "custom audio file is invalid, "
+ f"task_id: {task_id}, path: {requested_custom_audio_file}, error: {str(exc)}"
+ )
+ sm.state.update_task(task_id, state=const.TASK_STATE_FAILED)
+ return None, None, None
+
+ if not custom_audio_file:
+ logger.info("no custom audio file provided, using TTS to generate audio.")
audio_file = path.join(utils.task_dir(task_id), "audio.mp3")
sub_maker = voice.tts(
text=video_script,
diff --git a/config.example.toml b/config.example.toml
index 8a7433ec..9964ff54 100644
--- a/config.example.toml
+++ b/config.example.toml
@@ -288,7 +288,10 @@ endpoint = ""
material_directory = ""
-# Used for state management of the task
+# Used for state management of the task.
+# Redis is expected to be private to this app. Do not expose it to untrusted
+# writers, because task state is deserialized for compatibility with existing
+# records.
enable_redis = false
redis_host = "localhost"
redis_port = 6379
diff --git a/test/README.md b/test/README.md
index 1deb3a9a..967f4be9 100644
--- a/test/README.md
+++ b/test/README.md
@@ -27,6 +27,10 @@ python -m unittest test.services.test_video.TestVideoService
python -m unittest test.services.test_video.TestVideoService.test_preprocess_video
````
+Live provider tests are skipped by default. To run tests that may call external
+TTS or LLM services, set `MPT_RUN_INTEGRATION_TESTS=1` and provide the required
+provider credentials.
+
## Adding New Tests
To add tests for other components, follow these guidelines:
@@ -37,4 +41,4 @@ To add tests for other components, follow these guidelines:
## Test Resources
-Place any resource files required for testing in the `test/resources` directory.
\ No newline at end of file
+Place any resource files required for testing in the `test/resources` directory.
diff --git a/test/services/test_llm.py b/test/services/test_llm.py
index b53661ac..b446df69 100644
--- a/test/services/test_llm.py
+++ b/test/services/test_llm.py
@@ -14,6 +14,12 @@ from app.config import config
from app.models.schema import VideoScriptRequest, VideoSocialMetadataRequest
from app.services import llm
+RUN_INTEGRATION_TESTS = os.environ.get("MPT_RUN_INTEGRATION_TESTS", "").lower() in {
+ "1",
+ "true",
+ "yes",
+}
+
class TestScriptPromptOptions(unittest.TestCase):
def test_normalize_text_response_removes_think_blocks(self):
@@ -900,7 +906,10 @@ FOUNDRY_BASE = "https://amanrai-test-resource.services.ai.azure.com/anthropic"
FOUNDRY_MODEL = "azure_ai/claude-sonnet-4-6"
[email protected](FOUNDRY_KEY, "ANTHROPIC_FOUNDRY_API_KEY not set")
+ RUN_INTEGRATION_TESTS and FOUNDRY_KEY,
+ "MPT_RUN_INTEGRATION_TESTS and ANTHROPIC_FOUNDRY_API_KEY not set",
+)
class TestLiteLLMLiveIntegration(unittest.TestCase):
def setUp(self):
self.original_app_config = dict(config.app)
diff --git a/test/services/test_schema.py b/test/services/test_schema.py
new file mode 100644
index 00000000..ec8b0a30
--- /dev/null
+++ b/test/services/test_schema.py
@@ -0,0 +1,22 @@
+import sys
+import unittest
+from pathlib import Path
+
+sys.path.insert(0, str(Path(__file__).parent.parent.parent))
+
+from app.models.schema import VideoAspect
+
+
+class TestVideoAspect(unittest.TestCase):
+ def test_to_resolution_known_aspects(self):
+ self.assertEqual(VideoAspect.landscape.to_resolution(), (1920, 1080))
+ self.assertEqual(VideoAspect.portrait.to_resolution(), (1080, 1920))
+ self.assertEqual(VideoAspect.square.to_resolution(), (1080, 1080))
+
+ def test_to_resolution_rejects_unsupported_value(self):
+ with self.assertRaises(ValueError):
+ VideoAspect.to_resolution("4:5")
+
+
+if __name__ == "__main__":
+ unittest.main()
diff --git a/test/services/test_state.py b/test/services/test_state.py
index dcb67cef..5cd8a9f4 100644
--- a/test/services/test_state.py
+++ b/test/services/test_state.py
@@ -1,10 +1,12 @@
import sys
+import threading
import unittest
from pathlib import Path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
-from app.services.state import RedisState
+from app.models import const
+from app.services.state import MemoryState, RedisState
class _FakeRedis:
@@ -30,6 +32,53 @@ class _FakeRedis:
return self.data[key]
+class TestMemoryState(unittest.TestCase):
+ def test_get_task_and_get_all_tasks_return_isolated_snapshots(self):
+ state = MemoryState()
+ state.update_task(
+ "task-1",
+ state=const.TASK_STATE_PROCESSING,
+ progress=25,
+ videos=["first.mp4"],
+ )
+
+ task = state.get_task("task-1")
+ task["videos"].append("mutated.mp4")
+
+ tasks, total = state.get_all_tasks(page=1, page_size=10)
+ tasks[0]["videos"].append("mutated-again.mp4")
+
+ self.assertEqual(total, 1)
+ self.assertEqual(state.get_task("task-1")["videos"], ["first.mp4"])
+
+ def test_concurrent_memory_updates_are_preserved(self):
+ state = MemoryState()
+ thread_count = 5
+ tasks_per_thread = 50
+
+ def update_tasks(thread_index):
+ for task_index in range(tasks_per_thread):
+ state.update_task(
+ f"task-{thread_index}-{task_index}",
+ state=const.TASK_STATE_PROCESSING,
+ progress=task_index,
+ )
+
+ threads = [
+ threading.Thread(target=update_tasks, args=(thread_index,))
+ for thread_index in range(thread_count)
+ ]
+ for thread in threads:
+ thread.start()
+ for thread in threads:
+ thread.join()
+
+ tasks, total = state.get_all_tasks(page=1, page_size=thread_count * tasks_per_thread)
+
+ self.assertEqual(total, thread_count * tasks_per_thread)
+ self.assertEqual(len(tasks), total)
+
+
class TestRedisState(unittest.TestCase):
def _build_state(self, batch_sizes):
keys = [f"task:{i}".encode("utf-8") for i in range(sum(batch_sizes))]
diff --git a/test/services/test_task.py b/test/services/test_task.py
index 35d49cdc..13cd27d1 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -1,6 +1,8 @@
import unittest
import os
+import shutil
import sys
+import tempfile
from pathlib import Path
from unittest.mock import patch
@@ -9,8 +11,14 @@ sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from app.services import task as tm
from app.models.schema import MaterialInfo, VideoParams
+from app.utils import utils
resources_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "resources")
+RUN_INTEGRATION_TESTS = os.environ.get("MPT_RUN_INTEGRATION_TESTS", "").lower() in {
+ "1",
+ "true",
+ "yes",
+}
class TestTaskService(unittest.TestCase):
def setUp(self):
@@ -67,6 +75,98 @@ class TestTaskService(unittest.TestCase):
match_script_order=True,
)
+ def test_generate_audio_uses_custom_file_inside_task_directory(self):
+ task_id = "test-custom-audio-safe"
+ task_dir = utils.task_dir(task_id)
+ custom_audio_file = os.path.join(task_dir, "custom-audio.mp3")
+ with open(custom_audio_file, "wb") as audio:
+ audio.write(b"fake audio")
+
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="",
+ custom_audio_file=custom_audio_file,
+ voice_name="test-voice",
+ )
+
+ try:
+ with (
+ patch.object(tm.voice, "tts") as tts,
+ patch.object(tm.voice, "get_audio_duration", return_value=7),
+ ):
+ audio_file, audio_duration, sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertEqual(audio_file, os.path.realpath(custom_audio_file))
+ self.assertEqual(audio_duration, 7)
+ self.assertIsNone(sub_maker)
+ tts.assert_not_called()
+
+ def test_generate_audio_accepts_server_side_custom_file(self):
+ task_id = "test-custom-audio-server-side"
+ task_dir = utils.task_dir(task_id)
+
+ with tempfile.NamedTemporaryFile(suffix=".mp3") as server_audio:
+ server_audio.write(b"fake audio")
+ server_audio.flush()
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="",
+ custom_audio_file=server_audio.name,
+ voice_name="test-voice",
+ )
+
+ try:
+ with (
+ patch.object(tm.voice, "tts") as tts,
+ patch.object(tm.voice, "get_audio_duration", return_value=6),
+ ):
+ audio_file, audio_duration, result_sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertEqual(audio_file, os.path.realpath(server_audio.name))
+ self.assertEqual(audio_duration, 6)
+ self.assertIsNone(result_sub_maker)
+ tts.assert_not_called()
+
+ def test_generate_audio_rejects_missing_custom_file_without_tts(self):
+ task_id = "test-custom-audio-missing"
+ task_dir = utils.task_dir(task_id)
+ missing_audio_file = os.path.join(task_dir, "missing.mp3")
+ params = VideoParams(
+ video_subject="custom audio",
+ video_script="",
+ custom_audio_file=missing_audio_file,
+ voice_name="test-voice",
+ )
+
+ try:
+ with (
+ patch.object(tm.voice, "tts") as tts,
+ patch.object(tm.sm.state, "update_task") as update_task,
+ ):
+ audio_file, audio_duration, result_sub_maker = tm.generate_audio(
+ task_id, params, "script"
+ )
+ finally:
+ shutil.rmtree(task_dir, ignore_errors=True)
+
+ self.assertIsNone(audio_file)
+ self.assertIsNone(audio_duration)
+ self.assertIsNone(result_sub_maker)
+ tts.assert_not_called()
+ update_task.assert_called_with(task_id, state=tm.const.TASK_STATE_FAILED)
+
+ @unittest.skipUnless(
+ RUN_INTEGRATION_TESTS,
+ "MPT_RUN_INTEGRATION_TESTS not set",
+ )
def test_task_local_materials(self):
task_id = "00000000-0000-0000-0000-000000000000"
video_materials=[]
diff --git a/test/services/test_voice.py b/test/services/test_voice.py
index 6e18904f..a7814fe3 100644
--- a/test/services/test_voice.py
+++ b/test/services/test_voice.py
@@ -1,5 +1,6 @@
import asyncio
import base64
+import os
import unittest
import sys
import tempfile
@@ -36,6 +37,11 @@ text_zh = """
voice_rate=1.0
voice_volume=1.0
+RUN_INTEGRATION_TESTS = os.environ.get("MPT_RUN_INTEGRATION_TESTS", "").lower() in {
+ "1",
+ "true",
+ "yes",
+}
class TestVoiceService(unittest.TestCase):
def setUp(self):
@@ -153,6 +159,10 @@ class TestVoiceService(unittest.TestCase):
self.assertIs(result, sentinel)
azure_tts_v1.assert_called_once()
+ @unittest.skipUnless(
+ RUN_INTEGRATION_TESTS,
+ "MPT_RUN_INTEGRATION_TESTS not set",
+ )
def test_siliconflow(self):
# SiliconFlow 的 API Key 存在 [siliconflow].api_key 中,运行时代码也是从
# config.siliconflow 读取;这里必须使用同一配置源,避免正确配置凭据时
@@ -187,6 +197,10 @@ class TestVoiceService(unittest.TestCase):
self.loop.run_until_complete(_do())
+ @unittest.skipUnless(
+ RUN_INTEGRATION_TESTS,
+ "MPT_RUN_INTEGRATION_TESTS not set",
+ )
def test_azure_tts_v1(self):
voice_name = "zh-CN-XiaoyiNeural-Female"
voice_name = vs.parse_voice_name(voice_name)
@@ -305,6 +319,10 @@ class TestVoiceService(unittest.TestCase):
self.assertIsNone(sub_maker)
self.assertLess(elapsed, 2)
+ @unittest.skipUnless(
+ RUN_INTEGRATION_TESTS,
+ "MPT_RUN_INTEGRATION_TESTS not set",
+ )
def test_azure_tts_v2(self):
if not vs.config.azure.get("speech_key") or not vs.config.azure.get("speech_region"):
self.skipTest("Azure speech key or region is not configured")
diff --git a/webui/i18n/ru.json b/webui/i18n/ru.json
index 83228684..048f2482 100644
--- a/webui/i18n/ru.json
+++ b/webui/i18n/ru.json
@@ -31,6 +31,8 @@
"Video Concat Mode": "Порядок склейки фрагментов",
"Random": "Случайный порядок (рекомендуется)",
"Sequential": "По порядку",
+ "Match Materials to Script Order": "Подбирать материалы по порядку сценария",
+ "Match Materials to Script Order Help": "По умолчанию выключено. В основном применяется к онлайн-источникам материалов. Если включено, ключевые слова генерируются в порядке сценария, а материалы загружаются и объединяются в этом порядке, чтобы уменьшить расхождение изображения с текущей озвучкой.",
"Video Transition Mode": "Переходы между фрагментами",
"None": "Без перехода",
"Shuffle": "Случайный переход",