Kurian07's picture
Update app.py
932389d verified
import streamlit as st
import fitz # PyMuPDF for PDF text extraction
from gtts import gTTS
import tempfile
import os
# App Title
st.title("PDF to Speech with gTTS")
# Description
st.write("""
This app lets you upload a PDF, extracts the text, and converts it into a human-like voice using Google Text-to-Speech (gTTS).
You can adjust the **speed** and **language** for a personalized experience.
""")
# Sidebar options for customization
st.sidebar.header("Customize Voice")
speed = st.sidebar.slider("Speech Speed (rate)", min_value=100, max_value=300, value=150, step=10) # 100 to 300 words per minute
slow = st.sidebar.checkbox("Slow Down the Speech", value=False) # Slow down speech
language = st.sidebar.selectbox("Language", ["English", "French", "Spanish", "German"], index=0) # Select language
# File upload
uploaded_file = st.file_uploader("Upload a PDF", type=["pdf"])
# Language mapping for gTTS
language_mapping = {
"English": "en",
"French": "fr",
"Spanish": "es",
"German": "de"
}
if uploaded_file is not None:
# Extract text from the uploaded PDF
with fitz.open(stream=uploaded_file.read(), filetype="pdf") as pdf:
text = ""
for page in pdf:
text += page.get_text()
# Display extracted text
st.subheader("Extracted Text")
if text.strip():
st.write(text)
else:
st.warning("No text found in the uploaded PDF. Please upload a valid document.")
if text.strip():
# Limit text for TTS to avoid overly long processing
max_length = 1000 # Adjust as needed
text = text[:max_length]
# Generate speech using gTTS
tts = gTTS(text=text, lang=language_mapping[language], slow=slow)
# Save audio to a temporary file
with tempfile.NamedTemporaryFile(delete=False, suffix=".mp3") as temp_file:
tts.save(temp_file.name)
temp_file_path = temp_file.name
# Display audio player in Streamlit
st.subheader("Listen to the Speech")
st.audio(temp_file_path, format="audio/mp3")
# Add download button
with open(temp_file_path, "rb") as audio_file:
st.download_button(
label="Download MP3",
data=audio_file,
file_name="output_audio.mp3",
mime="audio/mp3"
)
# Clean up the temporary file after use
os.remove(temp_file_path)
else:
st.info("Upload a PDF to get started.")
# Footer
st.markdown("""
---
πŸ’‘ **Tip**: Adjust the settings in the sidebar to customize your speech experience.
""")