QuickLearnerAI commited on
Commit
30e58ca
Β·
verified Β·
1 Parent(s): 5773e7d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +21 -17
app.py CHANGED
@@ -3,38 +3,44 @@ from pdf2image import convert_from_path
3
  from PIL import Image
4
  import pytesseract
5
  from transformers import pipeline
 
 
6
 
7
-
8
-
9
- # Load summarization pipeline
10
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
11
 
12
- # Function to split text into chunks within model token limit
13
  def chunk_text(text, max_tokens=1000):
14
  words = text.split()
15
  for i in range(0, len(words), max_tokens):
16
  yield " ".join(words[i:i+max_tokens])
17
 
18
- # Main function to handle PDF upload and summarize
19
  def summarize_image_pdf(pdf_file):
20
  try:
21
- # Convert PDF pages to images
22
- images = convert_from_path(pdf_file.name, dpi=300)
 
 
23
 
24
- # OCR: Extract text from each page
 
 
 
25
  extracted_text = ""
26
  for i, img in enumerate(images):
27
  text = pytesseract.image_to_string(img)
28
  extracted_text += f"\n\n--- Page {i+1} ---\n{text}"
29
 
30
- # Summarize in chunks
 
 
31
  summaries = []
32
  for chunk in chunk_text(extracted_text):
33
  summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
34
  summaries.append(summary)
35
 
36
- final_summary = "\n\n".join(summaries)
37
- return final_summary
38
 
39
  except Exception as e:
40
  return f"❌ Error: {str(e)}"
@@ -42,12 +48,10 @@ def summarize_image_pdf(pdf_file):
42
  # Gradio UI
43
  interface = gr.Interface(
44
  fn=summarize_image_pdf,
45
- inputs=gr.File(label="πŸ“„ Upload Image-based PDF", type="file"),
46
  outputs=gr.Textbox(label="πŸ“ Summary"),
47
- title="🧠 Image PDF Summarizer with OCR",
48
- description="This app extracts text from scanned/image-based PDFs using OCR and summarizes it using Hugging Face's BART model.",
49
  )
50
 
51
- # Launch app
52
- if __name__ == "__main__":
53
- interface.launch()
 
3
  from PIL import Image
4
  import pytesseract
5
  from transformers import pipeline
6
+ import tempfile
7
+ import os
8
 
9
+ # Load Hugging Face summarization pipeline
 
 
10
  summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
11
 
12
+ # Chunk text into smaller pieces for model input limit
13
  def chunk_text(text, max_tokens=1000):
14
  words = text.split()
15
  for i in range(0, len(words), max_tokens):
16
  yield " ".join(words[i:i+max_tokens])
17
 
18
+ # Main function: PDF upload β†’ OCR β†’ Summarization
19
  def summarize_image_pdf(pdf_file):
20
  try:
21
+ # Save uploaded file temporarily
22
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".pdf") as tmp:
23
+ tmp.write(pdf_file.read())
24
+ tmp_path = tmp.name
25
 
26
+ # Convert PDF to images (Poppler is pre-installed on Spaces)
27
+ images = convert_from_path(tmp_path, dpi=300)
28
+
29
+ # OCR: Extract text from images
30
  extracted_text = ""
31
  for i, img in enumerate(images):
32
  text = pytesseract.image_to_string(img)
33
  extracted_text += f"\n\n--- Page {i+1} ---\n{text}"
34
 
35
+ os.remove(tmp_path) # Clean up temp file
36
+
37
+ # Summarize text
38
  summaries = []
39
  for chunk in chunk_text(extracted_text):
40
  summary = summarizer(chunk, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
41
  summaries.append(summary)
42
 
43
+ return "\n\n".join(summaries)
 
44
 
45
  except Exception as e:
46
  return f"❌ Error: {str(e)}"
 
48
  # Gradio UI
49
  interface = gr.Interface(
50
  fn=summarize_image_pdf,
51
+ inputs=gr.File(label="πŸ“„ Upload a scanned/image PDF", file_types=[".pdf"]),
52
  outputs=gr.Textbox(label="πŸ“ Summary"),
53
+ title="πŸ“˜ OCR PDF Summarizer",
54
+ description="Upload a scanned or image-based PDF. The app will extract text using OCR and summarize it using Hugging Face's BART model.",
55
  )
56
 
57
+ interface.launch()