Spaces:
Runtime error
Runtime error
Delete app.py
Browse files
app.py
DELETED
|
@@ -1,86 +0,0 @@
|
|
| 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 the summarizer pipeline
|
| 9 |
-
summarizer = pipeline("summarization")
|
| 10 |
-
|
| 11 |
-
# Helper to extract text from PDF
|
| 12 |
-
def extract_text_from_pdf(file):
|
| 13 |
-
reader = PdfReader(file)
|
| 14 |
-
text = ""
|
| 15 |
-
for page in reader.pages:
|
| 16 |
-
text += page.extract_text()
|
| 17 |
-
return text
|
| 18 |
-
|
| 19 |
-
# Helper to extract text from DOCX
|
| 20 |
-
def extract_text_from_docx(file):
|
| 21 |
-
doc = Document(file)
|
| 22 |
-
return "\n".join([para.text for para in doc.paragraphs])
|
| 23 |
-
|
| 24 |
-
# Helper to save summary as DOCX
|
| 25 |
-
def save_as_docx(text):
|
| 26 |
-
doc = Document()
|
| 27 |
-
doc.add_heading("Summary", 0)
|
| 28 |
-
doc.add_paragraph(text)
|
| 29 |
-
path = "summary.docx"
|
| 30 |
-
doc.save(path)
|
| 31 |
-
return path
|
| 32 |
-
|
| 33 |
-
# Helper to save summary as PDF
|
| 34 |
-
def save_as_pdf(text):
|
| 35 |
-
pdf = FPDF()
|
| 36 |
-
pdf.add_page()
|
| 37 |
-
pdf.set_font("Arial", size=12)
|
| 38 |
-
pdf.multi_cell(0, 10, text)
|
| 39 |
-
path = "summary.pdf"
|
| 40 |
-
pdf.output(path)
|
| 41 |
-
return path
|
| 42 |
-
|
| 43 |
-
# Main function
|
| 44 |
-
def summarize_input(text, file, export_type):
|
| 45 |
-
if file:
|
| 46 |
-
ext = os.path.splitext(file.name)[-1].lower()
|
| 47 |
-
if ext == ".pdf":
|
| 48 |
-
text = extract_text_from_pdf(file)
|
| 49 |
-
elif ext == ".docx":
|
| 50 |
-
text = extract_text_from_docx(file)
|
| 51 |
-
else:
|
| 52 |
-
return "Unsupported file type", None
|
| 53 |
-
|
| 54 |
-
if not text.strip():
|
| 55 |
-
return "No text to summarize.", None
|
| 56 |
-
|
| 57 |
-
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
|
| 58 |
-
|
| 59 |
-
# Save based on export type
|
| 60 |
-
if export_type == "PDF":
|
| 61 |
-
file_path = save_as_pdf(summary)
|
| 62 |
-
else:
|
| 63 |
-
file_path = save_as_docx(summary)
|
| 64 |
-
|
| 65 |
-
return summary, file_path
|
| 66 |
-
|
| 67 |
-
# UI
|
| 68 |
-
with gr.Blocks() as demo:
|
| 69 |
-
gr.Markdown("## 📝 Smart Text Summarizer\nUpload a file or enter text. Choose output format and get your summary!")
|
| 70 |
-
|
| 71 |
-
with gr.Row():
|
| 72 |
-
text_input = gr.Textbox(label="Enter Text", lines=8, placeholder="Or upload a file below instead...")
|
| 73 |
-
file_input = gr.File(label="Upload PDF or DOCX", file_types=[".pdf", ".docx"])
|
| 74 |
-
|
| 75 |
-
export_type = gr.Radio(["PDF", "Word (DOCX)"], label="Export As", value="PDF")
|
| 76 |
-
summarize_btn = gr.Button("Summarize")
|
| 77 |
-
|
| 78 |
-
output_text = gr.Textbox(label="Summary", lines=8)
|
| 79 |
-
download_link = gr.File(label="Download Summary")
|
| 80 |
-
|
| 81 |
-
summarize_btn.click(fn=summarize_input,
|
| 82 |
-
inputs=[text_input, file_input, export_type],
|
| 83 |
-
outputs=[output_text, download_link])
|
| 84 |
-
|
| 85 |
-
# Launch the app
|
| 86 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|