Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
import fitz # PyMuPDF
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 6 |
+
|
| 7 |
+
def extract_text_from_pdf(pdf_file):
|
| 8 |
+
try:
|
| 9 |
+
doc = fitz.open(pdf_file.name)
|
| 10 |
+
text = ""
|
| 11 |
+
for page in doc:
|
| 12 |
+
text += page.get_text()
|
| 13 |
+
return text
|
| 14 |
+
except Exception as e:
|
| 15 |
+
return f"Error extracting text from PDF: {e}"
|
| 16 |
+
|
| 17 |
+
def summarize_input(text, pdf_file):
|
| 18 |
+
if pdf_file is not None:
|
| 19 |
+
text = extract_text_from_pdf(pdf_file)
|
| 20 |
+
if text.startswith("Error"):
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
if not text or len(text.strip()) < 30:
|
| 24 |
+
return "Please provide more text or a valid PDF."
|
| 25 |
+
|
| 26 |
+
if len(text) > 3000:
|
| 27 |
+
text = text[:3000]
|
| 28 |
+
|
| 29 |
+
try:
|
| 30 |
+
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)
|
| 31 |
+
return summary[0]['summary_text']
|
| 32 |
+
except Exception as e:
|
| 33 |
+
return f"Summarization failed: {e}"
|
| 34 |
+
|
| 35 |
+
app = gr.Interface(
|
| 36 |
+
fn=summarize_input,
|
| 37 |
+
inputs=[
|
| 38 |
+
gr.Textbox(label="Enter Text (leave blank if uploading PDF)", lines=10),
|
| 39 |
+
gr.File(label="Upload PDF File", file_types=[".pdf"]),
|
| 40 |
+
],
|
| 41 |
+
outputs=gr.Textbox(label="Summary"),
|
| 42 |
+
title="Text & PDF Summarizer",
|
| 43 |
+
description="Paste text or upload a PDF to summarize using BART model from Hugging Face."
|
| 44 |
+
)
|
| 45 |
+
|
| 46 |
+
app.launch()
|