Update app.py
Browse files
app.py
CHANGED
|
@@ -48,24 +48,26 @@ def clean_generated_story(raw_story: str) -> str:
|
|
| 48 |
clean_story = " ".join(filtered_words[:100])
|
| 49 |
return clean_story
|
| 50 |
|
| 51 |
-
def
|
|
|
|
|
|
|
|
|
|
| 52 |
pil_image = Image.open(image)
|
| 53 |
-
|
| 54 |
-
# Generate caption from the image
|
| 55 |
caption = captioner(pil_image)[0]["generated_text"]
|
| 56 |
st.write("**🌟 What's in the picture: 🌟**")
|
| 57 |
st.write(caption)
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
prompt = (
|
| 63 |
-
f"Write a funny,bright,and playful story for young children precisely centered on this scene {caption}\nStory:"
|
| 64 |
-
f"mention the exact place, location or venue within {caption}"
|
| 65 |
-
|
| 66 |
)
|
| 67 |
|
| 68 |
-
# Generate raw story from the model
|
| 69 |
raw = storyer(
|
| 70 |
prompt,
|
| 71 |
max_new_tokens=150,
|
|
@@ -75,22 +77,33 @@ def generate_content(image):
|
|
| 75 |
return_full_text=False
|
| 76 |
)[0]["generated_text"].strip()
|
| 77 |
|
| 78 |
-
# Clean the raw story using our custom function
|
| 79 |
story = clean_generated_story(raw)
|
| 80 |
-
|
| 81 |
st.write("**📖 Your funny story: 📖**")
|
| 82 |
st.write(story)
|
|
|
|
| 83 |
|
| 84 |
-
|
|
|
|
|
|
|
|
|
|
| 85 |
chunks = textwrap.wrap(story, width=200)
|
| 86 |
audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
|
| 87 |
-
|
| 88 |
# Save audio to a temporary file
|
| 89 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 90 |
sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
|
| 91 |
temp_file_path = temp_file.name
|
|
|
|
| 92 |
|
| 93 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
# Streamlit UI section
|
| 96 |
st.title("✨ Magic Story Maker ✨")
|
|
@@ -111,4 +124,4 @@ if st.button("✨ Make My Story! ✨"):
|
|
| 111 |
st.audio(audio_path, format="audio/wav")
|
| 112 |
os.remove(audio_path)
|
| 113 |
else:
|
| 114 |
-
st.warning("Please upload a picture first! 📸")
|
|
|
|
| 48 |
clean_story = " ".join(filtered_words[:100])
|
| 49 |
return clean_story
|
| 50 |
|
| 51 |
+
def get_caption(image) -> str:
|
| 52 |
+
"""
|
| 53 |
+
Takes an image and returns a generated caption.
|
| 54 |
+
"""
|
| 55 |
pil_image = Image.open(image)
|
|
|
|
|
|
|
| 56 |
caption = captioner(pil_image)[0]["generated_text"]
|
| 57 |
st.write("**🌟 What's in the picture: 🌟**")
|
| 58 |
st.write(caption)
|
| 59 |
+
return caption
|
| 60 |
|
| 61 |
+
def get_story(caption: str) -> str:
|
| 62 |
+
"""
|
| 63 |
+
Takes a caption and returns a funny, bright, and playful story targeted toward young children.
|
| 64 |
+
"""
|
| 65 |
prompt = (
|
| 66 |
+
f"Write a funny, bright, and playful story for young children precisely centered on this scene {caption}\nStory: "
|
| 67 |
+
f"mention the exact place, location or venue within {caption}. "
|
| 68 |
+
"Make the story magical and exciting, with lots of friendly descriptions that little ones can enjoy."
|
| 69 |
)
|
| 70 |
|
|
|
|
| 71 |
raw = storyer(
|
| 72 |
prompt,
|
| 73 |
max_new_tokens=150,
|
|
|
|
| 77 |
return_full_text=False
|
| 78 |
)[0]["generated_text"].strip()
|
| 79 |
|
|
|
|
| 80 |
story = clean_generated_story(raw)
|
|
|
|
| 81 |
st.write("**📖 Your funny story: 📖**")
|
| 82 |
st.write(story)
|
| 83 |
+
return story
|
| 84 |
|
| 85 |
+
def generate_audio(story: str) -> str:
|
| 86 |
+
"""
|
| 87 |
+
Converts a text story into speech audio and returns the file path for the audio.
|
| 88 |
+
"""
|
| 89 |
chunks = textwrap.wrap(story, width=200)
|
| 90 |
audio = np.concatenate([tts(chunk)["audio"].squeeze() for chunk in chunks])
|
| 91 |
+
|
| 92 |
# Save audio to a temporary file
|
| 93 |
with tempfile.NamedTemporaryFile(delete=False, suffix=".wav") as temp_file:
|
| 94 |
sf.write(temp_file.name, audio, tts.model.config.sampling_rate)
|
| 95 |
temp_file_path = temp_file.name
|
| 96 |
+
return temp_file_path
|
| 97 |
|
| 98 |
+
def generate_content(image):
|
| 99 |
+
"""
|
| 100 |
+
Pipeline that takes an image, generates a caption, a story based on that caption,
|
| 101 |
+
and produces an audio file from the story.
|
| 102 |
+
"""
|
| 103 |
+
caption = get_caption(image)
|
| 104 |
+
story = get_story(caption)
|
| 105 |
+
audio_path = generate_audio(story)
|
| 106 |
+
return caption, story, audio_path
|
| 107 |
|
| 108 |
# Streamlit UI section
|
| 109 |
st.title("✨ Magic Story Maker ✨")
|
|
|
|
| 124 |
st.audio(audio_path, format="audio/wav")
|
| 125 |
os.remove(audio_path)
|
| 126 |
else:
|
| 127 |
+
st.warning("Please upload a picture first! 📸")
|