Update app.py
Browse files
app.py
CHANGED
@@ -1,20 +1,67 @@
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
from transformers import pipeline
|
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
def main():
|
5 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
-
|
8 |
-
|
|
|
|
|
9 |
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
confidence = result[0]["score"]
|
15 |
|
16 |
-
|
17 |
-
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
if __name__ == "__main__":
|
20 |
-
main()
|
|
|
|
1 |
+
# Your app.py goes here
|
2 |
+
# Program title: ______________
|
3 |
+
|
4 |
+
|
5 |
+
# import part
|
6 |
import streamlit as st
|
7 |
from transformers import pipeline
|
8 |
|
9 |
+
|
10 |
+
# function part - FOUR functions
|
11 |
+
|
12 |
+
|
13 |
+
# function part
|
14 |
+
# img2text
|
15 |
+
def img2text(url):
|
16 |
+
image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
|
17 |
+
text = image_to_text_model(url)[0]["generated_text"]
|
18 |
+
return text
|
19 |
+
|
20 |
+
# text2story
|
21 |
+
def text2story(text):
|
22 |
+
pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
|
23 |
+
story_text = pipe(text)[0]['generated_text']
|
24 |
+
return story_text
|
25 |
+
|
26 |
+
# text2audio
|
27 |
+
def text2audio(story_text):
|
28 |
+
pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
|
29 |
+
audio_data = pipe(story_text)
|
30 |
+
return audio_data
|
31 |
+
|
32 |
def main():
|
33 |
+
uploaded_file = st.file_uploader("Select an Image...")
|
34 |
+
|
35 |
+
if uploaded_file is not None:
|
36 |
+
print(uploaded_file)
|
37 |
+
bytes_data = uploaded_file.getvalue()
|
38 |
+
with open(uploaded_file.name, "wb") as file:
|
39 |
+
file.write(bytes_data)
|
40 |
+
st.image(uploaded_file, caption="Uploaded Image",
|
41 |
+
use_column_width=True)
|
42 |
|
43 |
+
#Stage 1: Image to Text
|
44 |
+
st.text('Processing img2text...')
|
45 |
+
scenario = img2text(uploaded_file.name)
|
46 |
+
st.write(scenario)
|
47 |
|
48 |
+
#Stage 2: Text to Story
|
49 |
+
st.text('Generating a story...')
|
50 |
+
story = txt2story(scenario)
|
51 |
+
st.write(story)
|
|
|
52 |
|
53 |
+
#Stage 3: Story to Audio data
|
54 |
+
st.text('Generating audio data...')
|
55 |
+
audio_data =text2audio(story)
|
56 |
|
57 |
+
# Play button
|
58 |
+
if st.button("Play Audio"):
|
59 |
+
st.audio(audio_data['audio'],
|
60 |
+
format="audio/wav",
|
61 |
+
start_time=0,
|
62 |
+
sample_rate = audio_data['sampling_rate'])
|
63 |
+
|
64 |
+
# main program part
|
65 |
if __name__ == "__main__":
|
66 |
+
main()
|
67 |
+
|