Leo Liu commited on
Commit
96702ff
·
verified ·
1 Parent(s): 8085c20

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -3
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
- # 构造更详细的提示要求生成适合3-10岁儿童阅读的童话故事并控制输出为50-150 words
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
- story_text = pipe(prompt, max_length=200, min_length=50, do_sample=True, top_p=0.95, temperature=0.8)[0]['generated_text']
 
 
 
 
 
 
 
 
 
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