Rocky080808 commited on
Commit
a427fbe
·
verified ·
1 Parent(s): cc63710

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +72 -0
app.py ADDED
@@ -0,0 +1,72 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Your app.py goes here
2
+
3
+ # Program title: ______________
4
+
5
+
6
+
7
+ # import part
8
+
9
+ import streamlit as st
10
+ from transformers import pipeline
11
+
12
+
13
+ # function part - FOUR functions
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
+
21
+ # text2story
22
+ def text2story(text):
23
+ pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
24
+ story_text = pipe(text)[0]['generated_text']
25
+ return story_text
26
+
27
+ # text2audio
28
+ def text2audio(story_text):
29
+ pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
30
+ audio_data = pipe(story_text)
31
+ return audio_data
32
+
33
+ # main()
34
+
35
+ if __name__ == "__main__":
36
+ main()
37
+
38
+ def main():
39
+
40
+ st.set_page_config(page_title="Your Image to Audio Story",
41
+ page_icon="🦜")
42
+ st.header("Turn Your Image to Audio Story")
43
+ uploaded_file = st.file_uploader("Select an Image...")
44
+
45
+ if uploaded_file is not None:
46
+ print(uploaded_file)
47
+ bytes_data = uploaded_file.getvalue()
48
+ with open(uploaded_file.name, "wb") as file:
49
+ file.write(bytes_data)
50
+ st.image(uploaded_file, caption="Uploaded Image",
51
+ use_column_width=True)
52
+
53
+ #Stage 1: Image to Text
54
+ st.text('Processing img2text...')
55
+ scenario = img2text(uploaded_file.name)
56
+ st.write(scenario)
57
+
58
+ #Stage 2: Text to Story
59
+ st.text('Generating a story...')
60
+ story = text2story(scenario)
61
+ st.write(story)
62
+
63
+ #Stage 3: Story to Audio data
64
+ st.text('Generating audio data...')
65
+ audio_data =text2audio(story)
66
+
67
+ # Play button
68
+ if st.button("Play Audio"):
69
+ st.audio(audio_data['audio'],
70
+ format="audio/wav",
71
+ start_time=0,
72
+ sample_rate = audio_data['sampling_rate'])