namuisam commited on
Commit
3ca79e1
·
verified ·
1 Parent(s): 6df4568

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +60 -0
  2. requirements.txt +3 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+
4
+ # function part
5
+ # img2text
6
+ def img2text(url):
7
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
8
+ text = image_to_text_model(url)[0]["generated_text"]
9
+ return text
10
+
11
+ # text2story
12
+ def text2story(text):
13
+ text_generation_model = pipeline("text-generation", model="pranavpsv/genre-story-generator-v2")
14
+ story_text = text_generation_model(text, min_length=50, max_length=100, do_sample=True, early_stopping=True, top_p=0.4)[0]["generated_text"]
15
+ return story_text
16
+
17
+ # text2audio
18
+ def text2audio(story_text):
19
+ text2audio_model = pipeline("text-to-speech", model="Matthijs/mms-tts-eng")
20
+ gen_audio = text2audio_model(story_text)
21
+ return gen_audio
22
+
23
+ def main():
24
+ st.set_page_config(page_title="Your Image to Audio Story",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_container_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'], sample_rate = audio_data['sampling_rate'])
53
+ st.audio(audio_data['audio'],
54
+ format="audio/wav",
55
+ start_time=0,
56
+ sample_rate = audio_data['sampling_rate'])
57
+ # st.audio("kids_playing_audio.wav")
58
+
59
+ if __name__ == "__main__":
60
+ main()
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ streamlit
2
+ transformers
3
+ torch