May_7th_2024 / app.py
Dominic0406's picture
Update app.py
71ac8b2 verified
raw
history blame contribute delete
No virus
1.68 kB
import streamlit as st
from transformers import pipeline
st.set_page_config(page_title="Your Image to Audio Story",
page_icon="🦜")
st.header("Turn Your Image to Audio Story")
uploaded_file = st.file_uploader("Select an Image...")
if uploaded_file is not None:
print(uploaded_file)
bytes_data = uploaded_file.getvalue()
with open(uploaded_file.name, "wb") as file:
file.write(bytes_data)
st.image(uploaded_file, caption="Uploaded Image",
use_column_width=True)
#Define function:
def img2txt(imgname):
pipe = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
scenario = pipe(imgname)
return scenario[0]['generated_text']
def txt2story(txtname):
pipe = pipeline("text-generation", model="openai-community/gpt2")
story = pipe(txtname)
return story[0]["generated_text"]
def text2audio(textname):
pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
audio_data = pipe(textname)
return audio_data
#Stage 1: Image to Text
st.text('Processing img2text...')
scenario = img2txt(uploaded_file.name)
st.write(scenario)
#Stage 2: Text to Story
st.text('Generating a story...')
story = txt2story(scenario)
st.write(story)
#Stage 3: Story to Audio data
st.text('Generating audio data...')
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'])