Leo Liu commited on
Commit
5756cb3
·
verified ·
1 Parent(s): 61fcb97

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -3
app.py CHANGED
@@ -11,9 +11,37 @@ def img2text(url):
11
 
12
  # text2story
13
  def text2story(text):
14
- pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
15
- story_text = pipe(text)[0]['generated_text']
16
- return story_text
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
 
18
  # text2audio
19
  def text2audio(story_text):
 
11
 
12
  # text2story
13
  def text2story(text):
14
+ # 添加儿童故事专用prompt模板
15
+ prompt = f"""Generate a VERY SHORT fairy tale for children aged 3-10 based on: {text}
16
+ Story must:
17
+ 1. Have animal/fairy characters
18
+ 2. Teach kindness or courage
19
+ 3. Use simple words
20
+ 4. Be 50-100 words
21
+ Story:"""
22
+
23
+ pipe = pipeline(
24
+ "text-generation",
25
+ model="pranavpsv/genre-story-generator-v2",
26
+ # 优化生成参数
27
+ max_new_tokens=150, # 严格控制输出长度
28
+ min_new_tokens=50, # 确保最低字数
29
+ do_sample=True,
30
+ temperature=0.7, # 平衡创意与连贯性
31
+ top_k=40, # 加速生成
32
+ top_p=0.9,
33
+ repetition_penalty=1.2,
34
+ num_return_sequences=1 # 减少计算量
35
+ )
36
+
37
+ # 生成后处理
38
+ raw_story = pipe(prompt)[0]['generated_text']
39
+
40
+ # 提取核心故事内容(过滤prompt重复)
41
+ story = raw_story.split("Story:")[-1].strip()
42
+
43
+ # 精确截断至150字(中文按字符计算)
44
+ return ' '.join(story.split()[:150]) if len(story) > 150 else story
45
 
46
  # text2audio
47
  def text2audio(story_text):