Meet Patel commited on
Commit
1f73b58
·
1 Parent(s): 2dc3c19

Refactor response handling in app.py to standardize JSON output formatting. Update error management to return pretty-printed JSON for both successful and failed responses across various interaction tools, enhancing readability and consistency in data handling.

Browse files
Files changed (1) hide show
  1. app.py +18 -18
app.py CHANGED
@@ -427,15 +427,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
427
  quiz_data = json.loads(item.text)
428
  return quiz_data
429
  except Exception:
430
- return {"raw": item.text}
431
  if isinstance(response, dict):
432
  return response
433
  if isinstance(response, str):
434
  try:
435
  return json.loads(response)
436
  except Exception:
437
- return {"raw": response}
438
- return {"raw": str(response)}
439
  except Exception as e:
440
  import traceback
441
  return {
@@ -474,15 +474,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
474
  lesson_data = json.loads(item.text)
475
  return lesson_data
476
  except Exception:
477
- return {"raw": item.text}
478
  if isinstance(response, dict):
479
  return response
480
  if isinstance(response, str):
481
  try:
482
  return json.loads(response)
483
  except Exception:
484
- return {"raw": response}
485
- return {"raw": str(response)}
486
 
487
  gen_lesson_btn.click(
488
  fn=generate_lesson_async,
@@ -516,15 +516,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
516
  lp_data = json.loads(item.text)
517
  return lp_data
518
  except Exception:
519
- return {"raw": item.text}
520
  if isinstance(result, dict):
521
  return result
522
  if isinstance(result, str):
523
  try:
524
  return json.loads(result)
525
  except Exception:
526
- return {"raw": result}
527
- return {"raw": str(result)}
528
  except Exception as e:
529
  return {"error": str(e)}
530
  lp_btn.click(
@@ -556,15 +556,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
556
  data = json.loads(item.text)
557
  return data
558
  except Exception:
559
- return {"raw": item.text}
560
  if isinstance(response, dict):
561
  return response
562
  if isinstance(response, str):
563
  try:
564
  return json.loads(response)
565
  except Exception:
566
- return {"raw": response}
567
- return {"raw": str(response)}
568
 
569
  text_btn.click(
570
  fn=text_interaction_async,
@@ -619,15 +619,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
619
  data = json.loads(item.text)
620
  return data
621
  except Exception:
622
- return {"raw": item.text}
623
  if isinstance(response, dict):
624
  return response
625
  if isinstance(response, str):
626
  try:
627
  return json.loads(response)
628
  except Exception:
629
- return {"raw": response}
630
- return {"raw": str(response)}
631
  except Exception as e:
632
  return {"error": f"Error processing document: {str(e)}", "success": False}
633
  doc_ocr_btn.click(
@@ -669,15 +669,15 @@ with gr.Blocks(title="TutorX Educational AI", theme=gr.themes.Soft()) as demo:
669
  data = json.loads(item.text)
670
  return data
671
  except Exception:
672
- return {"raw": item.text}
673
  if isinstance(response, dict):
674
  return response
675
  if isinstance(response, str):
676
  try:
677
  return json.loads(response)
678
  except Exception:
679
- return {"raw": response}
680
- return {"raw": str(response)}
681
 
682
  plagiarism_btn.click(
683
  fn=check_plagiarism_async,
 
427
  quiz_data = json.loads(item.text)
428
  return quiz_data
429
  except Exception:
430
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
431
  if isinstance(response, dict):
432
  return response
433
  if isinstance(response, str):
434
  try:
435
  return json.loads(response)
436
  except Exception:
437
+ return {"raw_pretty": json.dumps(response, indent=2)}
438
+ return {"raw_pretty": json.dumps(str(response), indent=2)}
439
  except Exception as e:
440
  import traceback
441
  return {
 
474
  lesson_data = json.loads(item.text)
475
  return lesson_data
476
  except Exception:
477
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
478
  if isinstance(response, dict):
479
  return response
480
  if isinstance(response, str):
481
  try:
482
  return json.loads(response)
483
  except Exception:
484
+ return {"raw_pretty": json.dumps(response, indent=2)}
485
+ return {"raw_pretty": json.dumps(str(response), indent=2)}
486
 
487
  gen_lesson_btn.click(
488
  fn=generate_lesson_async,
 
516
  lp_data = json.loads(item.text)
517
  return lp_data
518
  except Exception:
519
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
520
  if isinstance(result, dict):
521
  return result
522
  if isinstance(result, str):
523
  try:
524
  return json.loads(result)
525
  except Exception:
526
+ return {"raw_pretty": json.dumps(result, indent=2)}
527
+ return {"raw_pretty": json.dumps(str(result), indent=2)}
528
  except Exception as e:
529
  return {"error": str(e)}
530
  lp_btn.click(
 
556
  data = json.loads(item.text)
557
  return data
558
  except Exception:
559
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
560
  if isinstance(response, dict):
561
  return response
562
  if isinstance(response, str):
563
  try:
564
  return json.loads(response)
565
  except Exception:
566
+ return {"raw_pretty": json.dumps(response, indent=2)}
567
+ return {"raw_pretty": json.dumps(str(response), indent=2)}
568
 
569
  text_btn.click(
570
  fn=text_interaction_async,
 
619
  data = json.loads(item.text)
620
  return data
621
  except Exception:
622
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
623
  if isinstance(response, dict):
624
  return response
625
  if isinstance(response, str):
626
  try:
627
  return json.loads(response)
628
  except Exception:
629
+ return {"raw_pretty": json.dumps(response, indent=2)}
630
+ return {"raw_pretty": json.dumps(str(response), indent=2)}
631
  except Exception as e:
632
  return {"error": f"Error processing document: {str(e)}", "success": False}
633
  doc_ocr_btn.click(
 
669
  data = json.loads(item.text)
670
  return data
671
  except Exception:
672
+ return {"raw_pretty": json.dumps(item.text, indent=2)}
673
  if isinstance(response, dict):
674
  return response
675
  if isinstance(response, str):
676
  try:
677
  return json.loads(response)
678
  except Exception:
679
+ return {"raw_pretty": json.dumps(response, indent=2)}
680
+ return {"raw_pretty": json.dumps(str(response), indent=2)}
681
 
682
  plagiarism_btn.click(
683
  fn=check_plagiarism_async,