Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import BartTokenizer, BartForConditionalGeneration
|
| 3 |
+
import PyPDF2
|
| 4 |
+
|
| 5 |
+
# Load BART model and tokenizer
|
| 6 |
+
model_name = "facebook/bart-large-cnn"
|
| 7 |
+
tokenizer = BartTokenizer.from_pretrained(model_name)
|
| 8 |
+
model = BartForConditionalGeneration.from_pretrained(model_name)
|
| 9 |
+
|
| 10 |
+
# Function to summarize PDF text
|
| 11 |
+
def summarize_pdf(pdf_file):
|
| 12 |
+
# Read the PDF file
|
| 13 |
+
pdf_reader = PyPDF2.PdfReader(pdf_file.name)
|
| 14 |
+
text = ""
|
| 15 |
+
for page in pdf_reader.pages:
|
| 16 |
+
text += page.extract_text()
|
| 17 |
+
|
| 18 |
+
# Summarize the text
|
| 19 |
+
inputs = tokenizer.encode("summarize: " + text, return_tensors="pt", max_length=1024, truncation=True)
|
| 20 |
+
summary_ids = model.generate(inputs, max_length=150, min_length=40, length_penalty=2.0, num_beams=4, early_stopping=True)
|
| 21 |
+
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
|
| 22 |
+
return summary
|
| 23 |
+
|
| 24 |
+
# Interface for Gradio
|
| 25 |
+
iface = gr.Interface(
|
| 26 |
+
fn=summarize_pdf,
|
| 27 |
+
inputs="file",
|
| 28 |
+
outputs="text",
|
| 29 |
+
title="PDF Summarizer",
|
| 30 |
+
description="Upload a PDF file, and the model will provide a summary.",
|
| 31 |
+
live=True,
|
| 32 |
+
capture_session=True,
|
| 33 |
+
)
|
| 34 |
+
|
| 35 |
+
# Launch the Gradio interface
|
| 36 |
+
iface.launch()
|