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.
181 lines
5.3 KiB
181 lines
5.3 KiB
from flask import Flask, jsonify
|
|
from flask import request
|
|
import time
|
|
import concurrent.futures
|
|
import requests
|
|
import socket
|
|
|
|
|
|
def get_host_ip():
|
|
"""
|
|
查询本机ip地址
|
|
:return: ip
|
|
"""
|
|
try:
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
|
s.connect(('8.8.8.8', 80))
|
|
ip = s.getsockname()[0]
|
|
finally:
|
|
s.close()
|
|
|
|
return ip
|
|
|
|
chatgpt_url_predict = "http://{}:12000/predict".format(str(get_host_ip()))
|
|
chatgpt_url_search = "http://{}:12000/search".format(str(get_host_ip()))
|
|
# prompt = "<|role|>user<|says|>{}<|end|>\n<|role|>assistant<|says|>"
|
|
prompt = "<|im_start|>user\n{}<|im_end|>\n<|im_start|>assistant\n"
|
|
|
|
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 []
|
|
|
|
|
|
def request_api_chatgpt(prompt):
|
|
'''
|
|
{
|
|
"content": "<|role|>user<|says|>任务:生成目录\n为论文题目“基于“六经”伏邪理论治疗过敏性紫癜性肾炎初探”生成中文目录,要求只有一级标题,二级标题,一级标题使用中文数字 例如一、xxx;二级标题使用阿拉伯数字 例如1.1 xxx一级标题生成5个;每个一级标题包含4-7个二级标题<|end|>\n<|role|>assistant<|says|>",
|
|
"model": "openbuddy-qwen2.5llamaify-7b_train_11_prompt_mistral_gpt_xiaobiaot_real_paper_2",
|
|
"top_p": 1,
|
|
"temperature": 0
|
|
}
|
|
:param prompt:
|
|
:return:
|
|
'''
|
|
data = {
|
|
"content": prompt,
|
|
"model": "gpt-4-turbo",
|
|
"top_p": 0.95,
|
|
"temperature": 0.7
|
|
}
|
|
response = requests.post(
|
|
chatgpt_url_predict,
|
|
json=data,
|
|
timeout=100000
|
|
)
|
|
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(response.status_code, response.text))
|
|
return {}
|
|
|
|
|
|
def uuid_search(uuid):
|
|
data = {
|
|
"id": uuid
|
|
}
|
|
response = requests.post(
|
|
chatgpt_url_search,
|
|
json=data,
|
|
timeout=100000
|
|
)
|
|
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(response.status_code, response.text))
|
|
return {}
|
|
|
|
|
|
def uuid_search_mp(results):
|
|
results_list = [""] * len(results)
|
|
while True:
|
|
tiaochu_bool = True
|
|
|
|
for i in results_list:
|
|
if i == "":
|
|
tiaochu_bool = False
|
|
break
|
|
|
|
if tiaochu_bool == True:
|
|
break
|
|
|
|
for i in range(len(results)):
|
|
uuid = results[i]["texts"]["id"]
|
|
|
|
result = uuid_search(uuid)
|
|
if result["code"] == 200:
|
|
results_list[i] = result["text"]
|
|
time.sleep(3)
|
|
return results_list
|
|
|
|
|
|
def get_multiple_urls(urls):
|
|
input_values = []
|
|
|
|
for i in urls:
|
|
input_values.append(i[1])
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
# 使用map方法并发地调用worker_function
|
|
results = list(executor.map(request_api_chatgpt, input_values))
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
# 使用map方法并发地调用worker_function
|
|
results = list(executor.map(uuid_search_mp, [results]))
|
|
|
|
return_list = []
|
|
for i in results[0]:
|
|
data = {
|
|
"code": 200,
|
|
"data": i
|
|
}
|
|
return_list.append(data)
|
|
return return_list
|
|
|
|
|
|
@app.route("/articles_directory", methods=["POST"])
|
|
def articles_directory():
|
|
text = request.json["texts"] # 获取用户query中的文本 例如"I love you"
|
|
nums = request.json["nums"]
|
|
text = prompt.format(text)
|
|
nums = int(nums)
|
|
|
|
input_data = []
|
|
for i in range(nums):
|
|
# input_data.append([i, chatgpt_url, {"texts": text_sentence[i]}])
|
|
input_data.append([i, text])
|
|
|
|
results = get_multiple_urls(input_data)
|
|
|
|
return jsonify(results) # 返回结果
|
|
|
|
if __name__ == "__main__":
|
|
app.run(debug=False, host='0.0.0.0', port=18000)
|
|
|