Spaces:
Sleeping
Sleeping
Leo Liu
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,50 +1,58 @@
|
|
1 |
-
import
|
2 |
from transformers import pipeline
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# import part
|
2 |
from transformers import pipeline
|
3 |
+
import streamlit as st
|
4 |
+
|
5 |
+
# function part
|
6 |
+
# img2text
|
7 |
+
def img2text(url):
|
8 |
+
image_to_text_model = pipeline("image-to-text", model="nlpconnect/vit-gpt2-image-captioning")
|
9 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
10 |
+
return text
|
11 |
+
|
12 |
+
# text2story
|
13 |
+
def text2story(text):
|
14 |
+
story_text = pipeline("text-generation", model="distilbert/distilgpt2")
|
15 |
+
return story_text
|
16 |
+
|
17 |
+
# text2audio
|
18 |
+
def text2audio(story_text):
|
19 |
+
audio_data = pipeline("text-to-audio", model="facebook/musicgen-medium")
|
20 |
+
return audio_data
|
21 |
+
|
22 |
+
# main part
|
23 |
+
st.set_page_config(page_title="Your Image to Audio Story",
|
24 |
+
page_icon="🦜")
|
25 |
+
st.header("Turn Your Image to Audio Story")
|
26 |
+
|
27 |
+
# Upload image here
|
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",
|
36 |
+
use_column_width=True)
|
37 |
+
|
38 |
+
#Stage 1: Image to Text
|
39 |
+
st.text('Processing img2text...')
|
40 |
+
scenario = img2text(uploaded_file.name)
|
41 |
+
st.write(scenario)
|
42 |
+
|
43 |
+
#Stage 2: Text to Story
|
44 |
+
st.text('Generating a story...')
|
45 |
+
story = text2story(scenario)
|
46 |
+
st.write(story)
|
47 |
+
|
48 |
+
#Stage 3: Story to Audio data
|
49 |
+
st.text('Generating audio data...')
|
50 |
+
audio_data =text2audio(story)
|
51 |
+
|
52 |
+
# Play button
|
53 |
+
if st.button("Play Audio"):
|
54 |
+
st.audio(audio_data['audio'],
|
55 |
+
format="audio/wav",
|
56 |
+
start_time=0,
|
57 |
+
sample_rate = audio_data['sampling_rate'])
|
58 |
+
st.audio("kids_playing_audio.wav")
|