diff --git a/gunicorn_config.py b/gunicorn_config.py index c7216e5..a2a5069 100644 --- a/gunicorn_config.py +++ b/gunicorn_config.py @@ -1,7 +1,7 @@ # 并行工作线程数 workers = 8 # 监听内网端口5000【按需要更改】 -bind = '0.0.0.0:12003' +bind = '0.0.0.0:12000' loglevel = 'debug' diff --git a/mistral_api.py b/mistral_api.py index 3f3cd16..af560ed 100644 --- a/mistral_api.py +++ b/mistral_api.py @@ -59,19 +59,19 @@ def predict(): id_ = str(uuid.uuid1()) # 为query生成唯一标识 print("uuid: ", uuid) d = {'id': id_, 'text': text} # 绑定文本和query id - try: - load_request_path = './request_data_logs/{}.json'.format(id_) - with open(load_request_path, 'w', encoding='utf8') as f2: - # ensure_ascii=False才能输入中文,否则是Unicode字符 - # indent=2 JSON数据的缩进,美观 - json.dump(d, f2, ensure_ascii=False, indent=4) - redis_.rpush(db_key_query, json.dumps({"id": id_, "path": load_request_path})) # 加入redis - redis_.sadd(db_key_querying, id_) - redis_.sadd(db_key_queryset, id_) - return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 200} - except: - return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 400} - smtp_f("vllm-main") + # try: + load_request_path = './request_data_logs/{}.json'.format(id_) + with open(load_request_path, 'w', encoding='utf8') as f2: + # ensure_ascii=False才能输入中文,否则是Unicode字符 + # indent=2 JSON数据的缩进,美观 + json.dump(d, f2, ensure_ascii=False, indent=4) + redis_.rpush(db_key_query, json.dumps({"id": id_, "path": load_request_path})) # 加入redis + redis_.sadd(db_key_querying, id_) + redis_.sadd(db_key_queryset, id_) + return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 200} + # except: + # return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 400} + # smtp_f("vllm-main") return jsonify(return_text) # 返回结果 @@ -79,54 +79,54 @@ def predict(): def search(): id_ = request.json['id'] # 获取用户query中的文本 例如"I love you" result = redis_.get(id_) # 获取该query的模型结果 - try: - if result is not None: - result_path = result.decode('UTF-8') - with open(result_path, encoding='utf8') as f1: - # 加载文件的对象 - result_dict = json.load(f1) - code = result_dict["status_code"] - texts = result_dict["texts"] - probabilities = result_dict["probabilities"] - if str(code) == 400: - redis_.rpush(db_key_error, json.dumps({"id": id_})) - return False - result_text = {'code': code, 'text': texts, 'probabilities': probabilities} + # try: + if result is not None: + result_path = result.decode('UTF-8') + with open(result_path, encoding='utf8') as f1: + # 加载文件的对象 + result_dict = json.load(f1) + code = result_dict["status_code"] + texts = result_dict["texts"] + probabilities = result_dict["probabilities"] + if str(code) == 400: + redis_.rpush(db_key_error, json.dumps({"id": id_})) + return False + result_text = {'code': code, 'text': texts, 'probabilities': probabilities} + else: + querying_list = list(redis_.smembers(db_key_querying)) + querying_set = set() + for i in querying_list: + querying_set.add(i.decode()) + + querying_bool = False + if id_ in querying_set: + querying_bool = True + + query_list_json = redis_.lrange(db_key_query, 0, -1) + query_set_ids = set() + for i in query_list_json: + data_dict = json.loads(i) + query_id = data_dict['id'] + query_set_ids.add(query_id) + + query_bool = False + if id_ in query_set_ids: + query_bool = True + + if querying_bool == True and query_bool == True: + result_text = {'code': "201", 'text': "", 'probabilities': None} + elif querying_bool == True and query_bool == False: + result_text = {'code': "202", 'text': "", 'probabilities': None} else: - querying_list = list(redis_.smembers(db_key_querying)) - querying_set = set() - for i in querying_list: - querying_set.add(i.decode()) - - querying_bool = False - if id_ in querying_set: - querying_bool = True - - query_list_json = redis_.lrange(db_key_query, 0, -1) - query_set_ids = set() - for i in query_list_json: - data_dict = json.loads(i) - query_id = data_dict['id'] - query_set_ids.add(query_id) - - query_bool = False - if id_ in query_set_ids: - query_bool = True - - if querying_bool == True and query_bool == True: - result_text = {'code': "201", 'text': "", 'probabilities': None} - elif querying_bool == True and query_bool == False: - result_text = {'code': "202", 'text': "", 'probabilities': None} - else: - result_text = {'code': "203", 'text': "", 'probabilities': None} - load_request_path = './request_data_logs_203/{}.json'.format(id_) - with open(load_request_path, 'w', encoding='utf8') as f2: - # ensure_ascii=False才能输入中文,否则是Unicode字符 - # indent=2 JSON数据的缩进,美观 - json.dump(result_text, f2, ensure_ascii=False, indent=4) - except: - smtp_f("vllm-main") - result_text = {'code': "400", 'text': "", 'probabilities': None} + result_text = {'code': "203", 'text': "", 'probabilities': None} + load_request_path = './request_data_logs_203/{}.json'.format(id_) + with open(load_request_path, 'w', encoding='utf8') as f2: + # ensure_ascii=False才能输入中文,否则是Unicode字符 + # indent=2 JSON数据的缩进,美观 + json.dump(result_text, f2, ensure_ascii=False, indent=4) + # except: + # smtp_f("vllm-main") + # result_text = {'code': "400", 'text': "", 'probabilities': None} return jsonify(result_text) # 返回结果 if __name__ == "__main__": diff --git a/mistral_model_predict_vllm.py b/mistral_model_predict_vllm.py index 23e4956..478f51b 100644 --- a/mistral_model_predict_vllm.py +++ b/mistral_model_predict_vllm.py @@ -103,4 +103,4 @@ def classify(batch_size): # 调用模型,设置最大batch_size if __name__ == '__main__': t = Thread(target=classify, args=(batch_size,)) - t.start() \ No newline at end of file + t.start() diff --git a/run_api_gunicorn.sh b/run_api_gunicorn.sh index edd06d1..4060eb5 100644 --- a/run_api_gunicorn.sh +++ b/run_api_gunicorn.sh @@ -1 +1 @@ -gunicorn mistral_api:app -c gunicorn_config.py \ No newline at end of file +gunicorn mistral_api:app -c gunicorn_config.py diff --git a/run_model.sh b/run_model.sh index 209bcd4..d6388b0 100644 --- a/run_model.sh +++ b/run_model.sh @@ -1 +1 @@ -nohup python mistral_model_predict_vllm.py > myout.file 2>&1 & \ No newline at end of file +nohup python mistral_model_predict_vllm.py > myout_model_11.file 2>&1 &