huimanho commited on
Commit
5479a0b
1 Parent(s): 24e2a38

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +59 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #programe title: read photo to speech
2
+
3
+
4
+
5
+ def img2text(url):
6
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
7
+ text = image_to_text_model(url)[0]["generated_text"]
8
+ return text
9
+
10
+ # text2story
11
+ def text2story(text):
12
+ pipe = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
13
+ story_text = pipe(text)[0]['generated_text']
14
+ return story_text
15
+
16
+ # text2audio
17
+ def text2audio(story_text):
18
+ pipe = pipeline("text-to-audio", model="Matthijs/mms-tts-eng")
19
+ audio_data = pipe(story_text)
20
+ return audio_data
21
+
22
+ def main():
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
+ uploaded_file = st.file_uploader("Select an Image...")
27
+
28
+ if uploaded_file is not None:
29
+ print(uploaded_file)
30
+ bytes_data = uploaded_file.getvalue()
31
+ with open(uploaded_file.name, "wb") as file:
32
+ file.write(bytes_data)
33
+ st.image(uploaded_file, caption="Uploaded Image",
34
+ use_column_width=True)
35
+
36
+ #Stage 1: Image to Text
37
+ st.text('Processing img2text...')
38
+ scenario = img2text(uploaded_file.name)
39
+ st.write(scenario)
40
+
41
+ #Stage 2: Text to Story
42
+ st.text('Generating a story...')
43
+ story = text2story(scenario)
44
+ st.write(story)
45
+
46
+ #Stage 3: Story to Audio data
47
+ st.text('Generating audio data...')
48
+ audio_data =text2audio(story)
49
+
50
+ # Play button
51
+ if st.button("Play Audio"):
52
+ st.audio(audio_data['audio'],
53
+ format="audio/wav",
54
+ start_time=0,
55
+ sample_rate = audio_data['sampling_rate'])
56
+
57
+ # main program part
58
+ if __name__ == "__main__":
59
+ main()