Leo Liu commited on
Commit
00c8bd8
·
verified ·
1 Parent(s): 7c3c59f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +11 -16
app.py CHANGED
@@ -12,9 +12,7 @@ def img2text(url):
12
  # text2story
13
  def text2story(text):
14
  pipe = pipeline("text-generation",
15
- model="pranavpsv/genre-story-generator-v2",
16
- max_new_tokens=100,
17
- truncation=True)
18
  prompt_for_children = f"""Generate a fairy tale for children aged 3-10 based on: {text}.
19
  Requirements:
20
  1. Simple and interesting plot
@@ -23,21 +21,18 @@ def text2story(text):
23
  4. Use simple vocabulary (under 500 Lexile)
24
  5. Short sentences (max 15 words)
25
  6. Add onomatopoeia like 'Boom!', 'Whoosh!' etc.
26
-
27
  Story: Once upon a time,"""
28
 
29
- story_text = pipe(prompt_for_children,
30
- text,
31
- max_new_tokens=200, # 增加故事长度
32
- temperature=0.7, # 降低随机性
33
- top_p=0.95, # 使用核采样
34
- repetition_penalty=1.2,
35
- do_sample=True,
36
- num_return_sequences=1)[0]['generated_text']
37
- last_punctuation = max(story_text.rfind("."), story_text.rfind("!"), story_text.rfind("?"))
38
- if last_punctuation != -1:
39
- story_text = story_text[:last_punctuation+1]
40
- return story_text
41
 
42
  # Post-processing optimization
43
  story_text = story_text.replace(prompt, "").strip()
 
12
  # text2story
13
  def text2story(text):
14
  pipe = pipeline("text-generation",
15
+ model="pranavpsv/genre-story-generator-v2")
 
 
16
  prompt_for_children = f"""Generate a fairy tale for children aged 3-10 based on: {text}.
17
  Requirements:
18
  1. Simple and interesting plot
 
21
  4. Use simple vocabulary (under 500 Lexile)
22
  5. Short sentences (max 15 words)
23
  6. Add onomatopoeia like 'Boom!', 'Whoosh!' etc.
 
24
  Story: Once upon a time,"""
25
 
26
+ generation_params = {
27
+ "max_new_tokens" : 200 ,
28
+ "temperature" : 0.7 ,
29
+ "top_p" : 0.95 ,
30
+ "repetition_penalty" : 1.2 ,
31
+ "do_sample" : True ,
32
+ "num_return_sequences" : 1
33
+ }
34
+ story = pipe (prompt, **generation_params)[0] ['generated_text']
35
+ return postprocess_text(story)
 
 
36
 
37
  # Post-processing optimization
38
  story_text = story_text.replace(prompt, "").strip()