|
|
|
# -*- 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=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():
|
|
|
|
id_ = flask.request.json['id'] # 获取用户query中的文本 例如"I love you"
|
|
|
|
result = redis_.get(id_) # 获取该query的模型结果
|
|
|
|
if result is not None:
|
|
|
|
redis_.delete(id_)
|
|
|
|
result_dict = result.decode('UTF-8')
|
|
|
|
result_dict = json.loads(result_dict)
|
|
|
|
texts = result_dict["texts"]
|
|
|
|
probabilities = result_dict["probabilities"]
|
|
|
|
status_code = result_dict["status_code"]
|
|
|
|
result_text = {'code': status_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}
|
|
|
|
return flask.jsonify(result_text) # 返回结果
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
app.run(debug=False, host='0.0.0.0', port=14001)
|