@ -137,7 +137,6 @@ def dialog_line_parse(url, text):
print ( " 【 {} 】 Failed to get a proper response from remote "
" server. Status Code: {} . Response: {} "
" " . format ( url , response . status_code , response . text ) )
print ( text )
return { }
@ -274,17 +273,19 @@ def uuid_search_mp(results):
results_list [ i ] = result [ " text " ]
else :
results_list [ i ] = " Empty character "
print ( results_list )
time . sleep ( 3 )
return results_list
def get_multiple_urls ( urls ) :
def get_multiple_urls ( text_info ) :
input_values = [ ]
input_index = [ ]
for i in urls :
input_values . append ( i [ 1 ] )
for i in range ( len ( text_info ) ) :
if text_info [ i ] [ 3 ] == True :
input_values . append ( text_info [ i ] [ 4 ] )
input_index . append ( i )
with concurrent . futures . ThreadPoolExecutor ( 100 ) as executor :
# 使用map方法并发地调用worker_function
results_1 = list ( executor . map ( request_api_chatgpt , input_values ) )
@ -293,15 +294,24 @@ def get_multiple_urls(urls):
# 使用map方法并发地调用worker_function
results = list ( executor . map ( uuid_search_mp , [ results_1 ] ) )
return_list = [ ]
for i , j in zip ( urls , results [ 0 ] ) :
return_list . append ( [ i , j ] )
return return_list
# return_list = []
# for i,j in zip(urls, results[0]):
# return_list.append([i, j])
return_dict = { }
for i , j in zip ( input_index , results [ 0 ] ) :
return_dict [ i ] = j
for i in range ( len ( text_info ) ) :
if i in return_dict :
text_info [ i ] . append ( return_dict [ i ] )
else :
text_info [ i ] . append ( text_info [ i ] [ 0 ] )
return text_info
def chulipangban_test_1 ( snetence_id , text ) :
# 引号处理
text = text . strip ( )
dialogs_text , dialogs_index , other_index = get_dialogs_index ( text )
for dialogs_text_dan in dialogs_text :
@ -454,7 +464,7 @@ def predict_data_post_processing(text_list):
# # text_list.extend(i)
# # return_list = predict_data_post_processing(text_list)
# # return return_list
def post_sentence_ulit ( sentence , text_info ) :
def post_sentence_ulit ( text_info ) :
'''
后处理
: param sentence :
@ -467,7 +477,7 @@ def post_sentence_ulit(sentence, text_info):
if_change = text_info [ 3 ]
if if_change == True :
sentence = sentence . strip ( )
sentence = text_info [ - 1 ] . strip ( )
if " 改写后: " in sentence :
sentence_lable_index = sentence . index ( " 改写后: " )
sentence = sentence [ sentence_lable_index + 4 : ]
@ -483,7 +493,7 @@ def post_sentence_ulit(sentence, text_info):
# sentence = sentence[:-1] + text_info[0][-1]
else :
sentence = text_info [ 0 ]
return sentence
return text_info [ : 4 ] + [ sentence ]
def has_chinese ( s ) :
return bool ( re . search ( ' [ \u4e00 - \u9fa5 ] ' , s ) )
@ -491,16 +501,19 @@ def has_chinese(s):
def english_ulit ( sentence ) :
sentence = str ( sentence ) . strip ( )
if_change = Fals e
if_change = Tru e
# 判断句子长度
print ( " sen " , sentence )
if sentence [ - 1 ] != " . " :
text = f " <|im_start|>user \n Task:Rewrite a sentence \n Rewrite the following sentence fragment, ensuring that the meaning remains similar but with significant changes. The length of the rewritten sentence must be greater, not less. Additionally, the words in the short sentences must connect seamlessly with the preceding and following sentences: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
if " " != " " :
if sentence [ - 1 ] != " . " :
text = f " <|im_start|>user \n Rewrite the following sentence fragment, ensuring that the meaning remains similar but with significant changes. The length of the rewritten sentence must be greater, not less. Additionally, the words in the short sentences must connect seamlessly with the preceding and following sentences: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
else :
text = f " <|im_start|>user \n Rewrite the following sentence, requiring the meaning to be similar but the change to be larger, and the number of words can only be more but not less: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
else :
text = f " <|im_start|>user \n Task:Rewrite a sentence \n Rewrite the following sentence, requiring the meaning to be similar but the change to be larger, and the number of words can only be more but not less: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
# text = f"<|im_start|>user\n任务:改写句子\n改写下面这句话,要求不改变原句语义,短句之间衔接不能有误:\n{sentence}\n<|im_end|>\n<|im_start|>assistant\n"
if_change = False
text = f " <|im_start|>user \n Hello \n <|im_end|> \n <|im_start|>assistant \n "
return text , if_change
@ -517,7 +530,6 @@ def chinese_ulit(sentence):
text = f " <|im_start|>user \n 任务:改写句子 \n 改写下面半这句话,要求意思接近但是改动幅度比较大,字数只能多不能少,短句前后词跟上下句衔接不能有错误: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
else :
text = f " <|im_start|>user \n 任务:改写句子 \n 改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
# text = f"<|im_start|>user\n任务:改写句子\n改写下面这句话,要求不改变原句语义,短句之间衔接不能有误:\n{sentence}\n<|im_end|>\n<|im_start|>assistant\n"
else :
text = f " <|im_start|>user \n 下面句子不做任何变化: \n { sentence } \n <|im_end|> \n <|im_start|>assistant \n "
@ -573,9 +585,7 @@ def main(texts: dict):
for i in text_list :
print ( " sen " , i [ 0 ] )
text , if_change = pre_sentence_ulit ( i [ 0 ] )
text_sentence . append ( text )
text_info . append ( [ i [ 0 ] , i [ 1 ] , i [ 2 ] , if_change ] )
text_info . append ( [ i [ 0 ] , i [ 1 ] , i [ 2 ] , if_change , text ] )
# outputs = llm.generate(text_sentence, sampling_params) # 调用模型
#
@ -623,30 +633,14 @@ def main(texts: dict):
# [4, 'http://114.116.25.228:12000/predict', {'texts': '任务:改写句子\n改写下面这句话,要求意思接近但是改动幅度比较大,字数只能多不能少:\n一是新时代“枫桥经验”对'}]
# ]
input_data = [ ]
for i in range ( len ( text_sentence ) ) :
# input_data.append([i, chatgpt_url, {"texts": text_sentence[i]}])
input_data . append ( [ i , text_sentence [ i ] ] )
text_info = get_multiple_urls ( text_info )
results = get_multiple_urls ( input_data )
generated_text_list = [ " " ] * len ( input_data )
for url , result in results :
# print(f"Result for {url}: {result}")
if result != " " :
generated_text_list [ url [ 0 ] ] = result
else :
generated_text_list [ url [ 0 ] ] = " Empty character "
for i in range ( len ( generated_text_list ) ) :
# if len(text_list[i][0]) > 7:
# generated_text_list[i] = post_sentence_ulit(generated_text_list[i])
# else:
# generated_text_list[i] = text_list[i][0]
generated_text_list [ i ] = post_sentence_ulit ( generated_text_list [ i ] , text_info [ i ] )
for i in range ( len ( text_info ) ) :
text_info [ i ] = post_sentence_ulit ( text_info [ i ] )
for i , j in zip ( generated_text_list , text_info ) :
text_list_new . append ( [ i ] + j [ 1 : 3 ] )
for i in range ( len ( text_info ) ) :
text_list_new . append ( [ text_info [ i ] [ - 1 ] ] + text_info [ i ] [ 1 : 3 ] )
return_list = predict_data_post_processing ( text_list_new )
return return_list
@ -744,7 +738,7 @@ def handle_query():
return jsonify ( return_text )
if isinstance ( texts , dict ) :
id_ = str ( uuid . uuid1 ( ) ) # 为query生成唯一标识
print ( " uuid: " , uu id)
print ( " uuid: " , id_ )
d = { ' id ' : id_ , ' text ' : texts , " text_type " : text_type } # 绑定文本和query id
load_request_path = ' ./request_data_logs/ {} .json ' . format ( id_ )