Update app.py
Browse files
app.py
CHANGED
|
@@ -6,43 +6,35 @@ import tempfile
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
| 9 |
-
#
|
| 10 |
-
def
|
| 11 |
"""Generates a caption for the given image using a pre-trained model."""
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
return
|
| 15 |
|
| 16 |
-
#
|
| 17 |
-
def
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
return story_text
|
| 22 |
|
| 23 |
-
#
|
| 24 |
-
def
|
| 25 |
-
"
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
tts_pipe = pipeline("text-to-audio", model="facebook/mms-tts-eng")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
return None, None
|
| 38 |
|
| 39 |
# Main application
|
| 40 |
def main():
|
| 41 |
-
st.title("Image to Story with Speech")
|
| 42 |
-
st.write("Upload an image to generate a caption, story, and audio narration")
|
| 43 |
-
|
| 44 |
-
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 45 |
-
|
| 46 |
if uploaded_image is not None:
|
| 47 |
try:
|
| 48 |
# Process image
|
|
@@ -57,19 +49,19 @@ def main():
|
|
| 57 |
|
| 58 |
# Generate caption
|
| 59 |
with st.spinner("Generating caption..."):
|
| 60 |
-
caption =
|
| 61 |
st.subheader("Generated Caption")
|
| 62 |
st.write(caption)
|
| 63 |
|
| 64 |
# Generate story
|
| 65 |
with st.spinner("Generating story..."):
|
| 66 |
-
story =
|
| 67 |
st.subheader("Generated Story")
|
| 68 |
st.write(story)
|
| 69 |
|
| 70 |
# Generate speech
|
| 71 |
with st.spinner("Generating audio..."):
|
| 72 |
-
audio_array, sample_rate =
|
| 73 |
if audio_array is not None:
|
| 74 |
st.subheader("Audio Narration")
|
| 75 |
st.audio(audio_array, sample_rate=sample_rate)
|
|
|
|
| 6 |
import os
|
| 7 |
|
| 8 |
|
| 9 |
+
# Generate caption from image
|
| 10 |
+
def image_to_caption(image_path):
|
| 11 |
"""Generates a caption for the given image using a pre-trained model."""
|
| 12 |
+
imgtocaption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 13 |
+
caption = imgtocaption(image_path)
|
| 14 |
+
return caption[0]['generated_text']
|
| 15 |
|
| 16 |
+
# Generate story from caption
|
| 17 |
+
def caption_to_story(text):
|
| 18 |
+
captiontostory = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 19 |
+
story = captiontostory(text, max_length=150, min_length=50)
|
| 20 |
+
return story[0]['generated_text']
|
|
|
|
| 21 |
|
| 22 |
+
# Convert story to speech
|
| 23 |
+
def story_to_audio(text):
|
| 24 |
+
tts_pipe = pipeline("text-to-audio", model="facebook/mms-tts-eng")
|
| 25 |
+
audio_output = tts_pipe(text[:1000])
|
| 26 |
+
return audio_output['audio'], audio_output['sampling_rate']
|
|
|
|
| 27 |
|
| 28 |
+
# Child-Friendly Interface Design
|
| 29 |
+
st.set_page_config(page_title="Magic Story House", page_icon="🧚")
|
| 30 |
+
st.title("🧚 Magic Image Story Generator")
|
| 31 |
+
st.markdown("Upload an image and generate your exclusive fairy tale!")
|
| 32 |
+
|
| 33 |
+
# File Upload
|
| 34 |
+
uploaded_image = st.file_uploader("Choose a picture", type=["jpg", "jpeg", "png"], key="image_uploader")
|
|
|
|
| 35 |
|
| 36 |
# Main application
|
| 37 |
def main():
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
if uploaded_image is not None:
|
| 39 |
try:
|
| 40 |
# Process image
|
|
|
|
| 49 |
|
| 50 |
# Generate caption
|
| 51 |
with st.spinner("Generating caption..."):
|
| 52 |
+
caption = image_to_caption(image_path)
|
| 53 |
st.subheader("Generated Caption")
|
| 54 |
st.write(caption)
|
| 55 |
|
| 56 |
# Generate story
|
| 57 |
with st.spinner("Generating story..."):
|
| 58 |
+
story = caption_to_story(caption)
|
| 59 |
st.subheader("Generated Story")
|
| 60 |
st.write(story)
|
| 61 |
|
| 62 |
# Generate speech
|
| 63 |
with st.spinner("Generating audio..."):
|
| 64 |
+
audio_array, sample_rate = story_to_audio(story)
|
| 65 |
if audio_array is not None:
|
| 66 |
st.subheader("Audio Narration")
|
| 67 |
st.audio(audio_array, sample_rate=sample_rate)
|