Browse Source

增加文本为空判断

master
majiahui@haimaqingfan.com 11 months ago
parent
commit
c5585be230
  1. 19
      mistral_api.py

19
mistral_api.py

@ -59,7 +59,10 @@ def predict():
id_ = str(uuid.uuid1()) # 为query生成唯一标识 id_ = str(uuid.uuid1()) # 为query生成唯一标识
print("uuid: ", uuid) print("uuid: ", uuid)
d = {'id': id_, 'text': text} # 绑定文本和query id d = {'id': id_, 'text': text} # 绑定文本和query id
# try: try:
if text == None:
return_text = {"texts": "请求文本不能为空", "probabilities": None, "status_code": 401}
else:
load_request_path = './request_data_logs/{}.json'.format(id_) load_request_path = './request_data_logs/{}.json'.format(id_)
with open(load_request_path, 'w', encoding='utf8') as f2: with open(load_request_path, 'w', encoding='utf8') as f2:
# ensure_ascii=False才能输入中文,否则是Unicode字符 # ensure_ascii=False才能输入中文,否则是Unicode字符
@ -69,9 +72,9 @@ def predict():
redis_.sadd(db_key_querying, id_) redis_.sadd(db_key_querying, id_)
redis_.sadd(db_key_queryset, id_) redis_.sadd(db_key_queryset, id_)
return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 200} return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 200}
# except: except:
# return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 400} return_text = {"texts": {'id': id_, }, "probabilities": None, "status_code": 400}
# smtp_f("vllm-main") smtp_f("vllm-main")
return jsonify(return_text) # 返回结果 return jsonify(return_text) # 返回结果
@ -79,7 +82,7 @@ def predict():
def search(): def search():
id_ = request.json['id'] # 获取用户query中的文本 例如"I love you" id_ = request.json['id'] # 获取用户query中的文本 例如"I love you"
result = redis_.get(id_) # 获取该query的模型结果 result = redis_.get(id_) # 获取该query的模型结果
# try: try:
if result is not None: if result is not None:
result_path = result.decode('UTF-8') result_path = result.decode('UTF-8')
with open(result_path, encoding='utf8') as f1: with open(result_path, encoding='utf8') as f1:
@ -124,9 +127,9 @@ def search():
# ensure_ascii=False才能输入中文,否则是Unicode字符 # ensure_ascii=False才能输入中文,否则是Unicode字符
# indent=2 JSON数据的缩进,美观 # indent=2 JSON数据的缩进,美观
json.dump(result_text, f2, ensure_ascii=False, indent=4) json.dump(result_text, f2, ensure_ascii=False, indent=4)
# except: except:
# smtp_f("vllm-main") smtp_f("vllm-main")
# result_text = {'code': "400", 'text': "", 'probabilities': None} result_text = {'code': "400", 'text': "", 'probabilities': None}
return jsonify(result_text) # 返回结果 return jsonify(result_text) # 返回结果
if __name__ == "__main__": if __name__ == "__main__":

Loading…
Cancel
Save