mxiean commited on
Commit
0f308a3
·
verified ·
1 Parent(s): c956bf8

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -0
app.py ADDED
@@ -0,0 +1,64 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import part
2
+ import streamlit as st
3
+ from transformers import pipeline
4
+ import torch
5
+
6
+ # function part
7
+ # img2text
8
+ def img2text(url):
9
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
10
+ text = image_to_text_model(url)[0]["generated_text"]
11
+ return text
12
+
13
+ # text2story
14
+ def text2story(text):
15
+ pipe = pipeline("text-generation", model="gpt2")
16
+ story_text = pipe(text, max_length=100)[0]['generated_text']
17
+ return story_text
18
+
19
+ # text2audio
20
+ def text2audio(story_text):
21
+ pipe = pipeline("text-to-speech", model="facebook/mms-tts-eng")
22
+ audio_data = pipe(story_text)
23
+ return audio_data
24
+
25
+
26
+ # main part
27
+
28
+ st.set_page_config(page_title="Your Image to Audio Story",
29
+ page_icon="🦜")
30
+ st.header("Turn Your Image to Audio Story")
31
+ uploaded_file = st.file_uploader("Select an Image...")#, type=["jpg", "jpeg", "png"])
32
+
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", use_column_width=True)
40
+
41
+
42
+
43
+ #Stage 1: Image to Text
44
+ st.text('Processing img2text...')
45
+ scenario = img2text(uploaded_file.name)
46
+ st.write(scenario)
47
+
48
+
49
+ #Stage 2: Text to Story
50
+ st.text('Generating a story...')
51
+ story = text2story(scenario)
52
+ st.write(story)
53
+
54
+ #Stage 3: Story to Audio data
55
+ st.text('Generating audio data...')
56
+ audio_data =text2audio(story)
57
+
58
+ # Play button
59
+ if st.button("Play Audio"):
60
+ st.audio(audio_data['audio'],
61
+ format="audio/wav",
62
+ start_time=0,
63
+ sample_rate = audio_data['sampling_rate'])
64
+ # st.audio("kids_playing_audio.wav")