Sign-in code model v1 (Core ML and native)
Browse files- README.md +117 -0
- otp-v1.bin +3 -0
- otp-v1.mlmodel +3 -0
README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
library_name: picacho
|
| 4 |
+
tags:
|
| 5 |
+
- email
|
| 6 |
+
- one-time-password
|
| 7 |
+
- coreml
|
| 8 |
+
- multilingual
|
| 9 |
+
---
|
| 10 |
+
|
| 11 |
+
# Sign-in code model
|
| 12 |
+
|
| 13 |
+
The model behind **Settings → Local AI → Hide sign-in codes**. It decides
|
| 14 |
+
whether a number in an email is the message's sign-in, verification or
|
| 15 |
+
one-time code, so Picacho can show it as `••••••` in the subject, the message
|
| 16 |
+
and notifications.
|
| 17 |
+
|
| 18 |
+
Nothing here ships inside the app. When the feature is turned on, Picacho
|
| 19 |
+
downloads one file from this repository, checks its size and SHA-256 against
|
| 20 |
+
the values built into the app (`src/manage/local_ai/mod.rs` in
|
| 21 |
+
[Picacho](https://github.com/pinaltech/Picacho)), and keeps it in its data
|
| 22 |
+
directory under `models/`. Turning the feature (or Local AI) off deletes it.
|
| 23 |
+
Training code and data live in Picacho's `models/otp/` directory.
|
| 24 |
+
|
| 25 |
+
| File | Used on | Runtime |
|
| 26 |
+
| --- | --- | --- |
|
| 27 |
+
| `otp-v1.mlmodel` | macOS | Core ML (compiled for the Mac when installed) |
|
| 28 |
+
| `otp-v1.bin` | Windows, Linux | Picacho's own (`src/manage/local_ai/runtime.rs`) |
|
| 29 |
+
|
| 30 |
+
Both hold the same network.
|
| 31 |
+
|
| 32 |
+
## Languages
|
| 33 |
+
|
| 34 |
+
The model is trained on sign-in mail in English, Spanish, German, French,
|
| 35 |
+
Portuguese, Italian, Dutch, Swedish, Polish, Japanese, Chinese (simplified
|
| 36 |
+
and traditional), Korean, Russian, Ukrainian, Turkish, Arabic, Persian,
|
| 37 |
+
Hebrew, Hindi, Bengali, Indonesian, Malay, Vietnamese, Thai, Greek, Czech,
|
| 38 |
+
Slovak, Danish, Norwegian, Finnish, Romanian, Hungarian, Catalan, Croatian,
|
| 39 |
+
Bulgarian, Tagalog and Swahili, with codes in ASCII, fullwidth, Arabic-Indic,
|
| 40 |
+
Persian, Devanagari, Bengali and Thai digits. Copy Code gives ASCII digits.
|
| 41 |
+
|
| 42 |
+
## How it works
|
| 43 |
+
|
| 44 |
+
1. **Candidates.** A pattern finds numbers that could be codes: 2–4
|
| 45 |
+
hyphenated groups of 3–6 uppercase letters and digits with a letter among
|
| 46 |
+
them (`BAJ6K-654XM`), two groups of 3–4 digits (`482 913`, `482-913`),
|
| 47 |
+
4–10 uppercase letters and digits
|
| 48 |
+
(`AB12CD`, `482913`), or 4–10 digits in any script (`482913`,
|
| 49 |
+
`٤٨٢٩١٣`), optionally with a short prefix (`G-482913`). A candidate may
|
| 50 |
+
touch Chinese or Japanese text but not a Latin letter or another digit;
|
| 51 |
+
parts of phone numbers, dates and amounts are skipped.
|
| 52 |
+
2. **Words.** Runs of letters, marks and digits. Scripts written without
|
| 53 |
+
spaces (Chinese, Japanese kana, Thai, Lao, Khmer, Myanmar) are split into
|
| 54 |
+
overlapping pairs of characters (`验证码是` → `验证`, `证码`, `码是`).
|
| 55 |
+
3. **Features.** For each candidate: the words before and after it and their
|
| 56 |
+
first four letters, the nearest words and word pairs, the characters next
|
| 57 |
+
to it, its shape (`999999`, `A-999999`), whether it is on a line of its
|
| 58 |
+
own or in the subject, the subject's words, and the **concepts** the
|
| 59 |
+
nearby words and the subject name. Concepts (`lexicon.json`: code,
|
| 60 |
+
verify, sign in, one-time, expiry, don't share, order, money, shipping,
|
| 61 |
+
contact, promotion, reference) list word stems in every language above,
|
| 62 |
+
so "код", "コード" and "kód" all count as *code* (`=` marks a stem that
|
| 63 |
+
must be the whole word, `^` one the word must start with). Concepts are
|
| 64 |
+
also taken from the whole 200 characters on each side of the candidate
|
| 65 |
+
and from the subject, with Chinese, Japanese and Thai runs kept whole
|
| 66 |
+
rather than split into pairs, because a code often sits far from the
|
| 67 |
+
words naming it (`293393は、…ワンタイムコードです`). Everything is hashed
|
| 68 |
+
into 16,384 slots.
|
| 69 |
+
4. **Model.** A bag-of-features network (fastText style): one hidden layer of
|
| 70 |
+
16 units and a logistic output, about 260,000 weights (1 MB). A candidate
|
| 71 |
+
scoring 0.3 or more is a code.
|
| 72 |
+
|
| 73 |
+
`features.py` and `lexicon.json` are the reference; the app builds the same
|
| 74 |
+
features in `src/manage/local_ai/otp.rs`, with the lexicon compiled in, so a
|
| 75 |
+
lexicon change needs an app update as well as a new model. `parity.json`
|
| 76 |
+
(candidates, features, vectors and scores for messages in several languages
|
| 77 |
+
and scripts) is checked by `cargo test matches_the_training_code`.
|
| 78 |
+
|
| 79 |
+
## Training
|
| 80 |
+
|
| 81 |
+
```sh
|
| 82 |
+
python3 train.py # numpy: otp-v1.bin, parity.json
|
| 83 |
+
python3 train.py --coreml # also otp-v1.mlmodel (pip install coremltools)
|
| 84 |
+
```
|
| 85 |
+
|
| 86 |
+
The training mail is synthetic (`data.py`): sign-in messages in the languages
|
| 87 |
+
above, mail full of numbers that aren't codes (orders, invoices, tracking and
|
| 88 |
+
booking references, phone numbers, addresses, account endings, coupons,
|
| 89 |
+
license keys, meeting IDs) in many of them, and sign-in messages with another number beside
|
| 90 |
+
the code. Training is deterministic.
|
| 91 |
+
|
| 92 |
+
On messages built from templates held out of training:
|
| 93 |
+
|
| 94 |
+
| Held-out wording | Precision | Recall | Messages exactly right |
|
| 95 |
+
| --- | --- | --- | --- |
|
| 96 |
+
| English (and other Latin-script) templates | 95% | 100% | 97% |
|
| 97 |
+
| Templates in the newer languages | 97% | 96% | 96% |
|
| 98 |
+
|
| 99 |
+
Those are the honest figures for wording the model hasn't seen; on familiar
|
| 100 |
+
wording it is right nearly every time. The lexicon was written knowing every
|
| 101 |
+
template, so for an unseen language the second row is optimistic. Real mail
|
| 102 |
+
will have phrasings the templates don't cover; more varied examples improve
|
| 103 |
+
it.
|
| 104 |
+
|
| 105 |
+
## Publishing a new version
|
| 106 |
+
|
| 107 |
+
1. Retrain, then update the sizes and digests `train.py` prints in
|
| 108 |
+
`src/manage/local_ai/mod.rs` (`cargo test the_published_models_are_the_trained_ones`
|
| 109 |
+
fails until they match). Bump the version in the file names when the
|
| 110 |
+
features or lexicon change.
|
| 111 |
+
2. Commit and push the model files, then point `MODELS_URL` at that commit:
|
| 112 |
+
`https://raw.githubusercontent.com/juliaoverflow/Picacho/<commit>/models/otp`.
|
| 113 |
+
A commit, not a branch, so the file at the address never changes under the
|
| 114 |
+
pinned digest.
|
| 115 |
+
|
| 116 |
+
`PICACHO_MODELS_URL` makes the app download from somewhere else (an HTTPS
|
| 117 |
+
mirror) while developing.
|
otp-v1.bin
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:7667b357195ca20d04e3505700ca5db72d335befbafc7e1dbe803eb765347bc6
|
| 3 |
+
size 1048728
|
otp-v1.mlmodel
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:309484159aa766e287f9a0bffcc0f2d990ba524eaaed374ab63b25b3b0194c8a
|
| 3 |
+
size 1049025
|