xieqilenb commited on
Commit
7174441
·
verified ·
1 Parent(s): 1f50168

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -3
app.py CHANGED
@@ -26,14 +26,14 @@ def generate_story(caption):
26
  "You are a highly imaginative children's story writer celebrated for your creativity and captivating narratives. "
27
  "Using the image details provided below, please craft an enchanting tale tailored for children aged 3 to 10. "
28
  "Rather than simply reiterating the image details, enhance your story with imaginative characters, quirky adventures, "
29
- "and delightful surprises that ignite wonder in every young heart. Let your narrative flow naturally and kindle the magic of storytelling. "
30
- "Please ensure that your story is engaging, coherent, and the words count should lower than 100 in length.\n\n"
31
  f"Image Details: {caption}\n\nStory:"
32
  )
33
 
34
  result = story_generator(
35
  prompt,
36
- max_new_tokens=100,
37
  num_return_sequences=1,
38
  do_sample=True,
39
  temperature=1.0
@@ -45,6 +45,20 @@ def generate_story(caption):
45
  else:
46
  story = full_text.strip()
47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  return story
49
 
50
  def text_to_speech(text, output_file="output.mp3"):
 
26
  "You are a highly imaginative children's story writer celebrated for your creativity and captivating narratives. "
27
  "Using the image details provided below, please craft an enchanting tale tailored for children aged 3 to 10. "
28
  "Rather than simply reiterating the image details, enhance your story with imaginative characters, quirky adventures, "
29
+ "and delightful surprises that ignite wonder in every young heart. Let your narrative flow naturally and ensure that your story is complete, with a clear beginning, middle, and end. "
30
+ "Please ensure the total word count does not exceed 100 words, and do not leave the story incomplete.\n\n"
31
  f"Image Details: {caption}\n\nStory:"
32
  )
33
 
34
  result = story_generator(
35
  prompt,
36
+ max_new_tokens=150,
37
  num_return_sequences=1,
38
  do_sample=True,
39
  temperature=1.0
 
45
  else:
46
  story = full_text.strip()
47
 
48
+ # 对生成的故事进行后处理,确保不超过 100 个单词且结尾完整
49
+ words = story.split()
50
+ if len(words) > 100:
51
+ trimmed = " ".join(words[:100])
52
+ # 查找最后一个完整结束的标点位置,尽量在句子边界截断
53
+ last_period = trimmed.rfind(".")
54
+ last_exclam = trimmed.rfind("!")
55
+ last_question = trimmed.rfind("?")
56
+ end_index = max(last_period, last_exclam, last_question)
57
+ if end_index != -1:
58
+ trimmed = trimmed[:end_index+1]
59
+ story = trimmed
60
+ if story and story[-1] not in ".!?":
61
+ story += "."
62
  return story
63
 
64
  def text_to_speech(text, output_file="output.mp3"):