Spaces:
Runtime error
Runtime error
Delete app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586import gradio as grfrom transformers import pipelinefrom PyPDF2 import PdfReaderfrom docx import Documentfrom fpdf import FPDFimport os
# Load the summarizer pipelinesummarizer = pipeline("summarization")
# Helper to extract text from PDFdef extract_text_from_pdf(file): reader = PdfReader(file) text = "" for page in reader.pages: text += page.extract_text() return text
# Helper to extract text from DOCXdef extract_text_from_docx(file): doc = Document(file) return "\n".join([para.text for para in doc.paragraphs])
# Helper to save summary as DOCXdef save_as_docx(text): doc = Document() doc.add_heading("Summary", 0) doc.add_paragraph(text) path = "summary.docx" doc.save(path) return path
# Helper to save summary as PDFdef save_as_pdf(text): pdf = FPDF() pdf.add_page() pdf.set_font("Arial", size=12) pdf.multi_cell(0, 10, text) path = "summary.pdf" pdf.output(path) return path
# Main functiondef summarize_input(text, file, export_type): if file: ext = os.path.splitext(file.name)[-1].lower() if ext == ".pdf": text = extract_text_from_pdf(file) elif ext == ".docx": text = extract_text_from_docx(file) else: return "Unsupported file type", None
if not text.strip(): return "No text to summarize.", None
summary = summarizer(text, max_length=130, min_length=30, do_sample=False)[0]['summary_text']
# Save based on export type if export_type == "PDF": file_path = save_as_pdf(summary) else: file_path = save_as_docx(summary)
return summary, file_path
# UIwith gr.Blocks() as demo: gr.Markdown("## 📝 Smart Text Summarizer\nUpload a file or enter text. Choose output format and get your summary!") with gr.Row(): text_input = gr.Textbox(label="Enter Text", lines=8, placeholder="Or upload a file below instead...") file_input = gr.File(label="Upload PDF or DOCX", file_types=[".pdf", ".docx"]) export_type = gr.Radio(["PDF", "Word (DOCX)"], label="Export As", value="PDF") summarize_btn = gr.Button("Summarize")
output_text = gr.Textbox(label="Summary", lines=8) download_link = gr.File(label="Download Summary")
summarize_btn.click(fn=summarize_input, inputs=[text_input, file_input, export_type], outputs=[output_text, download_link])
# Launch the appdemo.launch()app.pyDeleted-86