Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,67 @@
|
|
| 1 |
# import part
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
|
|
|
|
|
|
| 4 |
import time
|
| 5 |
|
| 6 |
from transformers import pipeline
|
| 7 |
|
| 8 |
-
def generate_image_caption(image_path):
|
| 9 |
-
"""Generates a caption for the given image using a pre-trained model."""
|
| 10 |
-
img2caption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 11 |
-
result = img2caption(image_path)
|
| 12 |
-
return result[0]['generated_text']
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
-
# File uploader for image and audio
|
| 27 |
-
uploaded_image = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"])
|
| 28 |
-
uploaded_audio = st.file_uploader("Upload an audio file", type=["mp3", "wav", "ogg"])
|
| 29 |
|
| 30 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
if uploaded_image is not None:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# import part
|
| 2 |
import streamlit as st
|
| 3 |
from PIL import Image
|
| 4 |
+
from gtts import gTTS
|
| 5 |
+
import io
|
| 6 |
import time
|
| 7 |
|
| 8 |
from transformers import pipeline
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
+
def image_to_caption(image_path):
|
| 12 |
+
"""Generates a caption for the given image using a pre-trained model."""
|
| 13 |
+
imgtocaption = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 14 |
+
caption = imgtocaption(image_path)[0]['generated_text']
|
| 15 |
+
return caption
|
| 16 |
|
| 17 |
+
def caption_to_story(text):
|
| 18 |
+
"""Generates a story for the caption using a pre-trained model."""
|
| 19 |
+
captiontostory = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 20 |
+
story = captiontostory(text,max_length=150,min_length=50)[0]['generated_text']
|
| 21 |
+
return story
|
| 22 |
|
| 23 |
+
def story_to_audio(text):
|
| 24 |
+
"""Generates an audio for the story."""
|
| 25 |
+
audio = io.BytesIO()
|
| 26 |
+
tts = gTTS(text=text, lang='en', slow=False)
|
| 27 |
+
tts.write_to_fp(audio)
|
| 28 |
+
audio.seek(0)
|
| 29 |
+
return audio
|
| 30 |
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Child-Friendly Interface Design
|
| 33 |
+
st.set_page_config(page_title="Magic Story House", page_icon="🧚")
|
| 34 |
+
st.title("🧚 Magic Image Story Generator")
|
| 35 |
+
st.markdown("Upload an image and generate your exclusive fairy tale!")
|
| 36 |
+
|
| 37 |
+
# File Upload
|
| 38 |
+
uploaded_image = st.file_uploader("Choose a picture", type=["jpg", "jpeg", "png"], key="image_uploader")
|
| 39 |
+
|
| 40 |
+
# main part
|
| 41 |
if uploaded_image is not None:
|
| 42 |
+
# Display the uploaded image
|
| 43 |
+
st.image(uploaded_image, caption='Uploaded Image', use_column_width=True)
|
| 44 |
+
# Save the uploaded image as a temporary file since the pipeline requires a file path as input
|
| 45 |
+
import tempfile
|
| 46 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix='.jpg') as tmp_file:
|
| 47 |
+
tmp_file.write(uploaded_image.getvalue())
|
| 48 |
+
tmp_file_path = tmp_file.name
|
| 49 |
+
|
| 50 |
+
# Generate an image caption
|
| 51 |
+
caption = image_to_caption(tmp_file_path)
|
| 52 |
+
st.write(f"Caption: {caption}")
|
| 53 |
+
|
| 54 |
+
# Generate a story based on the caption
|
| 55 |
+
story = caption_to_story(caption)
|
| 56 |
+
st.write(f"Story: {story}")
|
| 57 |
+
|
| 58 |
+
# Convert the story to audio
|
| 59 |
+
audio = story_to_audio(story)
|
| 60 |
+
if audio:
|
| 61 |
+
st.audio(audio, format='audio/mp3')
|
| 62 |
+
|
| 63 |
+
# Delete the temporary file
|
| 64 |
+
import os
|
| 65 |
+
os.remove(tmp_file_path)
|
| 66 |
+
|
| 67 |
+
|