Spaces:
Sleeping
Sleeping
# import part | |
import streamlit as st | |
from transformers import pipeline | |
import torch | |
# function part | |
# img2text | |
def img2text(url): | |
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base") | |
text = image_to_text_model(url)[0]["generated_text"] | |
# Make the caption simple and fun for kids | |
fun_caption = f"Look what we found! 🎨 {text}" | |
return fun_caption | |
# text2story | |
def text2story(text): | |
story_generator = pipeline("text-generation", model="distilgpt2") | |
# Generate a story with a maximum of 90 words | |
story = story_generator(text, max_length=90, num_return_sequences=1)[0]["generated_text"] | |
# Ensure the story does not exceed 90 words | |
story = " ".join(story.split()[:90]) # Truncate to 90 words | |
# Make the story simple and fun for kids | |
fun_story = f"Once upon a time... 🌟 {story}" | |
return fun_story | |
# text2audio | |
def text2audio(story_text): | |
tts_pipeline = pipeline("text-to-speech", model="espnet/kan-bayashi_ljspeech_vits") | |
audio_data = tts_pipeline(story_text) | |
return audio_data | |
# main part | |
st.set_page_config(page_title="Story Maker", page_icon="🦜") | |
st.header("Story Maker: Turn Your Picture into a Story!") | |
uploaded_file = st.file_uploader("Select an Image...") | |
if uploaded_file is not None: | |
bytes_data = uploaded_file.getvalue() | |
with open(uploaded_file.name, "wb") as file: | |
file.write(bytes_data) | |
st.image(uploaded_file, caption="Your Picture", use_container_width=True) | |
# Stage 1: Image to Text | |
st.text('✨ Discovering what’s in your picture...') | |
scenario = img2text(uploaded_file.name) | |
st.write(f"Here’s what we found: {scenario}") | |
# Stage 2: Text to Story | |
st.text('🎭 Creating a fun story for you...') | |
story = text2story(scenario) | |
st.write(story) | |
# Stage 3: Story to Audio data | |
st.text('🔊 Turning your story into audio...') | |
audio_data = text2audio(story) | |
# Play button | |
if st.button("Play Audio"): | |
st.audio(audio_data['audio'], | |
format="audio/wav", | |
start_time=0, | |
sample_rate=audio_data['sampling_rate']) |