abhishekrs4 commited on
Commit
9abecf2
1 Parent(s): 181ae85

added try-catch block in the fastapi app for smooth execution if the file is sent via streamlit file_uploader

Browse files
Files changed (1) hide show
  1. app.py +8 -2
app.py CHANGED
@@ -115,8 +115,14 @@ def _file_upload(image_file: UploadFile = File(...)) -> dict:
115
  a dict as a response json for the post request
116
  """
117
  logging.info(image_file)
118
- img_str = image_file.file.read()
119
- img_decoded = cv2.imdecode(np.frombuffer(img_str, np.uint8), 0)
 
 
 
 
 
 
120
  pred_label_str = get_prediction(img_decoded)
121
  response_json = {"name": image_file.filename, "prediction": pred_label_str}
122
  logging.info(response_json)
 
115
  a dict as a response json for the post request
116
  """
117
  logging.info(image_file)
118
+ try:
119
+ # if the file is sent via post request with open()
120
+ img_str = image_file.file.read()
121
+ img_decoded = cv2.imdecode(np.frombuffer(img_str, np.uint8), 0)
122
+ except:
123
+ # if the file is sent via post request from streamlit
124
+ img_decoded = cv2.imdecode(np.frombuffer(image_file.getvalue(), np.uint8), 0)
125
+
126
  pred_label_str = get_prediction(img_decoded)
127
  response_json = {"name": image_file.filename, "prediction": pred_label_str}
128
  logging.info(response_json)