Spaces:
Runtime error
Runtime error
Update app.py
Browse files1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768import gradio as grfrom transformers import pipelinefrom PyPDF2 import PdfReaderfrom docx import Documentfrom fpdf import FPDFimport os# Load summarization pipelinesummarizer = pipeline("summarization")# Function to read text from different file typesdef extract_text(file):text = ""if file.name.endswith(".pdf"):reader = PdfReader(file.name)for page in reader.pages:text += page.extract_text()elif file.name.endswith(".docx"):doc = Document(file.name)for para in doc.paragraphs:text += para.text + "\n"else:text = file.read().decode("utf-8")return text# Function to summarize and return as PDF or Worddef summarize_file(file, output_format):text = extract_text(file)if not text.strip():return None, "File is empty or could not be read."summarized = summarizer(text, max_length=150, min_length=40, do_sample=False)[0]["summary_text"]output_path = "/tmp/summary_output"if output_format == "PDF":pdf = FPDF()pdf.add_page()pdf.set_auto_page_break(auto=True, margin=15)pdf.set_font("Arial", size=12)for line in summarized.split("\n"):pdf.multi_cell(0, 10, line)output_file = f"{output_path}.pdf"pdf.output(output_file)else:doc = Document()doc.add_heading("Summary", 0)doc.add_paragraph(summarized)output_file = f"{output_path}.docx"doc.save(output_file)return output_file, "Success"# Gradio interfaceiface = gr.Interface(fn=summarize_file,inputs=[gr.File(label="Upload a text, Word, or PDF file"),gr.Radio(["PDF", "Word"], label="Choose output format")],outputs=[gr.File(label="Download Summary"),gr.Textbox(label="Status")],title="Smart Text Summarizer",description="Upload a .txt, .docx, or .pdf file and get the summary back as a Word or PDF file.")iface.launch()1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980import streamlit as stfrom transformers import pipelinefrom PyPDF2 import PdfReaderimport docx2txtfrom fpdf import FPDFfrom io import BytesIOfrom docx import Document# Load summarizersummarizer = pipeline("summarization", model="sshleifer/distilbart-cnn-12-6")# Helper: Extract text from PDFdef extract_text_from_pdf(file):pdf = PdfReader(file)text = ""for page in pdf.pages:text += page.extract_text()return text# Helper: Save summary to DOCXdef generate_docx(text):doc = Document()doc.add_heading("Summary", 0)doc.add_paragraph(text)buffer = BytesIO()doc.save(buffer)buffer.seek(0)return buffer# Helper: Save summary to PDFdef generate_pdf(text):pdf = FPDF()pdf.add_page()pdf.set_font("Arial", size=12)for line in text.split("\n"):pdf.multi_cell(0, 10, line)buffer = BytesIO()pdf.output(buffer)buffer.seek(0)return buffer# App UIst.set_page_config(page_title="Smart Text Summarizer", layout="centered")st.title("π Smart Text Summarizer")uploaded_file = st.file_uploader("Upload a PDF or Word file", type=["pdf", "docx"])if uploaded_file:file_type = uploaded_file.name.split(".")[-1].lower()# Extract textif file_type == "pdf":text = extract_text_from_pdf(uploaded_file)elif file_type == "docx":text = docx2txt.process(uploaded_file)else:st.error("Unsupported file format.")st.stop()if not text:st.error("No readable text found in the file.")st.stop()st.subheader("π Extracted Text")st.text_area("Preview", text[:1000] + "...", height=200)# Summarizeif st.button("Summarize"):with st.spinner("Summarizing..."):summary = summarizer(text, max_length=150, min_length=30, do_sample=False)[0]['summary_text']st.success("Summary Generated β ")st.subheader("π Summary")st.write(summary)# Download optionsdocx_file = generate_docx(summary)pdf_file = generate_pdf(summary)st.download_button("π₯ Download as DOCX", docx_file, file_name="summary.docx")st.download_button("π₯ Download as PDF", pdf_file, file_name="summary.pdf")
app.py+67-55