smtsead commited on
Commit
f9e85ad
Β·
verified Β·
1 Parent(s): a63c8c4

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -14
app.py CHANGED
@@ -28,16 +28,24 @@ def text2story(text):
28
  text (str): Text caption generated from the image.
29
 
30
  Returns:
31
- str: Generated story suitable for kids aged 3-10.
32
  """
33
  # Add a prompt to ensure the story is kid-friendly and happy
34
- prompt = f"Write a happy and fun story for kids aged 3-10 based on the following scenario: {text}."
35
 
36
  # Load the text generation model
37
  story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
38
 
39
  # Generate the story
40
- story = story_generator(prompt, max_length=95, num_return_sequences=1)[0]["generated_text"]
 
 
 
 
 
 
 
 
41
 
42
  return story
43
 
@@ -60,12 +68,21 @@ def text2audio(story_text):
60
  return audio_file
61
 
62
  # Main application
63
- st.set_page_config(page_title="Your Image to Audio Story",
64
- page_icon="🦜")
65
- st.header("Turn Your Image into a Fun Audio Story! πŸŽ‰")
 
 
 
 
 
 
 
 
 
66
 
67
  # Upload image
68
- uploaded_file = st.file_uploader("Select an Image...", type=["jpg", "jpeg", "png"])
69
 
70
  if uploaded_file is not None:
71
  # Save the uploaded file
@@ -74,24 +91,24 @@ if uploaded_file is not None:
74
  file.write(bytes_data)
75
 
76
  # Display the uploaded image
77
- st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
78
 
79
  # Stage 1: Image to Text
80
- st.text('Processing image to text...')
81
  scenario = img2text(uploaded_file.name)
82
- st.write("**Caption:**", scenario)
83
 
84
  # Stage 2: Text to Story
85
- st.text('Generating a fun story for kids...')
86
  story = text2story(scenario)
87
- st.write("**Story:**", story)
88
 
89
  # Stage 3: Story to Audio
90
- st.text('Converting story to audio...')
91
  audio_file = text2audio(story)
92
 
93
  # Play button for the generated audio
94
- if st.button("Play Audio"):
95
  st.audio(audio_file, format="audio/mp3")
96
 
97
  # Clean up the generated audio file
 
28
  text (str): Text caption generated from the image.
29
 
30
  Returns:
31
+ str: Generated story suitable for kids aged 3-10, within 95 words.
32
  """
33
  # Add a prompt to ensure the story is kid-friendly and happy
34
+ prompt = f"Write a happy, fun and complete story for kids aged 3-10 based on the following scenario: {text}. Keep the story under 120 words."
35
 
36
  # Load the text generation model
37
  story_generator = pipeline("text-generation", model="aspis/gpt2-genre-story-generation")
38
 
39
  # Generate the story
40
+ story = story_generator(prompt, max_length=150, num_return_sequences=1)[0]["generated_text"]
41
+
42
+ # Remove the prompt from the generated story
43
+ story = story.replace(prompt, "").strip()
44
+
45
+ # Ensure the story is within 95 words
46
+ story_words = story.split()
47
+ if len(story_words) > 98:
48
+ story = " ".join(story_words[:98])
49
 
50
  return story
51
 
 
68
  return audio_file
69
 
70
  # Main application
71
+ st.set_page_config(page_title="Picture Stories πŸŽ¨πŸ“–", page_icon="πŸ¦„")
72
+ st.title("Picture Stories πŸŽ¨πŸ“–")
73
+ st.markdown("### Turn your pictures into fun stories and listen to them! πŸŽ‰")
74
+
75
+ # Instructions for kids
76
+ st.markdown("""
77
+ **How to use this app:**
78
+ 1. **Upload a picture** of something fun, like your favorite toy, a park, or your pet.
79
+ 2. Wait for the app to **create a story** from your picture.
80
+ 3. **Listen to the story** by clicking the "Play Audio" button.
81
+ 4. Enjoy your fun story! 🎧
82
+ """)
83
 
84
  # Upload image
85
+ uploaded_file = st.file_uploader("πŸ“· **Upload your picture here!**", type=["jpg", "jpeg", "png"])
86
 
87
  if uploaded_file is not None:
88
  # Save the uploaded file
 
91
  file.write(bytes_data)
92
 
93
  # Display the uploaded image
94
+ st.image(uploaded_file, caption="Your awesome picture!", use_container_width=True)
95
 
96
  # Stage 1: Image to Text
97
+ st.text('✨ Turning your picture into words...')
98
  scenario = img2text(uploaded_file.name)
99
+ st.write("**What we see:**", scenario)
100
 
101
  # Stage 2: Text to Story
102
+ st.text('πŸ“– Creating a fun story for you...')
103
  story = text2story(scenario)
104
+ st.write("**Your story:**", story)
105
 
106
  # Stage 3: Story to Audio
107
+ st.text('🎧 Turning your story into audio...')
108
  audio_file = text2audio(story)
109
 
110
  # Play button for the generated audio
111
+ if st.button("🎡 **Play Audio**"):
112
  st.audio(audio_file, format="audio/mp3")
113
 
114
  # Clean up the generated audio file