File size: 3,648 Bytes
b8b16cf
 
02f8934
cfeeb7f
 
 
 
b8b16cf
 
cfeeb7f
8c10b14
 
 
 
 
cfeeb7f
02f8934
b8b16cf
02f8934
b8b16cf
 
02f8934
 
 
8c10b14
02f8934
 
 
 
 
8c10b14
 
 
 
02f8934
 
 
cfeeb7f
02f8934
cfeeb7f
 
 
 
 
 
 
8c10b14
cfeeb7f
 
8c10b14
 
cfeeb7f
 
 
 
 
 
8c10b14
cfeeb7f
 
 
 
 
 
 
 
02f8934
 
 
b8b16cf
 
 
8c10b14
b8b16cf
 
02f8934
 
 
8c10b14
cfeeb7f
8c10b14
cfeeb7f
8c10b14
 
02f8934
b8b16cf
cfeeb7f
b8b16cf
 
 
 
 
cfeeb7f
b8b16cf
 
8c10b14
b8b16cf
 
 
cfeeb7f
b8b16cf
02f8934
b8b16cf
 
cfeeb7f
 
 
 
 
 
b8b16cf
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import gradio as gr
from gtts import gTTS
import pdfplumber
from sumy.parsers.plaintext import PlaintextParser
from sumy.nlp.tokenizers import Tokenizer
from sumy.summarizers.lsa import LsaSummarizer
import nltk
import os

# Download NLTK data for sumy
try:
    nltk.download('punkt')
    nltk.download('punkt_tab')
except Exception as e:
    print(f"Error downloading NLTK data: {str(e)}")

def extract_text_from_pdf(pdf_file):
    """
    Extract text from a PDF file using pdfplumber.
    
    Args:
        pdf_file: Uploaded PDF file.
    
    Returns:
        str: Extracted text or error message.
    """
    try:
        with pdfplumber.open(pdf_file) as pdf:
            text = ""
            for page in pdf.pages:
                page_text = page.extract_text()
                if page_text:
                    text += page_text + " "
        return text.strip() if text else "No text could be extracted from the PDF."
    except Exception as e:
        return f"Error extracting text: {str(e)}"

def summarize_text(text, sentences_count=12):
    """
    Summarize text to approximately four paragraphs using sumy LSA summarizer.
    
    Args:
        text (str): Text to summarize.
        sentences_count (int): Number of sentences in summary (approx. 3 sentences per paragraph).
    
    Returns:
        str: Summarized text or error message.
    """
    try:
        if len(text.split()) < 50:
            return "Text is too short to summarize."
        parser = PlaintextParser.from_string(text, Tokenizer("english"))
        summarizer = LsaSummarizer()
        summary = summarizer(parser.document, sentences_count)
        summary_text = ""
        for i, sentence in enumerate(summary):
            summary_text += str(sentence) + " "
            if (i + 1) % 3 == 0:
                summary_text += "\n\n"
        return summary_text.strip() if summary_text else "No summary generated."
    except Exception as e:
        return f"Error summarizing text: {str(e)}"

def pdf_to_speech(pdf_file, lang="en"):
    """
    Convert text from a PDF to summarized speech using gTTS.
    
    Args:
        pdf_file: Uploaded PDF file.
        lang (str): Language code (default is 'en' for English).
    
    Returns:
        tuple: (Path to audio file or None, summarized text or error message).
    """
    try:
        # Extract text from PDF
        text = extract_text_from_pdf(pdf_file)
        if "Error" in text:
            return None, text
        
        # Summarize text
        summarized_text = summarize_text(text, sentences_count=12)
        if "Error" in summarized_text or "too short" in summarized_text:
            return None, summarized_text
        
        # Create gTTS object
        tts = gTTS(text=summarized_text, lang=lang, slow=False)
        
        # Save the audio file
        output_file = "output.mp3"
        tts.save(output_file)
        
        return output_file, summarized_text
    
    except Exception as e:
        return None, f"An error occurred: {str(e)}"

# Define Gradio interface
demo = gr.Interface(
    fn=pdf_to_speech,
    inputs=[
        gr.File(label="Upload a PDF file", file_types=[".pdf"]),
        gr.Dropdown(choices=["en", "es", "fr"], label="Select Language", value="en")
    ],
    outputs=[
        gr.Audio(label="Generated Speech"),
        gr.Textbox(label="Summarized Text")
    ],
    title="PDF Summary to Speech",
    description="Upload an English PDF file, select a language, and generate speech from a summarized version (approx. 4 paragraphs). The summarized text is also displayed."
)

# Launch the app
if __name__ == "__main__":
    demo.launch()