fearlessbj4 commited on
Commit
7ee57c7
1 Parent(s): 43e4166

Upload g_h.py

Browse files
Files changed (1) hide show
  1. g_h.py +856 -0
g_h.py ADDED
@@ -0,0 +1,856 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ inset_th=1
3
+ #_config=json.load(open("config.json","r"))
4
+ _config={
5
+ "sug_based_list":["dispute","plaintiff"],
6
+ "sug_pool_list":["corpus3835","2022~2023"],
7
+ "embedder_list":["ftlf","ftrob"],
8
+ "based_index":0,
9
+ "pool_index":1,
10
+ "emb_index":1,
11
+ "sug_th":20,
12
+ "cluster_epsilon":0.67,
13
+ "similiar_trace_back_th":0.98,
14
+ "back_ground_RGB":[77, 6, 39]
15
+ }
16
+ emb_dim_lst=[768,1024]
17
+ bilstm_len_lst=[19,13]
18
+ cnn_len_lst=[32,18]
19
+
20
+ emb_dim=emb_dim_lst[_config["emb_index"]]
21
+ bilstm_len=bilstm_len_lst[_config["based_index"]]
22
+ cnn_len=cnn_len_lst[_config["based_index"]]
23
+
24
+
25
+ sug_type=_config["sug_based_list"][_config["based_index"]]
26
+ pool_type=_config["sug_pool_list"][_config["pool_index"]]
27
+ emb_type=_config["embedder_list"][_config["emb_index"]]
28
+
29
+ sug_th=_config["sug_th"]
30
+
31
+ clust_th=_config["cluster_epsilon"]
32
+ _th=_config["similiar_trace_back_th"]
33
+
34
+ bg_rgb=(_config["back_ground_RGB"][0],_config["back_ground_RGB"][1],_config["back_ground_RGB"][2])
35
+
36
+
37
+
38
+ import os,sys
39
+
40
+
41
+ #_gpu=(1==1)
42
+ #if not _gpu:
43
+ # os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
44
+ import cv2#opencv-python 4.6.0.66
45
+ import colorama
46
+ from colorama import Fore,Style,Back
47
+ import json
48
+ import numpy as np
49
+ from numpy.linalg import norm
50
+ from sentence_transformers import SentenceTransformer
51
+ from tqdm import tqdm
52
+ import tensorflow as tf
53
+ from tensorflow.keras.models import load_model
54
+ #---------------------------------------
55
+ def logistic(x_r,y_r,x_e,_proba=True):
56
+ from sklearn import linear_model
57
+ from sklearn.inspection import permutation_importance
58
+ model=linear_model.LogisticRegression(max_iter=100000)
59
+ model.fit(x_r,y_r)
60
+
61
+ p_e=model.predict(x_e)
62
+ prob_e=model.predict_proba(x_e)
63
+ prob_sum=[i[1] for i in prob_e]
64
+ return (prob_sum if _proba else p_e)
65
+
66
+ def cos_sim(a,b):
67
+ return np.dot(a,b)/(norm(a)*norm(b))
68
+ def replace_all(t,rp_lst,k,_type=0):
69
+ temp=t
70
+ for _e in rp_lst:
71
+
72
+ if _type==-1:
73
+ temp=temp.replace(_e,k+_e)
74
+ elif _type==1:
75
+ temp=temp.replace(_e,_e+k)
76
+ else:
77
+ temp=temp.replace(_e,k)
78
+ return temp
79
+ def jl(file_path):
80
+ with open(file_path, "r", encoding="utf8") as json_file:
81
+ json_list = list(json_file)
82
+ return [json.loads(json_str) for json_str in json_list]
83
+ def lst_2_dict(lst):
84
+ _dict={i["filename"]:[i["p_point"],i["d_point"],i["Controversy"]] for i in lst}
85
+ return _dict
86
+ def clust_2_dict(clust):
87
+ _dict={}
88
+
89
+ ct=0
90
+ for i in clust:
91
+
92
+ if len(clust[i])==1:
93
+ _dict[clust[i][0]]=-1
94
+ else:
95
+ ct+=1
96
+ for _e in clust[i]:
97
+
98
+ _dict[_e]=ct
99
+ return _dict
100
+ def clust_label(clust):
101
+ _dict={}
102
+ for i in clust:
103
+ for _e in clust[i]:
104
+ if len(clust[i])>1:
105
+ _dict[_e]=i
106
+ else:
107
+ _dict[_e]='-1'
108
+ return _dict
109
+ #-----------------------------
110
+ def clust_core(clust,vec_lst,id_lst,_type="mean"):
111
+ _dict={}
112
+ for i in clust:
113
+ if _type=="head":
114
+ _dict[i]=vec_lst[id_lst.index(clust[i][0])]
115
+ elif _type=="central":
116
+ tp_lst=np.array([vec_lst[id_lst.index(_e)] for _e in clust[i]])
117
+ temp=np.average(tp_lst, axis=0)
118
+ cs_lst=[[cos_sim(_e,temp),list(_e)] for _e in tp_lst]
119
+ _dict[i]=max(cs_lst)[-1]
120
+ else:#_type=="mean"
121
+ tp_lst=np.array([vec_lst[id_lst.index(_e)] for _e in clust[i]])
122
+ _dict[i]=np.average(tp_lst, axis=0)
123
+ return _dict
124
+
125
+ def clust_search(core_dict,target,clust_th=0.65):
126
+ temp=max([[cos_sim(target,core_dict[i]),i] for i in core_dict])
127
+ ot_,label_=temp
128
+
129
+ return label_ if ot_>=clust_th else '-1'
130
+
131
+ def vec2img(vec_lst1,clust_lst1,vec_lst2,clust_lst2,r):
132
+ tp_lst1=[[vec_lst1[i],clust_lst1[i]] for i in range(len(clust_lst1))]
133
+ tp_lst2=[[vec_lst2[i],clust_lst2[i]] for i in range(len(clust_lst2))]
134
+
135
+ lst1=sorted(tp_lst1,key=lambda x:x[1])
136
+ lst2=sorted(tp_lst2,key=lambda x:x[1])
137
+
138
+ m_lst=lst1+lst2
139
+ _img=[[255 for _ee in range(len(m_lst))] for _e in range(len(m_lst))]
140
+ for i in range(len(m_lst)):
141
+ for j in range(len(m_lst)):
142
+ if i<j:
143
+ temp=cos_sim(m_lst[i][0],m_lst[j][0])
144
+ _tp=(temp-r)/(1-r)*128+127 if temp>r else temp/r*128
145
+
146
+ _tp=int(_tp-1)
147
+ _img[i][j]=_tp
148
+ _img[j][i]=_tp
149
+ return _img
150
+ def img_resize(_img,_max_size):
151
+ return cv2.resize(np.array(_img).astype('float32'), (_max_size, _max_size), interpolation=cv2.INTER_AREA).tolist()
152
+ def cnn_load(_device="/gpu:0"):
153
+ global cnn_model
154
+ with tf.device(_device):
155
+ cnn_model=load_model("./models/"+sug_type+"_"+emb_type+"_cnn.dat")
156
+ cnn_model.load_weights("./models/"+sug_type+"_"+emb_type+"_cnn_best.hdf5")
157
+ def bilstm_load(_device="/gpu:0"):
158
+ global bilstm_model
159
+ with tf.device(_device):
160
+ bilstm_model=load_model("./models/"+sug_type+"_"+emb_type+"_sa.dat")
161
+ bilstm_model.load_weights("./models/"+sug_type+"_"+emb_type+"_sa_best.hdf5")
162
+ #---------------------------------------
163
+ _tranpose=(1==1)
164
+ from colorama import Fore,Style,Back
165
+ from pretty_html_table import build_table
166
+ import pandas as pd
167
+
168
+
169
+ def html_hl(lst):
170
+ #font_path = "./font/TaipeiSansTCBeta-Regular.ttf"
171
+ #font = ImageFont.truetype(font_path, font_size)
172
+
173
+ tp_lst=[]
174
+
175
+ for i in lst:
176
+ temp="<mark style=\"background:"+i["background_color"]+";color:"+i["font_color"]+"\">"+i["content"]+"</mark>"
177
+ tp_lst.append(temp)
178
+
179
+ return "".join(tp_lst)
180
+ def ansi_to_html_dis(_f,file_path,_tranpose=True):
181
+
182
+ if _tranpose:
183
+ _dict={"item":["plaintiff","defendant","dispute","score"],_f["target"]+"(target)":["plaintiff_anchor2","defendant_anchor2","dispute_anchor2",""],_f["case_id"]:["plaintiff_anchor1","defendant_anchor1","dispute_anchor1","score_anchor"]}
184
+ else:
185
+ _dict={"case_name":[_f["case_id"],_f["target"]+"(target)"],"plaintiff":["plaintiff_anchor1","plaintiff_anchor2"],"defendant":["defendant_anchor1","defendant_anchor2"],"dispute":["dispute_anchor1","dispute_anchor2"],"score":["","score_anchor"]}
186
+
187
+
188
+ p1=html_hl(_f["plaintiff_case1"])
189
+ p2=html_hl(_f["plaintiff_case2"])
190
+ d1=html_hl(_f["defendant_case1"])
191
+ d2=html_hl(_f["defendant_case2"])
192
+ dis1=html_hl(_f["dispute_case1"])
193
+ dis2=html_hl(_f["dispute_case2"])
194
+ score_="\n<mark style=\"background:#ffffff;color:"+("green" if _f["ensemble_pred"]>=0.75 else "yellow" if _f["ensemble_pred"]>=0.5 else "red")+"\">"+str(_f["ensemble_pred"])+"</mark>"
195
+ #score_="<mark style=\"color:>"++"\">"+str(_f["ensemble_pred"])+"</mark>"
196
+
197
+ df=pd.DataFrame(_dict)
198
+ html_table_blue_light = build_table(df, 'blue_light')
199
+ #print(type(html_table_blue_light))
200
+ injection="<meta charset=\"UTF-8\">"
201
+ #"<td style = \"background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto\">"
202
+ html_table_blue_light=html_table_blue_light[:html_table_blue_light.find("<thead>")+7]+injection+html_table_blue_light[html_table_blue_light.find("<thead>")+7:]
203
+ html_table_blue_light=html_table_blue_light.replace("plaintiff_anchor1",p1).replace("plaintiff_anchor2",p2)\
204
+ .replace("defendant_anchor1",d1).replace("defendant_anchor2",d2)\
205
+ .replace("dispute_anchor1",dis1).replace("dispute_anchor2",dis2)\
206
+ .replace("score_anchor",score_)
207
+
208
+ with open(file_path, 'w',) as f:
209
+ f.write(html_table_blue_light)
210
+ return html_table_blue_light
211
+ def ansi_to_html(_f,file_path,_tranpose=True):
212
+
213
+ if _tranpose:
214
+ _dict={"item":["plaintiff","p_point","score"],_f["target"]+"(target)":["plaintiff_anchor2","p_point_anchor2",""],_f["case_id"]:["plaintiff_anchor1","p_point_anchor1","score_anchor"]}
215
+ else:
216
+ _dict={"case_name":[_f["case_id"],_f["target"]+"(target)"],"plaintiff":["plaintiff_anchor1","plaintiff_anchor2"],"p_point":["p_point_anchor1","p_point_anchor2"],"score":["","score_anchor"]}
217
+
218
+
219
+ p1=html_hl(_f["plaintiff_case1"])
220
+ p2=html_hl(_f["plaintiff_case2"])
221
+
222
+ p_point1=html_hl(_f["p_point_case1"])
223
+ p_point2=html_hl(_f["p_point_case2"])
224
+ score_="\n<mark style=\"background:#ffffff;color:"+("green" if _f["ensemble_pred"]>=0.75 else "yellow" if _f["ensemble_pred"]>=0.5 else "red")+"\">"+str(_f["ensemble_pred"])+"</mark>"
225
+ #score_="<mark style=\"color:>"++"\">"+str(_f["ensemble_pred"])+"</mark>"
226
+
227
+ df=pd.DataFrame(_dict)
228
+ html_table_blue_light = build_table(df, 'blue_light')
229
+ #print(type(html_table_blue_light))
230
+ injection="<meta charset=\"UTF-8\">"
231
+ #"<td style = \"background-color: #D9E1F2;font-family: Century Gothic, sans-serif;font-size: medium;text-align: left;padding: 0px 20px 0px 0px;width: auto\">"
232
+ html_table_blue_light=html_table_blue_light[:html_table_blue_light.find("<thead>")+7]+injection+html_table_blue_light[html_table_blue_light.find("<thead>")+7:]
233
+ html_table_blue_light=html_table_blue_light.replace("plaintiff_anchor1",p1).replace("plaintiff_anchor2",p2)\
234
+ .replace("p_point_anchor1",p_point1).replace("p_point_anchor2",p_point2)\
235
+ .replace("score_anchor",score_)
236
+
237
+ with open(file_path, 'w',) as f:
238
+ f.write(html_table_blue_light)
239
+ return html_table_blue_light
240
+ #---------------------------------------
241
+ from PIL import Image, ImageDraw, ImageFont
242
+
243
+
244
+
245
+ # Dictionary mapping colorama codes to RGB colors
246
+ ANSI_BG_COLORS = {
247
+ Fore.BLACK: (0, 0, 0),
248
+ Fore.RED: (255, 0, 0),
249
+ Fore.GREEN: (0, 255, 0),
250
+ Fore.YELLOW: (255, 255, 0),
251
+ Fore.BLUE: (0, 0, 255),
252
+ Fore.MAGENTA: (255, 0, 255),
253
+ Fore.CYAN: (0, 255, 255),
254
+ Fore.WHITE: (255, 255, 255),
255
+ Fore.RESET: (0, 0, 0), # Reset to black
256
+ Back.BLACK: (0, 0, 0),
257
+ Back.RED: (255, 0, 0),
258
+ Back.GREEN: (0, 255, 0),
259
+ Back.YELLOW: (255, 255, 0),
260
+ Back.BLUE: (0, 0, 255),
261
+ Back.MAGENTA: (255, 0, 255),
262
+ Back.CYAN: (0, 255, 255),
263
+ Back.WHITE: (255, 255, 255),
264
+ '\033[0m': bg_rgb # Reset to White background
265
+ }
266
+
267
+ ANSI_COLORS={_e:"#"+str(hex(1*256*256*256+ANSI_BG_COLORS[_e][0]*256*256+ANSI_BG_COLORS[_e][1]*256+ANSI_BG_COLORS[_e][2]))[3:] for _e in ANSI_BG_COLORS}
268
+ def ansi_to_image(ansi_text, font_size=20, image_path="./test.png"):
269
+ global bg_rgb
270
+ font_path = "./font/TaipeiSansTCBeta-Regular.ttf"
271
+ font = ImageFont.truetype(font_path, font_size)
272
+
273
+
274
+ # Split the text into lines
275
+ lines = ansi_text.split('\n')
276
+
277
+ # Calculate image size
278
+ max_width = 0
279
+ total_height = 0
280
+ line_heights = []
281
+ for line in lines:
282
+ text_width, text_height = font.getsize(line)
283
+ max_width = max(max_width, text_width)
284
+ total_height += text_height
285
+ line_heights.append(text_height)
286
+
287
+ # Create a blank image
288
+ image = Image.new('RGB', (max_width, total_height), color=bg_rgb)
289
+ draw = ImageDraw.Draw(image)
290
+
291
+ y = 0
292
+ for line, line_height in zip(lines, line_heights):
293
+ x = 0
294
+ segments = line.split('\033')
295
+ anchor_bg_color=(255,255,255)
296
+ for segment in segments:
297
+ #print(segment)
298
+ if segment and segment[-1]=='m':
299
+ code= segment[:-1]
300
+ anchor_bg_color = ANSI_BG_COLORS.get(f'\033{code}m', anchor_bg_color)
301
+ #text_width, text_height = draw.textsize(text, font=font)
302
+ #draw.rectangle([x, y, x + text_width, y + line_height], fill=(255, 255, 255))
303
+ #draw.text((x, y), text, font=font, fill=anchor_bg_color)
304
+ x += 0
305
+ if 'm' in segment:
306
+ code, text = segment.split('m', 1)
307
+ font_color = ANSI_BG_COLORS.get(f'\033{code}m', anchor_bg_color)
308
+ text_width, text_height = draw.textsize(text, font=font)
309
+ draw.rectangle([x, y, x + text_width, y + line_height], anchor_bg_color)
310
+ draw.text((x, y), text, font=font, fill=font_color)
311
+ x += text_width
312
+ else:
313
+
314
+ text = segment
315
+ text_width, text_height = draw.textsize(text, font=font)
316
+ draw.text((x, y), text, font=font, fill=(255,255,255))
317
+ x += text_width
318
+ y += line_height
319
+
320
+ # Save the image
321
+ image.save(image_path)
322
+ return image_path
323
+
324
+ # 示例ANSI文本
325
+ #ansi_content = '\033[44m555\033[0m\n111\033[41m555\033[0m'
326
+
327
+ # 將ANSI轉換為圖像
328
+ #image_path = ansi_to_image(ansi_content)
329
+ #
330
+ #---------------------------------------
331
+ def suggesting_dis(the_pool,target_name,case_dict):
332
+ global ANSI_COLORS,_th,c_th,sug_th,corpus_dict,corpus_pd_f,vec_lst,id_lst,sen_lst,corpus_clust_label,_cluster_core_dict,_embedder
333
+ global bilstm_len,cnn_len,emb_dim,inset_th,clust_th
334
+ lst_2=[_e for _e in case_dict["dispute"]][:bilstm_len]
335
+
336
+ #for _e in lst2:
337
+ # temp=_embedder.encode(_e)
338
+ # vec_lst_2.append()
339
+ vec_lst_2=[_embedder.encode(_e) for _e in lst_2]
340
+
341
+ clst_2=[clust_search(_cluster_core_dict,_e,clust_th) for _e in vec_lst_2]
342
+ plst_2=replace_all("".join(case_dict["plaintiff"]),key_lst,sp_key,1).split(sp_key)
343
+ dlst_2=replace_all("".join(case_dict["defendant"]),key_lst,sp_key,1).split(sp_key)
344
+ v_plst_2=[_embedder.encode(_e) for _e in plst_2]
345
+ v_dlst_2=[_embedder.encode(_e) for _e in dlst_2]
346
+
347
+ print(clst_2)
348
+
349
+ rt_lst=[]
350
+ for i in tqdm(the_pool):
351
+ lst_1=[_e for _e in corpus_dict[i]]
352
+ id_lst_1=[id_lst[sen_lst.index(_e)] for _e in lst_1]
353
+ vec_lst_1=[vec_lst[sen_lst.index(_e)] for _e in lst_1]#[_embedder.encode(_e) for _e in lst_1]
354
+ clst_1=[corpus_clust_label[_e] for _e in id_lst_1]#[clust_search(_cluster_core_dict,_e,0.68) for _e in vec_lst_1]
355
+ #print(clst_1)
356
+ inset=sorted([_e for _e in set(clst_1)&set(clst_2) if _e!=-1])
357
+ temp_ot={}
358
+ if len(inset)>=max(1,inset_th):
359
+ temp_ot["target"]=target_name
360
+ temp_ot["inset"]=inset
361
+ #print(len(inset))
362
+ _img=img_resize(vec2img(vec_lst_1,clst_1,vec_lst_2,clst_2,clust_th),cnn_len)
363
+ cnn_pred=cnn_model.predict(np.array([_img])/255)
364
+
365
+ _con1,_con2=[],[]
366
+ for tp_i in range(bilstm_len):
367
+ if len(lst_1)>tp_i:
368
+ _con1.append(vec_lst_1[tp_i])
369
+ else:
370
+ _con1.append([0]*emb_dim)
371
+ for tp_i in range(bilstm_len):
372
+ if len(lst_2)>tp_i:
373
+ _con2.append(vec_lst_2[tp_i])
374
+ else:
375
+ _con2.append([0]*emb_dim)
376
+ _con1=np.array([_con1])
377
+ _con2=np.array([_con2])
378
+ print(len(_con1),len(_con2),len(_con2[0]))
379
+ #_con1=list(np.array(vec_lst_1).reshape(len(lst_1)*emb_dim))+[0]*(emb_dim*(bilstm_len-len(lst_1))) if len(lst_1)<=bilstm_len else list(np.array(vec_lst_1).reshape(len(lst_1)*emb_dim))[:bilstm_len*emb_dim]
380
+ #_con2=list(np.array(vec_lst_2).reshape(len(lst_2)*emb_dim))+[0]*(emb_dim*(bilstm_len-len(lst_2))) if len(lst_2)<=bilstm_len else list(np.array(vec_lst_2).reshape(len(lst_2)*emb_dim))[:bilstm_len*emb_dim]
381
+ bilstm_pred=bilstm_model.predict([_con1,_con2])
382
+
383
+
384
+ temp_ot["cnn_pred"]=float(cnn_pred[0][0])
385
+ temp_ot["bilstm_pred"]=float(bilstm_pred[0][0])
386
+ #print(cnn_pred)
387
+ #print(bilstm_pred)
388
+ x_e=[[bilstm_pred[0][0],cnn_pred[0][0]]]
389
+ ensemble_pred=logistic(x_r,y_r,x_e)
390
+ temp_ot["ensemble_pred"]=float(ensemble_pred[0])
391
+ #print(ensemble_pred)
392
+
393
+ pre_lst_1=[[color_lst[inset.index(clst_1[_e]) % len(color_lst)],Fore.WHITE,lst_1[_e],Style.RESET_ALL] if clst_1[_e] in inset else [Style.RESET_ALL,lst_1[_e]] for _e in range(len(lst_1))]
394
+ pre_lst_2=[[color_lst[inset.index(clst_2[_e]) % len(color_lst)],Fore.WHITE,lst_2[_e],Style.RESET_ALL] if clst_2[_e] in inset else [Style.RESET_ALL,lst_2[_e]] for _e in range(len(lst_2))]
395
+
396
+ vlst_1=[[vec_lst_1[_e],pre_lst_1[_e][0]] for _e in range(len(pre_lst_1)) if len(pre_lst_1[_e])==4]
397
+ vlst_2=[[vec_lst_2[_e],pre_lst_2[_e][0]] for _e in range(len(pre_lst_2)) if len(pre_lst_2[_e])==4]
398
+
399
+ #print(lst_1)
400
+
401
+ plst_1=replace_all("".join(corpus_pd_f[i.replace("_",",")][0]),key_lst,sp_key,1).split(sp_key)
402
+
403
+ dlst_1=replace_all("".join(corpus_pd_f[i.replace("_",",")][1]),key_lst,sp_key,1).split(sp_key)
404
+
405
+ v_plst_1=[_embedder.encode(_e) for _e in plst_1]
406
+
407
+ v_dlst_1=[_embedder.encode(_e) for _e in dlst_1]
408
+
409
+
410
+ cs_p1=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_1]) for _e in v_plst_1]
411
+ cs_d1=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_1]) for _e in v_dlst_1]
412
+
413
+ cs_p2=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_2]) for _e in v_plst_2]
414
+ cs_d2=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_2]) for _e in v_dlst_2]
415
+
416
+ pre_lst_p1=[[cs_p1[_e][-1],Fore.WHITE,plst_1[_e],Style.RESET_ALL] if cs_p1[_e][0]>_th else [Style.RESET_ALL,plst_1[_e]] for _e in range(len(cs_p1))]
417
+ pre_lst_d1=[[cs_d1[_e][-1],Fore.WHITE,dlst_1[_e],Style.RESET_ALL] if cs_d1[_e][0]>_th else [Style.RESET_ALL,dlst_1[_e]] for _e in range(len(cs_d1))]
418
+
419
+ pre_lst_p2=[[cs_p2[_e][-1],Fore.WHITE,plst_2[_e],Style.RESET_ALL] if cs_p2[_e][0]>_th else [Style.RESET_ALL,plst_2[_e]] for _e in range(len(cs_p2))]
420
+ pre_lst_d2=[[cs_d2[_e][-1],Fore.WHITE,dlst_2[_e],Style.RESET_ALL] if cs_d2[_e][0]>_th else [Style.RESET_ALL,dlst_2[_e]] for _e in range(len(cs_d2))]
421
+
422
+
423
+ #if max_dp<max([len(plst_1),len(plst_2),len(dlst_1),len(dlst_2)]):
424
+ # max_dp=max([len(plst_1),len(plst_2),len(dlst_1),len(dlst_2)])
425
+
426
+ #print(plst_1)
427
+ #print(plst_2)
428
+ #print(dlst_1)
429
+ #print(dlst_2)
430
+ draw_lst_1=["".join(_e) for _e in pre_lst_1]
431
+ draw_lst_2=["".join(_e) for _e in pre_lst_2]
432
+
433
+ draw_lst_p1=["".join(_e) for _e in pre_lst_p1]
434
+ draw_lst_p2=["".join(_e) for _e in pre_lst_p2]
435
+ draw_lst_d1=["".join(_e) for _e in pre_lst_d1]
436
+ draw_lst_d2=["".join(_e) for _e in pre_lst_d2]
437
+ #replace_all(temp_c,key_lst,",",0)
438
+
439
+ #print(plst_1)
440
+ tp_str=""
441
+
442
+ #print("---------------------")
443
+ #print(Fore.BLUE+str(i)+Style.RESET_ALL)
444
+ temp_ot["case_id"]=i
445
+ temp_ot["plaintiff_case1"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_p1]
446
+ temp_ot["defendant_case1"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_d1]
447
+ temp_ot["dispute_case1"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_1]
448
+ temp_ot["plaintiff_case2"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_p2]
449
+ temp_ot["defendant_case2"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_d2]
450
+ temp_ot["dispute_case2"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_2]
451
+
452
+ tp_str+=Fore.BLUE+str(i)+Style.RESET_ALL+"\n"
453
+ tp_str+=(Fore.GREEN if temp_ot["ensemble_pred"]>=0.75 else Fore.YELLOW if temp_ot["ensemble_pred"]>=0.5 else Fore.RED)+str(temp_ot["ensemble_pred"])+Style.RESET_ALL+"\n"
454
+ tp_str+=Fore.MAGENTA+"---plaintiff_case1---"+Style.RESET_ALL+"\n"
455
+ tp_str+="".join(draw_lst_p1)+Style.RESET_ALL+"\n"
456
+
457
+ tp_str+=Fore.MAGENTA+"---defendant_case1---"+Style.RESET_ALL+"\n"
458
+ tp_str+="".join(draw_lst_d1)+Style.RESET_ALL+"\n"
459
+
460
+ tp_str+=Fore.MAGENTA+"---dispute_case1---"+Style.RESET_ALL+"\n"
461
+ tp_str+="".join(draw_lst_1)+Style.RESET_ALL+"\n"
462
+ ###
463
+ tp_str+=Fore.BLUE+"target"+Style.RESET_ALL+"\n"
464
+
465
+ tp_str+=Fore.MAGENTA+"---plaintiff_case2---"+Style.RESET_ALL+"\n"
466
+ tp_str+="".join(draw_lst_p2)+Style.RESET_ALL+"\n"
467
+
468
+ tp_str+=Fore.MAGENTA+"---defendant_case2---"+Style.RESET_ALL+"\n"
469
+ tp_str+="".join(draw_lst_d2)+Style.RESET_ALL+"\n"
470
+
471
+ tp_str+=Fore.MAGENTA+"---dispute_case2---"+Style.RESET_ALL+"\n"
472
+ tp_str+="".join(draw_lst_2)+Style.RESET_ALL+"\n"
473
+
474
+ #tp_str+="---------------------"+"\n"
475
+
476
+
477
+
478
+ temp_ot["output"]=tp_str
479
+ rt_lst.append(temp_ot)
480
+ print(tp_str)
481
+ ot=sorted(rt_lst,key=lambda x:x["ensemble_pred"],reverse=True)
482
+ ot_lst=[i["output"] for i in ot[:sug_th]]
483
+
484
+ for i in ot[:sug_th]:
485
+ file=open("./json_file/"+str(target_name).replace(",","_")+"&"+str(i["case_id"])+".json","w",encoding='utf8')
486
+ json.dump({_e:i[_e] for _e in i if _e!="output"},file,indent=4,ensure_ascii=False)
487
+ file.close()
488
+ return ot_lst,ot[:sug_th]
489
+ def suggesting(the_pool,target_name,case_dict):
490
+ global ANSI_COLORS,_th,c_th,sug_th,corpus_dict,corpus_pd_f,vec_lst,id_lst,sen_lst,corpus_clust_label,_cluster_core_dict,_embedder
491
+ global bilstm_len,cnn_len,emb_dim,inset_th,clust_th
492
+ lst_2=[_e for _e in case_dict["p_point"]][:bilstm_len]
493
+
494
+ #for _e in lst2:
495
+ # temp=_embedder.encode(_e)
496
+ # vec_lst_2.append()
497
+ vec_lst_2=[_embedder.encode(_e) for _e in lst_2]
498
+
499
+ clst_2=[clust_search(_cluster_core_dict,_e,clust_th) for _e in vec_lst_2]
500
+ plst_2=replace_all("".join(case_dict["plaintiff"]),key_lst,sp_key,1).split(sp_key)
501
+
502
+ v_plst_2=[_embedder.encode(_e) for _e in plst_2]
503
+
504
+
505
+ print(clst_2)
506
+
507
+ rt_lst=[]
508
+ for i in tqdm(the_pool):
509
+ if target_name==i:
510
+ continue
511
+ lst_1=[_e for _e in corpus_dict[i]]
512
+ id_lst_1=[id_lst[sen_lst.index(_e)] for _e in lst_1]
513
+ vec_lst_1=[vec_lst[sen_lst.index(_e)] for _e in lst_1]#[_embedder.encode(_e) for _e in lst_1]
514
+ clst_1=[corpus_clust_label[_e] for _e in id_lst_1]#[clust_search(_cluster_core_dict,_e,0.68) for _e in vec_lst_1]
515
+ #print(clst_1)
516
+ inset=sorted([_e for _e in set(clst_1)&set(clst_2) if _e!=-1])
517
+ temp_ot={}
518
+ if len(inset)>=max(1,inset_th):
519
+ temp_ot["target"]=target_name
520
+ temp_ot["inset"]=inset
521
+ #print(len(inset))
522
+ _img=img_resize(vec2img(vec_lst_1,clst_1,vec_lst_2,clst_2,clust_th),cnn_len)
523
+ cnn_pred=cnn_model.predict(np.array([_img])/255)
524
+
525
+ _con1,_con2=[],[]
526
+ for tp_i in range(bilstm_len):
527
+ if len(lst_1)>tp_i:
528
+ _con1.append(vec_lst_1[tp_i])
529
+ else:
530
+ _con1.append([0]*emb_dim)
531
+ for tp_i in range(bilstm_len):
532
+ if len(lst_2)>tp_i:
533
+ _con2.append(vec_lst_2[tp_i])
534
+ else:
535
+ _con2.append([0]*emb_dim)
536
+ _con1=np.array([_con1])
537
+ _con2=np.array([_con2])
538
+ print(len(_con1),len(_con2),len(_con2[0]))
539
+ #_con1=list(np.array(vec_lst_1).reshape(len(lst_1)*emb_dim))+[0]*(emb_dim*(bilstm_len-len(lst_1))) if len(lst_1)<=bilstm_len else list(np.array(vec_lst_1).reshape(len(lst_1)*emb_dim))[:bilstm_len*emb_dim]
540
+ #_con2=list(np.array(vec_lst_2).reshape(len(lst_2)*emb_dim))+[0]*(emb_dim*(bilstm_len-len(lst_2))) if len(lst_2)<=bilstm_len else list(np.array(vec_lst_2).reshape(len(lst_2)*emb_dim))[:bilstm_len*emb_dim]
541
+ bilstm_pred=bilstm_model.predict([_con1,_con2])
542
+ temp_ot["cnn_pred"]=float(cnn_pred[0][0])
543
+ temp_ot["bilstm_pred"]=float(bilstm_pred[0][0])
544
+ #print(cnn_pred)
545
+ #print(bilstm_pred)
546
+ x_e=[[bilstm_pred[0][0],cnn_pred[0][0]]]
547
+ ensemble_pred=logistic(x_r,y_r,x_e)
548
+ temp_ot["ensemble_pred"]=float(ensemble_pred[0])
549
+ #print(ensemble_pred)
550
+
551
+ pre_lst_1=[[color_lst[inset.index(clst_1[_e]) % len(color_lst)],Fore.WHITE,lst_1[_e],Style.RESET_ALL] if clst_1[_e] in inset else [Style.RESET_ALL,lst_1[_e]] for _e in range(len(lst_1))]
552
+ pre_lst_2=[[color_lst[inset.index(clst_2[_e]) % len(color_lst)],Fore.WHITE,lst_2[_e],Style.RESET_ALL] if clst_2[_e] in inset else [Style.RESET_ALL,lst_2[_e]] for _e in range(len(lst_2))]
553
+
554
+ vlst_1=[[vec_lst_1[_e],pre_lst_1[_e][0]] for _e in range(len(pre_lst_1)) if len(pre_lst_1[_e])==4]
555
+ vlst_2=[[vec_lst_2[_e],pre_lst_2[_e][0]] for _e in range(len(pre_lst_2)) if len(pre_lst_2[_e])==4]
556
+
557
+ #print(lst_1)
558
+
559
+ plst_1=replace_all("".join(corpus_pd_f[i.replace("_",",")][0]),key_lst,sp_key,1).split(sp_key)
560
+
561
+
562
+ v_plst_1=[_embedder.encode(_e) for _e in plst_1]
563
+
564
+
565
+
566
+ cs_p1=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_1]) for _e in v_plst_1]
567
+
568
+ cs_p2=[max([[cos_sim(_e,_v[0]),_v[-1]] for _v in vlst_2]) for _e in v_plst_2]
569
+
570
+ pre_lst_p1=[[cs_p1[_e][-1],Fore.WHITE,plst_1[_e],Style.RESET_ALL] if cs_p1[_e][0]>_th else [Style.RESET_ALL,plst_1[_e]] for _e in range(len(cs_p1))]
571
+
572
+
573
+ pre_lst_p2=[[cs_p2[_e][-1],Fore.WHITE,plst_2[_e],Style.RESET_ALL] if cs_p2[_e][0]>_th else [Style.RESET_ALL,plst_2[_e]] for _e in range(len(cs_p2))]
574
+
575
+
576
+
577
+ #if max_dp<max([len(plst_1),len(plst_2),len(dlst_1),len(dlst_2)]):
578
+ # max_dp=max([len(plst_1),len(plst_2),len(dlst_1),len(dlst_2)])
579
+
580
+ #print(plst_1)
581
+ #print(plst_2)
582
+ #print(dlst_1)
583
+ #print(dlst_2)
584
+ draw_lst_1=["".join(_e) for _e in pre_lst_1]
585
+ draw_lst_2=["".join(_e) for _e in pre_lst_2]
586
+
587
+ draw_lst_p1=["".join(_e) for _e in pre_lst_p1]
588
+ draw_lst_p2=["".join(_e) for _e in pre_lst_p2]
589
+
590
+ #replace_all(temp_c,key_lst,",",0)
591
+
592
+ #print(plst_1)
593
+ tp_str=""
594
+
595
+ #print("---------------------")
596
+ #print(Fore.BLUE+str(i)+Style.RESET_ALL)
597
+ temp_ot["case_id"]=i
598
+ temp_ot["plaintiff_case1"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_p1]
599
+ temp_ot["p_point_case1"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_1]
600
+ temp_ot["plaintiff_case2"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_p2]
601
+ temp_ot["p_point_case2"]=[{"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[_e[1]],"content":_e[-2]} if len(_e)==4 else {"background_color":ANSI_COLORS[_e[0]],"font_color":ANSI_COLORS[Fore.WHITE],"content":_e[-1]} for _e in pre_lst_2]
602
+
603
+ tp_str+=Fore.BLUE+str(i)+Style.RESET_ALL+"\n"
604
+ tp_str+=(Fore.GREEN if temp_ot["ensemble_pred"]>=0.75 else Fore.YELLOW if temp_ot["ensemble_pred"]>=0.5 else Fore.RED)+str(temp_ot["ensemble_pred"])+Style.RESET_ALL+"\n"
605
+ tp_str+=Fore.MAGENTA+"---plaintiff_case1---"+Style.RESET_ALL+"\n"
606
+ tp_str+="".join(draw_lst_p1)+Style.RESET_ALL+"\n"
607
+
608
+
609
+ tp_str+=Fore.MAGENTA+"---p_point_case1---"+Style.RESET_ALL+"\n"
610
+ tp_str+="".join(draw_lst_1)+Style.RESET_ALL+"\n"
611
+ ###
612
+ tp_str+=Fore.BLUE+"target"+Style.RESET_ALL+"\n"
613
+
614
+ tp_str+=Fore.MAGENTA+"---plaintiff_case2---"+Style.RESET_ALL+"\n"
615
+ tp_str+="".join(draw_lst_p2)+Style.RESET_ALL+"\n"
616
+
617
+
618
+ tp_str+=Fore.MAGENTA+"---p_point_case2---"+Style.RESET_ALL+"\n"
619
+ tp_str+="".join(draw_lst_2)+Style.RESET_ALL+"\n"
620
+
621
+ #tp_str+="---------------------"+"\n"
622
+
623
+
624
+
625
+ temp_ot["output"]=tp_str
626
+ rt_lst.append(temp_ot)
627
+ print(tp_str)
628
+ ot=sorted(rt_lst,key=lambda x:x["ensemble_pred"],reverse=True)
629
+ ot_lst=[i["output"] for i in ot[:sug_th]]
630
+
631
+ for i in ot[:sug_th]:
632
+ file=open("./json_file/"+str(target_name).replace(",","_")+"&"+str(i["case_id"])+".json","w",encoding='utf8')
633
+ json.dump({_e:i[_e] for _e in i if _e!="output"},file,indent=4,ensure_ascii=False)
634
+ file.close()
635
+ return ot_lst,ot[:sug_th]
636
+ #---------------------------------------
637
+
638
+
639
+ _dir_lst=["../gpt4_0409_p_3/","../taide_llama3_8b_3/"]
640
+ _dir=_dir_lst[0]
641
+ sp_key="@"
642
+ emb_model="ftrob"
643
+ emb_model_path={\
644
+ "lf":"thunlp/Lawformer",\
645
+ "rob":'hfl/chinese-roberta-wwm-ext-large',\
646
+ "ftlf":"./sbert_pretrained_model/training-lawformer-clause_th10_100k_task-bs100-e2-2023-10-28/",
647
+ "ftrob":"./sbert_pretrained_model/training-roberta-clause_th10_100k_task-bs100-e2-2023-10-27",\
648
+ }
649
+
650
+ color_lst=[Back.BLUE,Back.GREEN,Back.MAGENTA,Back.YELLOW,Back.RED,Back.CYAN]#[Fore.RED,Fore.GREEN,Fore.YELLOW,Fore.BLUE,Fore.MAGENTA,Fore.CYAN]
651
+
652
+
653
+ log_f=json.load(open("./src/plaintiff_logistic_features.json","r"))["BiLSTM_CNN"]
654
+ x_r=np.array(log_f)[:,:-1]
655
+ y_r=np.array(log_f)[:,-1]
656
+
657
+
658
+
659
+ #pd_path,dis_path,s_path,v_path,c_path,t_path,cr_path,br_path=["TAIDE-LX-8B.jsonl","llama3_taide_8b_re_3_o_c.json","sentence.json","vector.json","hdb_cluster.json","hdb_ternary_array.json","hdb_cnn_result.json","hdb_sa_result.json"]
660
+
661
+
662
+
663
+ if sug_type=="plaintiff":
664
+ pd_f=corpus_pd_f=json.load(open("./src/corpus3835_raw.json","r"))["claim"]
665
+ s_f=json.load(open("./src/plaintiff_corpus3835_sen.json","r"))
666
+ v_f=json.load(open("./src/plaintiff_corpus3835_vec.json","r"))#json.load(open(_dir+v_path,"r"))
667
+
668
+ o_c_f=json.load(open("./src/plaintiff_corpus3835_cluster.json","r"))["clusters"]
669
+ c_f=clust_2_dict(o_c_f)
670
+ t_f=json.load(open("./src/plaintiff_ter.json","r"))
671
+ if pool_type=="corpus3835":
672
+ corpus_clust_label=clust_label(o_c_f)
673
+
674
+ vec_lst=v_f["vector"]
675
+ id_lst=v_f["id"]
676
+ sen_lst=s_f["sentence"]
677
+
678
+ corpus_dict={}
679
+ for i in range(len(id_lst)):
680
+ fid=id_lst[i].split("@")[0]
681
+ if fid not in corpus_dict:
682
+ corpus_dict[fid]=[sen_lst[i]]
683
+ else:
684
+ corpus_dict[fid].append(sen_lst[i])
685
+ corpus_pd_f=json.load(open("./src/corpus3835_raw.json","r"))["claim"]
686
+ else:
687
+ vec_f=json.load(open("./src/plaintiff_2022~2023_vec.json","r"))
688
+ vec_lst=[_e for i in vec_f for _e in vec_f[i]]
689
+
690
+
691
+ corpus_dict=json.load(open("./src/plaintiff_2022~2023_raw.json","r"))
692
+ corpus_pd_f=json.load(open("./src/2022~2023_raw.json","r"))["claim"]
693
+ corpus_clust_f=json.load(open("./src/plaintiff_2022~2023_clust.json","r"))
694
+
695
+ sen_lst=[_e for i in corpus_dict for _e in corpus_dict[i]]
696
+ id_lst=[i+"@"+str(_e) for i in corpus_dict for _e in range(len(corpus_dict[i]))]
697
+ corpus_clust_label={_e:corpus_clust_f[_e[:_e.find("@")]][int(_e[_e.find("@")+1:])] for _e in id_lst}
698
+
699
+ elif sug_type=="dispute":
700
+ pd_f=corpus_pd_f=json.load(open("./src/corpus3835_raw_dis.json","r"))["claim"]
701
+ s_f=json.load(open("./src/dispute_corpus3835_sen.json","r"))
702
+ v_f=json.load(open("./src/dispute_corpus3835_vec.json","r"))#json.load(open(_dir+v_path,"r"))
703
+
704
+ o_c_f=json.load(open("./src/dispute_corpus3835_cluster.json","r"))["clusters"]
705
+ c_f=clust_2_dict(o_c_f)
706
+ t_f=json.load(open("./src/dispute_ter.json","r"))
707
+ if pool_type=="corpus3835":
708
+ corpus_clust_label=clust_label(o_c_f)
709
+
710
+ vec_lst=v_f["vector"]
711
+ id_lst=v_f["id"]
712
+ sen_lst=s_f["sentence"]
713
+
714
+ corpus_dict={}
715
+ for i in range(len(id_lst)):
716
+ fid=id_lst[i].split("@")[0]
717
+ if fid not in corpus_dict:
718
+ corpus_dict[fid]=[sen_lst[i]]
719
+ else:
720
+ corpus_dict[fid].append(sen_lst[i])
721
+ corpus_pd_f=json.load(open("./src/corpus3835_raw_dis.json","r"))["claim"]
722
+ else:
723
+ vec_f=json.load(open("./src/dispute_2022~2023_vec.json","r"))
724
+ vec_lst=[_e for i in vec_f for _e in vec_f[i]]
725
+
726
+
727
+ corpus_dict=json.load(open("./src/dispute_2022~2023_raw.json","r"))
728
+ corpus_pd_f=json.load(open("./src/new22_23_3k3_corpus_raw.json","r"))["claim"]
729
+ corpus_clust_f=json.load(open("./src/dispute_22~23_clust.json","r"))
730
+
731
+ sen_lst=[_e for i in corpus_dict for _e in corpus_dict[i]]
732
+ id_lst=[i+"@"+str(_e) for i in corpus_dict for _e in range(len(corpus_dict[i]))]
733
+ corpus_clust_label={_e:corpus_clust_f[_e[:_e.find("@")]][int(_e[_e.find("@")+1:])] for _e in id_lst}
734
+
735
+
736
+
737
+ new_point_f=lst_2_dict(jl("./src/gpt-4-turbo-0409-0.3-new22_23.jsonl"))
738
+ new_pd_f=json.load(open("./src/new22_23_3k3_corpus_raw.json","r"))["claim"]
739
+ ###
740
+
741
+
742
+
743
+
744
+
745
+
746
+
747
+ key_lst=[",","。","?","?","!","!",";",":",";",":"]#["。","?","?","!","!",";",":",";",":"]
748
+
749
+
750
+ _embedder = SentenceTransformer(emb_model_path[emb_model])
751
+ cnn_model =...
752
+ bilstm_model =...
753
+
754
+ """#fifo
755
+ cnn_load()
756
+ bilstm_load()
757
+ """
758
+ cnn_load("/cpu:0")
759
+ bilstm_load("/cpu:0")
760
+ #"""
761
+
762
+
763
+
764
+ _cluster_core_dict=clust_core(o_c_f,v_f["vector"],v_f["id"],"central")
765
+ #---------------------------------------
766
+
767
+ from colorama import Fore,Style,Back
768
+
769
+ import gradio as gr
770
+
771
+ def case_sug_dis(file_name,plaintiff,defendant,p_point,d_point,dispute_list):
772
+ global new_pd_f,new_point_f,corpus_dict
773
+
774
+ #print(file_name)
775
+ #print(point_f)
776
+ #print(list(pd_f.keys()).index(file_name))
777
+ if file_name not in new_pd_f:
778
+ print("file not found")
779
+ file_name="user_input"
780
+ else:
781
+ plaintiff=new_pd_f[file_name][0]
782
+ defendant=new_pd_f[file_name][1]
783
+ p_point=new_point_f[file_name][0]
784
+ d_point=new_point_f[file_name][1]
785
+ dispute_list=new_point_f[file_name][2]
786
+
787
+ global sug_th
788
+
789
+
790
+ p_point="。".split(p_point) if type(p_point)==type("111") else p_point
791
+ d_point="。".split(d_point) if type(d_point)==type("111") else d_point
792
+ dispute_list="。".split(dispute_list) if type(dispute_list)==type("111") else dispute_list
793
+ _pool=[i for i in corpus_dict]
794
+ _case_dict={"plaintiff":plaintiff,"defendant":defendant,"p_point":p_point,"d_point":d_point,"dispute":dispute_list}
795
+ ot,ot_dict=suggesting_dis(_pool,file_name,_case_dict)
796
+
797
+
798
+ dispute="\n".join(dispute_list)
799
+ #ot=[Back.BLUE+dispute+Style.RESET_ALL]*10
800
+ output_list=[]
801
+ print("-----")
802
+ print(len(ot_dict))
803
+ out_path="./out_of_range.html"
804
+ for i in range(sug_th):
805
+ if i<len(ot_dict):
806
+ _path="./html_file/test"+str(i)+".html"
807
+ output_html=ansi_to_html_dis(ot_dict[i],_path)
808
+ #output_image = Image.open(_path)
809
+ output_list.append(_path)
810
+ else:
811
+ output_list.append(out_path)
812
+ return output_list
813
+ def case_sug(file_name,plaintiff,p_point):
814
+ global new_pd_f,new_point_f,corpus_dict
815
+
816
+ print(file_name)
817
+ #print(point_f)
818
+ #print(list(pd_f.keys()).index(file_name))
819
+ if file_name not in new_pd_f:
820
+ print("file not found")
821
+ file_name="user_input"
822
+ else:
823
+ plaintiff=new_pd_f[file_name][0]
824
+ p_point=new_point_f[file_name][0]
825
+
826
+
827
+ global sug_th
828
+
829
+ p_point=p_point.split("。") if type(p_point)==type("111") else p_point
830
+ _pool=[i for i in corpus_dict]
831
+ _case_dict={"plaintiff":plaintiff,"p_point":p_point}
832
+ print(_case_dict,[type(_case_dict[_e]) for _e in _case_dict])
833
+ ot,ot_dict=suggesting(_pool,file_name,_case_dict)
834
+
835
+
836
+
837
+ #ot=[Back.BLUE+dispute+Style.RESET_ALL]*10
838
+ output_list=[]
839
+ print("-----")
840
+ print(len(ot_dict))
841
+ out_path="./out_of_range.html"
842
+ for i in range(sug_th):
843
+ if i<len(ot_dict):
844
+ _path="./html_file/test"+str(i)+".html"
845
+ output_html=ansi_to_html(ot_dict[i],_path)
846
+ #output_image = Image.open(_path)
847
+ output_list.append(_path)
848
+ else:
849
+ output_list.append(out_path)
850
+ return output_list
851
+ if sug_type=="plaintiff":
852
+ demo = gr.Interface(fn=case_sug, inputs=["text","text","text"], outputs=[gr.outputs.File() for i in range(sug_th)])
853
+ demo.launch(share=True,server_port=4096,show_error=True)
854
+ elif sug_type=="dispute":
855
+ demo = gr.Interface(fn=case_sug_dis, inputs=["text","text","text","text","text","text"], outputs=[gr.outputs.File() for i in range(sug_th)])
856
+ demo.launch(share=True,server_port=2048,show_error=True)