def update_blacklist(): tornado.options.options.ip_blacklist = blacklist()
def post(self): uid = str(uuid.uuid1()) start_time = time.time() data = self.parse_param() request_def_map = system_config.request_def_map input_data_key = request_def_map['InputData'] model_name_key = request_def_map['ModelName'] if input_data_key not in data.keys(): raise tornado.web.HTTPError(400) model_name = ParamUtils.filter(data.get(model_name_key)) output_split = ParamUtils.filter(data.get('output_split')) need_color = ParamUtils.filter(data.get('need_color')) param_key = ParamUtils.filter(data.get('param_key')) extract_rgb = ParamUtils.filter(data.get('extract_rgb')) request_incr = self.request_incr global_count = self.global_request_incr request_count = " - Count[{}]".format(request_incr) log_params = " - ParamKey[{}]".format(param_key) if param_key else "" log_params += " - NeedColor[{}]".format( need_color) if need_color else "" if interface_manager.total == 0: self.request_desc() self.global_request_desc() logger.info( 'There is currently no model deployment and services are not available.' ) return self.finish( json_encode({ self.uid_key: uid, self.message_key: "", self.status_bool_key: False, self.status_code_key: -999 })) bytes_batch, response = self.image_utils.get_bytes_batch( data[input_data_key]) if not bytes_batch: logger.error('[{}] - [{} {}] | - Response[{}] - {} ms'.format( uid, self.request.remote_ip, self.request.uri, response, (time.time() - start_time) * 1000)) return self.finish(json_encode(response)) image_sample = bytes_batch[0] image_size = ImageUtils.size_of_image(image_sample) size_string = "{}x{}".format(image_size[0], image_size[1]) if system_config.request_size_limit and size_string not in system_config.request_size_limit: self.request_desc() self.global_request_desc() logger.info( '[{}] - [{} {}] | Size[{}] - [{}][{}] - Error[{}] - {} ms'. format(uid, self.request.remote_ip, self.request.uri, size_string, global_count, log_params, "Image size is invalid.", round((time.time() - start_time) * 1000))) msg = system_config.request_size_limit.get("msg") msg = msg if msg else "The size of the picture is wrong. " \ "Only the original image is supported. " \ "Please do not take a screenshot!" return self.finish( json.dumps( { self.uid_key: uid, self.message_key: msg, self.status_bool_key: False, self.status_code_key: -250 }, ensure_ascii=False)) if system_config.use_whitelist: assert_whitelist = self.match_whitelist(self.request.remote_ip) if not assert_whitelist: logger.info( '[{}] - [{} {}] | Size[{}]{}{} - Error[{}] - {} ms'.format( uid, self.request.remote_ip, self.request.uri, size_string, request_count, log_params, "Whitelist limit", round((time.time() - start_time) * 1000))) return self.finish( json.dumps( { self.uid_key: uid, self.message_key: "Only allow IP access in the whitelist", self.status_bool_key: False, self.status_code_key: -111 }, ensure_ascii=False)) if global_request_limit != -1 and global_count > global_request_limit: logger.info( '[{}] - [{} {}] | Size[{}]{}{} - Error[{}] - {} ms'.format( uid, self.request.remote_ip, self.request.uri, size_string, global_count, log_params, "Maximum number of requests exceeded (G)", round((time.time() - start_time) * 1000))) return self.finish( json.dumps( { self.uid_key: uid, self.message_key: system_config.exceeded_msg, self.status_bool_key: False, self.status_code_key: -555 }, ensure_ascii=False)) assert_blacklist = self.match_blacklist(self.request.remote_ip) if assert_blacklist: logger.info( '[{}] - [{} {}] | Size[{}]{}{} - Error[{}] - {} ms'.format( uid, self.request.remote_ip, self.request.uri, size_string, request_count, log_params, "The ip is on the risk blacklist (IP)", round((time.time() - start_time) * 1000))) return self.finish( json.dumps( { self.uid_key: uid, self.message_key: system_config.exceeded_msg, self.status_bool_key: False, self.status_code_key: -110 }, ensure_ascii=False)) if request_limit != -1 and request_incr > request_limit: self.risk_ip_count(self.request.remote_ip) assert_blacklist_trigger = system_config.blacklist_trigger_times != -1 if self.risk_ip( self.request.remote_ip ) > system_config.blacklist_trigger_times and assert_blacklist_trigger: if self.request.remote_ip not in blacklist(): set_blacklist(self.request.remote_ip) update_blacklist() logger.info( '[{}] - [{} {}] | Size[{}]{}{} - Error[{}] - {} ms'.format( uid, self.request.remote_ip, self.request.uri, size_string, request_count, log_params, "Maximum number of requests exceeded (IP)", round((time.time() - start_time) * 1000))) return self.finish( json.dumps( { self.uid_key: uid, self.message_key: system_config.exceeded_msg, self.status_bool_key: False, self.status_code_key: -444 }, ensure_ascii=False)) if model_name_key in data and data[model_name_key]: interface = interface_manager.get_by_name(model_name) else: interface = interface_manager.get_by_size(size_string) if not interface: self.request_desc() self.global_request_desc() logger.info('Service is not ready!') return self.finish( json_encode({ self.uid_key: uid, self.message_key: "", self.status_bool_key: False, self.status_code_key: 999 })) output_split = output_split if 'output_split' in data else interface.model_conf.output_split if interface.model_conf.corp_params: bytes_batch = corp_to_multi.parse_multi_img( bytes_batch, interface.model_conf.corp_params) exec_map = interface.model_conf.exec_map if exec_map and len(exec_map.keys()) > 1 and not param_key: self.request_desc() self.global_request_desc() logger.info( '[{}] - [{} {}] | [{}] - Size[{}]{}{} - Error[{}] - {} ms'. format( uid, self.request.remote_ip, self.request.uri, interface.name, size_string, request_count, log_params, "The model is missing the param_key parameter because the model is configured with ExecuteMap.", round((time.time() - start_time) * 1000))) return self.finish( json_encode({ self.uid_key: uid, self.message_key: "Missing the parameter [param_key].", self.status_bool_key: False, self.status_code_key: 474 })) elif exec_map and param_key and param_key not in exec_map: self.request_desc() self.global_request_desc() logger.info( '[{}] - [{} {}] | [{}] - Size[{}]{}{} - Error[{}] - {} ms'. format(uid, self.request.remote_ip, self.request.uri, interface.name, size_string, request_count, log_params, "The param_key parameter is not support in the model.", round((time.time() - start_time) * 1000))) return self.finish( json_encode({ self.uid_key: uid, self.message_key: "Not support the parameter [param_key].", self.status_bool_key: False, self.status_code_key: 474 })) elif exec_map and len(exec_map.keys()) == 1: param_key = list(interface.model_conf.exec_map.keys())[0] if interface.model_conf.external_model and interface.model_conf.corp_params: result = [] len_of_result = [] pre_corp_num = 0 for corp_param in interface.model_conf.corp_params: corp_size = corp_param['corp_size'] corp_num_list = corp_param['corp_num'] corp_num = corp_num_list[0] * corp_num_list[1] sub_bytes_batch = bytes_batch[pre_corp_num:pre_corp_num + corp_num] pre_corp_num = corp_num size_string = "{}x{}".format(corp_size[0], corp_size[1]) sub_interface = interface_manager.get_by_size(size_string) image_batch, response = ImageUtils.get_image_batch( sub_interface.model_conf, sub_bytes_batch, param_key=param_key) text = yield self.predict(sub_interface, image_batch, output_split, size_string, start_time, log_params, request_count, uid=uid) result.append(text) len_of_result.append( len(result[0].split( sub_interface.model_conf.category_split))) response[ self.message_key] = interface.model_conf.output_split.join( result) if interface.model_conf.corp_params and interface.model_conf.output_coord: # final_result = auxiliary_result + "," + response[self.message_key] # if auxiliary_result else response[self.message_key] final_result = response[self.message_key] response[self.message_key] = corp_to_multi.get_coordinate( label=final_result, param_group=interface.model_conf.corp_params, title_index=[i for i in range(len_of_result[0])]) return self.finish( json.dumps(response, ensure_ascii=False).replace("</", "<\\/")) else: image_batch, response = ImageUtils.get_image_batch( interface.model_conf, bytes_batch, param_key=param_key, extract_rgb=extract_rgb) if not image_batch: self.request_desc() self.global_request_desc() logger.error( '[{}] - [{} {}] | [{}] - Size[{}] - Response[{}] - {} ms'. format(uid, self.request.remote_ip, self.request.uri, interface.name, size_string, response, round((time.time() - start_time) * 1000))) response[self.uid_key] = uid return self.finish(json_encode(response)) predict_result = yield self.predict(interface, image_batch, output_split) # if need_color: # # only support six label and size [90x35]. # color_batch = np.resize(image_batch[0], (90, 35, 3)) # need_index = color_extract.predict_color(image_batch=[color_batch], color=color_map[need_color]) # predict_result = "".join([v for i, v in enumerate(predict_result) if i in need_index]) uid_str = "[{}] - ".format(uid) logger.info( '{}[{} {}] | [{}] - Size[{}]{}{} - Predict[{}] - {} ms'.format( uid_str, self.request.remote_ip, self.request.uri, interface.name, size_string, request_count, log_params, predict_result, round((time.time() - start_time) * 1000))) response[self.message_key] = predict_result response[self.uid_key] = uid self.executor.submit(self.save_image, uid, response[self.message_key], bytes_batch[0]) if interface.model_conf.corp_params and interface.model_conf.output_coord: # final_result = auxiliary_result + "," + response[self.message_key] # if auxiliary_result else response[self.message_key] final_result = response[self.message_key] response[self.message_key] = corp_to_multi.get_coordinate( label=final_result, param_group=interface.model_conf.corp_params, title_index=[0]) return self.finish( json.dumps(response, ensure_ascii=False).replace("</", "<\\/"))