ikraamkb commited on
Commit
0d83986
·
verified ·
1 Parent(s): c70bca3

link to frontend

Browse files
Files changed (1) hide show
  1. app.py +25 -59
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
- def summarize_document(file, summary_length: str, enable_tts: bool = True):
165
- if file is None:
166
- return "Please upload a document first", "", None, None
167
- file_path = file.name
168
- file_extension = file_path.split(".")[-1].lower()
169
- original_filename = os.path.basename(file_path)
170
- text, error = extract_text(file_path, file_extension)
171
- if error:
172
- return error, "", None, None
173
- if not text or len(text.split()) < 30:
174
- return "Document is too short or contains too little text to summarize", "", None, None
175
- try:
176
- summary = generate_summary(text, summary_length)
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="/")