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.
64 lines
1.8 KiB
64 lines
1.8 KiB
from flask import Flask, jsonify
|
|
from flask import request
|
|
import concurrent.futures
|
|
import requests
|
|
import socket
|
|
|
|
|
|
app = Flask(__name__)
|
|
app.config["JSON_AS_ASCII"] = False
|
|
|
|
|
|
def dialog_line_parse(url, text):
|
|
"""
|
|
将数据输入模型进行分析并输出结果
|
|
:param url: 模型url
|
|
:param text: 进入模型的数据
|
|
:return: 模型返回结果
|
|
"""
|
|
|
|
response = requests.post(
|
|
url,
|
|
json=text,
|
|
timeout=1000
|
|
)
|
|
if response.status_code == 200:
|
|
return response.json()
|
|
else:
|
|
# logger.error(
|
|
# "【{}】 Failed to get a proper response from remote "
|
|
# "server. Status Code: {}. Response: {}"
|
|
# "".format(url, response.status_code, response.text)
|
|
# )
|
|
print("【{}】 Failed to get a proper response from remote "
|
|
"server. Status Code: {}. Response: {}"
|
|
"".format(url, response.status_code, response.text))
|
|
print(text)
|
|
return []
|
|
|
|
|
|
@app.route("/articles_directory", methods=["POST"])
|
|
def articles_directory():
|
|
text = request.json["texts"] # 获取用户query中的文本 例如"I love you"
|
|
nums = request.json["nums"]
|
|
|
|
nums = int(nums)
|
|
url = "http://192.168.31.74:12000/predict"
|
|
|
|
input_data = []
|
|
for i in range(nums):
|
|
input_data.append([url, {"texts": text}])
|
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
# 使用submit方法将任务提交给线程池,并获取Future对象
|
|
futures = [executor.submit(dialog_line_parse, i[0], i[1]) for i in input_data]
|
|
|
|
# 使用as_completed获取已完成的任务,并获取返回值
|
|
results = [future.result() for future in concurrent.futures.as_completed(futures)]
|
|
|
|
return jsonify(results) # 返回结果
|
|
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=False, host='0.0.0.0', port=18000)
|