QuickLearnerAI commited on
Commit
6a4c7c4
·
verified ·
1 Parent(s): 5203434

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +53 -0
app.py ADDED
@@ -0,0 +1,53 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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)}"
41
+
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()