Text-summarizer / app.py
aspendse's picture
Update app.py
d6464e5 verified
raw
history blame
1.73 kB
import gradio as gr
from transformers import pipeline
import PyPDF2
import docx
# Load the summarization pipeline
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
def read_pdf(file):
try:
reader = PyPDF2.PdfReader(file.name)
text = ""
for page in reader.pages:
text += page.extract_text() or ""
return text
except Exception:
return "Failed to read PDF."
def read_docx(file):
try:
doc = docx.Document(file.name)
return "\n".join([para.text for para in doc.paragraphs])
except Exception:
return "Failed to read Word document."
def summarize_file(pdf_file, docx_file, text_input):
if pdf_file:
text = read_pdf(pdf_file)
elif docx_file:
text = read_docx(docx_file)
elif text_input:
text = text_input
else:
return "Please upload a file or enter some text."
if not text.strip():
return "No valid text found to summarize."
if len(text) > 3000:
text = text[:3000]
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]["summary_text"]
return summary
with gr.Blocks() as demo:
gr.Markdown("## 🧠 Smart Text Summarizer")
with gr.Row():
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
docx_input = gr.File(label="Upload Word File", file_types=[".docx"])
text_input = gr.Textbox(lines=8, label="Or Paste Text Here")
summarize_button = gr.Button("Summarize")
output = gr.Textbox(lines=8, label="Summary")
summarize_button.click(
summarize_file,
inputs=[pdf_input, docx_input, text_input],
outputs=output
)
demo.launch()