Fanny1366 commited on
Commit
5866ba2
·
verified ·
1 Parent(s): 635a64f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from transformers import pipeline
3
+ from gtts import gTTS
4
+ import os
5
+
6
+ # Function: Image to Text
7
+ def img2text(url):
8
+ image_to_text_model = pipeline("image-to-text", model="Salesforce/blip-image-captioning-base")
9
+ text = image_to_text_model(url)[0]["generated_text"]
10
+ return text
11
+
12
+ # Function: Text to Story (Placeholder)
13
+ def text2story(text):
14
+ story_text = text # Placeholder for now
15
+ return story_text
16
+
17
+ # Function: Text to Audio
18
+ def text2audio(story_text):
19
+ # Convert text to audio using gTTS
20
+ tts = gTTS(story_text, lang="en")
21
+ audio_file = "story_audio.wav"
22
+ tts.save(audio_file)
23
+ return audio_file
24
+
25
+ # Streamlit App
26
+ st.set_page_config(page_title="Your Image to Audio Story", page_icon="🦜")
27
+ st.header("Turn Your Image to Audio Story")
28
+ uploaded_file = st.file_uploader("Select an Image...")
29
+
30
+ if uploaded_file is not None:
31
+ print(uploaded_file)
32
+ bytes_data = uploaded_file.getvalue()
33
+ with open(uploaded_file.name, "wb") as file:
34
+ file.write(bytes_data)
35
+ st.image(uploaded_file, caption="Uploaded Image", 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 = text2story(scenario)
45
+ st.write(story)
46
+
47
+ # Stage 3: Story to Audio
48
+ st.text('Generating audio data...')
49
+ audio_file = text2audio(story)
50
+
51
+ # Play button
52
+ if st.button("Play Audio"):
53
+ st.audio(audio_file, format="audio/wav")