millid 0.2.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,26 @@
1
+ .venv/
2
+ models/
3
+ data/
4
+ # ...except the package's own data: the frozen script table.
5
+ !src/millid/data/
6
+ __pycache__/
7
+ # Training outputs and local runs: models and reports live on Hugging Face, runs in W&B.
8
+ wandb/
9
+ leaderboard/
10
+ millid-out/
11
+ millid-eval/
12
+ millid-onnx/
13
+ millid-smoke*/
14
+ *.log
15
+ .pytest_cache/
16
+ .ruff_cache/
17
+ dist/
18
+ build/
19
+ *.egg-info/
20
+
21
+ # JavaScript package
22
+ js/node_modules/
23
+
24
+ # Local secrets (NPM_API_TOKEN and the like): never committed.
25
+ .env
26
+ .env.*
millid-0.2.0/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
millid-0.2.0/PKG-INFO ADDED
@@ -0,0 +1,225 @@
1
+ Metadata-Version: 2.5
2
+ Name: millid
3
+ Version: 0.2.0
4
+ Summary: Detect the language of any text, offline: 1,550 languages in 56 scripts, many of them low-resource, with ONNX Runtime (no PyTorch, no server).
5
+ Project-URL: Homepage, https://huggingface.co/baobabtech/millid-tiny-v1.5
6
+ Project-URL: Documentation, https://github.com/baobab-tech/millid#readme
7
+ Project-URL: Repository, https://github.com/baobab-tech/millid
8
+ Project-URL: Issues, https://github.com/baobab-tech/millid/issues
9
+ Project-URL: Leaderboard, https://huggingface.co/baobabtech/millid
10
+ Project-URL: JavaScript package, https://www.npmjs.com/package/@baobabtechai/millid
11
+ Author: Baobab Tech
12
+ License-Expression: Apache-2.0
13
+ License-File: LICENSE
14
+ Keywords: african-languages,langid,language-detection,language-identification,low-resource-languages,multilingual,nlp,onnx,onnxruntime
15
+ Classifier: Development Status :: 4 - Beta
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: Operating System :: OS Independent
19
+ Classifier: Programming Language :: Python :: 3
20
+ Classifier: Programming Language :: Python :: 3.10
21
+ Classifier: Programming Language :: Python :: 3.11
22
+ Classifier: Programming Language :: Python :: 3.12
23
+ Classifier: Programming Language :: Python :: 3.13
24
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
25
+ Classifier: Topic :: Text Processing :: Linguistic
26
+ Requires-Python: >=3.10
27
+ Requires-Dist: huggingface-hub>=0.25
28
+ Requires-Dist: numpy>=1.24
29
+ Provides-Extra: cpu
30
+ Requires-Dist: onnxruntime>=1.18; extra == 'cpu'
31
+ Provides-Extra: data
32
+ Requires-Dist: datasets; extra == 'data'
33
+ Requires-Dist: fasttext-wheel; extra == 'data'
34
+ Requires-Dist: langcodes; extra == 'data'
35
+ Requires-Dist: pandas; extra == 'data'
36
+ Requires-Dist: pyarrow; extra == 'data'
37
+ Requires-Dist: pymupdf; extra == 'data'
38
+ Provides-Extra: dev
39
+ Requires-Dist: httpx; extra == 'dev'
40
+ Requires-Dist: pytest-asyncio>=0.23; extra == 'dev'
41
+ Requires-Dist: pytest>=8; extra == 'dev'
42
+ Requires-Dist: ruff; extra == 'dev'
43
+ Provides-Extra: gpu
44
+ Requires-Dist: onnxruntime-gpu[cuda,cudnn]>=1.20; extra == 'gpu'
45
+ Provides-Extra: server
46
+ Requires-Dist: fastapi>=0.110; extra == 'server'
47
+ Requires-Dist: pydantic>=2.6; extra == 'server'
48
+ Requires-Dist: uvicorn[standard]>=0.29; extra == 'server'
49
+ Provides-Extra: train
50
+ Requires-Dist: fasttext-wheel; extra == 'train'
51
+ Requires-Dist: onnx>=1.16; extra == 'train'
52
+ Requires-Dist: onnxscript; extra == 'train'
53
+ Requires-Dist: pandas; extra == 'train'
54
+ Requires-Dist: pyarrow; extra == 'train'
55
+ Requires-Dist: safetensors; extra == 'train'
56
+ Requires-Dist: torch>=2.4; extra == 'train'
57
+ Requires-Dist: tyro>=0.8; extra == 'train'
58
+ Requires-Dist: wandb; extra == 'train'
59
+ Description-Content-Type: text/markdown
60
+
61
+ # MillID
62
+
63
+ **Detect the language of any text, offline, in Python.** MillID recognises 1,550 languages written in 56 scripts, including hundreds of African, Asian and Indigenous languages that common language detectors do not cover. It runs on a CPU with no PyTorch and no server; the smallest model is 11 MB.
64
+
65
+ ```python
66
+ from millid import MillID
67
+
68
+ model = MillID.from_pretrained("tiny-v1.5")
69
+ model.predict("Mo fẹ́ràn láti ka ìwé ní àkókò òmìnira mi.", k=1)
70
+ # [Prediction(label='yor_Latn', language='Yoruba', probability=0.973627)]
71
+ ```
72
+
73
+ ## What is MillID?
74
+
75
+ MillID (MultiLingual Language IDentifier) is an open language identification model built by [Baobab Tech](https://huggingface.co/baobabtech). Give it a piece of text, from a short sentence to a whole document, and it tells you which language the text is written in, with a probability for each answer. Every answer names a language and its script, so Serbian in Cyrillic (`srp_Cyrl`) and Serbian in Latin (`srp_Latn`) are told apart.
76
+
77
+ Developers use it to:
78
+
79
+ - send each document to the right translation, OCR or speech model;
80
+ - sort or filter text by language when building a dataset;
81
+ - show an interface in the reader's language, or flag text in an unexpected one;
82
+ - count which languages a collection of documents contains.
83
+
84
+ ## What this package does
85
+
86
+ This package runs MillID on your own machine. The first time you load a model it downloads the model's files from the [Hugging Face Hub](https://huggingface.co/baobabtech/millid-tiny-v1.5) into the Hugging Face cache; after that it works offline. The model runs in [ONNX Runtime](https://onnxruntime.ai), on a CPU or an NVIDIA GPU, with NumPy for the text encoding. There is no API to call and no PyTorch to install, and the text you identify never leaves your machine.
87
+
88
+ It also gives you a command-line tool and, if you want one, an HTTP service. The JavaScript package [`@baobabtechai/millid`](https://www.npmjs.com/package/@baobabtechai/millid) gives the same answers in Node.js and the browser.
89
+
90
+ ## Install
91
+
92
+ ```bash
93
+ pip install "millid[cpu]" # ONNX Runtime for the CPU
94
+ pip install "millid[gpu]" # ONNX Runtime for NVIDIA GPUs (CUDA 12, its libraries from pip)
95
+ pip install "millid[cpu,server]" # and the HTTP service
96
+ ```
97
+
98
+ Python 3.10 or later. ONNX Runtime comes as an extra because its CPU and GPU builds cannot be installed side by side: choose one.
99
+
100
+ ## Quick start
101
+
102
+ ```python
103
+ from millid import MillID
104
+
105
+ model = MillID.from_pretrained("tiny-v1.5") # downloads 11 MB once
106
+
107
+ model.predict("Je voudrais réserver une table pour deux personnes ce soir.", k=2)
108
+ # [Prediction(label='fra_Latn', language='French', probability=0.954421),
109
+ # Prediction(label='frp_Latn', language='Arpitan', probability=0.001443)]
110
+
111
+ model.predict(["Lave men ou ak savon anvan ou manje.", "Ndimakonda kuwerenga mabuku nthawi yanga yaulere."], k=1)
112
+ # [[Prediction(label='hat_Latn', language='Haitian Creole', probability=0.978552)],
113
+ # [Prediction(label='nya_Latn', language='Nyanja', probability=0.940327)]]
114
+ ```
115
+
116
+ `predict(text, k)` returns the `k` most likely languages (3 by default), most likely first. Pass a list to identify many texts in one call: they are batched together, which is much faster than one call per text.
117
+
118
+ ## Command line
119
+
120
+ ```bash
121
+ millid predict "Lave men ou ak savon anvan ou manje." -k 2
122
+ # hat_Latn 0.979, crs_Latn 0.000 Lave men ou ak savon anvan ou manje.
123
+
124
+ cat sentences.txt | millid predict -k 1 # one text per line
125
+ millid predict --document < report.txt # the whole file as one document, as JSON
126
+ millid predict --model large-v1.5 --device cuda "..." # another model, on the GPU
127
+ ```
128
+
129
+ ## Choose a model
130
+
131
+ | Model | Download | Top-1 accuracy | Macro F1 | Best for |
132
+ |---|---|---|---|---|
133
+ | `tiny-v1.5` (default) | 11 MB | 95.9% | 0.955 | any CPU, laptops, small servers |
134
+ | `medium-v1` | 455 MB | 96.2% | 0.958 | servers |
135
+ | `large-v1.5` | 764 MB | 96.7% | 0.965 | servers with a GPU: the most accurate |
136
+
137
+ Measured on 240,241 test passages in 1,621 languages, from documents the models never saw in training. Macro F1 weighs every language equally, so it shows how well the small languages are served, not just the large ones. The [leaderboard](https://huggingface.co/baobabtech/millid) compares the three with GlotLID and Meta's fastText identifier, language by language.
138
+
139
+ `from_pretrained` also takes a Hugging Face repo id (`"baobabtech/millid-large-v1.5"`) or a local directory holding `config.json`, `labels.json` and `onnx/model.onnx`, and these options:
140
+
141
+ ```python
142
+ model = MillID.from_pretrained(
143
+ "large-v1.5",
144
+ device="cuda", # "cpu" (the default), "cuda", or "auto": the GPU when there is one
145
+ threads=4, # CPU threads for ONNX Runtime; its default when omitted
146
+ batch_size=64, # texts per model call
147
+ )
148
+ ```
149
+
150
+ ## Reading the results
151
+
152
+ Each answer is a `Prediction` with three fields:
153
+
154
+ | Field | Example | Meaning |
155
+ |---|---|---|
156
+ | `label` | `'yor_Latn'` | the language's [ISO 639-3](https://iso639-3.sil.org) code and its [ISO 15924](https://www.unicode.org/iso15924/) script code |
157
+ | `language` | `'Yoruba'` | the language's English name |
158
+ | `probability` | `0.973627` | how likely this answer is, from 0 to 1; the answers for a text add up to at most 1 |
159
+
160
+ A few labels end in a script alone, such as `und_Tibt`: text in that script whose language MillID cannot name more precisely.
161
+
162
+ ## Long text and whole documents
163
+
164
+ `predict` reads the first 255 bytes of a text, about 250 characters of English or 85 of Hindi. For anything longer, use `identify_document`: it reads the whole text in windows, gives the document's languages weighted by how much of the text each one covers, and marks where each language runs.
165
+
166
+ ```python
167
+ doc = model.identify_document(long_text, k=2)
168
+ doc.languages
169
+ # [Prediction(label='eng_Latn', language='English', probability=0.576472),
170
+ # Prediction(label='fra_Latn', language='French', probability=0.381211)]
171
+ doc.segments
172
+ # [Segment(start=0, end=250, label='eng_Latn', language='English', probability=0.952214),
173
+ # Segment(start=251, end=414, label='fra_Latn', language='French', probability=0.96499)]
174
+ doc.windows, doc.characters
175
+ # (2, 414)
176
+ ```
177
+
178
+ `start` and `end` count characters in the text after normalisation (`millid.normalize(text)`), which collapses whitespace and applies Unicode NFKC.
179
+
180
+ ## An HTTP service
181
+
182
+ ```bash
183
+ millid serve --models "tiny-v1.5=baobabtech/millid-tiny-v1.5@cpu" --port 8050
184
+ curl -s localhost:8050/identify -H 'content-type: application/json' -d '{"text": "Lave men ou ak savon anvan ou manje.", "k": 1}'
185
+ # {"model": "tiny-v1.5", "results": [{"languages": [{"label": "hat_Latn", "language": "Haitian Creole", "probability": 0.978552}]}]}
186
+ ```
187
+
188
+ `POST /identify` takes `text` or `texts`, `k`, `model`, and `document: true` for a whole document; `GET /models` describes the loaded models and `GET /health` reports that the service is up. Requests that arrive together are answered in shared batches, so throughput grows with load. To serve MillID inside an application of your own, build on `millid.Registry` and `millid.MicroBatcher`.
189
+
190
+ ## Tips
191
+
192
+ - **Give it a sentence or more.** A word or a short greeting carries little evidence: "Bonjour tout le monde" alone comes back as `nrm_Latn` (Norman) rather than French. A full sentence or more gives much more reliable answers.
193
+ - **Treat a low probability as unsure.** When the first answer is below 0.5, look at the next ones too, or ask for more text.
194
+ - **Close languages share their answers.** Swahili and Congo Swahili, or Bosnian and Croatian, differ by a few words; a short passage of either often gets both in its top answers. Ask for `k=3` and decide with what you know about your text.
195
+ - **Pass the text as it is.** Accents, tone marks and the script are much of what the model reads: do not strip, transliterate or romanise it first.
196
+ - **One text alone and the same text in a batch can score a little differently.** A batch is lower-cased as one string; call `predict` with one text where scores must not depend on their neighbours.
197
+
198
+ ## API
199
+
200
+ | | |
201
+ |---|---|
202
+ | `MillID.from_pretrained(ref="tiny-v1.5", revision=None, token=None, **options)` | Load a model: `"tiny-v1.5"`, `"medium-v1"`, `"large-v1.5"`, a Hugging Face repo id, or a directory. `token` only for a private repo; options `device`, `threads`, `batch_size`, `gpu_mem_limit_mb` |
203
+ | `model.predict(text, k=3)` | The `k` most likely languages of a text, as `Prediction`s. A list of texts gives one list per text |
204
+ | `model.identify_document(text, k=3, max_windows=256)` | A text of any length, as a `DocumentResult`: `languages`, `segments`, `windows`, `characters` |
205
+ | `model.probabilities(texts)` | A NumPy array, one row per text, a probability for each label in `model.labels` |
206
+ | `millid.MODELS` | The released models by short name |
207
+ | `millid.normalize`, `millid.encode_batch` | The model's input, to run the ONNX file elsewhere |
208
+
209
+ `MillID` is `millid.LanguageIdentifier` under a shorter name. A model is safe to call from several threads at once.
210
+
211
+ ## How it works
212
+
213
+ MillID is a small transformer that reads text as UTF-8 bytes rather than words, so there is no vocabulary to outgrow: any script, even one it has rarely seen, is input it can read. Each byte is described by the byte itself, the Unicode script of its character and the character sequences ending there; the network turns a passage into one vector and scores it against a vector for every label, built from the label itself, its language (shared across scripts) and its script. The full design is in [the architecture document](https://github.com/baobab-tech/millid/blob/main/training/ARCHITECTURE.md), and each model's card on the Hub has its evaluation.
214
+
215
+ ## Links
216
+
217
+ - Models: [tiny v1.5](https://huggingface.co/baobabtech/millid-tiny-v1.5), [medium v1](https://huggingface.co/baobabtech/millid-medium-v1), [large v1.5](https://huggingface.co/baobabtech/millid-large-v1.5)
218
+ - Leaderboard: [baobabtech/millid](https://huggingface.co/baobabtech/millid)
219
+ - Dataset: [baobabtech/millid-dataset](https://huggingface.co/datasets/baobabtech/millid-dataset)
220
+ - JavaScript package: [`@baobabtechai/millid` on npm](https://www.npmjs.com/package/@baobabtechai/millid)
221
+ - Source, training code and issues: [github.com/baobab-tech/millid](https://github.com/baobab-tech/millid)
222
+
223
+ ## Licence
224
+
225
+ Apache-2.0, for the code and the model weights. Built by [Baobab Tech](https://huggingface.co/baobabtech).
@@ -0,0 +1,165 @@
1
+ # MillID
2
+
3
+ **Detect the language of any text, offline, in Python.** MillID recognises 1,550 languages written in 56 scripts, including hundreds of African, Asian and Indigenous languages that common language detectors do not cover. It runs on a CPU with no PyTorch and no server; the smallest model is 11 MB.
4
+
5
+ ```python
6
+ from millid import MillID
7
+
8
+ model = MillID.from_pretrained("tiny-v1.5")
9
+ model.predict("Mo fẹ́ràn láti ka ìwé ní àkókò òmìnira mi.", k=1)
10
+ # [Prediction(label='yor_Latn', language='Yoruba', probability=0.973627)]
11
+ ```
12
+
13
+ ## What is MillID?
14
+
15
+ MillID (MultiLingual Language IDentifier) is an open language identification model built by [Baobab Tech](https://huggingface.co/baobabtech). Give it a piece of text, from a short sentence to a whole document, and it tells you which language the text is written in, with a probability for each answer. Every answer names a language and its script, so Serbian in Cyrillic (`srp_Cyrl`) and Serbian in Latin (`srp_Latn`) are told apart.
16
+
17
+ Developers use it to:
18
+
19
+ - send each document to the right translation, OCR or speech model;
20
+ - sort or filter text by language when building a dataset;
21
+ - show an interface in the reader's language, or flag text in an unexpected one;
22
+ - count which languages a collection of documents contains.
23
+
24
+ ## What this package does
25
+
26
+ This package runs MillID on your own machine. The first time you load a model it downloads the model's files from the [Hugging Face Hub](https://huggingface.co/baobabtech/millid-tiny-v1.5) into the Hugging Face cache; after that it works offline. The model runs in [ONNX Runtime](https://onnxruntime.ai), on a CPU or an NVIDIA GPU, with NumPy for the text encoding. There is no API to call and no PyTorch to install, and the text you identify never leaves your machine.
27
+
28
+ It also gives you a command-line tool and, if you want one, an HTTP service. The JavaScript package [`@baobabtechai/millid`](https://www.npmjs.com/package/@baobabtechai/millid) gives the same answers in Node.js and the browser.
29
+
30
+ ## Install
31
+
32
+ ```bash
33
+ pip install "millid[cpu]" # ONNX Runtime for the CPU
34
+ pip install "millid[gpu]" # ONNX Runtime for NVIDIA GPUs (CUDA 12, its libraries from pip)
35
+ pip install "millid[cpu,server]" # and the HTTP service
36
+ ```
37
+
38
+ Python 3.10 or later. ONNX Runtime comes as an extra because its CPU and GPU builds cannot be installed side by side: choose one.
39
+
40
+ ## Quick start
41
+
42
+ ```python
43
+ from millid import MillID
44
+
45
+ model = MillID.from_pretrained("tiny-v1.5") # downloads 11 MB once
46
+
47
+ model.predict("Je voudrais réserver une table pour deux personnes ce soir.", k=2)
48
+ # [Prediction(label='fra_Latn', language='French', probability=0.954421),
49
+ # Prediction(label='frp_Latn', language='Arpitan', probability=0.001443)]
50
+
51
+ model.predict(["Lave men ou ak savon anvan ou manje.", "Ndimakonda kuwerenga mabuku nthawi yanga yaulere."], k=1)
52
+ # [[Prediction(label='hat_Latn', language='Haitian Creole', probability=0.978552)],
53
+ # [Prediction(label='nya_Latn', language='Nyanja', probability=0.940327)]]
54
+ ```
55
+
56
+ `predict(text, k)` returns the `k` most likely languages (3 by default), most likely first. Pass a list to identify many texts in one call: they are batched together, which is much faster than one call per text.
57
+
58
+ ## Command line
59
+
60
+ ```bash
61
+ millid predict "Lave men ou ak savon anvan ou manje." -k 2
62
+ # hat_Latn 0.979, crs_Latn 0.000 Lave men ou ak savon anvan ou manje.
63
+
64
+ cat sentences.txt | millid predict -k 1 # one text per line
65
+ millid predict --document < report.txt # the whole file as one document, as JSON
66
+ millid predict --model large-v1.5 --device cuda "..." # another model, on the GPU
67
+ ```
68
+
69
+ ## Choose a model
70
+
71
+ | Model | Download | Top-1 accuracy | Macro F1 | Best for |
72
+ |---|---|---|---|---|
73
+ | `tiny-v1.5` (default) | 11 MB | 95.9% | 0.955 | any CPU, laptops, small servers |
74
+ | `medium-v1` | 455 MB | 96.2% | 0.958 | servers |
75
+ | `large-v1.5` | 764 MB | 96.7% | 0.965 | servers with a GPU: the most accurate |
76
+
77
+ Measured on 240,241 test passages in 1,621 languages, from documents the models never saw in training. Macro F1 weighs every language equally, so it shows how well the small languages are served, not just the large ones. The [leaderboard](https://huggingface.co/baobabtech/millid) compares the three with GlotLID and Meta's fastText identifier, language by language.
78
+
79
+ `from_pretrained` also takes a Hugging Face repo id (`"baobabtech/millid-large-v1.5"`) or a local directory holding `config.json`, `labels.json` and `onnx/model.onnx`, and these options:
80
+
81
+ ```python
82
+ model = MillID.from_pretrained(
83
+ "large-v1.5",
84
+ device="cuda", # "cpu" (the default), "cuda", or "auto": the GPU when there is one
85
+ threads=4, # CPU threads for ONNX Runtime; its default when omitted
86
+ batch_size=64, # texts per model call
87
+ )
88
+ ```
89
+
90
+ ## Reading the results
91
+
92
+ Each answer is a `Prediction` with three fields:
93
+
94
+ | Field | Example | Meaning |
95
+ |---|---|---|
96
+ | `label` | `'yor_Latn'` | the language's [ISO 639-3](https://iso639-3.sil.org) code and its [ISO 15924](https://www.unicode.org/iso15924/) script code |
97
+ | `language` | `'Yoruba'` | the language's English name |
98
+ | `probability` | `0.973627` | how likely this answer is, from 0 to 1; the answers for a text add up to at most 1 |
99
+
100
+ A few labels end in a script alone, such as `und_Tibt`: text in that script whose language MillID cannot name more precisely.
101
+
102
+ ## Long text and whole documents
103
+
104
+ `predict` reads the first 255 bytes of a text, about 250 characters of English or 85 of Hindi. For anything longer, use `identify_document`: it reads the whole text in windows, gives the document's languages weighted by how much of the text each one covers, and marks where each language runs.
105
+
106
+ ```python
107
+ doc = model.identify_document(long_text, k=2)
108
+ doc.languages
109
+ # [Prediction(label='eng_Latn', language='English', probability=0.576472),
110
+ # Prediction(label='fra_Latn', language='French', probability=0.381211)]
111
+ doc.segments
112
+ # [Segment(start=0, end=250, label='eng_Latn', language='English', probability=0.952214),
113
+ # Segment(start=251, end=414, label='fra_Latn', language='French', probability=0.96499)]
114
+ doc.windows, doc.characters
115
+ # (2, 414)
116
+ ```
117
+
118
+ `start` and `end` count characters in the text after normalisation (`millid.normalize(text)`), which collapses whitespace and applies Unicode NFKC.
119
+
120
+ ## An HTTP service
121
+
122
+ ```bash
123
+ millid serve --models "tiny-v1.5=baobabtech/millid-tiny-v1.5@cpu" --port 8050
124
+ curl -s localhost:8050/identify -H 'content-type: application/json' -d '{"text": "Lave men ou ak savon anvan ou manje.", "k": 1}'
125
+ # {"model": "tiny-v1.5", "results": [{"languages": [{"label": "hat_Latn", "language": "Haitian Creole", "probability": 0.978552}]}]}
126
+ ```
127
+
128
+ `POST /identify` takes `text` or `texts`, `k`, `model`, and `document: true` for a whole document; `GET /models` describes the loaded models and `GET /health` reports that the service is up. Requests that arrive together are answered in shared batches, so throughput grows with load. To serve MillID inside an application of your own, build on `millid.Registry` and `millid.MicroBatcher`.
129
+
130
+ ## Tips
131
+
132
+ - **Give it a sentence or more.** A word or a short greeting carries little evidence: "Bonjour tout le monde" alone comes back as `nrm_Latn` (Norman) rather than French. A full sentence or more gives much more reliable answers.
133
+ - **Treat a low probability as unsure.** When the first answer is below 0.5, look at the next ones too, or ask for more text.
134
+ - **Close languages share their answers.** Swahili and Congo Swahili, or Bosnian and Croatian, differ by a few words; a short passage of either often gets both in its top answers. Ask for `k=3` and decide with what you know about your text.
135
+ - **Pass the text as it is.** Accents, tone marks and the script are much of what the model reads: do not strip, transliterate or romanise it first.
136
+ - **One text alone and the same text in a batch can score a little differently.** A batch is lower-cased as one string; call `predict` with one text where scores must not depend on their neighbours.
137
+
138
+ ## API
139
+
140
+ | | |
141
+ |---|---|
142
+ | `MillID.from_pretrained(ref="tiny-v1.5", revision=None, token=None, **options)` | Load a model: `"tiny-v1.5"`, `"medium-v1"`, `"large-v1.5"`, a Hugging Face repo id, or a directory. `token` only for a private repo; options `device`, `threads`, `batch_size`, `gpu_mem_limit_mb` |
143
+ | `model.predict(text, k=3)` | The `k` most likely languages of a text, as `Prediction`s. A list of texts gives one list per text |
144
+ | `model.identify_document(text, k=3, max_windows=256)` | A text of any length, as a `DocumentResult`: `languages`, `segments`, `windows`, `characters` |
145
+ | `model.probabilities(texts)` | A NumPy array, one row per text, a probability for each label in `model.labels` |
146
+ | `millid.MODELS` | The released models by short name |
147
+ | `millid.normalize`, `millid.encode_batch` | The model's input, to run the ONNX file elsewhere |
148
+
149
+ `MillID` is `millid.LanguageIdentifier` under a shorter name. A model is safe to call from several threads at once.
150
+
151
+ ## How it works
152
+
153
+ MillID is a small transformer that reads text as UTF-8 bytes rather than words, so there is no vocabulary to outgrow: any script, even one it has rarely seen, is input it can read. Each byte is described by the byte itself, the Unicode script of its character and the character sequences ending there; the network turns a passage into one vector and scores it against a vector for every label, built from the label itself, its language (shared across scripts) and its script. The full design is in [the architecture document](https://github.com/baobab-tech/millid/blob/main/training/ARCHITECTURE.md), and each model's card on the Hub has its evaluation.
154
+
155
+ ## Links
156
+
157
+ - Models: [tiny v1.5](https://huggingface.co/baobabtech/millid-tiny-v1.5), [medium v1](https://huggingface.co/baobabtech/millid-medium-v1), [large v1.5](https://huggingface.co/baobabtech/millid-large-v1.5)
158
+ - Leaderboard: [baobabtech/millid](https://huggingface.co/baobabtech/millid)
159
+ - Dataset: [baobabtech/millid-dataset](https://huggingface.co/datasets/baobabtech/millid-dataset)
160
+ - JavaScript package: [`@baobabtechai/millid` on npm](https://www.npmjs.com/package/@baobabtechai/millid)
161
+ - Source, training code and issues: [github.com/baobab-tech/millid](https://github.com/baobab-tech/millid)
162
+
163
+ ## Licence
164
+
165
+ Apache-2.0, for the code and the model weights. Built by [Baobab Tech](https://huggingface.co/baobabtech).