smtsead commited on
Commit
02c1ec5
·
verified ·
1 Parent(s): 7788541

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -7
app.py CHANGED
@@ -21,20 +21,33 @@ def text2story(text):
21
  text_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-125M")
22
 
23
  # Add a strong prompt to guide the model
24
- prompt = f"Write a short, happy, and fun story for kids aged 3-10 based on the following description: {text}. " \
25
- "The story should be cheerful, imaginative, and suitable for young children. " \
26
- "Avoid any scary or sad elements. Keep the story under 95 words and make sure it has a clear beginning, middle, and end."
 
27
 
28
  # Generate the story
29
- story = text_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
30
 
31
- # Clean up the output to remove the prompt (if necessary)
32
  story = story.replace(prompt, "").strip()
 
 
 
 
 
 
 
 
 
 
 
 
33
  return story
34
 
35
  # Function to convert text to audio using gTTS
36
  def text2audio(story_text):
37
- audio_file = "story_audio.mp3"
38
  tts = gTTS(story_text, lang="en")
39
  tts.save(audio_file)
40
  return audio_file
@@ -77,7 +90,7 @@ if uploaded_file is not None:
77
 
78
  # Play button for audio
79
  if st.session_state.audio_file and st.button("🎧 Listen to the Story"):
80
- st.audio(st.session_state.audio_file, format="audio/mp3")
81
 
82
  # Clean up the generated audio file
83
  if st.session_state.audio_file and os.path.exists(st.session_state.audio_file):
 
21
  text_generator = pipeline("text-generation", model="EleutherAI/gpt-neo-125M")
22
 
23
  # Add a strong prompt to guide the model
24
+ prompt = f"Write a short, happy, and fun story for kids aged 3-10. " \
25
+ f"The story should be about: {text}. " \
26
+ "Make it cheerful, imaginative, and suitable for young children. " \
27
+ "Avoid any scary or sad elements. The story must be exactly 95 words."
28
 
29
  # Generate the story
30
+ story = text_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
31
 
32
+ # Clean up the output to remove the prompt
33
  story = story.replace(prompt, "").strip()
34
+
35
+ # Ensure the story is exactly 95 words
36
+ words = story.split()
37
+ if len(words) > 95:
38
+ story = " ".join(words[:95])
39
+ elif len(words) < 95:
40
+ # If the story is too short, pad it with a happy ending
41
+ story = " ".join(words) + " They all laughed and played together, making it the best day ever!"
42
+ words = story.split()
43
+ if len(words) > 95:
44
+ story = " ".join(words[:95])
45
+
46
  return story
47
 
48
  # Function to convert text to audio using gTTS
49
  def text2audio(story_text):
50
+ audio_file = "kids_playing_audio.wav" # Use the correct file name
51
  tts = gTTS(story_text, lang="en")
52
  tts.save(audio_file)
53
  return audio_file
 
90
 
91
  # Play button for audio
92
  if st.session_state.audio_file and st.button("🎧 Listen to the Story"):
93
+ st.audio(st.session_state.audio_file, format="audio/wav")
94
 
95
  # Clean up the generated audio file
96
  if st.session_state.audio_file and os.path.exists(st.session_state.audio_file):