PrathameshRaut commited on
Commit
50bf236
·
verified ·
1 Parent(s): 1c8395f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -5
app.py CHANGED
@@ -7,6 +7,7 @@ import asyncio
7
  import subprocess
8
  import tempfile
9
  import threading
 
10
  from datetime import datetime, timezone
11
  from pathlib import Path
12
 
@@ -59,6 +60,8 @@ LOG_HEADERS = [
59
  "parameters_requested",
60
  "model_raw_thinking",
61
  "extracted_json",
 
 
62
  "status",
63
  "error_detail",
64
  ]
@@ -124,8 +127,10 @@ def _download_or_create_workbook() -> openpyxl.Workbook:
124
  "F": 35,
125
  "G": 60,
126
  "H": 60,
127
- "I": 12,
128
- "J": 40,
 
 
129
  }
130
  for col, width in col_widths.items():
131
  ws.column_dimensions[col].width = width
@@ -159,6 +164,8 @@ def append_log(
159
  extracted_json: dict | None,
160
  status: str,
161
  error_detail: str = "",
 
 
162
  ):
163
  """Thread-safe: download → append row → upload."""
164
  if not HF_TOKEN:
@@ -173,6 +180,8 @@ def append_log(
173
  ", ".join(parameters),
174
  (raw_thinking or "")[:5000],
175
  json.dumps(extracted_json, ensure_ascii=False) if extracted_json else "",
 
 
176
  status,
177
  error_detail[:1000],
178
  ]
@@ -452,6 +461,8 @@ async def extract_document(
452
 
453
  raw_text = ""
454
  extracted = {}
 
 
455
 
456
  try:
457
  processed_bytes, processed_name = prepare_file(file_bytes, content_type, filename)
@@ -464,7 +475,9 @@ async def extract_document(
464
  raise
465
 
466
  try:
 
467
  raw_text = await asyncio.to_thread(extract_text_with_sdk, processed_bytes, processed_name)
 
468
  except HTTPException as exc:
469
  asyncio.get_event_loop().run_in_executor(
470
  None, append_log,
@@ -477,12 +490,15 @@ async def extract_document(
477
  raw_text = clean_raw_text(raw_text)
478
 
479
  try:
 
480
  extracted = await extract_json_via_chat(document_type, raw_text, param_list)
 
481
  except HTTPException as exc:
482
  asyncio.get_event_loop().run_in_executor(
483
  None, append_log,
484
  filename, file_size, content_type, document_type, param_list,
485
- raw_text, None, "error", str(exc.detail)
 
486
  )
487
  raise
488
 
@@ -490,7 +506,8 @@ async def extract_document(
490
  asyncio.get_event_loop().run_in_executor(
491
  None, append_log,
492
  filename, file_size, content_type, document_type, param_list,
493
- raw_text, extracted, "success", ""
 
494
  )
495
 
496
  return JSONResponse(content={
@@ -503,4 +520,4 @@ async def extract_document(
503
 
504
  @app.get("/health")
505
  def health():
506
- return {"status": "ok"}
 
7
  import subprocess
8
  import tempfile
9
  import threading
10
+ import time
11
  from datetime import datetime, timezone
12
  from pathlib import Path
13
 
 
60
  "parameters_requested",
61
  "model_raw_thinking",
62
  "extracted_json",
63
+ "vision_time_seconds",
64
+ "llm_time_seconds",
65
  "status",
66
  "error_detail",
67
  ]
 
127
  "F": 35,
128
  "G": 60,
129
  "H": 60,
130
+ "I": 18,
131
+ "J": 18,
132
+ "K": 12,
133
+ "L": 40,
134
  }
135
  for col, width in col_widths.items():
136
  ws.column_dimensions[col].width = width
 
164
  extracted_json: dict | None,
165
  status: str,
166
  error_detail: str = "",
167
+ vision_time: float | None = None,
168
+ llm_time: float | None = None,
169
  ):
170
  """Thread-safe: download → append row → upload."""
171
  if not HF_TOKEN:
 
180
  ", ".join(parameters),
181
  (raw_thinking or "")[:5000],
182
  json.dumps(extracted_json, ensure_ascii=False) if extracted_json else "",
183
+ round(vision_time, 2) if vision_time is not None else "",
184
+ round(llm_time, 2) if llm_time is not None else "",
185
  status,
186
  error_detail[:1000],
187
  ]
 
461
 
462
  raw_text = ""
463
  extracted = {}
464
+ vision_time = None
465
+ llm_time = None
466
 
467
  try:
468
  processed_bytes, processed_name = prepare_file(file_bytes, content_type, filename)
 
475
  raise
476
 
477
  try:
478
+ t0 = time.time()
479
  raw_text = await asyncio.to_thread(extract_text_with_sdk, processed_bytes, processed_name)
480
+ vision_time = time.time() - t0
481
  except HTTPException as exc:
482
  asyncio.get_event_loop().run_in_executor(
483
  None, append_log,
 
490
  raw_text = clean_raw_text(raw_text)
491
 
492
  try:
493
+ t0 = time.time()
494
  extracted = await extract_json_via_chat(document_type, raw_text, param_list)
495
+ llm_time = time.time() - t0
496
  except HTTPException as exc:
497
  asyncio.get_event_loop().run_in_executor(
498
  None, append_log,
499
  filename, file_size, content_type, document_type, param_list,
500
+ raw_text, None, "error", str(exc.detail),
501
+ vision_time
502
  )
503
  raise
504
 
 
506
  asyncio.get_event_loop().run_in_executor(
507
  None, append_log,
508
  filename, file_size, content_type, document_type, param_list,
509
+ raw_text, extracted, "success", "",
510
+ vision_time, llm_time
511
  )
512
 
513
  return JSONResponse(content={
 
520
 
521
  @app.get("/health")
522
  def health():
523
+ return {"status": "ok"}