You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
@Time : 2023/3/2 19:31
|
|
@Author :
|
|
@FileName:
|
|
@Software:
|
|
@Describe:
|
|
"""
|
|
#
|
|
# import redis
|
|
#
|
|
# redis_pool = redis.ConnectionPool(host='127.0.0.1', port=6379, password='', db=0)
|
|
# redis_conn = redis.Redis(connection_pool=redis_pool)
|
|
#
|
|
#
|
|
# name_dict = {
|
|
# 'name_4' : 'Zarten_4',
|
|
# 'name_5' : 'Zarten_5'
|
|
# }
|
|
# redis_conn.mset(name_dict)
|
|
|
|
import flask
|
|
import redis
|
|
import uuid
|
|
import json
|
|
from threading import Thread
|
|
import time
|
|
|
|
app = flask.Flask(__name__)
|
|
pool = redis.ConnectionPool(host='localhost', port=63179, max_connections=100, db=7, password="zhicheng123*")
|
|
redis_ = redis.Redis(connection_pool=pool, decode_responses=True)
|
|
|
|
db_key_query = 'query'
|
|
db_key_querying = 'querying'
|
|
db_key_error = 'error'
|
|
|
|
@app.route("/search", methods=["POST"])
|
|
def handle_query():
|
|
id_ = flask.request.json['id'] # 获取用户query中的文本 例如"I love you"
|
|
result = redis_.get(id_) # 获取该query的模型结果
|
|
if result is not None:
|
|
# redis_.delete(id_)
|
|
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("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)
|
|
|
|
return flask.jsonify(result_text) # 返回结果
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=False, host='0.0.0.0', port=14003)
|
|
|