scmlewis commited on
Commit
b3cad33
ยท
verified ยท
1 Parent(s): 5db9b0c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -39
app.py CHANGED
@@ -2,77 +2,72 @@ import streamlit as st
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
- # Function to generate a caption from an uploaded image
6
- # Uses the Hugging Face pipeline with the Salesforce BLIP model to analyze the image and produce a descriptive caption
7
  def generate_caption(image):
 
8
  image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
  caption = image_to_text(image)[0]["generated_text"]
10
  return caption
11
 
12
- # Function to generate a story based on the image caption
13
- # Uses the pranavpsv/genre-story-generator-v2 model to create a creative story inspired by the caption
14
  def generate_story(caption):
 
15
  pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
16
- story = pipe(caption, max_length=150, do_sample=True)[0]['generated_text'] # Added max_length for shorter, kid-friendly stories
17
  return story
18
 
19
- # Function to convert the story into audio
20
- # Uses the facebook/mms-tts-eng model to generate a spoken version of the story
21
  def generate_audio(story):
 
22
  pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
23
  audio = pipe(story)
24
  return audio
25
 
26
- # Streamlit UI: Creating a fun and interactive interface for kids
27
- # The app is designed to be simple, colorful, and engaging for young users
28
 
29
- # Display a vibrant, centered title with emojis to grab attention
30
- st.markdown("<h1 style='text-align: center; color: purple;'>๐ŸŒŸ Picture to Story Magic! ๐ŸŒŸ</h1>", unsafe_allow_html=True)
31
 
32
- # Add a welcoming description to explain the appโ€™s purpose in kid-friendly language
33
  st.markdown(
34
  """
35
  <p style='text-align: center; font-size: 16px;'>
36
- ๐ŸŽ‰ Hello, young adventurers! Upload a picture, and Iโ€™ll create a magical story just for you!
37
- This app is perfect for kids aged 3-10 who love fun and creative storytelling.
38
- Letโ€™s make some story magic together! ๐Ÿง™โ€โ™‚๏ธ
39
  </p>
40
  """,
41
  unsafe_allow_html=True
42
  )
43
 
44
- # File uploader for images, with a playful prompt
45
- uploaded_file = st.file_uploader("๐Ÿ“ธ Pick a cool picture!", type=["png", "jpg", "jpeg"], help="Choose a fun image to start your story adventure!")
46
 
47
- # Check if an image has been uploaded
48
  if uploaded_file is not None:
49
- # Load and display the uploaded image
50
- # The image is opened using Pillow and displayed with a caption
51
  image = Image.open(uploaded_file)
52
- st.image(image, caption="Your Awesome Picture! ๐Ÿ˜Š", use_column_width=True) # Changed to use_column_width for better scaling
53
 
54
- # Generate and display the image caption
55
- # The caption describes whatโ€™s in the image, serving as the storyโ€™s starting point
56
- with st.spinner("๐Ÿ” Looking at your picture..."):
57
  image_caption = generate_caption(image)
58
 
59
- st.subheader("โœจ Whatโ€™s in Your Picture?")
60
  st.write(f"{image_caption}")
61
- st.markdown("<p style='font-size: 14px;'>This is what I see in your picture! Itโ€™s the start of our story! ๐Ÿ“–</p>", unsafe_allow_html=True)
62
 
63
- # Generate and display the story
64
- # The story is created based on the caption, designed to be short and engaging for kids
65
- with st.spinner("๐Ÿ–Œ๏ธ Writing a magical story..."):
66
  story_telling = generate_story(image_caption)
67
 
68
- st.subheader("๐Ÿ“š Your Magical Story!")
69
  st.write(f"{story_telling}")
70
- st.markdown("<p style='font-size: 14px;'>Wow, what a fun story! Want to hear it out loud? Click below! ๐ŸŽถ</p>", unsafe_allow_html=True)
71
 
72
- # Generate and play audio for the story
73
- # Audio is generated only when the user clicks the button, keeping the app responsive
74
- if st.button("๐ŸŽ™๏ธ Listen to Your Story!"):
75
- with st.spinner("๐ŸŽต Turning your story into sound..."):
76
  audio = generate_audio(story_telling)
77
  st.audio(
78
  audio['audio'],
@@ -80,15 +75,15 @@ if uploaded_file is not None:
80
  start_time=0,
81
  sample_rate=audio['sampling_rate']
82
  )
83
- st.markdown("<p style='font-size: 14px;'>Amazing! You can listen to your story anytime! ๐Ÿ˜„</p>", unsafe_allow_html=True)
84
 
85
- # Add a footer to encourage users to try again
86
  st.markdown(
87
  """
88
  <hr>
89
  <p style='text-align: center; font-size: 14px;'>
90
- ๐Ÿ–ผ๏ธ Want another adventure? Upload a new picture and create a brand-new story!
91
- Made with love for curious kids! โค๏ธ
92
  </p>
93
  """,
94
  unsafe_allow_html=True
 
2
  from transformers import pipeline
3
  from PIL import Image
4
 
5
+ # Creates a short description from an uploaded picture using a smart model
 
6
  def generate_caption(image):
7
+ # Loads the BLIP model to analyze the image and describe it
8
  image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
  caption = image_to_text(image)[0]["generated_text"]
10
  return caption
11
 
12
+ # Turns the pictureโ€™s description into a fun story
 
13
  def generate_story(caption):
14
+ # Uses a story-generating model to craft a tale based on the description
15
  pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
16
+ story = pipe(caption)[0]['generated_text']
17
  return story
18
 
19
+ # Makes the story into a spoken audio clip
 
20
  def generate_audio(story):
21
+ # Employs a text-to-speech model to read the story aloud
22
  pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
23
  audio = pipe(story)
24
  return audio
25
 
26
+ # Streamlit UI: Builds a colorful, kid-friendly interface for storytelling fun
 
27
 
28
+ # Shows a big, exciting title to welcome young users
29
+ st.markdown("<h1 style='text-align: center; color: orange;'>โœจ Magic Story Creator! โœจ</h1>", unsafe_allow_html=True)
30
 
31
+ # Adds a fun message to explain what the app does
32
  st.markdown(
33
  """
34
  <p style='text-align: center; font-size: 16px;'>
35
+ ๐Ÿฆ„ Hey there, story explorers! Share a picture, and Iโ€™ll weave a super cool story for you!
36
+ This app is made for kids aged 3-10 who love adventures and imagination.
37
+ Ready to dive in? Letโ€™s go! ๐Ÿš€
38
  </p>
39
  """,
40
  unsafe_allow_html=True
41
  )
42
 
43
+ # Lets kids upload a picture to start their story
44
+ uploaded_file = st.file_uploader("๐Ÿ–ผ๏ธ Share a fun picture!", type=["png", "jpg", "jpeg"], help="Pick a picture to spark a story!")
45
 
46
+ # Checks if a picture has been uploaded
47
  if uploaded_file is not None:
48
+ # Opens and shows the picture on the screen
 
49
  image = Image.open(uploaded_file)
50
+ st.image(image, caption="Your Super Cool Picture! ๐Ÿ˜Ž", use_column_width=True)
51
 
52
+ # Creates a description of the picture
53
+ with st.spinner("๐Ÿ•ต๏ธโ€โ™‚๏ธ Exploring your picture..."):
 
54
  image_caption = generate_caption(image)
55
 
56
+ st.subheader("๐ŸŒˆ Whatโ€™s Your Picture About?")
57
  st.write(f"{image_caption}")
58
+ st.markdown("<p style='font-size: 14px;'>This is the start of your adventure! Letโ€™s build a story! ๐Ÿฐ</p>", unsafe_allow_html=True)
59
 
60
+ # Makes a story from the pictureโ€™s description
61
+ with st.spinner("โœ๏ธ Crafting a fantastic tale..."):
 
62
  story_telling = generate_story(image_caption)
63
 
64
+ st.subheader("๐ŸŽ‰ Your Awesome Story!")
65
  st.write(f"{story_telling}")
66
+ st.markdown("<p style='font-size: 14px;'>How cool is that? Want to hear your story come to life? Tap below! ๐ŸŽค</p>", unsafe_allow_html=True)
67
 
68
+ # Turns the story into audio when requested
69
+ if st.button("๐Ÿ”Š Hear Your Story!"):
70
+ with st.spinner("๐ŸŽง Creating story audio..."):
 
71
  audio = generate_audio(story_telling)
72
  st.audio(
73
  audio['audio'],
 
75
  start_time=0,
76
  sample_rate=audio['sampling_rate']
77
  )
78
+ st.markdown("<p style='font-size: 14px;'>Wow, your story sounds amazing! Listen again anytime! ๐ŸŒŸ</p>", unsafe_allow_html=True)
79
 
80
+ # Adds a cheerful note to encourage more storytelling
81
  st.markdown(
82
  """
83
  <hr>
84
  <p style='text-align: center; font-size: 14px;'>
85
+ ๐Ÿ“ธ Ready for another tale? Upload a new picture for a fresh adventure!
86
+ Created with sparkles for awesome kids! ๐Ÿ’–
87
  </p>
88
  """,
89
  unsafe_allow_html=True