From ea7a513ae780f5448ee3444df56f0857a852f6d5 Mon Sep 17 00:00:00 2001 From: "majiahui@haimaqingfan.com" Date: Wed, 8 Mar 2023 11:23:57 +0800 Subject: [PATCH] =?UTF-8?q?V1.0=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- flask_predict_no_batch_t5.py | 27 +++++++++++++++++---------- redis_check_uuid.py | 30 +++++++++++++++++++++++++++++- run_app_nohub_search_redis.sh | 1 + run_app_nohub_t5.sh | 1 + 4 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 run_app_nohub_search_redis.sh create mode 100644 run_app_nohub_t5.sh diff --git a/flask_predict_no_batch_t5.py b/flask_predict_no_batch_t5.py index a2a44fb..def0b44 100644 --- a/flask_predict_no_batch_t5.py +++ b/flask_predict_no_batch_t5.py @@ -20,7 +20,7 @@ pool = redis.ConnectionPool(host='localhost', port=6379, max_connections=50, db= redis_ = redis.Redis(connection_pool=pool, decode_responses=True) db_key_query = 'query' -db_key_result = 'result' +db_key_querying = 'querying' batch_size = 32 app = Flask(__name__) @@ -58,7 +58,6 @@ def get_dialogs_index(line: str): def chulichangju_1(text, snetence_id, chulipangban_return_list, short_num): fuhao = [",","?","!","…"] dialogs_text, dialogs_index, other_index = get_dialogs_index(text) - print(len(text)) text_1 = text[:120] text_2 = text[120:] text_1_new = "" @@ -263,7 +262,6 @@ def sentence(): texts_list = main(texts) if text_type == 'chapter': texts_list = main(texts) - return_text = {"texts": texts_list, "probabilities": None, "status_code": True} else: return_text = {"texts":"输入格式应该为list", "probabilities": None, "status_code":False} @@ -288,11 +286,12 @@ def classify(): # 调用模型,设置最大batch_size texts_list = main(texts) if text_type == 'chapter': texts_list = main(texts) - return_text = {"texts": texts_list, "probabilities": None, "status_code": 200} else: return_text = {"texts": "输入格式应该为字典", "probabilities": None, "status_code": 401} - redis_.set(query_id, json.dumps(return_text, ensure_ascii=False)) + + redis_.srem(db_key_querying, query_id) + redis_.set(query_id, json.dumps(return_text, ensure_ascii=False), 28800) @app.route("/predict", methods=["POST"]) @@ -300,11 +299,19 @@ def handle_query(): print(request.remote_addr) texts = request.json["texts"] text_type = request.json["text_type"] - id_ = str(uuid.uuid1()) # 为query生成唯一标识 - d = {'id': id_, 'text': texts, "text_type": text_type} # 绑定文本和query id - redis_.rpush(db_key_query, json.dumps(d, ensure_ascii=False)) # 加入redis - result_text = d - return jsonify(result_text) # 返回结果 + if texts is None: + return_text = {"texts": "输入了空值", "probabilities": None, "status_code": False} + return jsonify(return_text) + if isinstance(texts, dict): + id_ = str(uuid.uuid1()) # 为query生成唯一标识 + d = {'id': id_, 'text': texts, "text_type": text_type} # 绑定文本和query id + redis_.rpush(db_key_query, json.dumps(d, ensure_ascii=False)) # 加入redis + redis_.sadd(db_key_querying, id_) + return_text = d + print("ok") + else: + return_text = {"texts": "输入格式应该为字典", "probabilities": None, "status_code": 401} + return jsonify(return_text) # 返回结果 t = Thread(target=classify) diff --git a/redis_check_uuid.py b/redis_check_uuid.py index 0d31c8d..fb0538e 100644 --- a/redis_check_uuid.py +++ b/redis_check_uuid.py @@ -31,6 +31,8 @@ app = flask.Flask(__name__) pool = redis.ConnectionPool(host='localhost', port=6379, max_connections=50, db=1) redis_ = redis.Redis(connection_pool=pool, decode_responses=True) +db_key_query = 'query' +db_key_querying = 'querying' @app.route("/search", methods=["POST"]) def handle_query(): @@ -45,7 +47,33 @@ def handle_query(): status_code = result_dict["status_code"] result_text = {'code': status_code, 'text': texts, 'probabilities': probabilities} else: - result_text = {'code': "201", 'text': "", 'probabilities': None} + querying_list = list(redis_.smembers("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} return flask.jsonify(result_text) # 返回结果 diff --git a/run_app_nohub_search_redis.sh b/run_app_nohub_search_redis.sh new file mode 100644 index 0000000..349d74c --- /dev/null +++ b/run_app_nohub_search_redis.sh @@ -0,0 +1 @@ +nohup python redis_check_uuid.py > myout.redis_check_uuid.logs 2>&1 & \ No newline at end of file diff --git a/run_app_nohub_t5.sh b/run_app_nohub_t5.sh new file mode 100644 index 0000000..c07ca29 --- /dev/null +++ b/run_app_nohub_t5.sh @@ -0,0 +1 @@ +nohup python flask_predict_no_batch_t5.py > myout.flask_predict_no_batch_t5.logs 2>&1 & \ No newline at end of file