-
Notifications
You must be signed in to change notification settings - Fork 0
/
IndicLID.py
291 lines (246 loc) · 8.81 KB
/
IndicLID.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
import re
import pandas as pd
import fasttext
import torch
from torch.utils.data import Dataset
from transformers import AutoTokenizer
class IndicBERT_Data(Dataset):
def __init__(self, indices, X):
self.size = len(X)
self.x = X
self.i = indices
def __len__(self):
return self.size
def __getitem__(self, idx):
text = self.x[idx]
index = self.i[idx]
return tuple([index, text])
class IndicLID:
def __init__(self, input_threshold=0.5, roman_lid_threshold=0.6):
self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
self.IndicLID_FTN_path = "models/indiclid-ftn/model_baseline_roman.bin"
self.IndicLID_FTR_path = "models/indiclid-ftr/model_baseline_roman.bin"
self.IndicLID_BERT_path = "models/indiclid-bert/basline_nn_simple.pt"
self.IndicLID_FTN = fasttext.load_model(self.IndicLID_FTN_path)
self.IndicLID_FTR = fasttext.load_model(self.IndicLID_FTR_path)
self.IndicLID_BERT = torch.load(
self.IndicLID_BERT_path, map_location=self.device
)
self.IndicLID_BERT.eval()
self.IndicLID_BERT_tokenizer = AutoTokenizer.from_pretrained(
"ai4bharat/IndicBERTv2-MLM-only"
)
self.input_threshold = input_threshold
self.model_threshold = roman_lid_threshold
self.classes = 47
self.IndicLID_lang_code_dict = {
"asm_Latn": 0,
"ben_Latn": 1,
"brx_Latn": 2,
"guj_Latn": 3,
"hin_Latn": 4,
"kan_Latn": 5,
"kas_Latn": 6,
"kok_Latn": 7,
"mai_Latn": 8,
"mal_Latn": 9,
"mni_Latn": 10,
"mar_Latn": 11,
"nep_Latn": 12,
"ori_Latn": 13,
"pan_Latn": 14,
"san_Latn": 15,
"snd_Latn": 16,
"tam_Latn": 17,
"tel_Latn": 18,
"urd_Latn": 19,
"eng_Latn": 20,
"other": 21,
"asm_Beng": 22,
"ben_Beng": 23,
"brx_Deva": 24,
"doi_Deva": 25,
"guj_Gujr": 26,
"hin_Deva": 27,
"kan_Knda": 28,
"kas_Arab": 29,
"kas_Deva": 30,
"kok_Deva": 31,
"mai_Deva": 32,
"mal_Mlym": 33,
"mni_Beng": 34,
"mni_Meti": 35,
"mar_Deva": 36,
"nep_Deva": 37,
"ori_Orya": 38,
"pan_Guru": 39,
"san_Deva": 40,
"sat_Olch": 41,
"snd_Arab": 42,
"tam_Tamil": 43,
"tel_Telu": 44,
"urd_Arab": 45,
}
self.IndicLID_lang_code_dict_reverse = {
0: "asm_Latn",
1: "ben_Latn",
2: "brx_Latn",
3: "guj_Latn",
4: "hin_Latn",
5: "kan_Latn",
6: "kas_Latn",
7: "kok_Latn",
8: "mai_Latn",
9: "mal_Latn",
10: "mni_Latn",
11: "mar_Latn",
12: "nep_Latn",
13: "ori_Latn",
14: "pan_Latn",
15: "san_Latn",
16: "snd_Latn",
17: "tam_Latn",
18: "tel_Latn",
19: "urd_Latn",
20: "eng_Latn",
21: "other",
22: "asm_Beng",
23: "ben_Beng",
24: "brx_Deva",
25: "doi_Deva",
26: "guj_Gujr",
27: "hin_Deva",
28: "kan_Knda",
29: "kas_Arab",
30: "kas_Deva",
31: "kok_Deva",
32: "mai_Deva",
33: "mal_Mlym",
34: "mni_Beng",
35: "mni_Meti",
36: "mar_Deva",
37: "nep_Deva",
38: "ori_Orya",
39: "pan_Guru",
40: "san_Deva",
41: "sat_Olch",
42: "snd_Arab",
43: "tam_Tamil",
44: "tel_Telu",
45: "urd_Arab",
}
def pre_process(self, input):
return input
def char_percent_check(self, input):
input_len = len(list(input))
special_char_pattern = re.compile("[@_!#$%^&*()<>?/\|}{~:]")
special_char_matches = special_char_pattern.findall(input)
special_chars = len(special_char_matches)
spaces = len(re.findall("\s", input))
newlines = len(re.findall("\n", input))
total_chars = input_len - (special_chars + spaces + newlines)
en_pattern = re.compile("[a-zA-Z0-9]")
en_matches = en_pattern.findall(input)
en_chars = len(en_matches)
if total_chars == 0:
return 0
return en_chars / total_chars
def native_inference(self, input_list, output_dict):
if not input_list:
return output_dict
input_texts = [line[1] for line in input_list]
IndicLID_FTN_predictions = self.IndicLID_FTN.predict(input_texts)
for input, pred_label, pred_score in zip(
input_list, IndicLID_FTN_predictions[0], IndicLID_FTN_predictions[1]
):
output_dict[input[0]] = (
input[1],
pred_label[0][9:],
pred_score[0],
"IndicLID-FTN",
)
return output_dict
def roman_inference(self, input_list, output_dict, batch_size):
if not input_list:
return output_dict
input_texts = [line[1] for line in input_list]
IndicLID_FTR_predictions = self.IndicLID_FTR.predict(input_texts)
IndicLID_BERT_inputs = []
for input, pred_label, pred_score in zip(
input_list, IndicLID_FTR_predictions[0], IndicLID_FTR_predictions[1]
):
if pred_score[0] > self.model_threshold:
output_dict[input[0]] = (
input[1],
pred_label[0][9:],
pred_score[0],
"IndicLID-FTR",
)
else:
IndicLID_BERT_inputs.append(input)
output_dict = self.IndicBERT_roman_inference(
IndicLID_BERT_inputs, output_dict, batch_size
)
return output_dict
def IndicBERT_roman_inference(self, IndicLID_BERT_inputs, output_dict, batch_size):
if not IndicLID_BERT_inputs:
return output_dict
df = pd.DataFrame(IndicLID_BERT_inputs)
dataloader = self.get_dataloaders(df.iloc[:, 0], df.iloc[:, 1], batch_size)
with torch.no_grad():
for data in dataloader:
batch_indices = data[0]
batch_inputs = data[1]
word_embeddings = self.IndicLID_BERT_tokenizer(
batch_inputs,
return_tensors="pt",
padding=True,
truncation=True,
max_length=512,
)
word_embeddings = word_embeddings.to(self.device)
batch_outputs = self.IndicLID_BERT(
word_embeddings["input_ids"],
token_type_ids=word_embeddings["token_type_ids"],
attention_mask=word_embeddings["attention_mask"],
)
_, batch_predicted = torch.max(batch_outputs.logits, 1)
for index, input, pred_label, logit in zip(
batch_indices, batch_inputs, batch_predicted, batch_outputs.logits
):
output_dict[index] = (
input,
self.IndicLID_lang_code_dict_reverse[pred_label.item()],
logit[pred_label.item()].item(),
"IndicLID-BERT",
)
return output_dict
def post_process(self, output_dict):
results = []
keys = list(output_dict.keys())
keys.sort()
for index in keys:
results.append(output_dict[index])
return results
def get_dataloaders(self, indices, input_texts, batch_size):
data_obj = IndicBERT_Data(indices, input_texts)
dl = torch.utils.data.DataLoader(data_obj, batch_size=batch_size, shuffle=False)
return dl
def predict(self, input):
input_list = [
input,
]
self.batch_predict(input_list, 1)
def batch_predict(self, input_list, batch_size):
output_dict = {}
roman_inputs = []
native_inputs = []
for index, input in enumerate(input_list):
if self.char_percent_check(input) > self.input_threshold:
roman_inputs.append((index, input))
else:
native_inputs.append((index, input))
output_dict = self.native_inference(native_inputs, output_dict)
output_dict = self.roman_inference(roman_inputs, output_dict, batch_size)
results = self.post_process(output_dict)
return results