akazmi commited on
Commit
7db20a1
·
verified ·
1 Parent(s): c1fb4fe

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -10
app.py CHANGED
@@ -8,27 +8,35 @@ summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
8
  def pdf_to_text(pdf_file):
9
  """Extract text from a PDF file."""
10
  text = ""
11
- with open(pdf_file, 'rb') as file:
12
- reader = PyPDF2.PdfReader(file)
13
- for page in reader.pages:
14
- text += page.extract_text() + "\n"
 
 
 
 
 
15
  return text
16
 
17
  def summarize_pdf(pdf_file):
18
  """Summarize the content of a PDF file."""
19
  text = pdf_to_text(pdf_file)
20
- if len(text) == 0:
21
  return "No text found in the PDF."
22
-
23
- # Summarize the text
24
- summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
25
- return summary[0]['summary_text']
 
 
 
26
 
27
  # Create a Gradio interface
28
  interface = gr.Interface(
29
  fn=summarize_pdf,
30
  inputs=gr.File(label="Upload a PDF file"),
31
- outputs=gr.Textbox(label="Summary"),
32
  title="PDF Summarizer",
33
  description="Upload a PDF file to receive a summary."
34
  )
 
8
  def pdf_to_text(pdf_file):
9
  """Extract text from a PDF file."""
10
  text = ""
11
+ try:
12
+ with open(pdf_file, 'rb') as file:
13
+ reader = PyPDF2.PdfReader(file)
14
+ for page in reader.pages:
15
+ page_text = page.extract_text()
16
+ if page_text: # Only add non-empty pages
17
+ text += page_text + "\n"
18
+ except Exception as e:
19
+ return f"Error reading PDF: {str(e)}"
20
  return text
21
 
22
  def summarize_pdf(pdf_file):
23
  """Summarize the content of a PDF file."""
24
  text = pdf_to_text(pdf_file)
25
+ if len(text.strip()) == 0:
26
  return "No text found in the PDF."
27
+
28
+ # Attempt to summarize the text
29
+ try:
30
+ summary = summarizer(text, max_length=130, min_length=30, do_sample=False)
31
+ return summary[0]['summary_text']
32
+ except Exception as e:
33
+ return f"Error summarizing text: {str(e)}"
34
 
35
  # Create a Gradio interface
36
  interface = gr.Interface(
37
  fn=summarize_pdf,
38
  inputs=gr.File(label="Upload a PDF file"),
39
+ outputs=gr.Textbox(label="Summary", lines=10),
40
  title="PDF Summarizer",
41
  description="Upload a PDF file to receive a summary."
42
  )