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("""
""", unsafe_allow_html=True)
st.markdown("
📚 PDF to Audio Converter 🎧
", unsafe_allow_html=True)
col1, col2 = st.columns(2)
with col1:
st.markdown("
📜 PDF File
", unsafe_allow_html=True)
uploaded_file = st.file_uploader("Choose a file", "pdf")
with col2:
st.markdown("
🔢 Pages
", 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"
Total pages in the PDF: {X}
", 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"
Read aloud page {i+1} of {X} total pages.
", unsafe_allow_html=True)
i = i + 1
st.balloons()
st.markdown("
🎉 That's the whole PDF! Have an awesome day! 🎉
", unsafe_allow_html=True)
st.markdown("
✍️ Text to Audio
", 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("", unsafe_allow_html=True)