Spaces:
Running
Running
Deploy v0.4.0 from gabrielkpo/alias
Browse files- alias.js +43 -7
- app.js +5 -4
- index.html +5 -2
alias.js
CHANGED
|
@@ -105,7 +105,7 @@ const IBAN_LENGTHS = {
|
|
| 105 |
}; // prettier-ignore
|
| 106 |
|
| 107 |
export function compact(value) {
|
| 108 |
-
return value.replace(/[\s.\-]/gu, "").toUpperCase();
|
| 109 |
}
|
| 110 |
|
| 111 |
export function ibanIsValid(value) {
|
|
@@ -168,7 +168,7 @@ const ID_KEYWORD =
|
|
| 168 |
const FILLER = "(?:(?:is|was|est|était|=)\\s+)?(?:(?:le|la|the)\\s+)?";
|
| 169 |
const IDENTIFIER = new RegExp(
|
| 170 |
`(?:${B}${NUMBER_WORD}\\s*(?:de\\s+|du\\s+|d['’]\\s*|of\\s+)?)?${B}(?<keyword>${ID_KEYWORD})(?!${W})` +
|
| 171 |
-
`(?:\\s+${NUMBER_WORD})?\\s*(?:[:#]\\s*)?${FILLER}` +
|
| 172 |
"(?<value>[A-Z0-9](?:[A-Z0-9/_.\\-]*[A-Z0-9])?)",
|
| 173 |
"giud",
|
| 174 |
);
|
|
@@ -276,9 +276,41 @@ function* birthDates(text) {
|
|
| 276 |
}
|
| 277 |
}
|
| 278 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 279 |
/** rules.RuleDetector.find, without the phone matcher: every rule's spans, not merged yet. */
|
| 280 |
export function findRules(text) {
|
| 281 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 282 |
}
|
| 283 |
|
| 284 |
/** rules.RuleDetector.detect, without the phone matcher. */
|
|
@@ -443,8 +475,8 @@ export async function detect(text, model, idKeywords = ID_KEYWORDS) {
|
|
| 443 |
|
| 444 |
// ---------------------------------------------------------------- masking.py
|
| 445 |
|
| 446 |
-
|
| 447 |
-
const ALIAS = new RegExp(`\\[\\s*(${TYPES.join("|")})_([0-9]+)\\s*\\]`, "
|
| 448 |
|
| 449 |
export function normalize(e) {
|
| 450 |
if (e.type === "PHONE") {
|
|
@@ -455,13 +487,17 @@ export function normalize(e) {
|
|
| 455 |
if (digits.startsWith("0")) return `+33${digits.slice(1)}`;
|
| 456 |
return `+33${digits}`;
|
| 457 |
}
|
| 458 |
-
if (
|
|
|
|
|
|
|
|
|
|
|
|
|
| 459 |
// Upper then lower case is closer to Python's casefold than lower case alone (ß -> ss).
|
| 460 |
const folded = e.text.toUpperCase().toLowerCase().normalize("NFKD").replace(/\p{Mn}/gu, "");
|
| 461 |
return folded.split(/\s+/u).filter(Boolean).join(" ");
|
| 462 |
}
|
| 463 |
|
| 464 |
-
const canonical = (m) => `[${m[1]}_${parseInt(m[2], 10)}]`;
|
| 465 |
|
| 466 |
/**
|
| 467 |
* masking.mask with a fresh alias table: masked text, spans with their alias, counts, and what
|
|
|
|
| 105 |
}; // prettier-ignore
|
| 106 |
|
| 107 |
export function compact(value) {
|
| 108 |
+
return value.replace(/[\s.\-\u00ad\u200b\u200c\u200d\u2060\ufeff]/gu, "").toUpperCase();
|
| 109 |
}
|
| 110 |
|
| 111 |
export function ibanIsValid(value) {
|
|
|
|
| 168 |
const FILLER = "(?:(?:is|was|est|était|=)\\s+)?(?:(?:le|la|the)\\s+)?";
|
| 169 |
const IDENTIFIER = new RegExp(
|
| 170 |
`(?:${B}${NUMBER_WORD}\\s*(?:de\\s+|du\\s+|d['’]\\s*|of\\s+)?)?${B}(?<keyword>${ID_KEYWORD})(?!${W})` +
|
| 171 |
+
`(?:\\s+(?:\\p{L}{2,20}\\s+)?${NUMBER_WORD})?\\s*(?:[:#]\\s*)?${FILLER}` +
|
| 172 |
"(?<value>[A-Z0-9](?:[A-Z0-9/_.\\-]*[A-Z0-9])?)",
|
| 173 |
"giud",
|
| 174 |
);
|
|
|
|
| 276 |
}
|
| 277 |
}
|
| 278 |
|
| 279 |
+
// rules.SPACES and rules.INVISIBLE: typographic spaces read as a space, invisible characters
|
| 280 |
+
// skipped, so a value written with them does not stay readable.
|
| 281 |
+
const SPACES = new Set(
|
| 282 |
+
"\u00a0\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000",
|
| 283 |
+
);
|
| 284 |
+
const INVISIBLE = new Set("\u00ad\u200b\u200c\u200d\u2060\ufeff");
|
| 285 |
+
const TYPOGRAPHY = new RegExp(`[${[...SPACES, ...INVISIBLE].join("")}]`, "u");
|
| 286 |
+
|
| 287 |
+
/** rules.shadow: the text as the rules read it, and each position's offset in the original. */
|
| 288 |
+
export function shadow(text) {
|
| 289 |
+
let read = "";
|
| 290 |
+
const origin = [];
|
| 291 |
+
for (let i = 0; i < text.length; i++) {
|
| 292 |
+
const char = text[i];
|
| 293 |
+
if (INVISIBLE.has(char)) continue;
|
| 294 |
+
read += SPACES.has(char) ? " " : char;
|
| 295 |
+
origin.push(i);
|
| 296 |
+
}
|
| 297 |
+
origin.push(text.length);
|
| 298 |
+
return [read, origin];
|
| 299 |
+
}
|
| 300 |
+
|
| 301 |
+
function findRaw(text) {
|
| 302 |
+
return [...emails(text), ...ibans(text), ...nationalIds(text), ...identifiers(text), ...birthDates(text)];
|
| 303 |
+
}
|
| 304 |
+
|
| 305 |
/** rules.RuleDetector.find, without the phone matcher: every rule's spans, not merged yet. */
|
| 306 |
export function findRules(text) {
|
| 307 |
+
if (!TYPOGRAPHY.test(text)) return findRaw(text);
|
| 308 |
+
const [read, origin] = shadow(text);
|
| 309 |
+
return findRaw(read).map((e) => {
|
| 310 |
+
const start = origin[e.start];
|
| 311 |
+
const end = origin[e.end - 1] + 1;
|
| 312 |
+
return { ...e, start, end, text: text.slice(start, end) };
|
| 313 |
+
});
|
| 314 |
}
|
| 315 |
|
| 316 |
/** rules.RuleDetector.detect, without the phone matcher. */
|
|
|
|
| 475 |
|
| 476 |
// ---------------------------------------------------------------- masking.py
|
| 477 |
|
| 478 |
+
// masking.ALIAS_PATTERN: spaces in the brackets, another case, a dash or space for the underscore.
|
| 479 |
+
const ALIAS = new RegExp(`\\[\\s*(${TYPES.join("|")})[_\\- ]?([0-9]+)\\s*\\]`, "giu");
|
| 480 |
|
| 481 |
export function normalize(e) {
|
| 482 |
if (e.type === "PHONE") {
|
|
|
|
| 487 |
if (digits.startsWith("0")) return `+33${digits.slice(1)}`;
|
| 488 |
return `+33${digits}`;
|
| 489 |
}
|
| 490 |
+
if (e.type === "IBAN" || e.type === "NATIONAL_ID") return compact(e.text);
|
| 491 |
+
const visible = e.text.replace(/[\u00ad\u200b\u200c\u200d\u2060\ufeff]/gu, "");
|
| 492 |
+
// An e-mail only ignores case, an identifier case and spacing: a dot or dash makes another one.
|
| 493 |
+
if (e.type === "EMAIL") return visible.normalize("NFC").trim().toUpperCase().toLowerCase();
|
| 494 |
+
if (e.type === "IDENTIFIER") return visible.toUpperCase().split(/\s+/u).filter(Boolean).join(" ");
|
| 495 |
// Upper then lower case is closer to Python's casefold than lower case alone (ß -> ss).
|
| 496 |
const folded = e.text.toUpperCase().toLowerCase().normalize("NFKD").replace(/\p{Mn}/gu, "");
|
| 497 |
return folded.split(/\s+/u).filter(Boolean).join(" ");
|
| 498 |
}
|
| 499 |
|
| 500 |
+
const canonical = (m) => `[${m[1].toUpperCase()}_${parseInt(m[2], 10)}]`;
|
| 501 |
|
| 502 |
/**
|
| 503 |
* masking.mask with a fresh alias table: masked text, spans with their alias, counts, and what
|
app.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
| 1 |
// Alias demo page: loads the tokenizer and the ONNX model from Hugging Face once (the browser
|
| 2 |
-
// caches them), then masks the pasted text locally with alias.js.
|
| 3 |
-
// only requests are for the model files and the ONNX runtime
|
| 4 |
-
//
|
|
|
|
| 5 |
import {
|
| 6 |
AutoModelForTokenClassification,
|
| 7 |
AutoTokenizer,
|
|
@@ -14,7 +15,7 @@ const MODEL = "Gabrielkph/alias-mmbert-small";
|
|
| 14 |
// The model version the demo was tested with: a tag on the model repository, so a later upload
|
| 15 |
// of new weights cannot change the demo without a new release of it.
|
| 16 |
const MODEL_REVISION = "v1.0";
|
| 17 |
-
const VERSION = "0.
|
| 18 |
// What the first visit downloads: the ONNX model (563 MB) and the tokenizer (34 MB).
|
| 19 |
const EXPECTED_BYTES = 597e6;
|
| 20 |
|
|
|
|
| 1 |
// Alias demo page: loads the tokenizer and the ONNX model from Hugging Face once (the browser
|
| 2 |
+
// caches them), then masks the pasted text locally with alias.js. This code never sends the
|
| 3 |
+
// text: its only requests are for the model files and the ONNX runtime. The page's CSP limits
|
| 4 |
+
// requests to Hugging Face and cdn.jsdelivr.net, from which scripts are also allowed. Text is
|
| 5 |
+
// only ever inserted with textContent, never as HTML.
|
| 6 |
import {
|
| 7 |
AutoModelForTokenClassification,
|
| 8 |
AutoTokenizer,
|
|
|
|
| 15 |
// The model version the demo was tested with: a tag on the model repository, so a later upload
|
| 16 |
// of new weights cannot change the demo without a new release of it.
|
| 17 |
const MODEL_REVISION = "v1.0";
|
| 18 |
+
const VERSION = "0.4.0"; // x-release-please-version
|
| 19 |
// What the first visit downloads: the ONNX model (563 MB) and the tokenizer (34 MB).
|
| 20 |
const EXPECTED_BYTES = 597e6;
|
| 21 |
|
index.html
CHANGED
|
@@ -3,8 +3,11 @@
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
-
<!--
|
| 7 |
-
|
|
|
|
|
|
|
|
|
|
| 8 |
blob: in script-src: the runtime imports its WebAssembly glue module from a blob URL. -->
|
| 9 |
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'self' blob: https://cdn.jsdelivr.net 'wasm-unsafe-eval'; connect-src https://huggingface.co https://*.huggingface.co https://*.hf.co https://cdn.jsdelivr.net; worker-src 'self' blob: https://cdn.jsdelivr.net; style-src 'self'; img-src 'self' data:; base-uri 'none'; form-action 'none'">
|
| 10 |
<title>Alias</title>
|
|
|
|
| 3 |
<head>
|
| 4 |
<meta charset="utf-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1">
|
| 6 |
+
<!-- Scripts come from this page, blob URLs and the whole of cdn.jsdelivr.net (the ONNX
|
| 7 |
+
runtime). connect-src limits requests to huggingface.co, *.huggingface.co and *.hf.co
|
| 8 |
+
(the model files) and cdn.jsdelivr.net; forms are blocked, styles and images come from
|
| 9 |
+
the page itself. The page's code never puts the text in a request, but the CSP alone does
|
| 10 |
+
not stop a script served from cdn.jsdelivr.net from sending it to one of those hosts.
|
| 11 |
blob: in script-src: the runtime imports its WebAssembly glue module from a blob URL. -->
|
| 12 |
<meta http-equiv="Content-Security-Policy" content="default-src 'none'; script-src 'self' blob: https://cdn.jsdelivr.net 'wasm-unsafe-eval'; connect-src https://huggingface.co https://*.huggingface.co https://*.hf.co https://cdn.jsdelivr.net; worker-src 'self' blob: https://cdn.jsdelivr.net; style-src 'self'; img-src 'self' data:; base-uri 'none'; form-action 'none'">
|
| 13 |
<title>Alias</title>
|