Szeyu commited on
Commit
890cd41
·
verified ·
1 Parent(s): fc42b63

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -14
app.py CHANGED
@@ -22,12 +22,18 @@ captioner, storyer, tts = load_pipelines()
22
  def clean_generated_story(raw_story: str) -> str:
23
  """
24
  Cleans the generated story by:
25
- 1. Removing digits.
26
- 2. Removing words that are likely random letter combinations based on having no vowels.
27
- 3. Removing single-letter words unless they are allowed (such as 'a' or 'I').
 
28
  """
29
- # Remove all digits using regex
30
- story_without_numbers = re.sub(r'\d+', '', raw_story)
 
 
 
 
 
31
 
32
  vowels = set('aeiouAEIOU')
33
 
@@ -40,11 +46,11 @@ def clean_generated_story(raw_story: str) -> str:
40
  return False
41
  return True
42
 
43
- # Split the story into words, apply filtering, and recombine into a clean story
44
  words = story_without_numbers.split()
45
  filtered_words = [word for word in words if is_valid_word(word)]
46
 
47
- # Optionally, you can trim the clean story to a certain word count
48
  clean_story = " ".join(filtered_words[:100])
49
  return clean_story
50
 
@@ -63,8 +69,7 @@ def get_story(caption: str) -> str:
63
  Takes a caption and returns a funny, bright, and playful story targeted toward young children.
64
  """
65
  prompt = (
66
- f"Write a funny and playful story for young children"
67
- f"precisely centered on this scene {caption}\nStory:"
68
  f"mention the exact place, location or venue within {caption}. "
69
  f"Make the story magical and exciting."
70
  )
@@ -85,12 +90,12 @@ def get_story(caption: str) -> str:
85
 
86
  def generate_audio(story: str) -> str:
87
  """
88
- Converts a text story into speech audio and returns the file path for the audio.
89
  """
90
  chunks = textwrap.wrap(story, width=200)
91
  audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
92
 
93
- # Save audio to a temporary file
94
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
95
  sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
96
  temp_file_path = temp_file.name
@@ -98,8 +103,10 @@ def generate_audio(story: str) -> str:
98
 
99
  def generate_content(image):
100
  """
101
- Pipeline that takes an image, generates a caption, a story based on that caption,
102
- and produces an audio file from the story.
 
 
103
  """
104
  caption = get_caption(image)
105
  story = get_story(caption)
@@ -125,4 +132,4 @@ if st.button("✨ Make My Story! ✨"):
125
  st.audio(audio_path, format="audio/wav")
126
  os.remove(audio_path)
127
  else:
128
- st.warning("Please upload a picture first! 📸")
 
22
  def clean_generated_story(raw_story: str) -> str:
23
  """
24
  Cleans the generated story by:
25
+ 1. Removing URLs.
26
+ 2. Removing digits.
27
+ 3. Removing words likely to be random letter combinations based on having no vowels.
28
+ 4. Removing single-letter words unless allowed (such as 'a' or 'I').
29
  """
30
+ # Remove URLs starting with http://, https://, or www.
31
+ no_urls = re.sub(r'\b(?:https?://|www\.)\S+\b', '', raw_story)
32
+ # Remove domain names without protocol (e.g., erskybooks.com)
33
+ no_urls = re.sub(r'\b\w+\.(com|net|org|co\.uk|ca\.us|me)\b', '', no_urls)
34
+
35
+ # Remove all digits
36
+ story_without_numbers = re.sub(r'\d+', '', no_urls)
37
 
38
  vowels = set('aeiouAEIOU')
39
 
 
46
  return False
47
  return True
48
 
49
+ # Split the cleaned text into words, filter them, and reassemble
50
  words = story_without_numbers.split()
51
  filtered_words = [word for word in words if is_valid_word(word)]
52
 
53
+ # Trim the cleaned story to the first 100 words (optional)
54
  clean_story = " ".join(filtered_words[:100])
55
  return clean_story
56
 
 
69
  Takes a caption and returns a funny, bright, and playful story targeted toward young children.
70
  """
71
  prompt = (
72
+ f"Write a funny and playful story for young children precisely centered on this scene {caption}\nStory: "
 
73
  f"mention the exact place, location or venue within {caption}. "
74
  f"Make the story magical and exciting."
75
  )
 
90
 
91
  def generate_audio(story: str) -> str:
92
  """
93
+ Converts the text story into speech audio and returns the file path for the audio.
94
  """
95
  chunks = textwrap.wrap(story, width=200)
96
  audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
97
 
98
+ # Save the audio to a temporary file and return its path.
99
  with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
100
  sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
101
  temp_file_path = temp_file.name
 
103
 
104
  def generate_content(image):
105
  """
106
+ Pipeline function that:
107
+ - Generates a caption from the uploaded image.
108
+ - Uses the caption to generate a story.
109
+ - Converts the story to speech audio.
110
  """
111
  caption = get_caption(image)
112
  story = get_story(caption)
 
132
  st.audio(audio_path, format="audio/wav")
133
  os.remove(audio_path)
134
  else:
135
+ st.warning("Please upload a picture first! 📸")