Spaces:
Running
Running
link to frontend
Browse files
app.py
CHANGED
@@ -8,11 +8,13 @@ import re
|
|
8 |
import nltk
|
9 |
from nltk.tokenize import sent_tokenize
|
10 |
import torch
|
11 |
-
from fastapi import FastAPI
|
12 |
from fastapi.responses import RedirectResponse, FileResponse, JSONResponse
|
|
|
13 |
from gtts import gTTS
|
14 |
import tempfile
|
15 |
import os
|
|
|
16 |
import easyocr
|
17 |
from fpdf import FPDF
|
18 |
import datetime
|
@@ -23,6 +25,14 @@ nltk.download('punkt', quiet=True)
|
|
23 |
|
24 |
app = FastAPI()
|
25 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
MODEL_NAME = "facebook/bart-large-cnn"
|
27 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
28 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
@@ -161,61 +171,19 @@ def create_pdf(summary: str, original_filename: str):
|
|
161 |
print(f"Error creating PDF: {e}")
|
162 |
return ""
|
163 |
|
164 |
-
|
165 |
-
|
166 |
-
|
167 |
-
|
168 |
-
|
169 |
-
|
170 |
-
|
171 |
-
|
172 |
-
|
173 |
-
|
174 |
-
|
175 |
-
|
176 |
-
|
177 |
-
audio_path = text_to_speech(summary) if enable_tts else None
|
178 |
-
pdf_path = create_pdf(summary, original_filename) if summary else None
|
179 |
-
return summary, "", audio_path, pdf_path
|
180 |
-
except Exception as e:
|
181 |
-
return f"Summarization error: {str(e)}", "", None, None
|
182 |
-
|
183 |
-
with gr.Blocks(title="Document Summarizer", theme=gr.themes.Soft()) as demo:
|
184 |
-
gr.Markdown("# 📄 Advanced Document Summarizer")
|
185 |
-
gr.Markdown("Upload a document to generate a summary with audio and optional PDF download")
|
186 |
-
|
187 |
-
with gr.Row():
|
188 |
-
with gr.Column():
|
189 |
-
file_input = gr.File(
|
190 |
-
label="Upload Document",
|
191 |
-
file_types=[".pdf", ".docx", ".pptx", ".xlsx", ".jpg", ".jpeg", ".png"],
|
192 |
-
type="filepath"
|
193 |
-
)
|
194 |
-
length_radio = gr.Radio(
|
195 |
-
["short", "medium", "long"],
|
196 |
-
value="medium",
|
197 |
-
label="Summary Length"
|
198 |
-
)
|
199 |
-
submit_btn = gr.Button("Generate Summary", variant="primary")
|
200 |
-
|
201 |
-
with gr.Column():
|
202 |
-
output = gr.Textbox(label="Summary", lines=10)
|
203 |
-
audio_output = gr.Audio(label="Audio Summary")
|
204 |
-
pdf_download = gr.File(label="Download Summary as PDF", visible=False)
|
205 |
-
|
206 |
-
def summarize_and_return_ui(file, summary_length):
|
207 |
-
summary, _, audio_path, pdf_path = summarize_document(file, summary_length)
|
208 |
-
return (
|
209 |
-
summary,
|
210 |
-
audio_path,
|
211 |
-
gr.File(visible=pdf_path is not None, value=pdf_path)
|
212 |
-
)
|
213 |
-
|
214 |
-
submit_btn.click(
|
215 |
-
fn=summarize_and_return_ui,
|
216 |
-
inputs=[file_input, length_radio],
|
217 |
-
outputs=[output, audio_output, pdf_download]
|
218 |
-
)
|
219 |
|
220 |
@app.get("/files/{file_name}")
|
221 |
async def get_file(file_name: str):
|
@@ -224,8 +192,6 @@ async def get_file(file_name: str):
|
|
224 |
return FileResponse(file_path)
|
225 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
226 |
|
227 |
-
app = gr.mount_gradio_app(app, demo, path="/")
|
228 |
-
|
229 |
@app.get("/")
|
230 |
def redirect_to_interface():
|
231 |
-
return RedirectResponse(url="/")
|
|
|
8 |
import nltk
|
9 |
from nltk.tokenize import sent_tokenize
|
10 |
import torch
|
11 |
+
from fastapi import FastAPI, UploadFile, Form, File
|
12 |
from fastapi.responses import RedirectResponse, FileResponse, JSONResponse
|
13 |
+
from fastapi.middleware.cors import CORSMiddleware
|
14 |
from gtts import gTTS
|
15 |
import tempfile
|
16 |
import os
|
17 |
+
import shutil
|
18 |
import easyocr
|
19 |
from fpdf import FPDF
|
20 |
import datetime
|
|
|
25 |
|
26 |
app = FastAPI()
|
27 |
|
28 |
+
app.add_middleware(
|
29 |
+
CORSMiddleware,
|
30 |
+
allow_origins=["*"],
|
31 |
+
allow_credentials=True,
|
32 |
+
allow_methods=["*"],
|
33 |
+
allow_headers=["*"],
|
34 |
+
)
|
35 |
+
|
36 |
MODEL_NAME = "facebook/bart-large-cnn"
|
37 |
tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME)
|
38 |
model = AutoModelForSeq2SeqLM.from_pretrained(MODEL_NAME)
|
|
|
171 |
print(f"Error creating PDF: {e}")
|
172 |
return ""
|
173 |
|
174 |
+
@app.post("/summarize/")
|
175 |
+
async def summarize_api(file: UploadFile = File(...), length: str = Form("medium")):
|
176 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=os.path.splitext(file.filename)[1]) as temp:
|
177 |
+
shutil.copyfileobj(file.file, temp)
|
178 |
+
temp.flush()
|
179 |
+
class FileObj: name = temp.name
|
180 |
+
summary, _, audio_path, pdf_path = summarize_document(FileObj, length)
|
181 |
+
|
182 |
+
return {
|
183 |
+
"summary": summary,
|
184 |
+
"audio_url": f"/files/{os.path.basename(audio_path)}" if audio_path else None,
|
185 |
+
"pdf_url": f"/files/{os.path.basename(pdf_path)}" if pdf_path else None
|
186 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
187 |
|
188 |
@app.get("/files/{file_name}")
|
189 |
async def get_file(file_name: str):
|
|
|
192 |
return FileResponse(file_path)
|
193 |
return JSONResponse({"error": "File not found"}, status_code=404)
|
194 |
|
|
|
|
|
195 |
@app.get("/")
|
196 |
def redirect_to_interface():
|
197 |
+
return RedirectResponse(url="/")
|