Szeyu commited on
Commit
55eead0
·
verified ·
1 Parent(s): 1da8d3f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -11
app.py CHANGED
@@ -33,14 +33,14 @@ st.markdown(
33
  unsafe_allow_html=True
34
  )
35
 
36
- # ------------------ Model Loading ------------------
37
  def load_models():
38
  """
39
- Lazy-load the pipelines and store them in session state.
40
  Pipelines:
41
  1. Captioner: Generates descriptive text from an image.
42
  2. Storyer: Generates a humorous children's story using aspis/gpt2-genre-story-generation.
43
- 3. TTS: Converts text into spoken audio.
44
  """
45
  if "captioner" not in st.session_state:
46
  st.session_state.captioner = pipeline(
@@ -62,11 +62,10 @@ def load_models():
62
  @st.cache_data(show_spinner=False)
63
  def get_caption(image_bytes):
64
  """
65
- Convert the image bytes into a lower resolution image to speed up captioning,
66
- then generate and return the caption.
67
  """
68
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
69
- # Resize the image to speed up processing (keeping aspect ratio)
70
  image.thumbnail((384, 384))
71
  caption = st.session_state.captioner(image)[0]["generated_text"]
72
  return caption
@@ -75,21 +74,33 @@ def get_caption(image_bytes):
75
  def get_story(caption):
76
  """
77
  Generate a humorous and engaging children's story using the caption.
 
 
 
 
 
78
  """
79
  prompt = (
80
  f"Write a funny, warm, and imaginative children's story for ages 3-10, 50-100 words, "
81
  f"in third-person narrative, as if the author is playfully describing the scene in the image: {caption}. "
82
- "Explicitly mention the exact venue or location (e.g. a park, school, or home), describe specific characters "
83
  "(for example, a little girl named Lily or a boy named Jack), and detail the humorous actions they perform. "
84
  "Ensure the story is playful, engaging, and ends with a complete sentence."
85
  )
 
86
  raw_story = st.session_state.storyer(
87
  prompt,
88
  max_new_tokens=100,
89
  do_sample=True,
90
  temperature=0.7,
91
- top_p=0.9
 
92
  )[0]["generated_text"].strip()
 
 
 
 
 
93
  words = raw_story.split()
94
  return " ".join(words[:100])
95
 
@@ -97,7 +108,8 @@ def get_story(caption):
97
  def get_audio(story):
98
  """
99
  Convert the generated story text into audio.
100
- The text is split into 300-character chunks to reduce repeated TTS calls.
 
101
  """
102
  chunks = textwrap.wrap(story, width=300)
103
  audio_chunks = [st.session_state.tts(chunk)["audio"].squeeze() for chunk in chunks]
@@ -111,12 +123,13 @@ def get_audio(story):
111
  uploaded_file = st.file_uploader("Choose a Picture...", type=["jpg", "jpeg", "png"])
112
  if uploaded_file is not None:
113
  try:
114
- load_models() # Ensure models are loaded once
115
  image_bytes = uploaded_file.getvalue()
116
- # Display image
117
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
118
  st.image(image, caption="Your Amazing Picture!", use_column_width=True)
119
  st.markdown("<h3 style='text-align: center;'>Ready for your story?</h3>", unsafe_allow_html=True)
 
120
  if st.button("Story, Please!"):
121
  with st.spinner("Generating caption..."):
122
  caption = get_caption(image_bytes)
 
33
  unsafe_allow_html=True
34
  )
35
 
36
+ # ------------------ Lazy Model Loading ------------------
37
  def load_models():
38
  """
39
+ Lazy-load the required pipelines and store them in session state.
40
  Pipelines:
41
  1. Captioner: Generates descriptive text from an image.
42
  2. Storyer: Generates a humorous children's story using aspis/gpt2-genre-story-generation.
43
+ 3. TTS: Converts text into audio.
44
  """
45
  if "captioner" not in st.session_state:
46
  st.session_state.captioner = pipeline(
 
62
  @st.cache_data(show_spinner=False)
63
  def get_caption(image_bytes):
64
  """
65
+ Convert the image bytes into a lower resolution image and generate the caption.
 
66
  """
67
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
68
+ # Resize the image (preserving aspect ratio) to speed up processing.
69
  image.thumbnail((384, 384))
70
  caption = st.session_state.captioner(image)[0]["generated_text"]
71
  return caption
 
74
  def get_story(caption):
75
  """
76
  Generate a humorous and engaging children's story using the caption.
77
+ The prompt instructs the model to write a playful story for ages 3-10 (50-100 words) that includes:
78
+ - The venue or location (e.g. park, school, or home),
79
+ - Specific characters (like a little girl named Lily or a boy named Jack),
80
+ - Humorous actions.
81
+ The generated text should not include the prompt.
82
  """
83
  prompt = (
84
  f"Write a funny, warm, and imaginative children's story for ages 3-10, 50-100 words, "
85
  f"in third-person narrative, as if the author is playfully describing the scene in the image: {caption}. "
86
+ "Explicitly mention the exact venue or location (such as a park, school, or home), describe specific characters "
87
  "(for example, a little girl named Lily or a boy named Jack), and detail the humorous actions they perform. "
88
  "Ensure the story is playful, engaging, and ends with a complete sentence."
89
  )
90
+ # Set return_full_text=False so the prompt isn't included in the output.
91
  raw_story = st.session_state.storyer(
92
  prompt,
93
  max_new_tokens=100,
94
  do_sample=True,
95
  temperature=0.7,
96
+ top_p=0.9,
97
+ return_full_text=False
98
  )[0]["generated_text"].strip()
99
+
100
+ # Fallback check: if the generated text starts with the prompt, remove it.
101
+ if raw_story.startswith(prompt):
102
+ raw_story = raw_story[len(prompt):].strip()
103
+
104
  words = raw_story.split()
105
  return " ".join(words[:100])
106
 
 
108
  def get_audio(story):
109
  """
110
  Convert the generated story text into audio.
111
+ The text is split into 300-character chunks to reduce repeated TTS calls,
112
+ the resulting audio arrays are concatenated, and the output is stored in an in‑memory WAV buffer.
113
  """
114
  chunks = textwrap.wrap(story, width=300)
115
  audio_chunks = [st.session_state.tts(chunk)["audio"].squeeze() for chunk in chunks]
 
123
  uploaded_file = st.file_uploader("Choose a Picture...", type=["jpg", "jpeg", "png"])
124
  if uploaded_file is not None:
125
  try:
126
+ load_models() # Ensure models are loaded
127
  image_bytes = uploaded_file.getvalue()
128
+ # Display the image
129
  image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
130
  st.image(image, caption="Your Amazing Picture!", use_column_width=True)
131
  st.markdown("<h3 style='text-align: center;'>Ready for your story?</h3>", unsafe_allow_html=True)
132
+
133
  if st.button("Story, Please!"):
134
  with st.spinner("Generating caption..."):
135
  caption = get_caption(image_bytes)