smtsead commited on
Commit
abdb243
Β·
verified Β·
1 Parent(s): aa762d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -9
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
- story_generator = pipeline("text-generation", model="gpt2")
 
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=150, 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,17 +34,21 @@ def text2story(text):
34
  # text2audio
35
  def text2audio(story_text):
36
  try:
37
- tts = gTTS(text=story_text, lang='en')
38
- audio_file = "story_audio.mp3"
39
- tts.save(audio_file)
 
 
 
 
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="Fun Story Maker", page_icon="😊")
47
- st.header("πŸ˜„ Fun Story Maker: Turn Your Picture into a Happy Story! πŸ˜„")
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/mp3")
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)