Create PDF Summarizer
Browse files- PDF Summarizer +83 -0
PDF Summarizer
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import fitz
|
2 |
+
from transformers import BartForConditionalGeneration, BartTokenizer
|
3 |
+
import tkinter as tk
|
4 |
+
from tkinter import filedialog
|
5 |
+
|
6 |
+
model_name = "facebook/bart-large-cnn"
|
7 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
8 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
9 |
+
|
10 |
+
#Function to extract text from PDF
|
11 |
+
def extract_text_from_pdf(pdf_path):
|
12 |
+
text = ""
|
13 |
+
with fitz.open(pdf_path) as pdf_file:
|
14 |
+
for page_num in range(len(pdf_file)):
|
15 |
+
page = pdf_file.load_page(page_num)
|
16 |
+
text += page.get_text()
|
17 |
+
return text
|
18 |
+
|
19 |
+
#Function to summarize PDF and display on tkinter window
|
20 |
+
def summarize_and_display(pdf_path, summary_length):
|
21 |
+
summary_text.delete(1.0, tk.END)
|
22 |
+
summary_text.insert(tk.END, "This may take some time. Please be patient...\n")
|
23 |
+
|
24 |
+
document = extract_text_from_pdf(pdf_path)
|
25 |
+
valid_lengths = ["long", "medium", "short"]
|
26 |
+
while summary_length.lower() not in valid_lengths:
|
27 |
+
print("Invalid input. Please enter 'long', 'medium', or 'short'.")
|
28 |
+
summary_length = input("Enter length of summary (long/medium/short):")
|
29 |
+
|
30 |
+
if summary_length.lower() == "long":
|
31 |
+
min_length = 800
|
32 |
+
elif summary_length.lower() == "medium":
|
33 |
+
min_length = 500
|
34 |
+
elif summary_length.lower() == 'short':
|
35 |
+
min_length = 100
|
36 |
+
|
37 |
+
# Tokenize and encode the input document
|
38 |
+
inputs = tokenizer([document], max_length=1024, return_tensors='pt', truncation=True)
|
39 |
+
# Generate summary
|
40 |
+
summary_ids = model.generate(inputs ['input_ids'], num_beams=4, max_length=1000, min_length=min_length, early_stopping=False)
|
41 |
+
# Decode and print the summary
|
42 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
43 |
+
summary_text.delete(1.0, tk.END)
|
44 |
+
summary_text.insert(tk.END, summary)
|
45 |
+
|
46 |
+
|
47 |
+
# Function to handle file selection using tkinter
|
48 |
+
def select_file():
|
49 |
+
file_path = filedialog.askopenfilename(filetypes = [("PDF files")])
|
50 |
+
if file_path:
|
51 |
+
pdf_path_entry.delete(0, tk.END)
|
52 |
+
pdf_path_entry.insert(0, file_path)
|
53 |
+
|
54 |
+
# Create GUI
|
55 |
+
root = tk.Tk()
|
56 |
+
root.title("PDF Summarizer")
|
57 |
+
|
58 |
+
file_label = tk.Label(root, text="PDF File:")
|
59 |
+
file_label.grid(row=0, column=0)
|
60 |
+
|
61 |
+
pdf_path_entry= tk.Entry(root, width=50)
|
62 |
+
pdf_path_entry.grid(row=0, column=1)
|
63 |
+
|
64 |
+
browse_button= tk.Button(root, text="Browse", command=select_file)
|
65 |
+
browse_button.grid(row=0, column=2)
|
66 |
+
|
67 |
+
summary_length_label = tk.Label(root, text="Summary Length:")
|
68 |
+
summary_length_label.grid(row=1, column=0)
|
69 |
+
|
70 |
+
summary_length_entry = tk.Entry(root, width=10)
|
71 |
+
summary_length_entry.grid(row=1, column=1)
|
72 |
+
|
73 |
+
summarize_button = tk.Button(root, text="Summarize", command=lambda: summarize_and_display(pdf_path_entry.get(), summary_length_entry.get()))
|
74 |
+
summarize_button.grid(row=1, column=2)
|
75 |
+
|
76 |
+
summary_text_label = tk.Label(root, text="Summary:")
|
77 |
+
summary_text_label.grid(row=2, column=0, columnspan=3)
|
78 |
+
|
79 |
+
summary_text = tk.Text(root, wrap=tk.WORD, width=60, height=10)
|
80 |
+
summary_text.grid(row=3, column=0, columnspan=3)
|
81 |
+
|
82 |
+
root.update()
|
83 |
+
root.mainloop()
|