Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
import docx2txt
|
| 4 |
+
import PyPDF2
|
| 5 |
+
|
| 6 |
+
# Load the summarizer model
|
| 7 |
+
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
|
| 8 |
+
|
| 9 |
+
# Function to extract text from supported file types
|
| 10 |
+
def extract_text(file):
|
| 11 |
+
if file is None:
|
| 12 |
+
return ""
|
| 13 |
+
file_path = file.name
|
| 14 |
+
if file_path.endswith(".txt"):
|
| 15 |
+
with open(file_path, "r", encoding="utf-8") as f:
|
| 16 |
+
return f.read()
|
| 17 |
+
elif file_path.endswith(".pdf"):
|
| 18 |
+
reader = PyPDF2.PdfReader(file)
|
| 19 |
+
return "\n".join(page.extract_text() for page in reader.pages if page.extract_text())
|
| 20 |
+
elif file_path.endswith(".docx"):
|
| 21 |
+
return docx2txt.process(file)
|
| 22 |
+
else:
|
| 23 |
+
return "Unsupported file format. Please upload a .txt, .pdf, or .docx file."
|
| 24 |
+
|
| 25 |
+
# Summarize either text input or uploaded file
|
| 26 |
+
def summarize(text, file):
|
| 27 |
+
extracted_text = text.strip() or extract_text(file)
|
| 28 |
+
if not extracted_text:
|
| 29 |
+
return "Please enter text or upload a file."
|
| 30 |
+
summary = summarizer(extracted_text, max_length=130, min_length=30, do_sample=False)
|
| 31 |
+
return summary[0]['summary_text']
|
| 32 |
+
|
| 33 |
+
# Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=summarize,
|
| 36 |
+
inputs=[
|
| 37 |
+
gr.Textbox(lines=10, label="Enter text (optional)"),
|
| 38 |
+
gr.File(label="Upload a file (.txt, .pdf, .docx)")
|
| 39 |
+
],
|
| 40 |
+
outputs=gr.Textbox(label="Summary"),
|
| 41 |
+
title="Text Summarizer",
|
| 42 |
+
description="Paste text or upload a file to summarize using a BART model."
|
| 43 |
+
)
|
| 44 |
+
|
| 45 |
+
iface.launch()
|