|
|
@ -1,5 +1,4 @@ |
|
|
|
import os |
|
|
|
os.environ["CUDA_VISIBLE_DEVICES"] = "2" |
|
|
|
from flask import Flask, jsonify |
|
|
|
from flask import request |
|
|
|
import requests |
|
|
@ -10,7 +9,7 @@ from threading import Thread |
|
|
|
import time |
|
|
|
import re |
|
|
|
import logging |
|
|
|
from vllm import LLM, SamplingParams |
|
|
|
import concurrent.futures |
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(level=logging.DEBUG, # 控制台打印的日志级别 |
|
|
@ -42,7 +41,7 @@ pantten_biaoti_0 = '^[1-9一二三四五六七八九ⅠⅡⅢⅣⅤⅥⅦⅧⅨ] |
|
|
|
pantten_biaoti_1 = '^第[一二三四五六七八九]章\s{0,}?[\u4e00-\u9fa5a-zA-Z]+' |
|
|
|
pantten_biaoti_2 = '^[0-9.]+\s{0,}?[\u4e00-\u9fa5a-zA-Z]+' |
|
|
|
pantten_biaoti_3 = '^[((][1-9一二三四五六七八九ⅠⅡⅢⅣⅤⅥⅦⅧⅨ][)_)][、.]{0,}?\s{0,}?[\u4e00-\u9fa5a-zA-Z]+' |
|
|
|
chatgpt_url = "http://114.116.25.228:12000/predict" |
|
|
|
chatgpt_url = "http://192.168.31.145:12001/predict" |
|
|
|
|
|
|
|
|
|
|
|
class log: |
|
|
@ -141,6 +140,20 @@ def chulichangju_1(text, snetence_id, chulipangban_return_list, short_num): |
|
|
|
return chulipangban_return_list |
|
|
|
|
|
|
|
|
|
|
|
def get_multiple_urls(urls): |
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor: |
|
|
|
future_to_url = {executor.submit(dialog_line_parse, url[1], url[2]): url for url in urls} |
|
|
|
results = [] |
|
|
|
for future in concurrent.futures.as_completed(future_to_url): |
|
|
|
url = future_to_url[future] |
|
|
|
try: |
|
|
|
data = future.result() |
|
|
|
results.append((url, data)) |
|
|
|
except Exception as e: |
|
|
|
results.append((url, f"Error: {str(e)}")) |
|
|
|
return results |
|
|
|
|
|
|
|
|
|
|
|
def chulipangban_test_1(snetence_id, text): |
|
|
|
# 引号处理 |
|
|
|
|
|
|
@ -237,6 +250,13 @@ def batch_predict(batch_data_list): |
|
|
|
return batch_data_list_new |
|
|
|
|
|
|
|
|
|
|
|
def is_chinese(char): |
|
|
|
if '\u4e00' <= char <= '\u9fff': |
|
|
|
return True |
|
|
|
else: |
|
|
|
return False |
|
|
|
|
|
|
|
|
|
|
|
def predict_data_post_processing(text_list): |
|
|
|
text_list_sentence = [] |
|
|
|
# text_list_sentence.append([text_list[0][0], text_list[0][1]]) |
|
|
@ -290,6 +310,8 @@ def post_sentence_ulit(sentence, text_info): |
|
|
|
sentence = sentence[sentence_lable_index + 4:] |
|
|
|
if sentence[-1] == "\n": |
|
|
|
sentence = sentence[:-1] |
|
|
|
|
|
|
|
|
|
|
|
else: |
|
|
|
sentence = text_info[0] |
|
|
|
return sentence |
|
|
@ -304,9 +326,14 @@ def pre_sentence_ulit(sentence): |
|
|
|
sentence = str(sentence).strip() |
|
|
|
if_change = True |
|
|
|
if len(sentence) > 9: |
|
|
|
text = "You are a helpful assistant.\n\nUser:改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n{}\nAssistant:".format(sentence) |
|
|
|
|
|
|
|
if sentence[-1] != "。": |
|
|
|
text = f"User:改写下面这段文字,要求意思接近但是改动幅度比较大,字数只能多不能少,短句前后词跟上下句衔接不能有错误,并且如果结尾有标点符号,标点不能改变,如果结尾没有标点符号,不能擅自添加标点符号:\n{sentence}\nAssistant:" |
|
|
|
else: |
|
|
|
text = "You are a helpful assistant.\n\nUser:下面词不做任何变化:\n{}\nAssistant:".format(sentence) |
|
|
|
text = f"User:改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n{sentence}\nAssistant:" |
|
|
|
|
|
|
|
else: |
|
|
|
text = f"下面词不做任何变化:\n{sentence}" |
|
|
|
if_change = False |
|
|
|
return text, if_change |
|
|
|
|
|
|
@ -361,20 +388,42 @@ def main(texts: dict): |
|
|
|
|
|
|
|
## ==================================================================== |
|
|
|
|
|
|
|
# input_data = [] |
|
|
|
# for i in range(len(text_sentence)): |
|
|
|
# input_data.append([chatgpt_url, {"texts": text_sentence[i]}]) |
|
|
|
# |
|
|
|
# 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)] |
|
|
|
# |
|
|
|
# generated_text_list = [] |
|
|
|
# for dan in results: |
|
|
|
# generated_text_list.append(dan["data"]) |
|
|
|
|
|
|
|
## ============================================================================================== |
|
|
|
|
|
|
|
# urls = [ |
|
|
|
# [0, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n中华苏维埃共和国时期的法律制度建设是中国共产党'}], |
|
|
|
# [1, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n革命道路的实践中,无数革命先辈用鲜血和生命铸就了以坚定信念、求真务实、一心为民、清正廉洁、艰苦奋斗、争创一流、无私奉献等为主要内涵的苏区精神,孕育了'}], |
|
|
|
# [2, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n年前的浙江枫桥干部群众创造了“依靠群众就地化解矛盾”的“枫桥经验”,并根据形势变化不断赋予新的内涵,已成为基层治理的'}], |
|
|
|
# [3, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n新时代,习近平总书记反复强调要坚持和发展新时代“枫桥经验”,加强和创新基层社会治理,切实把矛盾化解在基层,维护好社会稳定。'}], |
|
|
|
# [4, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n一是新时代“枫桥经验”对'}] |
|
|
|
# ] |
|
|
|
|
|
|
|
input_data = [] |
|
|
|
for i in range(len(text_sentence)): |
|
|
|
input_data.append([url, {"texts": text_sentence[i]}]) |
|
|
|
input_data.append([i, chatgpt_url, {"texts": text_sentence[i]}]) |
|
|
|
|
|
|
|
with concurrent.futures.ThreadPoolExecutor() as executor: |
|
|
|
# 使用submit方法将任务提交给线程池,并获取Future对象 |
|
|
|
futures = [executor.submit(dialog_line_parse, i[0], i[1]) for i in input_data] |
|
|
|
results = get_multiple_urls(input_data) |
|
|
|
|
|
|
|
# 使用as_completed获取已完成的任务,并获取返回值 |
|
|
|
results = [future.result() for future in concurrent.futures.as_completed(futures)] |
|
|
|
generated_text_list = [""] * len(input_data) |
|
|
|
for url, result in results: |
|
|
|
# print(f"Result for {url}: {result}") |
|
|
|
generated_text_list[url[0]] = result["data"] |
|
|
|
|
|
|
|
generated_text_list = [] |
|
|
|
for dan in results: |
|
|
|
generated_text_list.append(dan["data"]) |
|
|
|
|
|
|
|
for i in range(len(generated_text_list)): |
|
|
|
# if len(text_list[i][0]) > 7: |
|
|
@ -511,4 +560,4 @@ if __name__ == "__main__": |
|
|
|
'%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s' |
|
|
|
# 日志格式 |
|
|
|
) |
|
|
|
app.run(host="0.0.0.0", port=14002, threaded=True, debug=False) |
|
|
|
app.run(host="0.0.0.0", port=14008, threaded=True, debug=False) |
|
|
|