Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
def generate_caption(image):
|
| 7 |
+
image_to_text = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 8 |
+
caption = image_to_text(image)[0]["generated_text"]
|
| 9 |
+
return caption
|
| 10 |
+
|
| 11 |
+
def generate_story(caption):
|
| 12 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 13 |
+
story = pipe(caption)[0]['generated_text']
|
| 14 |
+
return story
|
| 15 |
+
|
| 16 |
+
def generate_audio(story):
|
| 17 |
+
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 18 |
+
audio = pipe(story)
|
| 19 |
+
return audio
|
| 20 |
+
|
| 21 |
+
# Streamlit UI
|
| 22 |
+
|
| 23 |
+
# Title of the Streamlit app
|
| 24 |
+
st.title("Upload your image for an instant storytelling!")
|
| 25 |
+
|
| 26 |
+
# Write a description
|
| 27 |
+
st.write("This app is designed for 3-10 year-old kids by allowing them uploading image for fun storytelling entertainment.")
|
| 28 |
+
|
| 29 |
+
# File uploader for image
|
| 30 |
+
uploaded_file = st.file_uploader("Upload an image", type=["png", "jpg", "jpeg"])
|
| 31 |
+
|
| 32 |
+
if uploaded_file is not None:
|
| 33 |
+
# Display the uploaded image
|
| 34 |
+
image = Image.open(uploaded_file)
|
| 35 |
+
st.image(image, caption="Uploaded Image", use_container_width=True)
|
| 36 |
+
|
| 37 |
+
# Generate Image Caption
|
| 38 |
+
image_caption = generate_caption(image)
|
| 39 |
+
|
| 40 |
+
# Display results
|
| 41 |
+
st.subheader("Image Caption:")
|
| 42 |
+
st.write(f"{image_caption}")
|
| 43 |
+
|
| 44 |
+
# Generate Story
|
| 45 |
+
story_telling = generate_story(image_caption)
|
| 46 |
+
|
| 47 |
+
# Display results
|
| 48 |
+
st.subheader("Story:")
|
| 49 |
+
st.write(f"{story_telling}")
|
| 50 |
+
|
| 51 |
+
# Generate Audio
|
| 52 |
+
audio = generate_audio(story_telling)
|
| 53 |
+
|
| 54 |
+
# Display an audio file with a spinner effect
|
| 55 |
+
if st.button("Play Audio"):
|
| 56 |
+
st.audio(audio['audio'],
|
| 57 |
+
format="audio/wav",
|
| 58 |
+
start_time=0,
|
| 59 |
+
sample_rate = audio['sampling_rate'])
|