Skip to content

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

Add advanced script prompt settings

harry0703559b9eeb27a0
diff --git a/app/controllers/v1/llm.py b/app/controllers/v1/llm.py
index 54db26d0..177dde88 100644
--- a/app/controllers/v1/llm.py
+++ b/app/controllers/v1/llm.py
@@ -25,6 +25,8 @@ def generate_video_script(request: Request, body: VideoScriptRequest):
video_subject=body.video_subject,
language=body.video_language,
paragraph_number=body.paragraph_number,
+ video_script_prompt=body.video_script_prompt,
+ custom_system_prompt=body.custom_system_prompt,
)
response = {"video_script": video_script}
return utils.get_response(200, response)
diff --git a/app/models/schema.py b/app/models/schema.py
index 08c47d59..ef7958f9 100644
--- a/app/models/schema.py
+++ b/app/models/schema.py
@@ -3,7 +3,7 @@ from enum import Enum
from typing import Any, List, Optional, Union
import pydantic
-from pydantic import BaseModel
+from pydantic import BaseModel, Field
from app.config import config
@@ -105,7 +105,9 @@ class VideoParams(BaseModel):
stroke_color: Optional[str] = "#000000"
stroke_width: float = 1.5
n_threads: Optional[int] = 2
- paragraph_number: Optional[int] = 1
+ paragraph_number: int = Field(default=1, ge=1, le=10)
+ video_script_prompt: str = Field(default="", max_length=2000)
+ custom_system_prompt: str = Field(default="", max_length=8000)
class SubtitleRequest(BaseModel):
@@ -145,13 +147,17 @@ class VideoScriptParams:
{
"video_subject": "春天的花海",
"video_language": "",
- "paragraph_number": 1
+ "paragraph_number": 1,
+ "video_script_prompt": "",
+ "custom_system_prompt": ""
}
"""
video_subject: Optional[str] = "春天的花海"
video_language: Optional[str] = ""
- paragraph_number: Optional[int] = 1
+ paragraph_number: int = Field(default=1, ge=1, le=10)
+ video_script_prompt: str = Field(default="", max_length=2000)
+ custom_system_prompt: str = Field(default="", max_length=8000)
class VideoTermsParams:
@@ -333,4 +339,4 @@ class VideoMaterialUploadResponse(BaseResponse):
"file": "/MoneyPrinterTurbo/resource/videos/example.mp4",
},
},
- }
\ No newline at end of file
+ }
diff --git a/app/services/llm.py b/app/services/llm.py
index 243f917f..9630eaaf 100644
--- a/app/services/llm.py
+++ b/app/services/llm.py
@@ -13,6 +13,27 @@ from app.config import config
_max_retries = 5
_DEFAULT_GEMINI_MODEL = "gemini-2.5-flash"
_DEPRECATED_GEMINI_MODELS = {"gemini-pro", "gemini-1.0-pro"}
+MIN_SCRIPT_PARAGRAPH_NUMBER = 1
+MAX_SCRIPT_PARAGRAPH_NUMBER = 10
+MAX_SCRIPT_PROMPT_LENGTH = 2000
+MAX_SCRIPT_SYSTEM_PROMPT_LENGTH = 8000
+
+DEFAULT_SCRIPT_SYSTEM_PROMPT = """
+# Role: Video Script Generator
+
+## Goals:
+Generate a script for a video, depending on the subject of the video.
+
+## Constrains:
+1. the script is to be returned as a string with the specified number of paragraphs.
+2. do not under any circumstance reference this prompt in your response.
+3. get straight to the point, don't start with unnecessary things like, "welcome to this video".
+4. you must not include any type of markdown or formatting in the script, never use a title.
+5. only return the raw content of the script.
+6. do not include "voiceover", "narrator" or similar indicators of what should be spoken at the beginning of each paragraph or line.
+7. you must not mention the prompt, or anything about the script itself. also, never talk about the amount of paragraphs or lines. just write the script.
+8. respond in the same language as the video subject.
+""".strip()
def _normalize_text_response(content, llm_provider: str) -> str:
@@ -470,34 +491,102 @@ def _generate_response(prompt: str) -> str:
return f"Error: {str(e)}"
-def generate_script(
- video_subject: str, language: str = "", paragraph_number: int = 1
-) -> str:
- prompt = f"""
-# Role: Video Script Generator
+def _limit_script_text(text: str | None, max_length: int, field_name: str) -> str:
+ value = (text or "").strip()
+ if len(value) <= max_length:
+ return value
-## Goals:
-Generate a script for a video, depending on the subject of the video.
+ # API 层已经用 Pydantic 做长度校验;这里继续兜底,是为了保护
+ # WebUI 或内部服务直接调用 generate_script 时不会把超长提示词发送给模型,
+ # 避免 token 成本异常和请求失败。
+ logger.warning(
+ f"{field_name} is too long and will be truncated to {max_length} characters."
+ )
+ return value[:max_length]
-## Constrains:
-1. the script is to be returned as a string with the specified number of paragraphs.
-2. do not under any circumstance reference this prompt in your response.
-3. get straight to the point, don't start with unnecessary things like, "welcome to this video".
-4. you must not include any type of markdown or formatting in the script, never use a title.
-5. only return the raw content of the script.
-6. do not include "voiceover", "narrator" or similar indicators of what should be spoken at the beginning of each paragraph or line.
-7. you must not mention the prompt, or anything about the script itself. also, never talk about the amount of paragraphs or lines. just write the script.
-8. respond in the same language as the video subject.
+
+def _normalize_script_paragraph_number(paragraph_number: int | None) -> int:
+ try:
+ value = int(paragraph_number or MIN_SCRIPT_PARAGRAPH_NUMBER)
+ except (TypeError, ValueError):
+ value = MIN_SCRIPT_PARAGRAPH_NUMBER
+
+ if value < MIN_SCRIPT_PARAGRAPH_NUMBER or value > MAX_SCRIPT_PARAGRAPH_NUMBER:
+ # WebUI 和 API 都会限制范围;这里兜底处理内部调用,避免异常参数直接扩大
+ # LLM 生成成本或生成空结果。
+ logger.warning(
+ "script paragraph_number is out of range and will be clamped: "
+ f"{value}"
+ )
+ return max(MIN_SCRIPT_PARAGRAPH_NUMBER, min(value, MAX_SCRIPT_PARAGRAPH_NUMBER))
+
+ return value
+
+
+def build_script_prompt(
+ video_subject: str,
+ language: str = "",
+ paragraph_number: int = 1,
+ video_script_prompt: str = "",
+ custom_system_prompt: str = "",
+) -> str:
+ paragraph_number = _normalize_script_paragraph_number(paragraph_number)
+ video_script_prompt = _limit_script_text(
+ video_script_prompt, MAX_SCRIPT_PROMPT_LENGTH, "video_script_prompt"
+ )
+ custom_system_prompt = _limit_script_text(
+ custom_system_prompt, MAX_SCRIPT_SYSTEM_PROMPT_LENGTH, "custom_system_prompt"
+ )
+
+ # 将“脚本生成规则”和“运行时上下文”分开拼接。这样高级用户即使覆盖默认
+ # system prompt,也不会漏掉视频主题、语言、段落数这些每次生成都必须带上的参数。
+ prompt = custom_system_prompt or DEFAULT_SCRIPT_SYSTEM_PROMPT
+ prompt += f"""
# Initialization:
- video subject: {video_subject}
- number of paragraphs: {paragraph_number}
-""".strip()
+""".rstrip()
if language:
prompt += f"\n- language: {language}"
+ if video_script_prompt:
+ prompt += f"""
+
+# Additional User Requirements:
+{video_script_prompt}
+""".rstrip()
+ return prompt
+
+
+def generate_script(
+ video_subject: str,
+ language: str = "",
+ paragraph_number: int = 1,
+ video_script_prompt: str = "",
+ custom_system_prompt: str = "",
+) -> str:
+ paragraph_number = _normalize_script_paragraph_number(paragraph_number)
+ video_script_prompt = _limit_script_text(
+ video_script_prompt, MAX_SCRIPT_PROMPT_LENGTH, "video_script_prompt"
+ )
+ custom_system_prompt = _limit_script_text(
+ custom_system_prompt, MAX_SCRIPT_SYSTEM_PROMPT_LENGTH, "custom_system_prompt"
+ )
+ prompt = build_script_prompt(
+ video_subject=video_subject,
+ language=language,
+ paragraph_number=paragraph_number,
+ video_script_prompt=video_script_prompt,
+ custom_system_prompt=custom_system_prompt,
+ )
final_script = ""
- logger.info(f"subject: {video_subject}")
+ logger.info(
+ "generating video script: "
+ f"subject={video_subject}, paragraph_number={paragraph_number}, "
+ f"has_custom_prompt={bool(video_script_prompt.strip())}, "
+ f"has_custom_system_prompt={bool(custom_system_prompt.strip())}"
+ )
def format_response(response):
# Clean the script
diff --git a/app/services/task.py b/app/services/task.py
index 046dd1f4..ebb3a6d4 100644
--- a/app/services/task.py
+++ b/app/services/task.py
@@ -21,6 +21,8 @@ def generate_script(task_id, params):
video_subject=params.video_subject,
language=params.video_language,
paragraph_number=params.paragraph_number,
+ video_script_prompt=params.video_script_prompt,
+ custom_system_prompt=params.custom_system_prompt,
)
else:
logger.debug(f"video script: \n{video_script}")
diff --git a/test/services/test_llm.py b/test/services/test_llm.py
index 35b1a15a..edf21a83 100644
--- a/test/services/test_llm.py
+++ b/test/services/test_llm.py
@@ -6,12 +6,87 @@ import unittest
from pathlib import Path
from unittest.mock import patch
+from pydantic import ValidationError
+
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
from app.config import config
+from app.models.schema import VideoScriptRequest
from app.services import llm
+class TestScriptPromptOptions(unittest.TestCase):
+ def test_build_script_prompt_appends_advanced_requirements(self):
+ """
+ 高级文案要求只作为附加约束,不替换默认系统提示词。
+ 这样普通用户不配置时仍然走稳定默认规则,高级用户也能细化风格。
+ """
+ prompt = llm.build_script_prompt(
+ video_subject="咖啡",
+ language="zh-CN",
+ paragraph_number=3,
+ video_script_prompt="语气轻松,面向程序员",
+ )
+
+ self.assertIn("# Role: Video Script Generator", prompt)
+ self.assertIn("- video subject: 咖啡", prompt)
+ self.assertIn("- number of paragraphs: 3", prompt)
+ self.assertIn("- language: zh-CN", prompt)
+ self.assertIn("# Additional User Requirements:", prompt)
+ self.assertIn("语气轻松,面向程序员", prompt)
+
+ def test_custom_system_prompt_keeps_runtime_context(self):
+ """
+ 自定义 system prompt 会替换默认脚本规则,但视频主题、语言、段落数
+ 仍由服务层统一追加,避免高级用户漏写必要上下文。
+ """
+ prompt = llm.build_script_prompt(
+ video_subject="露营",
+ language="en",
+ paragraph_number=2,
+ custom_system_prompt="Only write cinematic narration.",
+ )
+
+ self.assertNotIn("# Role: Video Script Generator", prompt)
+ self.assertIn("Only write cinematic narration.", prompt)
+ self.assertIn("- video subject: 露营", prompt)
+ self.assertIn("- number of paragraphs: 2", prompt)
+ self.assertIn("- language: en", prompt)
+
+ def test_generate_script_sends_custom_prompt_to_llm(self):
+ captured = {}
+
+ def fake_generate_response(prompt):
+ captured["prompt"] = prompt
+ return "第一段。\n\n第二段。"
+
+ with patch.object(llm, "_generate_response", side_effect=fake_generate_response):
+ result = llm.generate_script(
+ video_subject="咖啡",
+ language="zh-CN",
+ paragraph_number=2,
+ video_script_prompt="开头更有悬念",
+ )
+
+ self.assertEqual(result, "第一段。\n\n第二段。")
+ self.assertIn("- number of paragraphs: 2", captured["prompt"])
+ self.assertIn("开头更有悬念", captured["prompt"])
+
+ def test_video_script_request_rejects_invalid_advanced_options(self):
+ """
+ API 请求模型需要限制高级 prompt 参数,避免外部调用绕过 WebUI
+ 传入异常段落数或超长提示词,导致模型成本和结果不可控。
+ """
+ with self.assertRaises(ValidationError):
+ VideoScriptRequest(video_subject="咖啡", paragraph_number=0)
+
+ with self.assertRaises(ValidationError):
+ VideoScriptRequest(
+ video_subject="咖啡",
+ video_script_prompt="x" * (llm.MAX_SCRIPT_PROMPT_LENGTH + 1),
+ )
+
+
class TestLiteLLMProvider(unittest.TestCase):
def setUp(self):
self.original_app_config = dict(config.app)
diff --git a/test/services/test_task.py b/test/services/test_task.py
index 8b241a97..a645facd 100644
--- a/test/services/test_task.py
+++ b/test/services/test_task.py
@@ -2,6 +2,7 @@ import unittest
import os
import sys
from pathlib import Path
+from unittest.mock import patch
# add project root to python path
sys.path.insert(0, str(Path(__file__).parent.parent.parent))
@@ -17,6 +18,32 @@ class TestTaskService(unittest.TestCase):
def tearDown(self):
pass
+
+ def test_generate_script_forwards_advanced_prompt_options(self):
+ """
+ 任务生成入口和 WebUI/API 共用 VideoParams。这里验证自动生成文案时,
+ 高级提示词参数会继续传到 LLM 服务层,避免只在 /scripts 接口生效。
+ """
+ params = VideoParams(
+ video_subject="咖啡",
+ video_script="",
+ video_language="zh-CN",
+ paragraph_number=2,
+ video_script_prompt="语气轻松",
+ custom_system_prompt="Only write short narration.",
+ )
+
+ with patch.object(tm.llm, "generate_script", return_value="生成的文案") as generate:
+ result = tm.generate_script("task-id", params)
+
+ self.assertEqual(result, "生成的文案")
+ generate.assert_called_once_with(
+ video_subject="咖啡",
+ language="zh-CN",
+ paragraph_number=2,
+ video_script_prompt="语气轻松",
+ custom_system_prompt="Only write short narration.",
+ )
def test_task_local_materials(self):
task_id = "00000000-0000-0000-0000-000000000000"
@@ -63,4 +90,4 @@ class TestTaskService(unittest.TestCase):
if __name__ == "__main__":
- unittest.main()
\ No newline at end of file
+ unittest.main()
diff --git a/webui.sh b/webui.sh
index 89b30899..fce05054 100644
--- a/webui.sh
+++ b/webui.sh
@@ -1,3 +1,5 @@
+#!/usr/bin/env sh
+
# If you could not download the model from the official site, you can use the mirror site.
# Just remove the comment of the following line .
# 如果你无法从官方网站下载模型,你可以使用镜像网站。
@@ -5,4 +7,72 @@
# export HF_ENDPOINT=https://hf-mirror.com
-streamlit run ./webui/Main.py --browser.serverAddress="0.0.0.0" --server.enableCORS=True --browser.gatherUsageStats=False
\ No newline at end of file
+CURRENT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
+export PYTHONPATH="$CURRENT_DIR${PYTHONPATH:+:$PYTHONPATH}"
+
+# 0.0.0.0 只能表示“监听所有网卡”,不适合作为浏览器访问地址。
+# macOS/Linux 下浏览器打开 http://0.0.0.0:8501 可能会经过代理或网关,
+# 最终出现 502。默认绑定并打开 127.0.0.1,与 Windows 启动脚本保持一致。
+MPT_WEBUI_HOST="${MPT_WEBUI_HOST:-127.0.0.1}"
+MPT_WEBUI_PORT="${MPT_WEBUI_PORT:-8501}"
+
+if [ -x "$CURRENT_DIR/.venv/bin/python" ]; then
+ PORT_CHECK_CMD="$CURRENT_DIR/.venv/bin/python"
+ set -- "$CURRENT_DIR/.venv/bin/python" -m streamlit
+elif command -v uv >/dev/null 2>&1; then
+ PORT_CHECK_CMD="uv run python"
+ set -- uv run streamlit
+elif command -v streamlit >/dev/null 2>&1; then
+ echo "***** Warning: using streamlit from PATH. If dependencies fail, run 'uv sync --frozen' first. *****"
+ PORT_CHECK_CMD="python3"
+ set -- streamlit
+else
+ echo "***** Neither project Python, uv, nor streamlit was found. Please install dependencies first. *****"
+ exit 1
+fi
+
+find_available_port() {
+ WEBUI_HOST="$MPT_WEBUI_HOST" WEBUI_PORT="$MPT_WEBUI_PORT" "$@" - <<'PY' 2>/dev/null
+import os
+import socket
+import sys
+
+host = os.environ.get("WEBUI_HOST", "127.0.0.1")
+preferred = int(os.environ.get("WEBUI_PORT", "8501"))
+candidates = [preferred] + [port for port in range(8502, 8600) if port != preferred]
+
+for port in candidates:
+ with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
+ try:
+ sock.bind((host, port))
+ except OSError:
+ continue
+ print(port)
+ sys.exit(0)
+
+sys.exit(1)
+PY
+}
+
+# 用 Python 做端口探测,避免依赖 lsof/nc 在不同 macOS/Linux 发行版上的差异。
+# shellcheck disable=SC2086
+SELECTED_WEBUI_PORT=$(find_available_port $PORT_CHECK_CMD)
+
+if [ -z "$SELECTED_WEBUI_PORT" ]; then
+ echo "***** No available WebUI port found in 8501-8599 for $MPT_WEBUI_HOST. *****"
+ exit 1
+fi
+
+if [ "$SELECTED_WEBUI_PORT" != "$MPT_WEBUI_PORT" ]; then
+ echo "***** Port $MPT_WEBUI_PORT is unavailable, using $SELECTED_WEBUI_PORT instead. *****"
+fi
+
+MPT_WEBUI_PORT="$SELECTED_WEBUI_PORT"
+
+echo "***** WebUI address: http://$MPT_WEBUI_HOST:$MPT_WEBUI_PORT *****"
+"$@" run "$CURRENT_DIR/webui/Main.py" \
+ --server.address="$MPT_WEBUI_HOST" \
+ --server.port="$MPT_WEBUI_PORT" \
+ --browser.serverAddress="$MPT_WEBUI_HOST" \
+ --browser.gatherUsageStats=False \
+ --server.enableCORS=True
diff --git a/webui/Main.py b/webui/Main.py
index 8d5e16f0..82492614 100644
--- a/webui/Main.py
+++ b/webui/Main.py
@@ -64,6 +64,12 @@ if "video_script" not in st.session_state:
st.session_state["video_script"] = ""
if "video_terms" not in st.session_state:
st.session_state["video_terms"] = ""
+if "video_script_prompt" not in st.session_state:
+ st.session_state["video_script_prompt"] = ""
+if "custom_system_prompt" not in st.session_state:
+ st.session_state["custom_system_prompt"] = llm.DEFAULT_SCRIPT_SYSTEM_PROMPT
+if "use_custom_system_prompt" not in st.session_state:
+ st.session_state["use_custom_system_prompt"] = False
if "ui_language" not in st.session_state:
st.session_state["ui_language"] = config.ui.get("language", system_locale)
if "local_video_materials" not in st.session_state:
@@ -548,8 +554,7 @@ with left_panel:
st.write(tr("Video Script Settings"))
params.video_subject = st.text_input(
tr("Video Subject"),
- value=st.session_state["video_subject"],
- key="video_subject_input",
+ key="video_subject",
).strip()
video_languages = [
@@ -570,12 +575,49 @@ with left_panel:
)
params.video_language = video_languages[selected_index][1]
+ with st.expander(tr("Advanced Script Settings"), expanded=False):
+ params.paragraph_number = st.slider(
+ tr("Script Paragraph Number"),
+ min_value=llm.MIN_SCRIPT_PARAGRAPH_NUMBER,
+ max_value=llm.MAX_SCRIPT_PARAGRAPH_NUMBER,
+ value=st.session_state.get("paragraph_number_input", 1),
+ key="paragraph_number_input",
+ )
+ params.video_script_prompt = st.text_area(
+ tr("Custom Script Requirements"),
+ height=100,
+ max_chars=llm.MAX_SCRIPT_PROMPT_LENGTH,
+ placeholder=tr("Custom Script Requirements Placeholder"),
+ key="video_script_prompt",
+ ).strip()
+
+ use_custom_system_prompt = st.checkbox(
+ tr("Use Custom System Prompt"),
+ help=tr("Use Custom System Prompt Help"),
+ key="use_custom_system_prompt",
+ )
+
+ if use_custom_system_prompt:
+ custom_system_prompt = st.text_area(
+ tr("Custom System Prompt"),
+ height=240,
+ max_chars=llm.MAX_SCRIPT_SYSTEM_PROMPT_LENGTH,
+ key="custom_system_prompt",
+ ).strip()
+ params.custom_system_prompt = custom_system_prompt
+ else:
+ params.custom_system_prompt = ""
+
if st.button(
tr("Generate Video Script and Keywords"), key="auto_generate_script"
):
with st.spinner(tr("Generating Video Script and Keywords")):
script = llm.generate_script(
- video_subject=params.video_subject, language=params.video_language
+ video_subject=params.video_subject,
+ language=params.video_language,
+ paragraph_number=params.paragraph_number,
+ video_script_prompt=params.video_script_prompt,
+ custom_system_prompt=params.custom_system_prompt,
)
terms = llm.generate_terms(params.video_subject, script)
if "Error: " in script:
diff --git a/webui/i18n/en.json b/webui/i18n/en.json
index 071f8821..dda0ca70 100644
--- a/webui/i18n/en.json
+++ b/webui/i18n/en.json
@@ -12,6 +12,13 @@
"Video Script Settings": "**Video Script Settings**",
"Video Subject": "Video Subject (Provide a keyword, :red[AI will automatically generate] video script)",
"Script Language": "Language for Generating Video Script (AI will automatically output based on the language of your subject)",
+ "Advanced Script Settings": "Advanced Script Settings",
+ "Script Paragraph Number": "Script Paragraph Count",
+ "Custom Script Requirements": "Custom Script Requirements",
+ "Custom Script Requirements Placeholder": "Example: use a lighter tone, fit TikTok style, target young viewers, make the opening more suspenseful",
+ "Use Custom System Prompt": "Use Custom System Prompt",
+ "Use Custom System Prompt Help": "When enabled, this replaces the default script generation rules. Intended for advanced prompt users.",
+ "Custom System Prompt": "Custom System Prompt",
"Generate Video Script and Keywords": "Click to use AI to generate [Video Script] and [Video Keywords] based on **subject**",
"Auto Detect": "Auto Detect",
"Video Script": "Video Script (:blue[① Optional, AI generated ② Proper punctuation helps with subtitle generation])",
diff --git a/webui/i18n/zh.json b/webui/i18n/zh.json
index 896fc804..1f8f66af 100644
--- a/webui/i18n/zh.json
+++ b/webui/i18n/zh.json
@@ -12,6 +12,13 @@
"Video Script Settings": "**文案设置**",
"Video Subject": "视频主题(给定一个关键词,:red[AI自动生成]视频文案)",
"Script Language": "生成视频脚本的语言(一般情况AI会自动根据你输入的主题语言输出)",
+ "Advanced Script Settings": "高级脚本设置",
+ "Script Paragraph Number": "文案段落数量",
+ "Custom Script Requirements": "自定义文案要求",
+ "Custom Script Requirements Placeholder": "例如:语气更轻松,适合小红书风格,面向年轻用户,开头更有悬念",
+ "Use Custom System Prompt": "使用自定义系统提示词",
+ "Use Custom System Prompt Help": "启用后会覆盖默认脚本生成规则,适合熟悉 Prompt 的高级用户。",
+ "Custom System Prompt": "自定义系统提示词",
"Generate Video Script and Keywords": "点击使用AI根据**主题**生成 【视频文案】 和 【视频关键词】",
"Auto Detect": "自动检测",
"Video Script": "视频文案(:blue[①可不填,使用AI生成 ②合理使用标点断句,有助于生成字幕])",