Prof-Hunt commited on
Commit
0826abd
·
verified ·
1 Parent(s): e9eeb50

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -5
app.py CHANGED
@@ -13,6 +13,7 @@ from datetime import datetime
13
  import spaces
14
  from kokoro import KModel, KPipeline
15
  import soundfile as sf
 
16
 
17
  def clear_memory():
18
  """Helper function to clear both CUDA and system memory, safe for Spaces environment"""
@@ -153,18 +154,55 @@ def analyze_image(image):
153
 
154
  @torch.inference_mode()
155
  @spaces.GPU(duration=30)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  def generate_story(image_description):
157
  clear_memory()
158
 
159
- story_prompt = f"""Write a short children's story (about 500 words) based on this scene: {image_description}
160
 
161
  Requirements:
162
  1. Main character: An English bulldog named Champ
163
  2. Include these values: confidence, teamwork, caring, and hope
164
  3. Theme: "Doing the right thing is important"
165
  4. Keep it simple and engaging for young children
166
- 5. End with a simple moral lesson
167
- 6. Each paragraph needs to be three sentences or less for readability"""
168
 
169
  try:
170
  messages = [{"role": "user", "content": story_prompt}]
@@ -181,9 +219,10 @@ def generate_story(image_description):
181
  repetition_penalty=1.2
182
  )
183
 
 
184
  story = tokenizer_lm.decode(outputs[0])
185
- story = clean_story_output(story)
186
-
187
  clear_memory()
188
  return story
189
 
@@ -192,6 +231,7 @@ def generate_story(image_description):
192
  clear_memory()
193
  return "Error generating story. Please try again."
194
 
 
195
  @torch.inference_mode()
196
  @spaces.GPU(duration=30)
197
  def generate_image_prompts(story_text):
 
13
  import spaces
14
  from kokoro import KModel, KPipeline
15
  import soundfile as sf
16
+ import math
17
 
18
  def clear_memory():
19
  """Helper function to clear both CUDA and system memory, safe for Spaces environment"""
 
154
 
155
  @torch.inference_mode()
156
  @spaces.GPU(duration=30)
157
+ def format_story_paragraphs(story_text, max_paragraphs=9):
158
+ """Formats the story by evenly distributing sentences over a max of 9 paragraphs."""
159
+
160
+ # Remove unwanted tokens and artifacts
161
+ story_text = story_text.replace("<|im_end|>", "").strip()
162
+
163
+ # Split into sentences
164
+ sentences = re.split(r'(?<=[.!?])\s+', story_text)
165
+ sentences = [s.strip() for s in sentences if s.strip()] # Remove empty sentences
166
+
167
+ # Determine optimal sentence distribution
168
+ total_sentences = len(sentences)
169
+ num_paragraphs = min(max_paragraphs, total_sentences) # Ensure we do not exceed max paragraphs
170
+ sentences_per_paragraph = math.ceil(total_sentences / num_paragraphs) # Distribute evenly
171
+
172
+ # Group sentences into paragraphs
173
+ paragraphs = []
174
+ current_paragraph = []
175
+
176
+ for sentence in sentences:
177
+ current_paragraph.append(sentence)
178
+
179
+ # Ensure even distribution across paragraphs
180
+ if len(current_paragraph) == sentences_per_paragraph:
181
+ paragraphs.append(" ".join(current_paragraph))
182
+ current_paragraph = []
183
+
184
+ # Stop if we reach max paragraphs
185
+ if len(paragraphs) == max_paragraphs:
186
+ break
187
+
188
+ # If there are leftover sentences, add them as a final paragraph
189
+ if current_paragraph:
190
+ paragraphs.append(" ".join(current_paragraph))
191
+
192
+ # Join paragraphs with a double newline
193
+ return "\n\n".join(paragraphs)
194
+
195
  def generate_story(image_description):
196
  clear_memory()
197
 
198
+ story_prompt = f"""Write a short children's story (about 200 words) based on this scene: {image_description}
199
 
200
  Requirements:
201
  1. Main character: An English bulldog named Champ
202
  2. Include these values: confidence, teamwork, caring, and hope
203
  3. Theme: "Doing the right thing is important"
204
  4. Keep it simple and engaging for young children
205
+ 5. End with a simple moral lesson"""
 
206
 
207
  try:
208
  messages = [{"role": "user", "content": story_prompt}]
 
219
  repetition_penalty=1.2
220
  )
221
 
222
+ # Decode and format the story
223
  story = tokenizer_lm.decode(outputs[0])
224
+ story = format_story_paragraphs(story) # Evenly distribute sentences
225
+
226
  clear_memory()
227
  return story
228
 
 
231
  clear_memory()
232
  return "Error generating story. Please try again."
233
 
234
+
235
  @torch.inference_mode()
236
  @spaces.GPU(duration=30)
237
  def generate_image_prompts(story_text):