Spaces:
Build error
Build error
Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -10,22 +10,34 @@ def img2text(url):
|
|
| 10 |
return text
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
-
def text2story(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
pipe = pipeline("text-generation",
|
| 15 |
model="pranavpsv/genre-story-generator-v2",
|
| 16 |
-
max_new_tokens=
|
| 17 |
-
truncation=True
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 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):
|
|
|
|
| 10 |
return text
|
| 11 |
|
| 12 |
# text2story
|
| 13 |
+
def text2story(scenario):
|
| 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=200,
|
| 22 |
+
truncation=True,
|
| 23 |
+
temperature=0.7, # 降低随机性
|
| 24 |
+
top_p=0.9,
|
| 25 |
+
repetition_penalty=1.2)
|
| 26 |
+
|
| 27 |
+
full_story = pipe(child_prompt + scenario,
|
| 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 |
+
return "🌈 " + safe_story[:safe_story.rfind(".")+1] + " 🎉"
|
|
|
|
|
|
|
|
|
|
| 41 |
|
| 42 |
# text2audio
|
| 43 |
def text2audio(story_text):
|