ritchi1 commited on
Commit
84038fa
·
verified ·
1 Parent(s): 6af8f4a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +36 -0
app.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ from transformers import pipeline
3
+ import PyPDF2
4
+
5
+ # Load the summarization pipeline
6
+ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
7
+
8
+ def summarize_pdf(pdf_file):
9
+ try:
10
+ # Extract text from the uploaded PDF
11
+ pdf_reader = PyPDF2.PdfReader(pdf_file)
12
+ text = ""
13
+ for page in pdf_reader.pages:
14
+ text += page.extract_text()
15
+
16
+ # Check if text was extracted
17
+ if not text.strip():
18
+ return "❌ Could not extract text from the PDF. Please upload a valid document."
19
+
20
+ # Summarize the extracted text
21
+ summary = summarizer(text, max_length=300, min_length=50, do_sample=False)
22
+ return summary[0]['summary_text']
23
+
24
+ except Exception as e:
25
+ return f"❌ An error occurred: {str(e)}"
26
+
27
+ # Create the Gradio interface
28
+ interface = gr.Interface(
29
+ fn=summarize_pdf,
30
+ inputs=gr.inputs.File(label="Upload PDF"),
31
+ outputs=gr.outputs.Textbox(label="Summary"),
32
+ title="PDF Summarizer",
33
+ description="Upload a PDF file to extract and summarize its content using state-of-the-art AI."
34
+ )
35
+
36
+ interface.launch()