File size: 2,079 Bytes
84038fa
 
 
15e827e
84038fa
 
 
 
15e827e
 
 
 
84038fa
15e827e
84038fa
15e827e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84038fa
f9464b7
84038fa
15e827e
 
 
 
 
 
 
 
 
 
84038fa
 
 
 
f9464b7
84038fa
 
f9464b7
 
84038fa
 
 
 
 
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
import gradio as gr
from transformers import pipeline
import PyPDF2
import pdfplumber

# Load the summarization pipeline
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")

def extract_text_from_pdf(pdf_file):
    """Extract text from a PDF using PyPDF2 with a fallback to pdfplumber."""
    text = ""
    
    try:
        # First try with PyPDF2
        pdf_reader = PyPDF2.PdfReader(pdf_file)
        for page in pdf_reader.pages:
            text += page.extract_text()
    except Exception as e:
        print(f"PyPDF2 failed: {e}")
        # Fallback to pdfplumber
        with pdfplumber.open(pdf_file) as pdf:
            for page in pdf.pages:
                text += page.extract_text()
    
    return text

def chunk_text(text, max_chunk_size=1024):
    """Split text into smaller chunks to fit within model token limits."""
    words = text.split()
    for i in range(0, len(words), max_chunk_size):
        yield " ".join(words[i:i + max_chunk_size])

def summarize_pdf(pdf_file):
    """Extract text from PDF, chunk it, and summarize."""
    try:
        # Extract text from the PDF
        text = extract_text_from_pdf(pdf_file)
        if not text.strip():
            return "❌ Could not extract any text from the PDF. Please upload a readable document."

        # Chunk text for summarization
        summaries = []
        for chunk in chunk_text(text):
            # Summarize each chunk
            summary = summarizer(chunk, max_length=200, min_length=50, do_sample=False)
            summaries.append(summary[0]['summary_text'])
        
        # Combine all summaries into one
        full_summary = "\n\n".join(summaries)
        return full_summary

    except Exception as e:
        return f"❌ An error occurred: {str(e)}"

# Gradio Interface
interface = gr.Interface(
    fn=summarize_pdf,
    inputs=gr.File(label="Upload PDF"),
    outputs=gr.Textbox(label="Summary"),
    title="PDF Summarizer",
    description="Upload a PDF file to extract and summarize its content using state-of-the-art AI."
)

interface.launch()