Update app.py
Browse files
app.py
CHANGED
|
@@ -1,42 +1,91 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
from gtts import gTTS
|
| 3 |
from io import BytesIO
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
-
st.
|
| 7 |
|
| 8 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
reader = PdfReader(uploaded_file)
|
| 14 |
-
# printing number of pages in pdf file
|
| 15 |
X = len(reader.pages)
|
| 16 |
-
|
| 17 |
|
| 18 |
i = 0
|
| 19 |
while i <= X and i <= x:
|
| 20 |
-
# getting a specific page from the pdf file
|
| 21 |
page = reader.pages[i]
|
| 22 |
-
|
| 23 |
-
text = page.extract_text()
|
| 24 |
-
print("Created text of page", i )
|
| 25 |
sound_file = BytesIO()
|
| 26 |
tts = gTTS(text, lang='en')
|
| 27 |
tts.write_to_fp(sound_file)
|
| 28 |
-
st.audio(sound_file)
|
| 29 |
-
|
| 30 |
i = i + 1
|
| 31 |
-
st.write("π That's the whole PDF! Have an awesome day! π")
|
| 32 |
-
|
| 33 |
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
if prompt:
|
| 36 |
st.write(prompt)
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
+
from gtts import gTTS
|
| 3 |
from io import BytesIO
|
| 4 |
from PyPDF2 import PdfReader
|
| 5 |
|
| 6 |
+
st.set_page_config(page_title="π PDF to Audio π§", page_icon="π")
|
| 7 |
|
| 8 |
+
st.markdown("""
|
| 9 |
+
<style>
|
| 10 |
+
body {
|
| 11 |
+
background-color: #F0F8FF;
|
| 12 |
+
font-family: 'Arial', sans-serif;
|
| 13 |
+
}
|
| 14 |
+
h1 {
|
| 15 |
+
color: #1E90FF;
|
| 16 |
+
font-size: 36px;
|
| 17 |
+
text-align: center;
|
| 18 |
+
margin-bottom: 30px;
|
| 19 |
+
}
|
| 20 |
+
h2 {
|
| 21 |
+
color: #00BFFF;
|
| 22 |
+
font-size: 24px;
|
| 23 |
+
margin-bottom: 10px;
|
| 24 |
+
}
|
| 25 |
+
p {
|
| 26 |
+
color: #333;
|
| 27 |
+
font-size: 18px;
|
| 28 |
+
}
|
| 29 |
+
.stAudio {
|
| 30 |
+
margin-bottom: 20px;
|
| 31 |
+
}
|
| 32 |
+
.stTextArea {
|
| 33 |
+
font-size: 18px;
|
| 34 |
+
padding: 10px;
|
| 35 |
+
}
|
| 36 |
+
.stButton {
|
| 37 |
+
background-color: #1E90FF;
|
| 38 |
+
color: #FFF;
|
| 39 |
+
font-size: 18px;
|
| 40 |
+
padding: 10px 20px;
|
| 41 |
+
border-radius: 5px;
|
| 42 |
+
margin-top: 20px;
|
| 43 |
+
}
|
| 44 |
+
</style>
|
| 45 |
+
""", unsafe_allow_html=True)
|
| 46 |
|
| 47 |
+
st.markdown("<h1>π PDF to Audio Converter π§</h1>", unsafe_allow_html=True)
|
| 48 |
+
|
| 49 |
+
col1, col2 = st.columns(2)
|
| 50 |
+
|
| 51 |
+
with col1:
|
| 52 |
+
st.markdown("<h2>π PDF File</h2>", unsafe_allow_html=True)
|
| 53 |
+
uploaded_file = st.file_uploader("Choose a file", "pdf")
|
| 54 |
+
|
| 55 |
+
with col2:
|
| 56 |
+
st.markdown("<h2>π’ Pages</h2>", unsafe_allow_html=True)
|
| 57 |
+
x = st.slider('Select the number of pages you wish to transcribe', min_value=1, max_value=100, value=1)
|
| 58 |
+
|
| 59 |
+
if uploaded_file is not None:
|
| 60 |
reader = PdfReader(uploaded_file)
|
|
|
|
| 61 |
X = len(reader.pages)
|
| 62 |
+
st.markdown(f"<p>Total pages in the PDF: {X}</p>", unsafe_allow_html=True)
|
| 63 |
|
| 64 |
i = 0
|
| 65 |
while i <= X and i <= x:
|
|
|
|
| 66 |
page = reader.pages[i]
|
| 67 |
+
text = page.extract_text()
|
|
|
|
|
|
|
| 68 |
sound_file = BytesIO()
|
| 69 |
tts = gTTS(text, lang='en')
|
| 70 |
tts.write_to_fp(sound_file)
|
| 71 |
+
st.audio(sound_file, format='audio/mp3')
|
| 72 |
+
st.markdown(f"<p>Read aloud page {i+1} of {X} total pages.</p>", unsafe_allow_html=True)
|
| 73 |
i = i + 1
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
st.balloons()
|
| 76 |
+
st.markdown("<h2>π That's the whole PDF! Have an awesome day! π</h2>", unsafe_allow_html=True)
|
| 77 |
+
|
| 78 |
+
st.markdown("<h2>βοΈ Text to Audio</h2>", unsafe_allow_html=True)
|
| 79 |
+
prompt = st.text_area("Copy/Paste or type in text to have read aloud", height=200)
|
| 80 |
+
|
| 81 |
if prompt:
|
| 82 |
st.write(prompt)
|
| 83 |
+
sound_file = BytesIO()
|
| 84 |
+
tts = gTTS(prompt, lang='en')
|
| 85 |
+
tts.write_to_fp(sound_file)
|
| 86 |
+
st.audio(sound_file, format='audio/mp3')
|
| 87 |
+
|
| 88 |
+
st.markdown("<button class='stButton'>Convert to Audio</button>", unsafe_allow_html=True)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
|