smtsead commited on
Commit
2bdfc3f
Β·
verified Β·
1 Parent(s): c8f4b28

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +15 -13
app.py CHANGED
@@ -25,24 +25,25 @@ def img2text(url):
25
 
26
  return text.strip()
27
 
28
- # Function to generate a kid-friendly story from the text caption
29
  def text2story(text):
30
  """
31
- Generates a kid-friendly story from the text caption using the pranavpsv/gpt2-genre-story-generator model.
32
 
33
  Args:
34
  text (str): Text caption generated from the image.
35
 
36
  Returns:
37
- str: Generated story suitable for kids aged 3-10, within 100 words.
38
  """
39
-
40
  # Load the text generation model
41
  story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator")
42
 
43
- # Generate the story
44
- story = story_generator(f"<BOS> <superhero> {text}"))
 
45
 
 
46
 
47
  # Function to convert text to audio using gTTS
48
  def text2audio(story_text):
@@ -65,15 +66,15 @@ def text2audio(story_text):
65
  # Main application
66
  st.set_page_config(page_title="Picture Stories πŸŽ¨πŸ“–", page_icon="πŸ¦„")
67
  st.title("Picture Stories πŸŽ¨πŸ“–")
68
- st.markdown("### Turn your pictures into fun stories and listen to them! πŸŽ‰")
69
 
70
  # Instructions for kids
71
  st.markdown("""
72
  **How to use this app:**
73
  1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
74
- 2. Wait for the app to **create a story** from your picture.
75
  3. **Listen to the story** by clicking the "Play Audio" button.
76
- 4. Enjoy your fun story! 🎧
77
  """)
78
 
79
  # Upload image
@@ -94,9 +95,9 @@ if uploaded_file is not None:
94
  st.write("**What we see:**", scenario)
95
 
96
  # Stage 2: Text to Story
97
- st.text('πŸ“– Creating a fun story for you...')
98
  story = text2story(scenario)
99
- st.write("**Your story:**", story)
100
 
101
  # Stage 3: Story to Audio
102
  st.text('🎧 Turning your story into audio...')
@@ -106,5 +107,6 @@ if uploaded_file is not None:
106
  if st.button("🎡 **Play Audio**"):
107
  st.audio(audio_file, format="audio/mp3")
108
 
109
- # Clean up the generated audio file
110
- os.remove(audio_file)
 
 
25
 
26
  return text.strip()
27
 
28
+ # Function to generate a kid-friendly superhero story from the text caption
29
  def text2story(text):
30
  """
31
+ Generates a kid-friendly superhero story from the text caption using the pranavpsv/gpt2-genre-story-generator model.
32
 
33
  Args:
34
  text (str): Text caption generated from the image.
35
 
36
  Returns:
37
+ str: Generated superhero story suitable for kids aged 3-10, within 100 words.
38
  """
 
39
  # Load the text generation model
40
  story_generator = pipeline("text-generation", model="pranavpsv/gpt2-genre-story-generator")
41
 
42
+ # Generate the story with the superhero genre
43
+ prompt = f"<BOS> <superhero> {text}"
44
+ story = story_generator(prompt, max_length=100, num_return_sequences=1)[0]['generated_text']
45
 
46
+ return story
47
 
48
  # Function to convert text to audio using gTTS
49
  def text2audio(story_text):
 
66
  # Main application
67
  st.set_page_config(page_title="Picture Stories πŸŽ¨πŸ“–", page_icon="πŸ¦„")
68
  st.title("Picture Stories πŸŽ¨πŸ“–")
69
+ st.markdown("### Turn your pictures into fun superhero stories and listen to them! πŸŽ‰")
70
 
71
  # Instructions for kids
72
  st.markdown("""
73
  **How to use this app:**
74
  1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
75
+ 2. Wait for the app to **create a superhero story** from your picture.
76
  3. **Listen to the story** by clicking the "Play Audio" button.
77
+ 4. Enjoy your fun superhero story! 🎧
78
  """)
79
 
80
  # Upload image
 
95
  st.write("**What we see:**", scenario)
96
 
97
  # Stage 2: Text to Story
98
+ st.text('πŸ“– Creating a fun superhero story for you...')
99
  story = text2story(scenario)
100
+ st.write("**Your superhero story:**", story)
101
 
102
  # Stage 3: Story to Audio
103
  st.text('🎧 Turning your story into audio...')
 
107
  if st.button("🎡 **Play Audio**"):
108
  st.audio(audio_file, format="audio/mp3")
109
 
110
+ # Clean up the generated audio file and uploaded image
111
+ os.remove(audio_file)
112
+ os.remove(uploaded_file.name)