Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,6 @@
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
4 |
-
from gtts import gTTS
|
5 |
import os
|
6 |
|
7 |
# function part
|
@@ -20,10 +19,11 @@ def img2text(url):
|
|
20 |
# text2story
|
21 |
def text2story(text):
|
22 |
try:
|
23 |
-
|
|
|
24 |
# Add a fun and happy prompt to guide the story generation
|
25 |
prompt = f"One sunny day, {text}. "
|
26 |
-
story = story_generator(prompt, max_length=
|
27 |
# Make the story more fun by adding a happy ending
|
28 |
happy_story = story + " And everyone had a big smile on their faces at the end of the day! ππ"
|
29 |
return happy_story
|
@@ -34,17 +34,21 @@ def text2story(text):
|
|
34 |
# text2audio
|
35 |
def text2audio(story_text):
|
36 |
try:
|
37 |
-
|
38 |
-
|
39 |
-
|
|
|
|
|
|
|
|
|
40 |
return audio_file
|
41 |
except Exception as e:
|
42 |
st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! π")
|
43 |
return None
|
44 |
|
45 |
# main part
|
46 |
-
st.set_page_config(page_title="
|
47 |
-
st.header("π
|
48 |
uploaded_file = st.file_uploader("π· Choose a picture to create a fun story...", type=["jpg", "jpeg", "png"])
|
49 |
|
50 |
if uploaded_file is not None:
|
@@ -73,6 +77,6 @@ if uploaded_file is not None:
|
|
73 |
if audio_file:
|
74 |
# Play button
|
75 |
if st.button("π§ Listen to Your Story!"):
|
76 |
-
st.audio(audio_file, format="audio/
|
77 |
# Clean up the audio file after playing
|
78 |
os.remove(audio_file)
|
|
|
1 |
# import part
|
2 |
import streamlit as st
|
3 |
from transformers import pipeline
|
|
|
4 |
import os
|
5 |
|
6 |
# function part
|
|
|
19 |
# text2story
|
20 |
def text2story(text):
|
21 |
try:
|
22 |
+
# Use DistilGPT2 for faster text generation
|
23 |
+
story_generator = pipeline("text-generation", model="distilgpt2")
|
24 |
# Add a fun and happy prompt to guide the story generation
|
25 |
prompt = f"One sunny day, {text}. "
|
26 |
+
story = story_generator(prompt, max_length=100, num_return_sequences=1)[0]["generated_text"]
|
27 |
# Make the story more fun by adding a happy ending
|
28 |
happy_story = story + " And everyone had a big smile on their faces at the end of the day! ππ"
|
29 |
return happy_story
|
|
|
34 |
# text2audio
|
35 |
def text2audio(story_text):
|
36 |
try:
|
37 |
+
# Use a fast TTS model from Hugging Face (e.g., Facebook's FastSpeech2)
|
38 |
+
tts_pipeline = pipeline("text-to-speech", model="facebook/fastspeech2-en-ljspeech")
|
39 |
+
audio_output = tts_pipeline(story_text)
|
40 |
+
audio_file = "story_audio.wav"
|
41 |
+
# Save the audio file
|
42 |
+
with open(audio_file, "wb") as f:
|
43 |
+
f.write(audio_output["audio"])
|
44 |
return audio_file
|
45 |
except Exception as e:
|
46 |
st.error(f"Oops! Something went wrong while turning your story into audio. Please try again! π")
|
47 |
return None
|
48 |
|
49 |
# main part
|
50 |
+
st.set_page_config(page_title="Story Maker", page_icon="π")
|
51 |
+
st.header("π Story Maker: Turn Your Picture into a Happy Story! π")
|
52 |
uploaded_file = st.file_uploader("π· Choose a picture to create a fun story...", type=["jpg", "jpeg", "png"])
|
53 |
|
54 |
if uploaded_file is not None:
|
|
|
77 |
if audio_file:
|
78 |
# Play button
|
79 |
if st.button("π§ Listen to Your Story!"):
|
80 |
+
st.audio(audio_file, format="audio/wav")
|
81 |
# Clean up the audio file after playing
|
82 |
os.remove(audio_file)
|