Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
|
| 4 |
+
# function part
|
| 5 |
+
# img2text
|
| 6 |
+
def img2text(url):
|
| 7 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
| 8 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
| 9 |
+
return text
|
| 10 |
+
|
| 11 |
+
# text2story
|
| 12 |
+
def text2story(text):
|
| 13 |
+
text_to_story_model = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
| 14 |
+
story_text = text_to_story_model(text, max_new_tokens=150)[0]['generated_text']
|
| 15 |
+
|
| 16 |
+
words = story_text.split()
|
| 17 |
+
if len(words) > 100:
|
| 18 |
+
story_text = ' '.join(words[:100]) + '.'
|
| 19 |
+
|
| 20 |
+
return story_text
|
| 21 |
+
|
| 22 |
+
# text2audio
|
| 23 |
+
def text2audio(story_text):
|
| 24 |
+
story_to_audio_model = pipeline("text-to-speech", model="facebook/mms-tts-eng")
|
| 25 |
+
speech_data = story_to_audio_model(story_text)
|
| 26 |
+
return speech_data
|
| 27 |
+
|
| 28 |
+
# program part
|
| 29 |
+
st.set_page_config(page_title="Your Image to Audio Story",
|
| 30 |
+
page_icon="🦜")
|
| 31 |
+
st.header("Turn Your Image to Audio Story")
|
| 32 |
+
uploaded_file = st.file_uploader("Select an Image...")
|
| 33 |
+
|
| 34 |
+
if uploaded_file is not None:
|
| 35 |
+
print(uploaded_file)
|
| 36 |
+
bytes_data = uploaded_file.getvalue()
|
| 37 |
+
with open(uploaded_file.name, "wb") as file:
|
| 38 |
+
file.write(bytes_data)
|
| 39 |
+
st.image(uploaded_file, caption="Uploaded Image",
|
| 40 |
+
use_column_width=True)
|
| 41 |
+
|
| 42 |
+
#Stage 1: Image to Text
|
| 43 |
+
st.text('Processing img2text...')
|
| 44 |
+
scenario = img2text(uploaded_file.name)
|
| 45 |
+
st.write(scenario)
|
| 46 |
+
|
| 47 |
+
#Stage 2: Text to Story
|
| 48 |
+
st.text('Generating a story...')
|
| 49 |
+
story = text2story(scenario)
|
| 50 |
+
st.write(story)
|
| 51 |
+
|
| 52 |
+
#Stage 3: Story to Audio data
|
| 53 |
+
st.text('Generating audio data...')
|
| 54 |
+
audio_data =text2audio(story)
|
| 55 |
+
|
| 56 |
+
# Play button
|
| 57 |
+
if st.button("Play Audio"):
|
| 58 |
+
st.audio(audio_data['audio'],
|
| 59 |
+
format="audio/wav",
|
| 60 |
+
start_time=0,
|
| 61 |
+
sample_rate = audio_data['sampling_rate'])
|
| 62 |
+
st.audio("kids_playing_audio.wav")
|