dewdev commited on
Commit
a53ef18
·
verified ·
1 Parent(s): 915f601

Upload 9 files

Browse files
README.md CHANGED
@@ -1,3 +1,113 @@
1
- ---
2
- license: mit
3
- ---
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ library_name: transformers
3
+ tags:
4
+ - language
5
+ - detection
6
+ - classification
7
+ license: mit
8
+ datasets:
9
+ - hac541309/open-lid-dataset
10
+ pipeline_tag: text-classification
11
+ ---
12
+
13
+ # Language Detection Model
14
+
15
+ A **BERT-based** language detection model trained on [hac541309/open-lid-dataset](https://huggingface.co/datasets/hac541309/open-lid-dataset), which includes **121 million sentences across 200 languages**. This model is optimized for **fast and accurate** language identification in text classification tasks.
16
+
17
+ ## Model Details
18
+
19
+ - **Architecture**: [BertForSequenceClassification](https://huggingface.co/transformers/model_doc/bert.html)
20
+ - **Hidden Size**: 384
21
+ - **Number of Layers**: 4
22
+ - **Attention Heads**: 6
23
+ - **Max Sequence Length**: 512
24
+ - **Dropout**: 0.1
25
+ - **Vocabulary Size**: 50,257
26
+
27
+ ## Training Process
28
+
29
+ - **Dataset**:
30
+ - Used the [open-lid-dataset](https://huggingface.co/datasets/hac541309/open-lid-dataset)
31
+ - Split into train (90%) and test (10%)
32
+ - **Tokenizer**: A custom `BertTokenizerFast` with special tokens for `[UNK]`, `[CLS]`, `[SEP]`, `[PAD]`, `[MASK]`
33
+ - **Hyperparameters**:
34
+ - Learning Rate: 2e-5
35
+ - Batch Size: 256 (training) / 512 (testing)
36
+ - Epochs: 1
37
+ - Scheduler: Cosine
38
+ - **Trainer**: Leveraged the Hugging Face [Trainer API](https://huggingface.co/docs/transformers/main_classes/trainer) with Weights & Biases for logging
39
+
40
+ ## Evaluation
41
+
42
+ The model was evaluated on the test split. Below are the overall metrics:
43
+
44
+ - **Accuracy**: 0.969466
45
+ - **Precision**: 0.969586
46
+ - **Recall**: 0.969466
47
+ - **F1 Score**: 0.969417
48
+
49
+ Detailled evaluation (Size is the number of languages supported)
50
+
51
+ | Script | Support | Precision | Recall | F1 Score | Size |
52
+ |--------|---------|-----------|--------|----------|------|
53
+ | Arab | 819219 | 0.9038 | 0.9014 | 0.9023 | 21 |
54
+ | Latn | 7924704 | 0.9678 | 0.9663 | 0.9670 | 125 |
55
+ | Ethi | 144403 | 0.9967 | 0.9964 | 0.9966 | 2 |
56
+ | Beng | 163983 | 0.9949 | 0.9935 | 0.9942 | 3 |
57
+ | Deva | 423895 | 0.9495 | 0.9326 | 0.9405 | 10 |
58
+ | Cyrl | 831949 | 0.9899 | 0.9883 | 0.9891 | 12 |
59
+ | Tibt | 35683 | 0.9925 | 0.9930 | 0.9927 | 2 |
60
+ | Grek | 131155 | 0.9984 | 0.9990 | 0.9987 | 1 |
61
+ | Gujr | 86912 | 0.99999 | 0.9999 | 0.99995 | 1 |
62
+ | Hebr | 100530 | 0.9966 | 0.9995 | 0.9981 | 2 |
63
+ | Armn | 67203 | 0.9999 | 0.9998 | 0.9998 | 1 |
64
+ | Jpan | 88004 | 0.9983 | 0.9987 | 0.9985 | 1 |
65
+ | Knda | 67170 | 0.9999 | 0.9998 | 0.9999 | 1 |
66
+ | Geor | 70769 | 0.99997 | 0.9998 | 0.9999 | 1 |
67
+ | Khmr | 39708 | 1.0000 | 0.9997 | 0.9999 | 1 |
68
+ | Hang | 108509 | 0.9997 | 0.9999 | 0.9998 | 1 |
69
+ | Laoo | 29389 | 0.9999 | 0.9999 | 0.9999 | 1 |
70
+ | Mlym | 68418 | 0.99996 | 0.9999 | 0.9999 | 1 |
71
+ | Mymr | 100857 | 0.9999 | 0.9992 | 0.9995 | 2 |
72
+ | Orya | 44976 | 0.9995 | 0.9998 | 0.9996 | 1 |
73
+ | Guru | 67106 | 0.99999 | 0.9999 | 0.9999 | 1 |
74
+ | Olck | 22279 | 1.0000 | 0.9991 | 0.9995 | 1 |
75
+ | Sinh | 67492 | 1.0000 | 0.9998 | 0.9999 | 1 |
76
+ | Taml | 76373 | 0.99997 | 0.9999 | 0.9999 | 1 |
77
+ | Tfng | 41325 | 0.8512 | 0.8246 | 0.8247 | 2 |
78
+ | Telu | 62387 | 0.99997 | 0.9999 | 0.9999 | 1 |
79
+ | Thai | 83820 | 0.99995 | 0.9998 | 0.9999 | 1 |
80
+ | Hant | 152723 | 0.9945 | 0.9954 | 0.9949 | 2 |
81
+ | Hans | 92689 | 0.9893 | 0.9870 | 0.9882 | 1 |
82
+
83
+
84
+ A detailed per-script classification report is also provided in the repository for further analysis.
85
+
86
+ ---
87
+
88
+ ### How to Use
89
+
90
+ You can quickly load and run inference with this model using the [Transformers pipeline](https://huggingface.co/docs/transformers/main_classes/pipelines):
91
+
92
+ ```python
93
+ from transformers import AutoTokenizer, AutoModelForSequenceClassification, pipeline
94
+
95
+ tokenizer = AutoTokenizer.from_pretrained("alexneakameni/language_detection")
96
+ model = AutoModelForSequenceClassification.from_pretrained("alexneakameni/language_detection")
97
+
98
+ language_detection = pipeline("text-classification", model=model, tokenizer=tokenizer)
99
+
100
+ text = "Hello world!"
101
+ predictions = language_detection(text)
102
+ print(predictions)
103
+ ```
104
+
105
+ This will output the predicted language code or label with the corresponding confidence score.
106
+
107
+ ---
108
+
109
+ **Note**: The model’s performance may vary depending on text length, language variety, and domain-specific vocabulary. Always validate results against your own datasets for critical applications.
110
+
111
+ For more information, see the [repository documentation](https://github.com/KameniAlexNea/learning_language).
112
+
113
+ Thank you for using this model—feedback and contributions are welcome!
config.json ADDED
@@ -0,0 +1,433 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "_name_or_path": "data/results/checkpoint-76000",
3
+ "architectures": [
4
+ "BertForSequenceClassification"
5
+ ],
6
+ "attention_probs_dropout_prob": 0.1,
7
+ "classifier_dropout": null,
8
+ "gradient_checkpointing": false,
9
+ "hidden_act": "gelu",
10
+ "hidden_dropout_prob": 0.1,
11
+ "hidden_size": 384,
12
+ "id2label": {
13
+ "0": "lit_Latn",
14
+ "1": "fon_Latn",
15
+ "2": "kin_Latn",
16
+ "3": "khm_Khmr",
17
+ "4": "bjn_Latn",
18
+ "5": "prs_Arab",
19
+ "6": "wol_Latn",
20
+ "7": "run_Latn",
21
+ "8": "eng_Latn",
22
+ "9": "gla_Latn",
23
+ "10": "lvs_Latn",
24
+ "11": "nya_Latn",
25
+ "12": "kac_Latn",
26
+ "13": "lua_Latn",
27
+ "14": "tuk_Latn",
28
+ "15": "tpi_Latn",
29
+ "16": "grn_Latn",
30
+ "17": "xho_Latn",
31
+ "18": "bam_Latn",
32
+ "19": "mri_Latn",
33
+ "20": "san_Deva",
34
+ "21": "isl_Latn",
35
+ "22": "kas_Deva",
36
+ "23": "bel_Cyrl",
37
+ "24": "heb_Hebr",
38
+ "25": "zho_Hant",
39
+ "26": "bak_Cyrl",
40
+ "27": "fra_Latn",
41
+ "28": "por_Latn",
42
+ "29": "ukr_Cyrl",
43
+ "30": "umb_Latn",
44
+ "31": "kan_Knda",
45
+ "32": "smo_Latn",
46
+ "33": "als_Latn",
47
+ "34": "kbp_Latn",
48
+ "35": "lin_Latn",
49
+ "36": "urd_Arab",
50
+ "37": "yor_Latn",
51
+ "38": "azb_Arab",
52
+ "39": "ltz_Latn",
53
+ "40": "twi_Latn",
54
+ "41": "hin_Deva",
55
+ "42": "tgl_Latn",
56
+ "43": "asm_Beng",
57
+ "44": "gaz_Latn",
58
+ "45": "ell_Grek",
59
+ "46": "taq_Tfng",
60
+ "47": "nso_Latn",
61
+ "48": "dan_Latn",
62
+ "49": "pes_Arab",
63
+ "50": "pan_Guru",
64
+ "51": "war_Latn",
65
+ "52": "mar_Deva",
66
+ "53": "mni_Beng",
67
+ "54": "acm_Arab",
68
+ "55": "srd_Latn",
69
+ "56": "vec_Latn",
70
+ "57": "ory_Orya",
71
+ "58": "lug_Latn",
72
+ "59": "ltg_Latn",
73
+ "60": "guj_Gujr",
74
+ "61": "ita_Latn",
75
+ "62": "swe_Latn",
76
+ "63": "cjk_Latn",
77
+ "64": "ace_Latn",
78
+ "65": "taq_Latn",
79
+ "66": "cat_Latn",
80
+ "67": "zsm_Latn",
81
+ "68": "hun_Latn",
82
+ "69": "kaz_Cyrl",
83
+ "70": "pol_Latn",
84
+ "71": "ban_Latn",
85
+ "72": "nus_Latn",
86
+ "73": "acq_Arab",
87
+ "74": "aeb_Arab",
88
+ "75": "spa_Latn",
89
+ "76": "slk_Latn",
90
+ "77": "hrv_Latn",
91
+ "78": "crh_Latn",
92
+ "79": "tur_Latn",
93
+ "80": "bos_Latn",
94
+ "81": "ssw_Latn",
95
+ "82": "kik_Latn",
96
+ "83": "ydd_Hebr",
97
+ "84": "snd_Arab",
98
+ "85": "hau_Latn",
99
+ "86": "tam_Taml",
100
+ "87": "plt_Latn",
101
+ "88": "kmr_Latn",
102
+ "89": "ace_Arab",
103
+ "90": "mkd_Cyrl",
104
+ "91": "lij_Latn",
105
+ "92": "dyu_Latn",
106
+ "93": "mos_Latn",
107
+ "94": "ayr_Latn",
108
+ "95": "ast_Latn",
109
+ "96": "fij_Latn",
110
+ "97": "lmo_Latn",
111
+ "98": "zho_Hans",
112
+ "99": "nob_Latn",
113
+ "100": "hye_Armn",
114
+ "101": "amh_Ethi",
115
+ "102": "jav_Latn",
116
+ "103": "sag_Latn",
117
+ "104": "mai_Deva",
118
+ "105": "lao_Laoo",
119
+ "106": "uzn_Latn",
120
+ "107": "mya_Mymr",
121
+ "108": "fin_Latn",
122
+ "109": "knc_Latn",
123
+ "110": "tat_Cyrl",
124
+ "111": "ajp_Arab",
125
+ "112": "dzo_Tibt",
126
+ "113": "pag_Latn",
127
+ "114": "kir_Cyrl",
128
+ "115": "sna_Latn",
129
+ "116": "zul_Latn",
130
+ "117": "kab_Latn",
131
+ "118": "fur_Latn",
132
+ "119": "ckb_Arab",
133
+ "120": "vie_Latn",
134
+ "121": "mal_Mlym",
135
+ "122": "bem_Latn",
136
+ "123": "som_Latn",
137
+ "124": "ars_Arab",
138
+ "125": "szl_Latn",
139
+ "126": "tgk_Cyrl",
140
+ "127": "tel_Telu",
141
+ "128": "quy_Latn",
142
+ "129": "deu_Latn",
143
+ "130": "bjn_Arab",
144
+ "131": "azj_Latn",
145
+ "132": "eus_Latn",
146
+ "133": "ces_Latn",
147
+ "134": "nld_Latn",
148
+ "135": "shn_Mymr",
149
+ "136": "bul_Cyrl",
150
+ "137": "kam_Latn",
151
+ "138": "kmb_Latn",
152
+ "139": "ron_Latn",
153
+ "140": "bho_Deva",
154
+ "141": "glg_Latn",
155
+ "142": "awa_Deva",
156
+ "143": "tha_Thai",
157
+ "144": "lim_Latn",
158
+ "145": "hat_Latn",
159
+ "146": "mag_Deva",
160
+ "147": "kon_Latn",
161
+ "148": "pbt_Arab",
162
+ "149": "kat_Geor",
163
+ "150": "khk_Cyrl",
164
+ "151": "arb_Arab",
165
+ "152": "knc_Arab",
166
+ "153": "kor_Hang",
167
+ "154": "oci_Latn",
168
+ "155": "lus_Latn",
169
+ "156": "ary_Arab",
170
+ "157": "epo_Latn",
171
+ "158": "pap_Latn",
172
+ "159": "ibo_Latn",
173
+ "160": "fao_Latn",
174
+ "161": "ben_Beng",
175
+ "162": "yue_Hant",
176
+ "163": "ceb_Latn",
177
+ "164": "luo_Latn",
178
+ "165": "srp_Cyrl",
179
+ "166": "ind_Latn",
180
+ "167": "slv_Latn",
181
+ "168": "min_Latn",
182
+ "169": "scn_Latn",
183
+ "170": "apc_Arab",
184
+ "171": "sin_Sinh",
185
+ "172": "mlt_Latn",
186
+ "173": "kea_Latn",
187
+ "174": "uig_Arab",
188
+ "175": "npi_Deva",
189
+ "176": "kas_Arab",
190
+ "177": "bug_Latn",
191
+ "178": "hne_Deva",
192
+ "179": "sat_Olck",
193
+ "180": "swh_Latn",
194
+ "181": "tso_Latn",
195
+ "182": "nno_Latn",
196
+ "183": "rus_Cyrl",
197
+ "184": "dik_Latn",
198
+ "185": "sun_Latn",
199
+ "186": "afr_Latn",
200
+ "187": "arz_Arab",
201
+ "188": "gle_Latn",
202
+ "189": "sot_Latn",
203
+ "190": "ewe_Latn",
204
+ "191": "fuv_Latn",
205
+ "192": "tum_Latn",
206
+ "193": "ilo_Latn",
207
+ "194": "cym_Latn",
208
+ "195": "tir_Ethi",
209
+ "196": "tzm_Tfng",
210
+ "197": "bod_Tibt",
211
+ "198": "tsn_Latn",
212
+ "199": "est_Latn",
213
+ "200": "jpn_Jpan"
214
+ },
215
+ "initializer_range": 0.02,
216
+ "intermediate_size": 768,
217
+ "label2id": {
218
+ "ace_Arab": 89,
219
+ "ace_Latn": 64,
220
+ "acm_Arab": 54,
221
+ "acq_Arab": 73,
222
+ "aeb_Arab": 74,
223
+ "afr_Latn": 186,
224
+ "ajp_Arab": 111,
225
+ "als_Latn": 33,
226
+ "amh_Ethi": 101,
227
+ "apc_Arab": 170,
228
+ "arb_Arab": 151,
229
+ "ars_Arab": 124,
230
+ "ary_Arab": 156,
231
+ "arz_Arab": 187,
232
+ "asm_Beng": 43,
233
+ "ast_Latn": 95,
234
+ "awa_Deva": 142,
235
+ "ayr_Latn": 94,
236
+ "azb_Arab": 38,
237
+ "azj_Latn": 131,
238
+ "bak_Cyrl": 26,
239
+ "bam_Latn": 18,
240
+ "ban_Latn": 71,
241
+ "bel_Cyrl": 23,
242
+ "bem_Latn": 122,
243
+ "ben_Beng": 161,
244
+ "bho_Deva": 140,
245
+ "bjn_Arab": 130,
246
+ "bjn_Latn": 4,
247
+ "bod_Tibt": 197,
248
+ "bos_Latn": 80,
249
+ "bug_Latn": 177,
250
+ "bul_Cyrl": 136,
251
+ "cat_Latn": 66,
252
+ "ceb_Latn": 163,
253
+ "ces_Latn": 133,
254
+ "cjk_Latn": 63,
255
+ "ckb_Arab": 119,
256
+ "crh_Latn": 78,
257
+ "cym_Latn": 194,
258
+ "dan_Latn": 48,
259
+ "deu_Latn": 129,
260
+ "dik_Latn": 184,
261
+ "dyu_Latn": 92,
262
+ "dzo_Tibt": 112,
263
+ "ell_Grek": 45,
264
+ "eng_Latn": 8,
265
+ "epo_Latn": 157,
266
+ "est_Latn": 199,
267
+ "eus_Latn": 132,
268
+ "ewe_Latn": 190,
269
+ "fao_Latn": 160,
270
+ "fij_Latn": 96,
271
+ "fin_Latn": 108,
272
+ "fon_Latn": 1,
273
+ "fra_Latn": 27,
274
+ "fur_Latn": 118,
275
+ "fuv_Latn": 191,
276
+ "gaz_Latn": 44,
277
+ "gla_Latn": 9,
278
+ "gle_Latn": 188,
279
+ "glg_Latn": 141,
280
+ "grn_Latn": 16,
281
+ "guj_Gujr": 60,
282
+ "hat_Latn": 145,
283
+ "hau_Latn": 85,
284
+ "heb_Hebr": 24,
285
+ "hin_Deva": 41,
286
+ "hne_Deva": 178,
287
+ "hrv_Latn": 77,
288
+ "hun_Latn": 68,
289
+ "hye_Armn": 100,
290
+ "ibo_Latn": 159,
291
+ "ilo_Latn": 193,
292
+ "ind_Latn": 166,
293
+ "isl_Latn": 21,
294
+ "ita_Latn": 61,
295
+ "jav_Latn": 102,
296
+ "jpn_Jpan": 200,
297
+ "kab_Latn": 117,
298
+ "kac_Latn": 12,
299
+ "kam_Latn": 137,
300
+ "kan_Knda": 31,
301
+ "kas_Arab": 176,
302
+ "kas_Deva": 22,
303
+ "kat_Geor": 149,
304
+ "kaz_Cyrl": 69,
305
+ "kbp_Latn": 34,
306
+ "kea_Latn": 173,
307
+ "khk_Cyrl": 150,
308
+ "khm_Khmr": 3,
309
+ "kik_Latn": 82,
310
+ "kin_Latn": 2,
311
+ "kir_Cyrl": 114,
312
+ "kmb_Latn": 138,
313
+ "kmr_Latn": 88,
314
+ "knc_Arab": 152,
315
+ "knc_Latn": 109,
316
+ "kon_Latn": 147,
317
+ "kor_Hang": 153,
318
+ "lao_Laoo": 105,
319
+ "lij_Latn": 91,
320
+ "lim_Latn": 144,
321
+ "lin_Latn": 35,
322
+ "lit_Latn": 0,
323
+ "lmo_Latn": 97,
324
+ "ltg_Latn": 59,
325
+ "ltz_Latn": 39,
326
+ "lua_Latn": 13,
327
+ "lug_Latn": 58,
328
+ "luo_Latn": 164,
329
+ "lus_Latn": 155,
330
+ "lvs_Latn": 10,
331
+ "mag_Deva": 146,
332
+ "mai_Deva": 104,
333
+ "mal_Mlym": 121,
334
+ "mar_Deva": 52,
335
+ "min_Latn": 168,
336
+ "mkd_Cyrl": 90,
337
+ "mlt_Latn": 172,
338
+ "mni_Beng": 53,
339
+ "mos_Latn": 93,
340
+ "mri_Latn": 19,
341
+ "mya_Mymr": 107,
342
+ "nld_Latn": 134,
343
+ "nno_Latn": 182,
344
+ "nob_Latn": 99,
345
+ "npi_Deva": 175,
346
+ "nso_Latn": 47,
347
+ "nus_Latn": 72,
348
+ "nya_Latn": 11,
349
+ "oci_Latn": 154,
350
+ "ory_Orya": 57,
351
+ "pag_Latn": 113,
352
+ "pan_Guru": 50,
353
+ "pap_Latn": 158,
354
+ "pbt_Arab": 148,
355
+ "pes_Arab": 49,
356
+ "plt_Latn": 87,
357
+ "pol_Latn": 70,
358
+ "por_Latn": 28,
359
+ "prs_Arab": 5,
360
+ "quy_Latn": 128,
361
+ "ron_Latn": 139,
362
+ "run_Latn": 7,
363
+ "rus_Cyrl": 183,
364
+ "sag_Latn": 103,
365
+ "san_Deva": 20,
366
+ "sat_Olck": 179,
367
+ "scn_Latn": 169,
368
+ "shn_Mymr": 135,
369
+ "sin_Sinh": 171,
370
+ "slk_Latn": 76,
371
+ "slv_Latn": 167,
372
+ "smo_Latn": 32,
373
+ "sna_Latn": 115,
374
+ "snd_Arab": 84,
375
+ "som_Latn": 123,
376
+ "sot_Latn": 189,
377
+ "spa_Latn": 75,
378
+ "srd_Latn": 55,
379
+ "srp_Cyrl": 165,
380
+ "ssw_Latn": 81,
381
+ "sun_Latn": 185,
382
+ "swe_Latn": 62,
383
+ "swh_Latn": 180,
384
+ "szl_Latn": 125,
385
+ "tam_Taml": 86,
386
+ "taq_Latn": 65,
387
+ "taq_Tfng": 46,
388
+ "tat_Cyrl": 110,
389
+ "tel_Telu": 127,
390
+ "tgk_Cyrl": 126,
391
+ "tgl_Latn": 42,
392
+ "tha_Thai": 143,
393
+ "tir_Ethi": 195,
394
+ "tpi_Latn": 15,
395
+ "tsn_Latn": 198,
396
+ "tso_Latn": 181,
397
+ "tuk_Latn": 14,
398
+ "tum_Latn": 192,
399
+ "tur_Latn": 79,
400
+ "twi_Latn": 40,
401
+ "tzm_Tfng": 196,
402
+ "uig_Arab": 174,
403
+ "ukr_Cyrl": 29,
404
+ "umb_Latn": 30,
405
+ "urd_Arab": 36,
406
+ "uzn_Latn": 106,
407
+ "vec_Latn": 56,
408
+ "vie_Latn": 120,
409
+ "war_Latn": 51,
410
+ "wol_Latn": 6,
411
+ "xho_Latn": 17,
412
+ "ydd_Hebr": 83,
413
+ "yor_Latn": 37,
414
+ "yue_Hant": 162,
415
+ "zho_Hans": 98,
416
+ "zho_Hant": 25,
417
+ "zsm_Latn": 67,
418
+ "zul_Latn": 116
419
+ },
420
+ "layer_norm_eps": 1e-12,
421
+ "max_position_embeddings": 512,
422
+ "model_type": "bert",
423
+ "num_attention_heads": 6,
424
+ "num_hidden_layers": 4,
425
+ "pad_token_id": 3,
426
+ "position_embedding_type": "absolute",
427
+ "problem_type": "single_label_classification",
428
+ "torch_dtype": "float32",
429
+ "transformers_version": "4.48.3",
430
+ "type_vocab_size": 2,
431
+ "use_cache": true,
432
+ "vocab_size": 50257
433
+ }
language_detection.onnx ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:e4ccd30df1196c19d4b227bd82ca4d79aca9cd0c74c9622e3ca80288ff9bb304
3
+ size 97945176
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:ec3137634f58a55ae6127d61d12d4aa05c92380852909c1160e03f82f51a8a68
3
+ size 97838484
special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
to_onnx.py ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import torch
3
+ from transformers import AutoModelForSequenceClassification, AutoTokenizer
4
+ from onnxruntime.quantization import quantize_dynamic, quantize_static, QuantType
5
+ from onnxruntime.quantization.calibrate import CalibrationDataReader
6
+ import onnx
7
+ import time
8
+ import numpy as np
9
+
10
+ def ensure_directory(path):
11
+ """Create directory if it doesn't exist"""
12
+ abs_path = os.path.abspath(path)
13
+ if not os.path.exists(abs_path):
14
+ os.makedirs(abs_path)
15
+ print(f"Created directory: {abs_path}")
16
+ return abs_path
17
+
18
+ def verify_file_exists(file_path, timeout=5):
19
+ """Verify that a file exists and is not empty"""
20
+ start_time = time.time()
21
+ while time.time() - start_time < timeout:
22
+ if os.path.exists(file_path) and os.path.getsize(file_path) > 0:
23
+ return True
24
+ time.sleep(0.1)
25
+ return False
26
+
27
+ def export_to_onnx(model, tokenizer, save_path):
28
+ """Export model to ONNX format"""
29
+ try:
30
+ # Create a dummy input for the model
31
+ dummy_input = tokenizer("This is a sample input", return_tensors="pt")
32
+
33
+ # Export the model to ONNX
34
+ torch.onnx.export(
35
+ model,
36
+ (dummy_input["input_ids"], dummy_input["attention_mask"]),
37
+ save_path,
38
+ opset_version=14,
39
+ input_names=["input_ids", "attention_mask"],
40
+ output_names=["output"],
41
+ dynamic_axes={
42
+ "input_ids": {0: "batch_size"},
43
+ "attention_mask": {0: "batch_size"},
44
+ "output": {0: "batch_size"}
45
+ }
46
+ )
47
+
48
+ # Verify the file was created
49
+ if verify_file_exists(save_path):
50
+ print(f"Successfully exported ONNX model to {save_path}")
51
+ return True
52
+ else:
53
+ print(f"Failed to verify ONNX model at {save_path}")
54
+ return False
55
+ except Exception as e:
56
+ print(f"Error exporting to ONNX: {str(e)}")
57
+ return False
58
+
59
+ def create_calibration_dataset(tokenizer, max_length=512):
60
+ """Generate calibration dataset for static quantization with padding"""
61
+ samples = [
62
+ "This is an English sentence.",
63
+ "Dies ist ein deutscher Satz.",
64
+ "C'est une phrase française.",
65
+ "Esta es una frase en español.",
66
+ "这是一个中文句子。",
67
+ "これは日本語の文章です。"
68
+ ]
69
+
70
+ # Tokenize with padding and truncation
71
+ encoded_samples = []
72
+ for text in samples:
73
+ encoded = tokenizer(
74
+ text,
75
+ padding='max_length',
76
+ max_length=max_length,
77
+ truncation=True,
78
+ return_tensors="pt"
79
+ )
80
+ encoded_samples.append({
81
+ 'input_ids': encoded['input_ids'],
82
+ 'attention_mask': encoded['attention_mask']
83
+ })
84
+
85
+ return encoded_samples
86
+
87
+ class CalibrationLoader(CalibrationDataReader):
88
+ def __init__(self, calibration_data):
89
+ self.calibration_data = calibration_data
90
+ self.current_index = 0
91
+
92
+ def get_next(self):
93
+ if self.current_index >= len(self.calibration_data):
94
+ return None
95
+
96
+ current_data = self.calibration_data[self.current_index]
97
+ self.current_index += 1
98
+
99
+ # Ensure we're returning numpy arrays with the correct shape
100
+ return {
101
+ 'input_ids': current_data['input_ids'].numpy(),
102
+ 'attention_mask': current_data['attention_mask'].numpy()
103
+ }
104
+
105
+ def rewind(self):
106
+ self.current_index = 0
107
+
108
+ def export_to_onnx(model, tokenizer, save_path, max_length=512):
109
+ """Export model to ONNX format with fixed dimensions"""
110
+ try:
111
+ # Create a dummy input with fixed dimensions
112
+ dummy_input = tokenizer(
113
+ "This is a sample input",
114
+ padding='max_length',
115
+ max_length=max_length,
116
+ truncation=True,
117
+ return_tensors="pt"
118
+ )
119
+
120
+ # Export the model to ONNX
121
+ torch.onnx.export(
122
+ model,
123
+ (dummy_input["input_ids"], dummy_input["attention_mask"]),
124
+ save_path,
125
+ opset_version=14,
126
+ input_names=["input_ids", "attention_mask"],
127
+ output_names=["output"],
128
+ dynamic_axes={
129
+ "input_ids": {0: "batch_size"},
130
+ "attention_mask": {0: "batch_size"}
131
+ }
132
+ )
133
+
134
+ if verify_file_exists(save_path):
135
+ print(f"Successfully exported ONNX model to {save_path}")
136
+ return True
137
+ else:
138
+ print(f"Failed to verify ONNX model at {save_path}")
139
+ return False
140
+ except Exception as e:
141
+ print(f"Error exporting to ONNX: {str(e)}")
142
+ return False
143
+
144
+ def quantize_model(base_onnx_path, onnx_dir, config_name, calibration_dataset=None):
145
+ """
146
+ Quantize ONNX model using either dynamic or static quantization.
147
+
148
+ Args:
149
+ base_onnx_path (str): Path to the base ONNX model
150
+ onnx_dir (str): Directory to save quantized models
151
+ config_name (str): Type of quantization ('dynamic' or 'static')
152
+ calibration_dataset (list, optional): Dataset for static quantization calibration
153
+ """
154
+ try:
155
+ quantized_model_path = os.path.join(onnx_dir, f"model_{config_name}_quantized.onnx")
156
+
157
+ if config_name == "dynamic":
158
+ print(f"\nPerforming dynamic quantization...")
159
+ quantize_dynamic(
160
+ model_input=base_onnx_path,
161
+ model_output=quantized_model_path,
162
+ weight_type=QuantType.QUInt8
163
+ )
164
+
165
+ elif config_name == "static" and calibration_dataset is not None:
166
+ print(f"\nPerforming static quantization...")
167
+ calibration_loader = CalibrationLoader(calibration_dataset)
168
+ quantize_static(
169
+ model_input=base_onnx_path,
170
+ model_output=quantized_model_path,
171
+ calibration_data_reader=calibration_loader,
172
+ quant_format=QuantType.QUInt8
173
+ )
174
+
175
+ else:
176
+ print(f"Invalid quantization configuration: {config_name}")
177
+ return False
178
+
179
+ # Verify the quantized model exists
180
+ if verify_file_exists(quantized_model_path):
181
+ print(f"Successfully created {config_name} quantized model at {quantized_model_path}")
182
+
183
+ # Print file sizes for comparison
184
+ base_size = os.path.getsize(base_onnx_path) / (1024 * 1024) # Convert to MB
185
+ quantized_size = os.path.getsize(quantized_model_path) / (1024 * 1024) # Convert to MB
186
+
187
+ print(f"Original model size: {base_size:.2f} MB")
188
+ print(f"Quantized model size: {quantized_size:.2f} MB")
189
+ print(f"Size reduction: {((base_size - quantized_size) / base_size * 100):.2f}%")
190
+
191
+ return True
192
+ else:
193
+ print(f"Failed to verify quantized model at {quantized_model_path}")
194
+ return False
195
+
196
+ except Exception as e:
197
+ print(f"Error during {config_name} quantization: {str(e)}")
198
+ return False
199
+
200
+
201
+ def main():
202
+ # Get absolute paths
203
+ current_dir = os.path.abspath(os.getcwd())
204
+ onnx_dir = ensure_directory(os.path.join(current_dir, "onnx"))
205
+ base_onnx_path = os.path.join(onnx_dir, "model.onnx")
206
+
207
+ print(f"Working directory: {current_dir}")
208
+ print(f"ONNX directory: {onnx_dir}")
209
+ print(f"Base ONNX model path: {base_onnx_path}")
210
+
211
+ # Step 1: Load model and tokenizer
212
+ print("\nLoading model and tokenizer...")
213
+ model_name = "alexneakameni/language_detection"
214
+ model = AutoModelForSequenceClassification.from_pretrained(model_name)
215
+ tokenizer = AutoTokenizer.from_pretrained(model_name)
216
+
217
+ # Get the model's default max_length
218
+ max_length = tokenizer.model_max_length
219
+
220
+ # Step 2: Export base ONNX model
221
+ if not export_to_onnx(model, tokenizer, base_onnx_path, max_length):
222
+ print("Failed to export base ONNX model. Exiting.")
223
+ return
224
+
225
+ # Verify the ONNX model
226
+ try:
227
+ print(f"Verifying ONNX model at: {base_onnx_path}")
228
+ onnx_model = onnx.load(base_onnx_path)
229
+ print("Successfully verified ONNX model")
230
+ except Exception as e:
231
+ print(f"Error verifying ONNX model: {str(e)}")
232
+ return
233
+
234
+ # Step 3: Create calibration dataset
235
+ calibration_dataset = create_calibration_dataset(tokenizer, max_length)
236
+
237
+ # Step 4: Create quantized versions
238
+ print("\nCreating quantized versions...")
239
+
240
+ # Dynamic quantization
241
+ quantize_model(
242
+ base_onnx_path=base_onnx_path,
243
+ onnx_dir=onnx_dir,
244
+ config_name="dynamic"
245
+ )
246
+
247
+ # Static quantization
248
+ quantize_model(
249
+ base_onnx_path=base_onnx_path,
250
+ onnx_dir=onnx_dir,
251
+ config_name="static",
252
+ calibration_dataset=calibration_dataset
253
+ )
254
+
255
+ if __name__ == "__main__":
256
+ main()
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[UNK]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "1": {
12
+ "content": "[CLS]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "2": {
20
+ "content": "[SEP]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "3": {
28
+ "content": "[PAD]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "4": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": false,
45
+ "cls_token": "[CLS]",
46
+ "do_lower_case": true,
47
+ "extra_special_tokens": {},
48
+ "mask_token": "[MASK]",
49
+ "model_max_length": 512,
50
+ "pad_token": "[PAD]",
51
+ "sep_token": "[SEP]",
52
+ "strip_accents": null,
53
+ "tokenize_chinese_chars": true,
54
+ "tokenizer_class": "BertTokenizer",
55
+ "unk_token": "[UNK]"
56
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff