ishworrsubedii commited on
Commit
bfb697d
·
1 Parent(s): 7956aa1

update: custom error messages

Browse files
src/api/nto_api.py CHANGED
@@ -66,7 +66,7 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
66
  logger.info(">>> IMAGE LOADED SUCCESSFULLY <<<")
67
  except Exception as e:
68
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
69
- return JSONResponse(status_code=500, content={"error": f"Error reading image: {str(e)}", "code": 500})
70
 
71
  try:
72
  mask = await pipeline.shoulderPointMaskGeneration_(image=image)
@@ -74,7 +74,7 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
74
  except Exception as e:
75
  logger.error(f">>> MASK GENERATION ERROR: {str(e)} <<<")
76
  return JSONResponse(status_code=500,
77
- content={"error": f"Error generating mask: {str(e)}", "code": 500})
78
 
79
  try:
80
  mask_img_base_64, act_img_base_64 = BytesIO(), BytesIO()
@@ -89,7 +89,7 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
89
  except Exception as e:
90
  logger.error(f">>> IMAGE ENCODING ERROR: {str(e)} <<<")
91
  return JSONResponse(status_code=500,
92
- content={"error": f"Error converting images to base64: {str(e)}", "code": 500})
93
 
94
  input = {
95
  "mask": mask_data_uri,
@@ -104,7 +104,7 @@ async def clothing_try_on_v2(image: UploadFile = File(...), clothing_type: str =
104
  logger.info(">>> REPLICATE PROCESSING COMPLETED <<<")
105
  except Exception as e:
106
  logger.error(f">>> REPLICATE PROCESSING ERROR: {str(e)} <<<")
107
- return JSONResponse(content={"error": f"Error running CTO Replicate: {str(e)}", "code": 500}, status_code=500)
108
 
109
  total_inference_time = round((time.time() - start_time), 2)
110
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
@@ -134,7 +134,7 @@ async def clothing_try_on(image: UploadFile = File(...),
134
  logger.info(">>> IMAGES LOADED SUCCESSFULLY <<<")
135
  except Exception as e:
136
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
137
- return JSONResponse(status_code=500, content={"error": f"Error reading image or mask: {str(e)}", "code": 500})
138
 
139
  try:
140
  actual_image = image.copy()
@@ -153,7 +153,7 @@ async def clothing_try_on(image: UploadFile = File(...),
153
  except Exception as e:
154
  logger.error(f">>> IMAGE PROCESSING ERROR: {str(e)} <<<")
155
  return JSONResponse(status_code=500,
156
- content={"error": f"Error processing image or mask: {str(e)}", "code": 500})
157
 
158
  try:
159
  mask_img_base_64, act_img_base_64 = BytesIO(), BytesIO()
@@ -168,7 +168,7 @@ async def clothing_try_on(image: UploadFile = File(...),
168
  except Exception as e:
169
  logger.error(f">>> IMAGE ENCODING ERROR: {str(e)} <<<")
170
  return JSONResponse(status_code=500,
171
- content={"error": f"Error encoding images to base64: {str(e)}", "code": 500})
172
 
173
  input = {
174
  "mask": mask_data_uri,
@@ -183,7 +183,7 @@ async def clothing_try_on(image: UploadFile = File(...),
183
  logger.info(">>> REPLICATE PROCESSING COMPLETED <<<")
184
  except Exception as e:
185
  logger.error(f">>> REPLICATE PROCESSING ERROR: {str(e)} <<<")
186
- return JSONResponse(content={"error": f"Error running CTO Replicate: {str(e)}", "code": 500}, status_code=500)
187
 
188
  try:
189
  response = requests.get(output[0])
@@ -205,7 +205,7 @@ async def clothing_try_on(image: UploadFile = File(...),
205
  }
206
  except Exception as e:
207
  logger.error(f">>> OUTPUT IMAGE PROCESSING ERROR: {str(e)} <<<")
208
- return JSONResponse(status_code=500, content={"error": f"Error processing output image: {str(e)}", "code": 500})
209
 
210
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
211
  logger.info(">>> REQUEST COMPLETED SUCCESSFULLY <<<")
@@ -213,6 +213,7 @@ async def clothing_try_on(image: UploadFile = File(...),
213
 
214
  return JSONResponse(content=response, status_code=200)
215
 
 
216
  @nto_cto_router.post("/productData/{storeId}")
217
  async def product_data(
218
  storeId: str,
@@ -373,7 +374,7 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
373
  logger.info(">>> API KEY VALIDATION SUCCESSFUL <<<")
374
  except Exception as e:
375
  logger.error(f">>> API KEY VALIDATION ERROR: {str(e)} <<<")
376
- return JSONResponse(content={"error": f"Error validating API key: {str(e)}", "code": 500}, status_code=500)
377
 
378
  try:
379
  imageBytes = await image.read()
@@ -383,16 +384,22 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
383
  except Exception as e:
384
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
385
  return JSONResponse(content={
386
- "error": f"The requested resource (Image, necklace category, or store) is not available. Please verify the availability and try again. Error: {str(e)}",
387
  "code": 404}, status_code=404)
388
 
389
  try:
390
  result, headetText, mask = await pipeline.necklaceTryOn_(image=image, jewellery=jewellery,
391
  storename=necklace_try_on_id.storename)
 
 
 
 
 
 
392
  logger.info(">>> NECKLACE TRY ON PROCESSING COMPLETED <<<")
393
  except Exception as e:
394
  logger.error(f">>> NECKLACE TRY ON PROCESSING ERROR: {str(e)} <<<")
395
- return JSONResponse(content={"error": f"Error during necklace try-on process: {str(e)}", "code": 500},
396
  status_code=500)
397
 
398
  try:
@@ -405,7 +412,7 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
405
  logger.info(">>> RESULT IMAGES SAVED <<<")
406
  except Exception as e:
407
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
408
- return JSONResponse(content={"error": f"Error saving result images: {str(e)}", "code": 500}, status_code=500)
409
 
410
  try:
411
  creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
@@ -423,7 +430,7 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
423
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
424
  except Exception as e:
425
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
426
- return JSONResponse(content={"error": f"Error deducting credits: {str(e)}", "code": 500}, status_code=500)
427
 
428
  logger.info(f">>> TOTAL INFERENCE TIME: {total_backend_time}s <<<")
429
  logger.info(f">>> NECKLACE TRY ON COMPLETED :: {necklace_try_on_id.storename} <<<")
@@ -431,6 +438,7 @@ async def necklace_try_on_id(necklace_try_on_id: NecklaceTryOnIDEntity = Depends
431
 
432
  return JSONResponse(content=response, status_code=200)
433
 
 
434
  @nto_cto_router.post("/canvasPoints")
435
  async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
436
  image: UploadFile = File(...)):
@@ -446,7 +454,7 @@ async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(pars
446
  except Exception as e:
447
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
448
  return JSONResponse(content={
449
- "error": f"The requested resource (Image, necklace category, or store) is not available. Please verify the availability and try again. Error: {str(e)}",
450
  "code": 404}, status_code=404)
451
 
452
  try:
@@ -455,7 +463,7 @@ async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(pars
455
  logger.info(">>> CANVAS POINTS PROCESSING COMPLETED <<<")
456
  except Exception as e:
457
  logger.error(f">>> CANVAS POINTS PROCESSING ERROR: {str(e)} <<<")
458
- return JSONResponse(content={"error": f"Error during canvas point process: {str(e)}", "code": 500},
459
  status_code=500)
460
 
461
  try:
@@ -466,7 +474,7 @@ async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(pars
466
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
467
  except Exception as e:
468
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
469
- return JSONResponse(content={"error": f"Error deducting credits: {str(e)}", "code": 500}, status_code=500)
470
 
471
  total_inference_time = round((time.time() - start_time), 2)
472
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
@@ -506,7 +514,7 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
506
  logger.info(">>> NECKLACE TRY ON PROCESSING COMPLETED <<<")
507
  except Exception as e:
508
  logger.error(f">>> NECKLACE TRY ON PROCESSING ERROR: {str(e)} <<<")
509
- return JSONResponse(content={"error": f"Error during necklace try-on process: {str(e)}", "code": 500},
510
  status_code=500)
511
 
512
  try:
@@ -519,7 +527,7 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
519
  logger.info(">>> RESULT IMAGES SAVED <<<")
520
  except Exception as e:
521
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
522
- return JSONResponse(content={"error": f"Error saving result images: {str(e)}", "code": 500}, status_code=500)
523
 
524
  try:
525
  creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
@@ -537,7 +545,7 @@ async def necklace_try_on_with_points(necklace_try_on_id: NecklaceTryOnIDEntity
537
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
538
  except Exception as e:
539
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
540
- return JSONResponse(content={"error": f"Error deducting credits: {str(e)}", "code": 500}, status_code=500)
541
 
542
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
543
  logger.info(f">>> NECKLACE TRY ON WITH POINTS COMPLETED :: {necklace_try_on_id.storename} <<<")
 
66
  logger.info(">>> IMAGE LOADED SUCCESSFULLY <<<")
67
  except Exception as e:
68
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
69
+ return JSONResponse(status_code=500, content={"error": f"Error reading image", "code": 500})
70
 
71
  try:
72
  mask = await pipeline.shoulderPointMaskGeneration_(image=image)
 
74
  except Exception as e:
75
  logger.error(f">>> MASK GENERATION ERROR: {str(e)} <<<")
76
  return JSONResponse(status_code=500,
77
+ content={"error": f"Error generating mask", "code": 500})
78
 
79
  try:
80
  mask_img_base_64, act_img_base_64 = BytesIO(), BytesIO()
 
89
  except Exception as e:
90
  logger.error(f">>> IMAGE ENCODING ERROR: {str(e)} <<<")
91
  return JSONResponse(status_code=500,
92
+ content={"error": f"Error converting images to base64", "code": 500})
93
 
94
  input = {
95
  "mask": mask_data_uri,
 
104
  logger.info(">>> REPLICATE PROCESSING COMPLETED <<<")
105
  except Exception as e:
106
  logger.error(f">>> REPLICATE PROCESSING ERROR: {str(e)} <<<")
107
+ return JSONResponse(content={"error": f"Error running CTO Replicate", "code": 500}, status_code=500)
108
 
109
  total_inference_time = round((time.time() - start_time), 2)
110
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
 
134
  logger.info(">>> IMAGES LOADED SUCCESSFULLY <<<")
135
  except Exception as e:
136
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
137
+ return JSONResponse(status_code=500, content={"error": f"Error reading image or mask", "code": 500})
138
 
139
  try:
140
  actual_image = image.copy()
 
153
  except Exception as e:
154
  logger.error(f">>> IMAGE PROCESSING ERROR: {str(e)} <<<")
155
  return JSONResponse(status_code=500,
156
+ content={"error": f"Error processing image or mask", "code": 500})
157
 
158
  try:
159
  mask_img_base_64, act_img_base_64 = BytesIO(), BytesIO()
 
168
  except Exception as e:
169
  logger.error(f">>> IMAGE ENCODING ERROR: {str(e)} <<<")
170
  return JSONResponse(status_code=500,
171
+ content={"error": f"Error encoding images", "code": 500})
172
 
173
  input = {
174
  "mask": mask_data_uri,
 
183
  logger.info(">>> REPLICATE PROCESSING COMPLETED <<<")
184
  except Exception as e:
185
  logger.error(f">>> REPLICATE PROCESSING ERROR: {str(e)} <<<")
186
+ return JSONResponse(content={"error": f"Error running clothing try on", "code": 500}, status_code=500)
187
 
188
  try:
189
  response = requests.get(output[0])
 
205
  }
206
  except Exception as e:
207
  logger.error(f">>> OUTPUT IMAGE PROCESSING ERROR: {str(e)} <<<")
208
+ return JSONResponse(status_code=500, content={"error": f"Error processing output image", "code": 500})
209
 
210
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
211
  logger.info(">>> REQUEST COMPLETED SUCCESSFULLY <<<")
 
213
 
214
  return JSONResponse(content=response, status_code=200)
215
 
216
+
217
  @nto_cto_router.post("/productData/{storeId}")
218
  async def product_data(
219
  storeId: str,
 
374
  logger.info(">>> API KEY VALIDATION SUCCESSFUL <<<")
375
  except Exception as e:
376
  logger.error(f">>> API KEY VALIDATION ERROR: {str(e)} <<<")
377
+ return JSONResponse(content={"error": f"Error validating API key", "code": 500}, status_code=500)
378
 
379
  try:
380
  imageBytes = await image.read()
 
384
  except Exception as e:
385
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
386
  return JSONResponse(content={
387
+ "error": f"The requested resource (Image, necklace category, or store) is not available. Please verify the availability and try again",
388
  "code": 404}, status_code=404)
389
 
390
  try:
391
  result, headetText, mask = await pipeline.necklaceTryOn_(image=image, jewellery=jewellery,
392
  storename=necklace_try_on_id.storename)
393
+
394
+ if result is None:
395
+ logger.error(">>> NO FACE DETECTED IN THE IMAGE <<<")
396
+ return JSONResponse(
397
+ content={"error": "No face detected in the image please try again with a different image",
398
+ "code": 400}, status_code=400)
399
  logger.info(">>> NECKLACE TRY ON PROCESSING COMPLETED <<<")
400
  except Exception as e:
401
  logger.error(f">>> NECKLACE TRY ON PROCESSING ERROR: {str(e)} <<<")
402
+ return JSONResponse(content={"error": f"Error during necklace try-on process", "code": 500},
403
  status_code=500)
404
 
405
  try:
 
412
  logger.info(">>> RESULT IMAGES SAVED <<<")
413
  except Exception as e:
414
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
415
+ return JSONResponse(content={"error": f"Error saving result images", "code": 500}, status_code=500)
416
 
417
  try:
418
  creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
 
430
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
431
  except Exception as e:
432
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
433
+ return JSONResponse(content={"error": f"Error deducting credits", "code": 500}, status_code=500)
434
 
435
  logger.info(f">>> TOTAL INFERENCE TIME: {total_backend_time}s <<<")
436
  logger.info(f">>> NECKLACE TRY ON COMPLETED :: {necklace_try_on_id.storename} <<<")
 
438
 
439
  return JSONResponse(content=response, status_code=200)
440
 
441
+
442
  @nto_cto_router.post("/canvasPoints")
443
  async def canvas_points(necklace_try_on_id: NecklaceTryOnIDEntity = Depends(parse_necklace_try_on_id),
444
  image: UploadFile = File(...)):
 
454
  except Exception as e:
455
  logger.error(f">>> IMAGE LOADING ERROR: {str(e)} <<<")
456
  return JSONResponse(content={
457
+ "error": f"The requested resource (Image, necklace category, or store) is not available. Please verify the availability and try again. Error",
458
  "code": 404}, status_code=404)
459
 
460
  try:
 
463
  logger.info(">>> CANVAS POINTS PROCESSING COMPLETED <<<")
464
  except Exception as e:
465
  logger.error(f">>> CANVAS POINTS PROCESSING ERROR: {str(e)} <<<")
466
+ return JSONResponse(content={"error": f"Error during canvas point process", "code": 500},
467
  status_code=500)
468
 
469
  try:
 
474
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
475
  except Exception as e:
476
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
477
+ return JSONResponse(content={"error": f"Error deducting credits", "code": 500}, status_code=500)
478
 
479
  total_inference_time = round((time.time() - start_time), 2)
480
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
 
514
  logger.info(">>> NECKLACE TRY ON PROCESSING COMPLETED <<<")
515
  except Exception as e:
516
  logger.error(f">>> NECKLACE TRY ON PROCESSING ERROR: {str(e)} <<<")
517
+ return JSONResponse(content={"error": f"Error during necklace try-on process", "code": 500},
518
  status_code=500)
519
 
520
  try:
 
527
  logger.info(">>> RESULT IMAGES SAVED <<<")
528
  except Exception as e:
529
  logger.error(f">>> RESULT SAVING ERROR: {str(e)} <<<")
530
+ return JSONResponse(content={"error": f"Error saving result images", "code": 500}, status_code=500)
531
 
532
  try:
533
  creditResponse = deductAndTrackCredit(storename=necklace_try_on_id.storename, endpoint="/necklaceTryOnID")
 
545
  logger.info(">>> CREDITS DEDUCTED SUCCESSFULLY <<<")
546
  except Exception as e:
547
  logger.error(f">>> CREDIT DEDUCTION ERROR: {str(e)} <<<")
548
+ return JSONResponse(content={"error": f"Error deducting credits", "code": 500}, status_code=500)
549
 
550
  logger.info(f">>> TOTAL INFERENCE TIME: {total_inference_time}s <<<")
551
  logger.info(f">>> NECKLACE TRY ON WITH POINTS COMPLETED :: {necklace_try_on_id.storename} <<<")
src/components/necklaceTryOn.py CHANGED
@@ -92,8 +92,9 @@ class NecklaceTryOn:
92
  return [result, headerText, mask]
93
 
94
  except Exception as e:
 
95
  logger.error(f"{CustomException(e)}:: {storename}")
96
- raise CustomException(e)
97
 
98
  def necklaceTryOnV3(self, image: Image.Image, jewellery: Image.Image, storename: str) -> list[
99
  Union[Image.Image, str]]:
 
92
  return [result, headerText, mask]
93
 
94
  except Exception as e:
95
+ logger.error(f">>> NECKLACE TRY ON ERROR: {str(e)} <<<")
96
  logger.error(f"{CustomException(e)}:: {storename}")
97
+ return [None, "error", None]
98
 
99
  def necklaceTryOnV3(self, image: Image.Image, jewellery: Image.Image, storename: str) -> list[
100
  Union[Image.Image, str]]: