Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import fitz # PyMuPDF
|
| 2 |
+
import gradio as gr
|
| 3 |
+
from transformers import pipeline
|
| 4 |
+
|
| 5 |
+
# Initialize summarizer pipeline
|
| 6 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 7 |
+
|
| 8 |
+
def extract_text_from_pdf(file):
|
| 9 |
+
doc = fitz.open(stream=file.read(), filetype="pdf")
|
| 10 |
+
text = ""
|
| 11 |
+
for page in doc:
|
| 12 |
+
text += page.get_text()
|
| 13 |
+
return text
|
| 14 |
+
|
| 15 |
+
def summarize_pdf(file):
|
| 16 |
+
raw_text = extract_text_from_pdf(file)
|
| 17 |
+
# Limit to avoid token overflow
|
| 18 |
+
max_chunk = 1024
|
| 19 |
+
chunks = [raw_text[i:i+max_chunk] for i in range(0, len(raw_text), max_chunk)]
|
| 20 |
+
summary = ""
|
| 21 |
+
for chunk in chunks:
|
| 22 |
+
res = summarizer(chunk, max_length=130, min_length=30, do_sample=False)
|
| 23 |
+
summary += res[0]['summary_text'] + " "
|
| 24 |
+
return summary.strip()
|
| 25 |
+
|
| 26 |
+
# Gradio UI
|
| 27 |
+
demo = gr.Interface(
|
| 28 |
+
fn=summarize_pdf,
|
| 29 |
+
inputs=gr.File(label="Upload a PDF"),
|
| 30 |
+
outputs=gr.Textbox(label="Summary"),
|
| 31 |
+
title="📄 PDF Summarizer",
|
| 32 |
+
description="Upload a PDF file and get an AI-generated summary using Hugging Face Transformers."
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|