Leo Liu commited on
Update app.py
Browse files
app.py
CHANGED
|
@@ -12,15 +12,24 @@ def img2text(url):
|
|
| 12 |
# text2story
|
| 13 |
def text2story(text):
|
| 14 |
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 15 |
-
# 构造
|
| 16 |
prompt = (
|
| 17 |
f"Write a complete fairy tale story for children aged 3-10. "
|
| 18 |
f"The story should be engaging, imaginative, and written in clear simple language. "
|
| 19 |
f"Make sure the story is between 50 and 150 words. "
|
| 20 |
f"Use the following scene description as inspiration: {text}"
|
| 21 |
)
|
| 22 |
-
#
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
return story_text
|
| 25 |
|
| 26 |
|
|
|
|
| 12 |
# text2story
|
| 13 |
def text2story(text):
|
| 14 |
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 15 |
+
# 构造 prompt,但只用于内部生成,不让模型在最终输出中重复 prompt
|
| 16 |
prompt = (
|
| 17 |
f"Write a complete fairy tale story for children aged 3-10. "
|
| 18 |
f"The story should be engaging, imaginative, and written in clear simple language. "
|
| 19 |
f"Make sure the story is between 50 and 150 words. "
|
| 20 |
f"Use the following scene description as inspiration: {text}"
|
| 21 |
)
|
| 22 |
+
# 通过 return_full_text=False,避免将 prompt 原样拼接在输出里
|
| 23 |
+
# 同时使用 max_new_tokens 控制生成长度,防止生成被截断或过长
|
| 24 |
+
story_text = pipe(
|
| 25 |
+
prompt,
|
| 26 |
+
max_new_tokens=250, # 根据需要可适当调大,以保证字数
|
| 27 |
+
min_new_tokens=50, # 保证至少生成一定长度
|
| 28 |
+
do_sample=True,
|
| 29 |
+
top_p=0.95,
|
| 30 |
+
temperature=0.8,
|
| 31 |
+
return_full_text=False
|
| 32 |
+
)[0]['generated_text']
|
| 33 |
return story_text
|
| 34 |
|
| 35 |
|