Update app.py
Browse files
app.py
CHANGED
@@ -45,20 +45,20 @@ def generate_story(caption):
|
|
45 |
else:
|
46 |
story = full_text.strip()
|
47 |
|
48 |
-
#
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
return story
|
63 |
|
64 |
def text_to_speech(text, output_file="output.mp3"):
|
|
|
45 |
else:
|
46 |
story = full_text.strip()
|
47 |
|
48 |
+
# 对生成的故事进行后处理,确保不超过 100 个单词且结尾完整
|
49 |
+
words = story.split()
|
50 |
+
if len(words) > 100:
|
51 |
+
trimmed = " ".join(words[:100])
|
52 |
+
# 查找最后一个完整结束的标点位置,尽量在句子边界截断
|
53 |
+
last_period = trimmed.rfind(".")
|
54 |
+
last_exclam = trimmed.rfind("!")
|
55 |
+
last_question = trimmed.rfind("?")
|
56 |
+
end_index = max(last_period, last_exclam, last_question)
|
57 |
+
if end_index != -1:
|
58 |
+
trimmed = trimmed[:end_index+1]
|
59 |
+
story = trimmed
|
60 |
+
if story and story[-1] not in ".!?":
|
61 |
+
story += "."
|
62 |
return story
|
63 |
|
64 |
def text_to_speech(text, output_file="output.mp3"):
|