Spaces:
Runtime error
Runtime error
Create app.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445import gradio as grfrom transformers import pipelineimport docx2txtimport PyPDF2
# Load the summarizer modelsummarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Function to extract text from supported file typesdef extract_text(file): if file is None: return "" file_path = file.name if file_path.endswith(".txt"): with open(file_path, "r", encoding="utf-8") as f: return f.read() elif file_path.endswith(".pdf"): reader = PyPDF2.PdfReader(file) return "\n".join(page.extract_text() for page in reader.pages if page.extract_text()) elif file_path.endswith(".docx"): return docx2txt.process(file) else: return "Unsupported file format. Please upload a .txt, .pdf, or .docx file."
# Summarize either text input or uploaded filedef summarize(text, file): extracted_text = text.strip() or extract_text(file) if not extracted_text: return "Please enter text or upload a file." summary = summarizer(extracted_text, max_length=130, min_length=30, do_sample=False) return summary[0]['summary_text']
# Gradio interfaceiface = gr.Interface( fn=summarize, inputs=[ gr.Textbox(lines=10, label="Enter text (optional)"), gr.File(label="Upload a file (.txt, .pdf, .docx)") ], outputs=gr.Textbox(label="Summary"), title="Text Summarizer", description="Paste text or upload a file to summarize using a BART model.")
iface.launch()app.pyAdded+45