Leo Liu commited on
Commit
546773b
·
verified ·
1 Parent(s): 47eafbe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -25
app.py CHANGED
@@ -10,34 +10,22 @@ def img2text(url):
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):
 
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):