Srikesh commited on
Commit
e82253e
·
verified ·
1 Parent(s): 58fab4b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ from PyPDF2 import PdfReader
4
+
5
+ # Load summarization model
6
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
+
8
+ # Function to read and summarize PDF
9
+ def summarize_pdf(pdf_file):
10
+ if pdf_file is None:
11
+ return "Please upload a PDF file."
12
+
13
+ reader = PdfReader(pdf_file.name)
14
+ text = ""
15
+ for page in reader.pages:
16
+ page_text = page.extract_text()
17
+ if page_text:
18
+ text += page_text + "\n"
19
+
20
+ # Chunk the text
21
+ max_chunk = 1000
22
+ chunks = [text[i:i+max_chunk] for i in range(0, len(text), max_chunk)]
23
+
24
+ # Summarize each chunk
25
+ summaries = []
26
+ for chunk in chunks:
27
+ summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
28
+ summaries.append(summary[0]['summary_text'])
29
+
30
+ final_summary = " ".join(summaries)
31
+ return final_summary
32
+
33
+
34
+ # Gradio UI
35
+ iface = gr.Interface(
36
+ fn=summarize_pdf,
37
+ inputs=gr.File(label="Upload a PDF"),
38
+ outputs=gr.Textbox(label="Summary"),
39
+ title="PDF Summarizer",
40
+ description="Upload a PDF to get a summarized version of its content using Hugging Face transformers."
41
+ )
42
+
43
+ iface.launch()