exampletwo / app.py
tejovanth's picture
Update app.py
21a3052 verified
raw
history blame
1.35 kB
import gradio as gr
from transformers import pipeline
import fitz # PyMuPDF
# Load the summarization model from Hugging Face
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Function to extract text from the uploaded PDF
def extract_text_from_pdf(pdf_file):
doc = fitz.open(pdf_file.name) # βœ… Use file path instead of .read()
text = ""
for page in doc:
text += page.get_text()
return text
# Function to summarize the extracted text
def summarize_pdf(pdf_file):
try:
text = extract_text_from_pdf(pdf_file)
if len(text.strip()) == 0:
return "❌ The PDF seems empty or has no extractable text."
text = text[:3000] # Truncate to fit within model's token limit
summary = summarizer(text, max_length=150, min_length=40, do_sample=False)
return summary[0]['summary_text']
except Exception as e:
return f"❌ Error: {str(e)}"
# Gradio UI
demo = gr.Interface(
fn=summarize_pdf,
inputs=gr.File(label="πŸ“„ Upload PDF of Academic Notes", type="file"),
outputs=gr.Textbox(label="πŸ“ Summarized Notes"),
title="πŸ“š Academic Note Summarizer",
description="Upload a PDF of your academic notes. The app extracts and summarizes the content using a Hugging Face transformer model."
)
# Launch the app
demo.launch()