Jamshaid-Saleem's picture
Update app.py
438f71c verified
import os
import streamlit as st
from PyPDF2 import PdfReader
from TTS.api import TTS
# Function to extract text from PDF
def extract_text_from_pdf(pdf_path):
reader = PdfReader(pdf_path)
text = ""
for page in reader.pages:
if page.extract_text(): # Avoid NoneType errors for empty pages
text += page.extract_text()
return text
# Function to convert text to audio
def text_to_audio(text, output_path, speaker=None, language=None):
# Initialize the TTS model
model_name = TTS.list_models()[0] # Use the first available model
tts = TTS(model_name)
# Set default speaker and language if not provided
if speaker is None and hasattr(tts, "speakers"):
speaker = tts.speakers[0] # Default to the first speaker
if language is None and hasattr(tts, "languages"):
language = tts.languages[0] # Default to the first language
tts.tts_to_file(text=text, file_path=output_path, speaker=speaker, language=language)
# Streamlit app
st.title("PDF to Audio Converter")
st.write("Upload a PDF file, and we'll convert its content to an audio file.")
uploaded_file = st.file_uploader("Choose a PDF file", type="pdf")
if uploaded_file is not None:
with st.spinner("Processing the PDF..."):
try:
# Save the uploaded file
temp_pdf_path = "temp.pdf"
with open(temp_pdf_path, "wb") as f:
f.write(uploaded_file.read())
# Extract text
text = extract_text_from_pdf(temp_pdf_path)
if not text.strip():
st.error("The uploaded PDF is empty or does not contain readable text.")
else:
# Get speaker and language options if available
model_name = TTS.list_models()[0]
tts = TTS(model_name)
speakers = tts.speakers if hasattr(tts, "speakers") else None
languages = tts.languages if hasattr(tts, "languages") else None
# Allow user to select a speaker and language if available
speaker = None
if speakers:
speaker = st.selectbox("Choose a speaker:", speakers)
language = None
if languages:
language = st.selectbox("Choose a language:", languages)
# Save audio
temp_audio_path = "output_audio.wav"
text_to_audio(text, temp_audio_path, speaker, language)
st.success("Conversion complete!")
# Allow user to download the audio file
with open(temp_audio_path, "rb") as f:
st.download_button(
label="Download Audio",
data=f,
file_name="output_audio.wav",
mime="audio/wav"
)
except Exception as e:
st.error(f"An error occurred: {e}")
finally:
# Clean up temporary files
if os.path.exists(temp_pdf_path):
os.remove(temp_pdf_path)
if os.path.exists("output_audio.wav"):
os.remove("output_audio.wav")