Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,20 +1,23 @@
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
from transformers import pipeline
|
| 3 |
from gtts import gTTS
|
| 4 |
import os
|
| 5 |
|
| 6 |
-
#
|
|
|
|
| 7 |
def img2text(url):
|
| 8 |
-
image_to_text_model = pipeline("image-to-text",
|
|
|
|
| 9 |
text = image_to_text_model(url)[0]["generated_text"]
|
| 10 |
return text
|
| 11 |
|
| 12 |
-
#
|
| 13 |
def text2story(text):
|
| 14 |
-
story_text = text
|
| 15 |
return story_text
|
| 16 |
|
| 17 |
-
#
|
| 18 |
def text2audio(story_text):
|
| 19 |
# Convert text to audio using gTTS
|
| 20 |
tts = gTTS(story_text, lang="en")
|
|
@@ -22,32 +25,42 @@ def text2audio(story_text):
|
|
| 22 |
tts.save(audio_file)
|
| 23 |
return audio_file
|
| 24 |
|
| 25 |
-
#
|
| 26 |
-
st.set_page_config(page_title="Your Image to Audio Story",
|
|
|
|
| 27 |
st.header("Turn Your Image to Audio Story")
|
|
|
|
|
|
|
| 28 |
uploaded_file = st.file_uploader("Select an Image...")
|
| 29 |
|
|
|
|
| 30 |
if uploaded_file is not None:
|
| 31 |
print(uploaded_file)
|
| 32 |
bytes_data = uploaded_file.getvalue()
|
| 33 |
with open(uploaded_file.name, "wb") as file:
|
| 34 |
file.write(bytes_data)
|
| 35 |
-
st.image(uploaded_file, caption="Uploaded Image", use_column_width=True)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
| 38 |
st.text('Processing img2text...')
|
| 39 |
scenario = img2text(uploaded_file.name)
|
| 40 |
st.write(scenario)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
st.text('Generating a story...')
|
| 44 |
story = text2story(scenario)
|
| 45 |
st.write(story)
|
| 46 |
|
| 47 |
-
#
|
| 48 |
st.text('Generating audio data...')
|
| 49 |
-
|
| 50 |
|
| 51 |
# Play button
|
| 52 |
if st.button("Play Audio"):
|
| 53 |
-
st.audio(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# import part
|
| 2 |
import streamlit as st
|
| 3 |
from transformers import pipeline
|
| 4 |
from gtts import gTTS
|
| 5 |
import os
|
| 6 |
|
| 7 |
+
# function part
|
| 8 |
+
# img2text
|
| 9 |
def img2text(url):
|
| 10 |
+
image_to_text_model = pipeline("image-to-text",
|
| 11 |
+
model="Salesforce/blip-image-captioning-base")
|
| 12 |
text = image_to_text_model(url)[0]["generated_text"]
|
| 13 |
return text
|
| 14 |
|
| 15 |
+
# text2story
|
| 16 |
def text2story(text):
|
| 17 |
+
story_text = "pipeline("text-generation", model="perplexity-ai/r1-1776", trust_remote_code=True)" # to be completed
|
| 18 |
return story_text
|
| 19 |
|
| 20 |
+
# text2audio
|
| 21 |
def text2audio(story_text):
|
| 22 |
# Convert text to audio using gTTS
|
| 23 |
tts = gTTS(story_text, lang="en")
|
|
|
|
| 25 |
tts.save(audio_file)
|
| 26 |
return audio_file
|
| 27 |
|
| 28 |
+
# main part
|
| 29 |
+
st.set_page_config(page_title="Your Image to Audio Story",
|
| 30 |
+
page_icon="🦜") # prepare configuration
|
| 31 |
st.header("Turn Your Image to Audio Story")
|
| 32 |
+
|
| 33 |
+
# Upload image
|
| 34 |
uploaded_file = st.file_uploader("Select an Image...")
|
| 35 |
|
| 36 |
+
# If it is none, skip all the following things
|
| 37 |
if uploaded_file is not None:
|
| 38 |
print(uploaded_file)
|
| 39 |
bytes_data = uploaded_file.getvalue()
|
| 40 |
with open(uploaded_file.name, "wb") as file:
|
| 41 |
file.write(bytes_data)
|
|
|
|
| 42 |
|
| 43 |
+
st.image(uploaded_file, caption="Uploaded Image",
|
| 44 |
+
use_column_width=True)
|
| 45 |
+
|
| 46 |
+
#Stage 1: Image to Text
|
| 47 |
st.text('Processing img2text...')
|
| 48 |
scenario = img2text(uploaded_file.name)
|
| 49 |
st.write(scenario)
|
| 50 |
|
| 51 |
+
#Stage 2: Text to Story
|
| 52 |
st.text('Generating a story...')
|
| 53 |
story = text2story(scenario)
|
| 54 |
st.write(story)
|
| 55 |
|
| 56 |
+
#Stage 3: Story to Audio data
|
| 57 |
st.text('Generating audio data...')
|
| 58 |
+
audio_data =text2audio(story)
|
| 59 |
|
| 60 |
# Play button
|
| 61 |
if st.button("Play Audio"):
|
| 62 |
+
st.audio(audio_data['audio'],
|
| 63 |
+
format="audio/wav",
|
| 64 |
+
start_time=0,
|
| 65 |
+
sample_rate = audio_data['sampling_rate'])
|
| 66 |
+
st.audio("kids_playing_audio.wav")
|