Nemai's picture
Update app.py
03b8cbc verified
from transformers import pipeline
import fitz # PyMuPDF
import gradio as gr
# Load summarization model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Extract text from PDF
def extract_text_from_pdf(pdf_file):
try:
doc = fitz.open(pdf_file.name)
text = ""
for page in doc:
text += page.get_text()
return text
except Exception as e:
return f"Error extracting text from PDF: {e}"
# Main summarizer function
def summarize_input(text, pdf_file):
if pdf_file is not None:
text = extract_text_from_pdf(pdf_file)
if text.startswith("Error"):
return text
if not text or len(text.strip()) < 30:
return "Please provide more text or a valid PDF."
if len(text) > 3000:
text = text[:3000]
try:
summary = summarizer(text, max_length=120, min_length=30, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"Summarization failed: {e}"
# Gradio interface
app = gr.Interface(
fn=summarize_input,
inputs=[
gr.Textbox(label="Enter Text (leave blank if uploading PDF)", lines=10),
gr.File(label="Upload PDF File", file_types=[".pdf"]),
],
outputs=gr.Textbox(label="Summary"),
title="Text & PDF Summarizer",
description="Paste text or upload a PDF to summarize using BART model from Hugging Face."
)
app.launch()