nhathuy07 commited on
Commit
bc2d16f
·
verified ·
1 Parent(s): d06d9d7

Add error handling for LLM requests

Browse files
Files changed (1) hide show
  1. main.py +13 -6
main.py CHANGED
@@ -322,9 +322,12 @@ async def __query_ml_predict(qtype: QType, content: str, header: str, token_limi
322
  timeout=None
323
  )
324
 
325
- _w_content = _w.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
326
- print(time() - stopwatch)
327
- return {"content": f"{_r_content}\n{_w_content}", "style": QType.STMT}
 
 
 
328
 
329
 
330
  async def parse_wh_question(raw_qa_list, pgph_i):
@@ -503,8 +506,10 @@ async def generate_questions(request):
503
 
504
  for pgph_i, o in enumerate(__raw_outputs):
505
  # print(o)
506
- print(pgph_i)
507
  # TODO: Parse ML output to JSON
 
 
508
  if (o['style'] == QType.WH):
509
 
510
  raw_qa_list = []
@@ -527,8 +532,10 @@ async def generate_questions(request):
527
  __parsed_outputs.extend(await parse_stmt_question(stmts, pgph_i, request.path_params.get('lang', lang.VI_VN)))
528
 
529
  # Return the question data
530
-
531
- return JSONResponse({"questions": __parsed_outputs, "paragraphs": __ps, "title": __cont[0]})
 
 
532
 
533
  async def scan2OCR(request):
534
 
 
322
  timeout=None
323
  )
324
 
325
+ try:
326
+ _w_content = _w.json()['choices'][0]['message']['content'].split('\n\n',1)[1]
327
+ #print(time() - stopwatch)
328
+ return {"content": f"{_r_content}\n{_w_content}", "style": QType.STMT, "success": True}
329
+ except:
330
+ return {"content": "", "style": None, "success": False}
331
 
332
 
333
  async def parse_wh_question(raw_qa_list, pgph_i):
 
506
 
507
  for pgph_i, o in enumerate(__raw_outputs):
508
  # print(o)
509
+ # print(pgph_i)
510
  # TODO: Parse ML output to JSON
511
+ if (not o['success']):
512
+ continue
513
  if (o['style'] == QType.WH):
514
 
515
  raw_qa_list = []
 
532
  __parsed_outputs.extend(await parse_stmt_question(stmts, pgph_i, request.path_params.get('lang', lang.VI_VN)))
533
 
534
  # Return the question data
535
+ if len(__parsed_outputs):
536
+ return JSONResponse({"questions": __parsed_outputs, "paragraphs": __ps, "title": __cont[0]})
537
+ else:
538
+ raise HTTPException(500)
539
 
540
  async def scan2OCR(request):
541