Spaces:
Paused
Paused
Update app.py
Browse files
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 |
-
#
|
| 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
|
| 19 |
def summarize_image_pdf(pdf_file):
|
| 20 |
try:
|
| 21 |
-
#
|
| 22 |
-
|
|
|
|
|
|
|
| 23 |
|
| 24 |
-
#
|
|
|
|
|
|
|
|
|
|
| 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 |
-
#
|
|
|
|
|
|
|
| 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 |
-
|
| 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
|
| 46 |
outputs=gr.Textbox(label="π Summary"),
|
| 47 |
-
title="
|
| 48 |
-
description="
|
| 49 |
)
|
| 50 |
|
| 51 |
-
|
| 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()
|
|
|
|
|
|