|
import streamlit as st |
|
from gtts import gTTS |
|
from io import BytesIO |
|
from PyPDF2 import PdfReader |
|
|
|
st.set_page_config(page_title="π PDF to Audio π§", page_icon="π") |
|
|
|
st.markdown(""" |
|
<style> |
|
body { |
|
background-color: #F0F8FF; |
|
font-family: 'Arial', sans-serif; |
|
} |
|
h1 { |
|
color: #1E90FF; |
|
font-size: 36px; |
|
text-align: center; |
|
margin-bottom: 30px; |
|
} |
|
h2 { |
|
color: #00BFFF; |
|
font-size: 24px; |
|
margin-bottom: 10px; |
|
} |
|
p { |
|
color: #333; |
|
font-size: 18px; |
|
} |
|
.stAudio { |
|
margin-bottom: 20px; |
|
} |
|
.stTextArea { |
|
font-size: 18px; |
|
padding: 10px; |
|
} |
|
.stButton { |
|
background-color: #1E90FF; |
|
color: #FFF; |
|
font-size: 18px; |
|
padding: 10px 20px; |
|
border-radius: 5px; |
|
margin-top: 20px; |
|
} |
|
</style> |
|
""", unsafe_allow_html=True) |
|
|
|
st.markdown("<h1>π PDF to Audio Converter π§</h1>", unsafe_allow_html=True) |
|
|
|
col1, col2 = st.columns(2) |
|
|
|
with col1: |
|
st.markdown("<h2>π PDF File</h2>", unsafe_allow_html=True) |
|
uploaded_file = st.file_uploader("Choose a file", "pdf") |
|
|
|
with col2: |
|
st.markdown("<h2>π’ Pages</h2>", unsafe_allow_html=True) |
|
x = st.slider('Select the number of pages you wish to transcribe', min_value=1, max_value=100, value=10) |
|
|
|
if uploaded_file is not None: |
|
reader = PdfReader(uploaded_file) |
|
X = len(reader.pages) |
|
st.markdown(f"<p>Total pages in the PDF: {X}</p>", unsafe_allow_html=True) |
|
|
|
i = 0 |
|
while i <= X and i <= x: |
|
page = reader.pages[i] |
|
text = page.extract_text() |
|
sound_file = BytesIO() |
|
tts = gTTS(text, lang='en') |
|
tts.write_to_fp(sound_file) |
|
st.audio(sound_file, format='audio/mp3') |
|
st.markdown(f"<p>Read aloud page {i+1} of {X} total pages.</p>", unsafe_allow_html=True) |
|
i = i + 1 |
|
|
|
st.balloons() |
|
st.markdown("<h2>π That's the whole PDF! Have an awesome day! π</h2>", unsafe_allow_html=True) |
|
|
|
st.markdown("<h2>βοΈ Text to Audio</h2>", unsafe_allow_html=True) |
|
prompt = st.text_area("Copy/Paste or type in text to have read aloud", height=200) |
|
|
|
if prompt: |
|
st.write(prompt) |
|
sound_file = BytesIO() |
|
tts = gTTS(prompt, lang='en') |
|
tts.write_to_fp(sound_file) |
|
st.audio(sound_file, format='audio/mp3') |
|
|
|
st.markdown("<button class='stButton'>Convert to Audio</button>", unsafe_allow_html=True) |
|
|
|
|
|
|
|
|