Skip to content

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

fix(llm): preserve inline sharp symbols in narration

Rudy Celekli393b78e087b3
diff --git a/app/services/llm.py b/app/services/llm.py
index 4485fcb7..924b9753 100644
--- a/app/services/llm.py
+++ b/app/services/llm.py
@@ -835,9 +835,14 @@ def generate_script(
def format_response(response):
# Clean the script
- # Remove asterisks, hashes
+ # Remove emphasis and heading markers without changing inline names
+ # or musical notes such as C#, F# and G#.
response = response.replace("*", "")
- response = response.replace("#", "")
+ response = re.sub(
+ r"(?m)^[ \t]{0,3}#{1,6}[ \t]+(.*?)[ \t]*$",
+ lambda match: re.sub(r"[ \t]+#+[ \t]*$", "", match.group(1)),
+ response,
+ )
# Link labels are narration; discard destinations before stripping
# bracketed stage directions. Common parenthesized URL components fit.
diff --git a/test/services/test_script_semantic_hash_characters.py b/test/services/test_script_semantic_hash_characters.py
new file mode 100644
index 00000000..c1d06c08
--- /dev/null
+++ b/test/services/test_script_semantic_hash_characters.py
@@ -0,0 +1,21 @@
+from unittest.mock import patch
+
+import pytest
+
+from app.services import llm
+
+
[email protected]('narration', ['Learn C# and F# today.', 'Play the F# note before G.'])
+def test_script_cleanup_preserves_inline_hashes(narration):
+ with patch.object(llm, '_generate_response', return_value=narration):
+ assert llm.generate_script('lesson') == narration
+
+
[email protected]('response,expected', [
+ ('## Lesson\nRead this aloud.', 'Lesson\nRead this aloud.'),
+ ('## Lesson ##\nRead this aloud.', 'Lesson\nRead this aloud.'),
+ ('## Learn C#', 'Learn C#'),
+])
+def test_script_cleanup_still_removes_markdown_heading_markers(response, expected):
+ with patch.object(llm, '_generate_response', return_value=response):
+ assert llm.generate_script('lesson') == expected