File size: 2,619 Bytes
0215a2b
bda0ae4
de20c52
 
 
0215a2b
ec34246
de20c52
ec34246
 
 
37224c9
de20c52
ec34246
 
 
 
de20c52
37224c9
de20c52
0215a2b
 
 
 
de20c52
 
 
 
 
 
 
 
0215a2b
ec34246
bda0ae4
0215a2b
 
 
9294b93
bda0ae4
0215a2b
ec34246
 
 
 
daeeb06
9294b93
ec34246
 
 
 
de20c52
37224c9
de20c52
 
 
 
 
ec34246
de20c52
 
 
ec34246
932389d
 
 
 
 
 
 
 
 
de20c52
 
ec34246
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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.
""")