Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,45 +1,64 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
| 3 |
-
import docx2txt
|
| 4 |
import PyPDF2
|
|
|
|
| 5 |
|
| 6 |
-
# Load the
|
| 7 |
-
summarizer = pipeline("summarization", model="
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
else:
|
| 23 |
-
return "
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
if
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
gr.File(label="Upload
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
)
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from transformers import pipeline
|
|
|
|
| 3 |
import PyPDF2
|
| 4 |
+
import docx
|
| 5 |
|
| 6 |
+
# Load the summarization pipeline
|
| 7 |
+
summarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")
|
| 8 |
+
|
| 9 |
+
def read_pdf(file):
|
| 10 |
+
try:
|
| 11 |
+
reader = PyPDF2.PdfReader(file.name)
|
| 12 |
+
text = ""
|
| 13 |
+
for page in reader.pages:
|
| 14 |
+
text += page.extract_text() or ""
|
| 15 |
+
return text
|
| 16 |
+
except Exception:
|
| 17 |
+
return "Failed to read PDF."
|
| 18 |
+
|
| 19 |
+
def read_docx(file):
|
| 20 |
+
try:
|
| 21 |
+
doc = docx.Document(file.name)
|
| 22 |
+
return "\n".join([para.text for para in doc.paragraphs])
|
| 23 |
+
except Exception:
|
| 24 |
+
return "Failed to read Word document."
|
| 25 |
+
|
| 26 |
+
def summarize_file(pdf_file, docx_file, text_input):
|
| 27 |
+
if pdf_file:
|
| 28 |
+
text = read_pdf(pdf_file)
|
| 29 |
+
elif docx_file:
|
| 30 |
+
text = read_docx(docx_file)
|
| 31 |
+
elif text_input:
|
| 32 |
+
text = text_input
|
| 33 |
else:
|
| 34 |
+
return "Please upload a file or enter some text."
|
| 35 |
+
|
| 36 |
+
if not text.strip():
|
| 37 |
+
return "No valid text found to summarize."
|
| 38 |
+
|
| 39 |
+
if len(text) > 3000:
|
| 40 |
+
text = text[:3000]
|
| 41 |
+
|
| 42 |
+
summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]["summary_text"]
|
| 43 |
+
return summary
|
| 44 |
+
|
| 45 |
+
with gr.Blocks() as demo:
|
| 46 |
+
gr.Markdown("## 🧠 Smart Text Summarizer")
|
| 47 |
+
|
| 48 |
+
with gr.Row():
|
| 49 |
+
pdf_input = gr.File(label="Upload PDF", file_types=[".pdf"])
|
| 50 |
+
docx_input = gr.File(label="Upload Word File", file_types=[".docx"])
|
| 51 |
+
|
| 52 |
+
text_input = gr.Textbox(lines=8, label="Or Paste Text Here")
|
| 53 |
+
|
| 54 |
+
summarize_button = gr.Button("Summarize")
|
| 55 |
+
|
| 56 |
+
output = gr.Textbox(lines=8, label="Summary")
|
| 57 |
+
|
| 58 |
+
summarize_button.click(
|
| 59 |
+
summarize_file,
|
| 60 |
+
inputs=[pdf_input, docx_input, text_input],
|
| 61 |
+
outputs=output
|
| 62 |
+
)
|
| 63 |
+
|
| 64 |
+
demo.launch()
|