Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -104,13 +104,9 @@ from nltk.tokenize import sent_tokenize
|
|
104 |
import torch
|
105 |
from fastapi import FastAPI
|
106 |
from fastapi.responses import RedirectResponse
|
107 |
-
import os
|
108 |
|
109 |
# Download required NLTK data
|
110 |
-
|
111 |
-
nltk.data.find('tokenizers/punkt')
|
112 |
-
except LookupError:
|
113 |
-
nltk.download('punkt')
|
114 |
|
115 |
# Initialize components
|
116 |
app = FastAPI()
|
@@ -226,22 +222,23 @@ def generate_summary(text: str, length: str = "medium") -> str:
|
|
226 |
def summarize_document(file, summary_length: str):
|
227 |
"""Main processing function for Gradio interface"""
|
228 |
if file is None:
|
229 |
-
return "Please upload a document first"
|
230 |
|
231 |
file_path = file.name
|
232 |
file_extension = file_path.split(".")[-1].lower()
|
233 |
|
234 |
text, error = extract_text(file_path, file_extension)
|
235 |
if error:
|
236 |
-
return error
|
237 |
|
238 |
if not text or len(text.split()) < 30:
|
239 |
-
return "Document is too short or contains too little text to summarize"
|
240 |
|
241 |
try:
|
242 |
-
|
|
|
243 |
except Exception as e:
|
244 |
-
return f"Summarization error: {str(e)}"
|
245 |
|
246 |
# Gradio Interface
|
247 |
with gr.Blocks(title="Document Summarizer", theme=gr.themes.Soft()) as demo:
|
|
|
104 |
import torch
|
105 |
from fastapi import FastAPI
|
106 |
from fastapi.responses import RedirectResponse
|
|
|
107 |
|
108 |
# Download required NLTK data
|
109 |
+
nltk.download('punkt', quiet=True)
|
|
|
|
|
|
|
110 |
|
111 |
# Initialize components
|
112 |
app = FastAPI()
|
|
|
222 |
def summarize_document(file, summary_length: str):
|
223 |
"""Main processing function for Gradio interface"""
|
224 |
if file is None:
|
225 |
+
return "Please upload a document first", "Ready"
|
226 |
|
227 |
file_path = file.name
|
228 |
file_extension = file_path.split(".")[-1].lower()
|
229 |
|
230 |
text, error = extract_text(file_path, file_extension)
|
231 |
if error:
|
232 |
+
return error, "Error"
|
233 |
|
234 |
if not text or len(text.split()) < 30:
|
235 |
+
return "Document is too short or contains too little text to summarize", "Ready"
|
236 |
|
237 |
try:
|
238 |
+
summary = generate_summary(text, summary_length)
|
239 |
+
return summary, "Summary complete"
|
240 |
except Exception as e:
|
241 |
+
return f"Summarization error: {str(e)}", "Error"
|
242 |
|
243 |
# Gradio Interface
|
244 |
with gr.Blocks(title="Document Summarizer", theme=gr.themes.Soft()) as demo:
|