-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
541 lines (453 loc) · 18.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
import bdb
import json
import pdb
from pickle import TRUE
from typing import List
import numpy as np
import requests
import uvicorn
from sklearn.preprocessing import label_binarize
from sklearn.metrics import f1_score
from helpers import *
from utils import *
from llm_utils import (
get_best_translation_propmt,
get_mqm_erros,
get_postedit,
get_translation_quality,
select_best,
)
@app.post("/get_next_sample_automl/")
async def get_next_sample_automl(
strategy: str = "Greedy (Regression)",
sort: str = "desc",
topics: List[str] = topic_label_list,
):
app.df = pd.read_csv("./data/temp_data.csv")
# Read from dataframe app.df and gets next sample based on sample_id
filtered_df = app.df[app.df.isUpdated == False]
filtered_df = filtered_df[filtered_df.source_topic.isin(topics)]
model_sev = load_model(
app.df_model[app.df_model["type"] == "sev"].iloc[-1]["model_path"]
)
model_cat = load_model(
app.df_model[app.df_model["type"] == "cat"].iloc[-1]["model_path"]
)
model_rank = load_model(
app.df_model[app.df_model["type"] == "rank"].iloc[-1]["model_path"]
)
model_sev.update_query_strategy(strategy_map[strategy])
idxs = filtered_df.index.values.reshape(-1)
X_pool, y_pool, seg_ids = get_ranking_features(idxs)
query_idx, query_sample = model_rank.query(X_pool, n_instances=1)
# query_idx, query_sample = model_sev.query(
# get_features(filtered_df.index.values)[0], n_instances=1
# )
sample_seg_id = seg_ids[query_idx][0][0]
temp_df = filtered_df[filtered_df.seg_id == sample_seg_id]
samples = []
severity_sort_list = []
for index, item in temp_df.groupby("system"):
sample = {}
sample["sample_id"] = int(item.index.values[0])
sample["source"] = item.source.values[0]
sample["target"] = item.target.values[0]
sample["severity"] = int(round(float(item.severity.mean())))
sample["category"] = list(set(item.category.values))
sample["system"] = index
sample["predicted_severity"] = int(
model_sev.predict(get_features([sample["sample_id"]])[0])[0]
)
sample["source_major_severity_prob"] = float(
round(
model_sev.predict_proba(get_features([sample["sample_id"]])[0])[0][1], 2
)
)
if sample["predicted_severity"] == 1:
sample["predicted_category"] = []
else:
sample["predicted_category"] = binarizer_cat.inverse_transform(
model_cat.predict(get_features([sample["sample_id"]])[0])
)[0]
try:
sample["predicted_category"].remove("No-error")
except:
pass
sample["best_model"] = id2system[
model_rank.predict(get_ranking_features(sample["sample_id"])[0])[0]
]
samples.append(sample)
severity_sort_list.append(sample["predicted_severity"])
# Sort the best to worst
if sort == "asc":
sorted_idx = np.argsort(severity_sort_list)
elif sort == "desc":
sorted_idx = np.argsort(severity_sort_list)[::-1]
else:
raise ValueError("sort must be either asc or desc")
samples = np.array(samples)[sorted_idx].tolist()
response = {"samples": samples}
app.df.to_csv("./data/temp_data.csv", index=False)
return response
@app.post("/update_sample_automl/")
async def update_sample_automl(
sample_id: int,
post_edit: str,
best_model: str,
cat_label: List[str],
sev_label: str,
skip: bool = False,
):
app.df = pd.read_csv("./data/temp_data.csv")
if cat_label == []:
cat_label = ["No-error"]
model_sev = load_model(
app.df_model[app.df_model["type"] == "sev"].iloc[-1]["model_path"]
)
model_cat = load_model(
app.df_model[app.df_model["type"] == "cat"].iloc[-1]["model_path"]
)
model_rank = load_model(
app.df_model[app.df_model["type"] == "rank"].iloc[-1]["model_path"]
)
latest_model_id = int(app.df_model.iloc[-1]["id"])
new_model_id = latest_model_id + 1
df_stats_cat = pd.read_csv("./data/cat_stats.csv", index_col=None)
# Update the statistics in the dataframe
system_name = app.df["system"].iloc[sample_id]
categories = binarizer_cat.transform([cat_label])
df_cat_dict = {}
for k, v in category2id.items():
if k in cat_label:
df_cat_dict[k] = 1
else:
df_cat_dict[k] = 0
for k, v in class2id.items():
if k == sev_label:
df_cat_dict[f"Severity_{k}"] = 1
else:
df_cat_dict[f"Severity_{k}"] = 0
df_cat_dict["system"] = system_name
df_stats_cat = df_stats_cat.append(df_cat_dict, ignore_index=True)
df_stats_cat.to_csv("./data/cat_stats.csv", index=False)
# TODO: Calculate the features with updated data and teach the model
# But we already have the features calculated for these samples
if not skip:
# TODO extract features for post_edited text
X, y = get_features([sample_id])
model_cat.teach(X, categories, **automl_cat_settings)
model_sev.teach(
X, np.array(class2id[sev_label]).reshape(1, 1), **automl_sev_settings
)
X, y, _ = get_ranking_features(sample_id)
model_rank.teach(
X,
np.array(system2id[best_model]).reshape(
1,
),
**automl_rank_settings,
)
app.df.loc[(app.df.seg_id == _[0][0]), "isUpdated"] = True
model_sev_path = f"./models/model_sev_{new_model_id}.pkl"
model_cat_path = f"./models/model_cat_{new_model_id}.pkl"
model_rank_path = f"./models/model_rank_{new_model_id}.pkl"
save_model(model_cat, model_cat_path)
save_model(model_sev, model_sev_path)
save_model(model_rank, model_rank_path)
app.df_model = app.df_model.append(
{"model_path": model_sev_path, "id": new_model_id, "type": "sev"},
ignore_index=True,
)
app.df_model = app.df_model.append(
{"model_path": model_cat_path, "id": new_model_id, "type": "cat"},
ignore_index=True,
)
app.df_model = app.df_model.append(
{"model_path": model_rank_path, "id": new_model_id, "type": "rank"},
ignore_index=True,
)
app.df.to_csv("./data/temp_data.csv", index=False)
return {"status": "success"}
@app.get("/predict_sample_automl/")
async def predict_sample_automl(source: str, post_edit: str):
app.df = pd.read_csv("./data/temp_data.csv")
model_sev = load_model(
app.df_model[app.df_model["type"] == "sev"].iloc[-1]["model_path"]
)
model_cat = load_model(
app.df_model[app.df_model["type"] == "cat"].iloc[-1]["model_path"]
)
model_rank = load_model(
app.df_model[app.df_model["type"] == "rank"].iloc[-1]["model_path"]
)
# Calculate the features for the post_edit text
X = get_features_from_text(source, post_edit)
predicted_proba = model_sev.predict_proba(X)[0]
app.df.to_csv("./data/temp_data.csv", index=False)
return {"major_severity_prob": round(predicted_proba[1], 2)}
@app.post("/get_data_stats/")
async def get_data_stats(rater: str, topics: List[str] = topic_label_list):
app.df = pd.read_csv("./data/temp_data.csv")
filtered_df = app.df
filtered_df = filtered_df[filtered_df.source_topic.isin(topics)]
# read unlabeled and labeled sample count
unlabeled = (
filtered_df[filtered_df.isUpdated == False].groupby("seg_id").count().shape[0]
)
labeled = (
filtered_df[filtered_df.isUpdated == True].groupby("seg_id").count().shape[0]
)
app.df.to_csv("./data/temp_data.csv", index=False)
return {"unlabeled": unlabeled, "labeled": labeled}
@app.post("/get_next_sample_online/")
async def get_next_sample_online(
strategy: str = "Greedy (Regression)",
sort: str = "desc",
topics: List[str] = topic_label_list,
use_llm: bool = True,
):
app.df = pd.read_csv("./data/temp_data.csv")
# Read from dataframe app.df and gets next sample based on sample_id
filtered_df = app.df[app.df.isUpdated == False]
filtered_df = filtered_df[filtered_df.source_topic.isin(topics)]
# model_sev_path = f"./models/model_online_sev.pkl"
model_cat_path = f"./models/model_cat_0.pkl"
# model_rank_path = f"./models/model_online_rank.pkl"
# model_sev = load_model_online(model_sev_path)
# model_rank = load_model_online(model_rank_path)
model_cat = load_model(model_cat_path)
if strategy == "Greedy (Regression)":
query_idx = query_next_sample_interaction(
app.model_sev_online, get_features(filtered_df.index.values)[0], n=1
)
sample_seg_id = filtered_df.iloc[query_idx]["seg_id"].values[0]
else:
## TODO: Implement the strategy for Entopy
# randomly select from filtered_df index
query_idx = np.random.choice(filtered_df.index.values)
sample_seg_id = query_idx
temp_df = filtered_df[filtered_df.seg_id == sample_seg_id]
samples = []
severity_sort_list = []
for index, item in temp_df.groupby("system"):
sample = {}
sample["sample_id"] = int(item.index.values[0])
sample["source"] = item.source.values[0]
sample["target"] = item.target.values[0]
sample["severity"] = int(round(float(item.severity.mean())))
sample["category"] = list(set(item.category.values))
sample["system"] = index
if not use_llm:
probs = app.model_sev_online.predict(
get_test_sample(get_features([sample["sample_id"]])[0][0])
)
sample["predicted_severity"] = int(int(np.argmax(probs)) + 1)
sample["source_major_severity_prob"] = float(round(probs[1], 2))
if sample["predicted_severity"] == 1:
sample["predicted_category"] = []
else:
sample["predicted_category"] = list(
binarizer_cat.inverse_transform(
model_cat.predict(get_features([sample["sample_id"]])[0])
)[0]
)
try:
sample["predicted_category"].remove("No-error")
except:
pass
best_model = np.argmax(
app.model_rank_online.predict(
get_test_example(get_ranking_features(sample["sample_id"])[0][0])
)
)
sample["is_best_model"] = False
else:
sample["predicted_category"] = get_mqm_erros(
sample["source"], sample["target"]
)
sample["predicted_category"] = [
x.capitalize() for x in sample["predicted_category"]
]
# predicted_sev = if (get_translation_quality(source, post_edit)/100.0) bigger than 50 then 1 else 2
translation_quality = get_translation_quality(
sample["source"], sample["target"]
)
if translation_quality > 50:
sample["predicted_severity"] = 1
else:
sample["predicted_severity"] = 2
sample["source_major_severity_prob"] = float(
1 - round(translation_quality / 100.0, 2)
)
sample["is_best_model"] = False
samples.append(sample)
severity_sort_list.append(sample["predicted_severity"])
if use_llm:
# select best
source = temp_df.source.values[0]
mts = temp_df.target.values.tolist()
best_model = select_best(source, mts)
samples[best_model]["is_best_model"] = True
# set the other s as false
# Sort the best to worst
if sort == "asc":
sorted_idx = np.argsort(severity_sort_list)
elif sort == "desc":
sorted_idx = np.argsort(severity_sort_list)[::-1]
else:
raise ValueError("sort must be either asc or desc")
samples = np.array(samples)[sorted_idx].tolist()
response = {"samples": samples}
app.df.to_csv("./data/temp_data.csv", index=False)
return response
## methods to get app stats
@app.get("/get_cat_stats/")
async def get_cat_stats():
app.df = pd.read_csv("./data/temp_data.csv")
df_stats_cat = pd.read_csv("./data/cat_stats.csv", index_col=None)
app.df.to_csv("./data/temp_data.csv", index=False)
return df_stats_cat.to_dict(orient="records")
@app.get("/get_sev_model_performance_online/")
async def get_sev_model_performanc_online():
app.df = pd.read_csv("./data/temp_data.csv")
# Read from dataframe app.df and gets next sample based on sample_id
filtered_df = app.df[app.df.isUpdated == False]
y_pred_sev = []
y_true_sev = []
for i, sample in filtered_df.iterrows():
probs = app.model_sev_online.predict(get_test_sample(get_features([i])[0][0]))
predicted_class = int(int(np.argmax(probs)) + 1)
y_pred_sev.append(predicted_class)
y_true_sev.append(int(sample.severity))
return {
"severity_model": {
"f1_score": float(
round(100 * f1_score(y_true_sev, y_pred_sev, average="weighted"), 3)
)
}
}
@app.post("/update_sample_online/")
async def update_sample_online(
sample_id: int,
post_edit: str,
best_model: str,
cat_label: List[str],
sev_label: str,
skip: bool = False,
):
app.df = pd.read_csv("./data/temp_data.csv")
if cat_label == []:
cat_label = ["No-error"]
# model_sev_path = f"./models/model_online_sev.pkl"
# model_rank_path = f"./models/model_online_rank.pkl"
model_cat_path = f"./models/model_cat_0.pkl"
# model_sev = load_model_online(model_sev_path)
# model_rank = load_model_online(model_rank_path)
model_cat = load_model(model_cat_path)
df_stats_cat = pd.read_csv("./data/cat_stats.csv", index_col=None)
# Update the statistics in the dataframe
system_name = app.df["system"].iloc[sample_id]
categories = binarizer_cat.transform([cat_label])
df_cat_dict = {}
for k, v in category2id.items():
if k in cat_label:
df_cat_dict[k] = 1
else:
df_cat_dict[k] = 0
for k, v in class2id.items():
if k == sev_label:
df_cat_dict[f"Severity_{k}"] = 1
else:
df_cat_dict[f"Severity_{k}"] = 0
df_cat_dict["system"] = system_name
df_stats_cat = df_stats_cat.append(df_cat_dict, ignore_index=True)
df_stats_cat.to_csv("./data/cat_stats.csv", index=False)
# TODO: Calculate the features with updated data and teach the model
# But we already have the features calculated for these samples
if not skip:
# TODO extract features for post_edited text
X, y = get_features([sample_id])
model_cat.teach(X, categories, **automl_cat_settings)
app.model_sev_online.learn(get_training_sample(X[0], class2id[sev_label]))
X, y, _ = get_ranking_features(sample_id)
app.model_rank_online.learn(get_training_example(X[0], system2id[best_model]))
app.df.loc[(app.df.seg_id == _[0][0]), "isUpdated"] = True
save_model(model_cat, model_cat_path)
save_model_online(app.model_sev_online, model_sev_path)
save_model_online(app.model_rank_online, model_rank_path)
app.df.to_csv("./data/temp_data.csv", index=False)
return True
@app.get("/predict_sample_online/")
async def predict_sample_online(source: str, post_edit: str, use_llm=True):
app.df = pd.read_csv("./data/temp_data.csv")
if not use_llm:
# model_sev_path = f"./models/model_online_sev.pkl"
# model_rank_path = f"./models/model_online_rank.pkl"
model_cat_path = f"./models/model_cat_0.pkl"
# model_sev = load_model_online(model_sev_path)
# model_rank = load_model_online(model_rank_path)
model_cat = load_model(model_cat_path)
# Calculate the features for the post_edit text
X = get_features_from_text(source, post_edit)
predicted_proba = app.model_sev_online.predict(get_test_sample(X[0]))
predicted_sev = id2class[int(np.argmax(predicted_proba)) + 1]
if predicted_sev == 1:
predicted_category = []
else:
predicted_category = list(
binarizer_cat.inverse_transform(model_cat.predict(X))[0]
)
try:
predicted_category.remove("No-error")
except:
pass
major_severity_prob = (float(round(predicted_proba[1], 2)),)
else:
predicted_category = get_mqm_erros(source, post_edit)
predicted_category = [x.capitalize() for x in predicted_category]
# predicted_sev = if (get_translation_quality(source, post_edit)/100.0) bigger than 50 then 1 else 2
translation_quality = get_translation_quality(source, post_edit)
if translation_quality > 50:
predicted_sev = 1
else:
predicted_sev = 2
major_severity_prob = float(1 - round(translation_quality / 100.0, 2))
app.df.to_csv("./data/temp_data.csv", index=False)
return {
"major_severity_prob": major_severity_prob,
"predicted_sev": predicted_sev,
"predicted_category": predicted_category,
}
@app.get("/get_pseudo_labelable_samples_count_online/")
async def get_pseudo_labelable_samples_count_online(threshold: float = None):
from sklearn.metrics import roc_curve
# Read from dataframe app.df and gets next sample based on sample_id
filtered_df = app.df[app.df.isUpdated == False]
yhat = []
y_true = []
for index, item in filtered_df.iterrows():
sample_id = int(index)
proba = app.model_sev_online.predict(
get_test_sample(get_features([sample_id])[0][0])
)
yhat.append(proba)
y_true.append(int(item.severity) - 1)
yhat = np.array(yhat)
y_true = np.array(y_true)
if threshold is None:
fpr, tpr, thresholds = roc_curve(y_true, yhat[:, 1])
# calculate the g-mean for each threshold
gmeans = np.sqrt(tpr * (1 - fpr))
ix = np.argmax(gmeans)
threshold = float(thresholds[ix])
# get number of samples more than threshold in yhat[:,1]
yhat_threshold = int(sum(yhat[:, 1] > threshold))
return {
"count": yhat_threshold,
"percentage": round(100 * float(yhat_threshold / len(yhat)), 2),
"threshold": threshold,
}
if __name__ == "__main__":
uvicorn.run(
"helpers:app", host="0.0.0.0", port=8088, reload=False, debug=False, workers=1
)