smtsead commited on
Commit
8de2446
Β·
verified Β·
1 Parent(s): 3a25fa2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -9
app.py CHANGED
@@ -1,6 +1,8 @@
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
 
 
4
 
5
  # function part
6
  # img2text
@@ -18,16 +20,17 @@ def text2story(text):
18
  # Add a prompt to guide the story generation (avoid magical elements)
19
  prompt = f"Write a fun and realistic story for kids based on this: {text}. Keep it simple and under 95 words."
20
  story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
21
- # Ensure the story is simple and fun for kids
22
- simple_story = story[:95] # Limit to 95 words
23
- return simple_story
24
 
25
  # text2audio
26
  def text2audio(story_text):
27
- # Use a TTS model to convert text to audio
28
- tts = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
29
- audio_data = tts(story_text)
30
- return audio_data
 
31
 
32
  # main part
33
  st.set_page_config(page_title="Story Explorer", page_icon="🦜")
@@ -54,8 +57,11 @@ if uploaded_file is not None:
54
 
55
  # Stage 3: Story to Audio data
56
  st.text('Turning your story into audio... 🎧')
57
- audio_data = text2audio(story)
58
 
59
  # Play button
60
  if st.button("Play Audio"):
61
- st.audio(audio_data['audio'], format="audio/wav", start_time=0, sample_rate=audio_data['sampling_rate'])
 
 
 
 
1
  # import part
2
  import streamlit as st
3
  from transformers import pipeline
4
+ from gtts import gTTS # Using gTTS for text-to-speech
5
+ import os
6
 
7
  # function part
8
  # img2text
 
20
  # Add a prompt to guide the story generation (avoid magical elements)
21
  prompt = f"Write a fun and realistic story for kids based on this: {text}. Keep it simple and under 95 words."
22
  story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
23
+ # Remove the prompt from the generated story
24
+ story = story.replace(prompt, "").strip() # Clean up the output
25
+ return story[:95] # Limit to 95 words
26
 
27
  # text2audio
28
  def text2audio(story_text):
29
+ # Use gTTS for text-to-speech conversion
30
+ tts = gTTS(text=story_text, lang="en")
31
+ audio_file = "story_audio.mp3"
32
+ tts.save(audio_file)
33
+ return audio_file
34
 
35
  # main part
36
  st.set_page_config(page_title="Story Explorer", page_icon="🦜")
 
57
 
58
  # Stage 3: Story to Audio data
59
  st.text('Turning your story into audio... 🎧')
60
+ audio_file = text2audio(story)
61
 
62
  # Play button
63
  if st.button("Play Audio"):
64
+ st.audio(audio_file, format="audio/mp3")
65
+
66
+ # Clean up the audio file after use
67
+ os.remove(audio_file)