Charleshhh commited on
Commit
9e85403
·
verified ·
1 Parent(s): a5b667b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from gtts import gTTS
4
+ import io
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
+ story_generator = pipeline("text-generation", model="perplexity-ai/r1-1776", trust_remote_code=True)
16
+ story = story_generator(text, max_length=200, num_return_sequences=1)[0]['generated_text']
17
+ return story
18
+
19
+ # text2audio
20
+ def text2audio(story_text):
21
+ tts = gTTS(text=story_text, lang='en')
22
+ audio_file = io.BytesIO()
23
+ tts.write_to_fp(audio_file)
24
+ audio_file.seek(0)
25
+ return audio_file
26
+
27
+ st.set_page_config(page_title="Your Image to Audio Story",
28
+ page_icon="🦜")
29
+ st.header("Turn Your Image to Audio Story")
30
+ uploaded_file = st.file_uploader("Select an Image...")
31
+
32
+ if uploaded_file is not None:
33
+ print(uploaded_file)
34
+ bytes_data = uploaded_file.getvalue()
35
+ with open(uploaded_file.name, "wb") as file:
36
+ file.write(bytes_data)
37
+ st.image(uploaded_file, caption="Uploaded Image",
38
+ use_column_width=True)
39
+
40
+ #Stage 1: Image to Text
41
+ st.text('Processing img2text...')
42
+ scenario = img2text(uploaded_file.name)
43
+ st.write(scenario)
44
+
45
+ #Stage 2: Text to Story
46
+ st.text('Generating a story...')
47
+ story = text2story(scenario)
48
+ st.write(story)
49
+
50
+ #Stage 3: Story to Audio data
51
+ st.text('Generating audio data...')
52
+ audio_data = text2audio(story)
53
+
54
+ # Play button
55
+ if st.button("Play Audio"):
56
+ st.audio(audio_data,
57
+ format="audio/mpeg",
58
+ start_time=0)
59
+
60
+