def unhandled_exception(error): main.stop_hooks() main.logger.debug("Unhandled exception generated") error_message = "Encountered Exception: {0}".format(traceback.format_exc()) main.logger.error(error_message) internal_error = "An unexpected internal error occurred. {0}".format(error_message) return AMLResponse(internal_error, 500, json_str=False)
def run_scoring(user_func): main.start_hooks( request.environ.get('REQUEST_ID', '00000000-0000-0000-0000-000000000000')) try: response = invoke_user_with_timer(user_func) except ClientSideException: raise except ServerSideException: raise except TimeoutException: main.stop_hooks() main.send_exception_to_app_insights(request) raise except Exception as exc: main.stop_hooks() main.send_exception_to_app_insights(request) raise RunFunctionException(str(exc)) finally: main.stop_hooks() if isinstance(response, flask.Response): return response else: return AMLResponse(response, 200)
def run(request): try: input = request.get_data(False) # Darknet only has file-based APIs for reading images, so we have to write->read files. # If you are replacing scoring engine and it supports getting data from memory, then use `input` directly. fileIn = 'image.in' fileOut = 'image.out' with open(fileIn, "wb") as file: file.write(input) returnImage = request.args.get('output') == 'image' res = dn.detect(net, meta, fileIn.encode(), fileOut.encode() if returnImage else None) os.remove(fileIn) if returnImage: fileOut += '.jpg' with open(fileOut, "rb") as file: outBytes = file.read() os.remove(fileOut) resp = AMLResponse(outBytes, 200, json_str=False) resp.headers['Content-Type'] = 'image/png' return resp else: return res except Exception as e: return str(e)
def handle_exception(error): main.logger.debug("Run function timeout caught") main.stop_hooks() main.logger.error("Encountered Exception: {0}".format( traceback.format_exc())) return AMLResponse(error.message, error.status_code, json_str=False, run_function_failed=True)
def score_realtime(): flask.g.apiName = "/score" # always enforce content-type json as either the sdk or the user code is expected to json deserialize this main.logger.info("Validation Request Content-Type") if 'Content-Type' not in request.headers or parse_options_header( request.headers['Content-Type'])[0] != 'application/json': return AMLResponse( {"message": "Expects Content-Type to be application/json"}, 415) # always expects the response to be utf-8 encodeable service_input = request.data.decode("utf-8") # run the user-provided run function return run_scoring(service_input, request.headers)
def run_scoring(service_input, request_headers): main.logger.info("Received input: {0}".format(service_input)) main.logger.info("Headers passed in (total {0}):".format( len(request_headers))) for k, v in request_headers.items(): main.logger.info("\t{0}: {1}".format(k, v)) main.start_hooks( request.environ.get('REQUEST_ID', '00000000-0000-0000-0000-000000000000')) try: response = invoke_user_with_timer(service_input, request_headers) except ClientSideException: raise except ServerSideException: raise except TimeoutException: main.stop_hooks() main.send_exception_to_app_insights(request) raise except Exception as exc: main.stop_hooks() main.send_exception_to_app_insights(request) raise RunFunctionException(str(exc)) finally: main.stop_hooks() response_headers = {} response_body = response response_status_code = 200 if isinstance(response, dict): if 'aml_response_headers' in response: main.logger.info( "aml_response_headers are available from run() output") response_body = None response_headers = response['aml_response_headers'] if 'aml_response_body' in response: main.logger.info( "aml_response_body is available from run() output") response_body = response['aml_response_body'] return AMLResponse(response_body, response_status_code, response_headers)
def get_swagger_specification(): flask.g.apiName = "/swagger.json" if main.swagger: return AMLResponse(main.swagger, 200) main.logger.info("Swagger file not present") return AMLResponse("Swagger not found", 404)
def handle_exception(error): main.logger.debug("Client request exception caught") main.stop_hooks() main.logger.error("Encountered Exception: {0}".format( traceback.format_exc())) return AMLResponse(error.to_dict(), error.status_code)