Spaces:
Build error
Build error
Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,34 +10,22 @@ def img2text(url):
|
|
| 10 |
return text
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
-
def text2story(
|
| 14 |
-
child_prompt = f"Create a children's story suitable for 3-10 years old about {scenario}. " \
|
| 15 |
-
"Include talking animals and magical elements. " \
|
| 16 |
-
"Use simple words and happy ending. " \
|
| 17 |
-
"Story structure: Beginning -> Problem -> Solution -> Happy ending.\n\n"
|
| 18 |
-
|
| 19 |
pipe = pipeline("text-generation",
|
| 20 |
model="pranavpsv/genre-story-generator-v2",
|
| 21 |
-
max_new_tokens=
|
| 22 |
-
truncation=True
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
do_sample=True,
|
| 29 |
-
num_return_sequences=1)[0]['generated_text']
|
| 30 |
-
|
| 31 |
-
# 过滤不合适内容
|
| 32 |
-
safe_story = full_story.replace("violence", "").replace("scary", "").replace("died", "solved")
|
| 33 |
-
|
| 34 |
-
# 确保以快乐结局结束
|
| 35 |
-
happy_endings = ["happily ever after", "big smile", "learned a good lesson"]
|
| 36 |
-
if not any(ending in safe_story.lower() for ending in happy_endings):
|
| 37 |
-
safe_story += " And they all lived happily ever after!"
|
| 38 |
|
| 39 |
-
#
|
| 40 |
-
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# text2audio
|
| 43 |
def text2audio(story_text):
|
|
|
|
| 10 |
return text
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
+
def text2story(text):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
pipe = pipeline("text-generation",
|
| 15 |
model="pranavpsv/genre-story-generator-v2",
|
| 16 |
+
max_new_tokens=300, # 控制生成内容长度
|
| 17 |
+
truncation=True) # 确保输入不被截断
|
| 18 |
+
story_text = pipe(text,
|
| 19 |
+
do_sample=True, # 启用随机采样
|
| 20 |
+
temperature=0.9, # 控制随机性(0-1,越大越有创意)
|
| 21 |
+
top_k=50, # 限制采样词汇量
|
| 22 |
+
num_return_sequences=1)[0]['generated_text']
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
+
# 确保输出以完整句子结尾
|
| 25 |
+
last_punctuation = max(story_text.rfind("."), story_text.rfind("!"), story_text.rfind("?"))
|
| 26 |
+
if last_punctuation != -1:
|
| 27 |
+
story_text = story_text[:last_punctuation+1]
|
| 28 |
+
return story_text
|
| 29 |
|
| 30 |
# text2audio
|
| 31 |
def text2audio(story_text):
|