hskwon7 commited on
Commit
6c53b18
·
verified ·
1 Parent(s): 3ac5eff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -12
app.py CHANGED
@@ -3,8 +3,9 @@
3
  app.py
4
 
5
  Streamlit application for Image-to-Story demo.
6
- Allows users to upload an image, generates a caption, creates a child-friendly story,
7
  and plays it back as audio.
 
8
  """
9
  import streamlit as st
10
  from PIL import Image
@@ -25,7 +26,8 @@ warnings.filterwarnings("ignore", category=DeprecationWarning)
25
  def main():
26
  st.title("🖼️ → 📖 Image-to-Story App for Kids")
27
  st.write(
28
- "Upload an image, get a caption, spin it into a story, and play it aloud!"
 
29
  )
30
 
31
  # Load pipelines (cached) with a friendly spinner
@@ -40,25 +42,37 @@ def main():
40
  story_gen = st.session_state.story_gen
41
  tts_pipe = st.session_state.tts_pipe
42
 
43
- # File uploader for images
44
- uploaded = st.file_uploader(
45
- "Upload an image (PNG, JPG, JPEG)", type=["png", "jpg", "jpeg"]
 
46
  )
47
- if not uploaded:
48
- return
49
 
50
- # Display uploaded image
51
- img = Image.open(uploaded)
 
 
 
 
 
 
 
 
 
 
 
 
52
  st.image(img, use_container_width=True)
53
 
54
  # Generate caption once
55
- if "caption" not in st.session_state:
56
  with st.spinner("Generating image caption…"):
57
  st.session_state.caption = generate_caption(captioner, img)
 
58
  st.markdown(f"**Caption:** {st.session_state.caption}")
59
 
60
  # Generate story once
61
- if "story" not in st.session_state:
62
  with st.spinner("Creating story for kids…"):
63
  st.session_state.story = generate_story(
64
  story_gen,
@@ -66,14 +80,16 @@ def main():
66
  min_words=50,
67
  max_words=100
68
  )
 
69
  st.markdown(f"**Story:** {st.session_state.story}")
70
 
71
  # Generate TTS audio once
72
- if "audio_data" not in st.session_state:
73
  with st.spinner("Synthesizing speech…"):
74
  audio_array, sr = generate_audio(tts_pipe, st.session_state.story)
75
  st.session_state.audio_data = audio_array
76
  st.session_state.audio_sr = sr
 
77
 
78
  # Audio playback button
79
  if st.button("🔊 Play Story Audio"):
 
3
  app.py
4
 
5
  Streamlit application for Image-to-Story demo.
6
+ Allows users to upload an image or use a demo image, generates a caption, creates a child-friendly story,
7
  and plays it back as audio.
8
+ Suitable for deployment on Hugging Face Spaces.
9
  """
10
  import streamlit as st
11
  from PIL import Image
 
26
  def main():
27
  st.title("🖼️ → 📖 Image-to-Story App for Kids")
28
  st.write(
29
+ "Upload an image or use the demo image to get an engaging story suitable for 3–10 year-olds, "
30
+ "with audio playback powered by Hugging Face pipelines!"
31
  )
32
 
33
  # Load pipelines (cached) with a friendly spinner
 
42
  story_gen = st.session_state.story_gen
43
  tts_pipe = st.session_state.tts_pipe
44
 
45
+ # Choose image source
46
+ source = st.radio(
47
+ "Choose image source:",
48
+ ("Upload my own image", "Use demo image")
49
  )
 
 
50
 
51
+ if source == "Use demo image":
52
+ # Load the bundled demo image
53
+ img = Image.open("test_kids_playing.jpg")
54
+ else:
55
+ uploaded = st.file_uploader(
56
+ "Upload an image (PNG, JPG, JPEG)",
57
+ type=["png", "jpg", "jpeg"],
58
+ key="upload"
59
+ )
60
+ if not uploaded:
61
+ return
62
+ img = Image.open(uploaded)
63
+
64
+ # Display image
65
  st.image(img, use_container_width=True)
66
 
67
  # Generate caption once
68
+ if "caption" not in st.session_state or source != st.session_state.get("last_source"):
69
  with st.spinner("Generating image caption…"):
70
  st.session_state.caption = generate_caption(captioner, img)
71
+ st.session_state.last_source = source
72
  st.markdown(f"**Caption:** {st.session_state.caption}")
73
 
74
  # Generate story once
75
+ if "story" not in st.session_state or source != st.session_state.get("last_source"):
76
  with st.spinner("Creating story for kids…"):
77
  st.session_state.story = generate_story(
78
  story_gen,
 
80
  min_words=50,
81
  max_words=100
82
  )
83
+ st.session_state.last_source = source
84
  st.markdown(f"**Story:** {st.session_state.story}")
85
 
86
  # Generate TTS audio once
87
+ if "audio_data" not in st.session_state or source != st.session_state.get("last_source"):
88
  with st.spinner("Synthesizing speech…"):
89
  audio_array, sr = generate_audio(tts_pipe, st.session_state.story)
90
  st.session_state.audio_data = audio_array
91
  st.session_state.audio_sr = sr
92
+ st.session_state.last_source = source
93
 
94
  # Audio playback button
95
  if st.button("🔊 Play Story Audio"):