djohari commited on
Commit
0cfb5ba
·
verified ·
1 Parent(s): 6b79f90

Create app.py

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