aspendse commited on
Commit
4d13c37
·
verified ·
1 Parent(s): 9829ba6

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -28
app.py DELETED
@@ -1,28 +0,0 @@
1
- import gradio as gr from transformers import pipeline import docx2txt import PyPDF2 import os
2
-
3
- Load the summarizer model
4
-
5
- summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
6
-
7
- Extract text based on file type
8
-
9
- def extract_text(file): ext = file.name.split(".")[-1].lower() if ext == "txt": return file.read().decode("utf-8") elif ext == "pdf": reader = PyPDF2.PdfReader(file) return "\n".join(page.extract_text() for page in reader.pages if page.extract_text()) elif ext == "docx": return docx2txt.process(file) else: return "Unsupported file type. Please upload a .pdf, .docx, or .txt file."
10
-
11
- Chunk long text for full-document summarization
12
-
13
- def chunk_text(text, max_length=1000): paragraphs = text.split("\n") chunks = [] current_chunk = "" for para in paragraphs: if len(current_chunk) + len(para) < max_length: current_chunk += para + "\n" else: chunks.append(current_chunk.strip()) current_chunk = para + "\n" if current_chunk: chunks.append(current_chunk.strip()) return chunks
14
-
15
- Full summarization function
16
-
17
- def summarize_input(text, file): source_text = text.strip() if text.strip() else extract_text(file) if not source_text: return "Please enter text or upload a valid file."
18
-
19
- chunks = chunk_text(source_text)
20
- summaries = [summarizer(chunk, max_length=130, min_length=30, do_sample=False)[0]['summary_text'] for chunk in chunks]
21
- return "\n\n".join(summaries)
22
-
23
- Gradio interface
24
-
25
- iface = gr.Interface( fn=summarize_input, inputs=[ gr.Textbox(lines=8, label="Enter text (optional)"), gr.File(label="Upload file (.txt, .pdf, .docx)", file_types=[".pdf", ".docx", ".txt"]) ], outputs=gr.Textbox(label="Summary"), title="📄 Smart Document Summarizer", description="Paste text or upload a document to get a full summary using a Hugging Face transformer." )
26
-
27
- iface.launch()
28
-