Spaces:
Sleeping
Sleeping
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from transformers import pipeline
|
| 3 |
+
from PyPDF2 import PdfReader
|
| 4 |
+
from docx import Document
|
| 5 |
+
from fpdf import FPDF
|
| 6 |
+
import os
|
| 7 |
+
|
| 8 |
+
# Load summarization pipeline
|
| 9 |
+
summarizer = pipeline("summarization")
|
| 10 |
+
|
| 11 |
+
# Function to read text from different file types
|
| 12 |
+
def extract_text(file):
|
| 13 |
+
text = ""
|
| 14 |
+
if file.name.endswith(".pdf"):
|
| 15 |
+
reader = PdfReader(file.name)
|
| 16 |
+
for page in reader.pages:
|
| 17 |
+
text += page.extract_text()
|
| 18 |
+
elif file.name.endswith(".docx"):
|
| 19 |
+
doc = Document(file.name)
|
| 20 |
+
for para in doc.paragraphs:
|
| 21 |
+
text += para.text + "\n"
|
| 22 |
+
else:
|
| 23 |
+
text = file.read().decode("utf-8")
|
| 24 |
+
return text
|
| 25 |
+
|
| 26 |
+
# Function to summarize and return as PDF or Word
|
| 27 |
+
def summarize_file(file, output_format):
|
| 28 |
+
text = extract_text(file)
|
| 29 |
+
if not text.strip():
|
| 30 |
+
return None, "File is empty or could not be read."
|
| 31 |
+
|
| 32 |
+
summarized = summarizer(text, max_length=150, min_length=40, do_sample=False)[0]["summary_text"]
|
| 33 |
+
|
| 34 |
+
output_path = "/tmp/summary_output"
|
| 35 |
+
if output_format == "PDF":
|
| 36 |
+
pdf = FPDF()
|
| 37 |
+
pdf.add_page()
|
| 38 |
+
pdf.set_auto_page_break(auto=True, margin=15)
|
| 39 |
+
pdf.set_font("Arial", size=12)
|
| 40 |
+
for line in summarized.split("\n"):
|
| 41 |
+
pdf.multi_cell(0, 10, line)
|
| 42 |
+
output_file = f"{output_path}.pdf"
|
| 43 |
+
pdf.output(output_file)
|
| 44 |
+
else:
|
| 45 |
+
doc = Document()
|
| 46 |
+
doc.add_heading("Summary", 0)
|
| 47 |
+
doc.add_paragraph(summarized)
|
| 48 |
+
output_file = f"{output_path}.docx"
|
| 49 |
+
doc.save(output_file)
|
| 50 |
+
|
| 51 |
+
return output_file, "Success"
|
| 52 |
+
|
| 53 |
+
# Gradio interface
|
| 54 |
+
iface = gr.Interface(
|
| 55 |
+
fn=summarize_file,
|
| 56 |
+
inputs=[
|
| 57 |
+
gr.File(label="Upload a text, Word, or PDF file"),
|
| 58 |
+
gr.Radio(["PDF", "Word"], label="Choose output format")
|
| 59 |
+
],
|
| 60 |
+
outputs=[
|
| 61 |
+
gr.File(label="Download Summary"),
|
| 62 |
+
gr.Textbox(label="Status")
|
| 63 |
+
],
|
| 64 |
+
title="Smart Text Summarizer",
|
| 65 |
+
description="Upload a .txt, .docx, or .pdf file and get the summary back as a Word or PDF file."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
iface.launch()
|