Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
22 |
-
|
23 |
-
return
|
24 |
|
25 |
# text2audio
|
26 |
def text2audio(story_text):
|
27 |
-
# Use
|
28 |
-
tts =
|
29 |
-
|
30 |
-
|
|
|
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 |
-
|
58 |
|
59 |
# Play button
|
60 |
if st.button("Play Audio"):
|
61 |
-
st.audio(
|
|
|
|
|
|
|
|
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)
|