tinyfix 0.1.0 → 0.1.1
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.
- package/README.md +4 -2
- package/dist/engine/model.d.ts +44 -0
- package/dist/engine/model.js +20 -0
- package/dist/engine/modelInWorker.d.ts +7 -0
- package/dist/engine/modelInWorker.js +54 -0
- package/dist/engine/modelWorker.d.ts +1 -0
- package/dist/engine/modelWorker.js +32 -0
- package/dist/engine/wrongWord.d.ts +2 -3
- package/dist/engine/wrongWord.js +5 -3
- package/dist/index.js +22 -14
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ await tinyfix.fix("I got a letter form the bank and did not recieve it.");
|
|
|
21
21
|
<img src=".github/what-it-does.png" alt="You type: I got a letter form the bank. Their is a problem, and I am to tired to recieve it. You get: I got a letter from the bank. There is a problem, and I am too tired to receive it." width="880">
|
|
22
22
|
</p>
|
|
23
23
|
|
|
24
|
-
It's the autocorrect in [offlinepage.app](https://offlinepage.app), a writing app that works in airplane mode.
|
|
24
|
+
**[Try it in your browser](https://huggingface.co/spaces/paperweight/tinyfix).** It's also the autocorrect in [offlinepage.app](https://offlinepage.app), a writing app that works in airplane mode.
|
|
25
25
|
|
|
26
26
|
## Getting the fixes
|
|
27
27
|
|
|
@@ -116,7 +116,7 @@ To serve them from your own site, copy the four files from the [model repo](http
|
|
|
116
116
|
const tinyfix = await loadTinyfix({ baseUrl: "/models/" });
|
|
117
117
|
```
|
|
118
118
|
|
|
119
|
-
The model runs on [ONNX Runtime Web](https://onnxruntime.ai/docs/tutorials/web/), which is installed with this package. On a page it runs in
|
|
119
|
+
The model runs on [ONNX Runtime Web](https://onnxruntime.ai/docs/tutorials/web/), which is installed with this package. On a page it runs in tinyfix's own worker, so loading it and checking words don't freeze typing. Vite and webpack give the worker a file of its own with no setup. With a bundler that doesn't, the model runs on the page instead, which works the same but can pause typing briefly while it loads.
|
|
120
120
|
|
|
121
121
|
## Where it runs
|
|
122
122
|
|
|
@@ -130,6 +130,8 @@ npm run check # types
|
|
|
130
130
|
npm run lint
|
|
131
131
|
```
|
|
132
132
|
|
|
133
|
+
The [demo page](https://huggingface.co/spaces/paperweight/tinyfix) is in [`demo/`](demo), and runs on this repo's build of the package.
|
|
134
|
+
|
|
133
135
|
## License
|
|
134
136
|
|
|
135
137
|
MIT, for this package and for the model.
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import type * as Ort from "onnxruntime-web/wasm";
|
|
2
|
+
/** One input to the wrong-word model: token ids, as many rows as there are words to score. */
|
|
3
|
+
export interface ModelInput {
|
|
4
|
+
data: BigInt64Array;
|
|
5
|
+
dims: [rows: number, width: number];
|
|
6
|
+
}
|
|
7
|
+
export interface ModelInputs {
|
|
8
|
+
left: ModelInput;
|
|
9
|
+
right: ModelInput;
|
|
10
|
+
candidates: ModelInput;
|
|
11
|
+
}
|
|
12
|
+
/** The wrong-word model, wherever it runs: in this thread, or in tinyfix's own worker. */
|
|
13
|
+
export interface Model {
|
|
14
|
+
/** Scores each row's candidate words. */
|
|
15
|
+
run(inputs: ModelInputs): Promise<Float32Array>;
|
|
16
|
+
release(): Promise<void>;
|
|
17
|
+
}
|
|
18
|
+
/** What the page sends tinyfix's worker. */
|
|
19
|
+
export type ToWorker = {
|
|
20
|
+
type: "load";
|
|
21
|
+
model: ArrayBuffer;
|
|
22
|
+
wasmPaths: string | undefined;
|
|
23
|
+
} | {
|
|
24
|
+
type: "run";
|
|
25
|
+
id: number;
|
|
26
|
+
inputs: ModelInputs;
|
|
27
|
+
};
|
|
28
|
+
/** What tinyfix's worker sends back. */
|
|
29
|
+
export type FromWorker = {
|
|
30
|
+
type: "loaded";
|
|
31
|
+
} | {
|
|
32
|
+
type: "load-failed";
|
|
33
|
+
message: string;
|
|
34
|
+
} | {
|
|
35
|
+
type: "result";
|
|
36
|
+
id: number;
|
|
37
|
+
scores: Float32Array;
|
|
38
|
+
} | {
|
|
39
|
+
type: "run-failed";
|
|
40
|
+
id: number;
|
|
41
|
+
message: string;
|
|
42
|
+
};
|
|
43
|
+
/** Starts the model in this thread. ONNX Runtime is passed in, so a worker can import it and a page needn't. */
|
|
44
|
+
export declare function createModel(ort: typeof Ort, bytes: ArrayBuffer, wasmPaths: string | undefined): Promise<Model>;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/** Starts the model in this thread. ONNX Runtime is passed in, so a worker can import it and a page needn't. */
|
|
2
|
+
export async function createModel(ort, bytes, wasmPaths) {
|
|
3
|
+
// One thread, so a page needs no cross-origin isolation headers.
|
|
4
|
+
ort.env.wasm.numThreads = 1;
|
|
5
|
+
if (wasmPaths)
|
|
6
|
+
ort.env.wasm.wasmPaths = wasmPaths;
|
|
7
|
+
const session = await ort.InferenceSession.create(new Uint8Array(bytes), { executionProviders: ["wasm"] });
|
|
8
|
+
const tensor = (input) => new ort.Tensor("int64", input.data, input.dims);
|
|
9
|
+
return {
|
|
10
|
+
async run(inputs) {
|
|
11
|
+
const output = await session.run({
|
|
12
|
+
left: tensor(inputs.left),
|
|
13
|
+
right: tensor(inputs.right),
|
|
14
|
+
candidates: tensor(inputs.candidates),
|
|
15
|
+
});
|
|
16
|
+
return output.scores.data;
|
|
17
|
+
},
|
|
18
|
+
release: () => session.release(),
|
|
19
|
+
};
|
|
20
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { Model } from "./model.js";
|
|
2
|
+
/**
|
|
3
|
+
* Starts the model in tinyfix's own worker. Bundlers that understand `new Worker(new URL(…, import.meta.url))`, such
|
|
4
|
+
* as Vite and webpack, give the worker a file of its own. If the worker can't start, this rejects, and tinyfix runs
|
|
5
|
+
* the model on the page instead.
|
|
6
|
+
*/
|
|
7
|
+
export declare function loadModelInWorker(bytes: ArrayBuffer, wasmPaths: string | undefined): Promise<Model>;
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Starts the model in tinyfix's own worker. Bundlers that understand `new Worker(new URL(…, import.meta.url))`, such
|
|
3
|
+
* as Vite and webpack, give the worker a file of its own. If the worker can't start, this rejects, and tinyfix runs
|
|
4
|
+
* the model on the page instead.
|
|
5
|
+
*/
|
|
6
|
+
export function loadModelInWorker(bytes, wasmPaths) {
|
|
7
|
+
return new Promise((resolve, reject) => {
|
|
8
|
+
const worker = new Worker(new URL("./modelWorker.js", import.meta.url), { type: "module", name: "tinyfix" });
|
|
9
|
+
const send = (message, transfer = []) => worker.postMessage(message, transfer);
|
|
10
|
+
const pending = new Map();
|
|
11
|
+
let nextId = 0;
|
|
12
|
+
const fail = (error) => {
|
|
13
|
+
reject(error);
|
|
14
|
+
for (const run of pending.values())
|
|
15
|
+
run.reject(error);
|
|
16
|
+
pending.clear();
|
|
17
|
+
worker.terminate();
|
|
18
|
+
};
|
|
19
|
+
const model = {
|
|
20
|
+
run: (inputs) => new Promise((resolveRun, rejectRun) => {
|
|
21
|
+
const id = nextId++;
|
|
22
|
+
pending.set(id, { resolve: resolveRun, reject: rejectRun });
|
|
23
|
+
const buffers = [inputs.left.data.buffer, inputs.right.data.buffer, inputs.candidates.data.buffer];
|
|
24
|
+
send({ type: "run", id, inputs }, buffers);
|
|
25
|
+
}),
|
|
26
|
+
release: async () => worker.terminate(),
|
|
27
|
+
};
|
|
28
|
+
worker.onerror = (event) => {
|
|
29
|
+
event.preventDefault();
|
|
30
|
+
fail(new Error(`tinyfix's worker stopped: ${event.message || "it couldn't start"}.`));
|
|
31
|
+
};
|
|
32
|
+
worker.onmessage = ({ data }) => {
|
|
33
|
+
switch (data.type) {
|
|
34
|
+
case "loaded":
|
|
35
|
+
resolve(model);
|
|
36
|
+
return;
|
|
37
|
+
case "load-failed":
|
|
38
|
+
fail(new Error(`tinyfix's model couldn't start: ${data.message}`));
|
|
39
|
+
return;
|
|
40
|
+
case "result":
|
|
41
|
+
pending.get(data.id)?.resolve(data.scores);
|
|
42
|
+
pending.delete(data.id);
|
|
43
|
+
return;
|
|
44
|
+
case "run-failed":
|
|
45
|
+
pending.get(data.id)?.reject(new Error(data.message));
|
|
46
|
+
pending.delete(data.id);
|
|
47
|
+
return;
|
|
48
|
+
default:
|
|
49
|
+
data;
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
send({ type: "load", model: bytes, wasmPaths }, [bytes]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// tinyfix's worker: it holds the model, so starting it and running it never block the page. It only imports ONNX
|
|
2
|
+
// Runtime and the code below, never the page's own scripts.
|
|
3
|
+
import * as ort from "onnxruntime-web/wasm";
|
|
4
|
+
import { createModel } from "./model.js";
|
|
5
|
+
const reply = globalThis.postMessage;
|
|
6
|
+
let model = null;
|
|
7
|
+
globalThis.onmessage = async ({ data }) => {
|
|
8
|
+
switch (data.type) {
|
|
9
|
+
case "load":
|
|
10
|
+
try {
|
|
11
|
+
model = await createModel(ort, data.model, data.wasmPaths);
|
|
12
|
+
reply({ type: "loaded" });
|
|
13
|
+
}
|
|
14
|
+
catch (error) {
|
|
15
|
+
reply({ type: "load-failed", message: String(error) });
|
|
16
|
+
}
|
|
17
|
+
return;
|
|
18
|
+
case "run":
|
|
19
|
+
try {
|
|
20
|
+
if (!model)
|
|
21
|
+
throw new Error("The model isn't loaded yet.");
|
|
22
|
+
const scores = await model.run(data.inputs);
|
|
23
|
+
reply({ type: "result", id: data.id, scores }, [scores.buffer]);
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
reply({ type: "run-failed", id: data.id, message: String(error) });
|
|
27
|
+
}
|
|
28
|
+
return;
|
|
29
|
+
default:
|
|
30
|
+
data;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
@@ -1,8 +1,7 @@
|
|
|
1
|
-
import type
|
|
1
|
+
import type { Model } from "./model.js";
|
|
2
2
|
import type { ContextScorer, Correction } from "./fixer.js";
|
|
3
3
|
export interface WrongWordOptions {
|
|
4
|
-
|
|
5
|
-
session: Ort.InferenceSession;
|
|
4
|
+
model: Model;
|
|
6
5
|
/** Lines of the model's vocab.txt, most common word first. */
|
|
7
6
|
vocabulary: readonly string[];
|
|
8
7
|
/** Lines of the model's confusions.txt: a typed word, then words people meant by it. */
|
package/dist/engine/wrongWord.js
CHANGED
|
@@ -150,13 +150,15 @@ export class WrongWordChecker {
|
|
|
150
150
|
.map((token) => this.tokenId(token))
|
|
151
151
|
.reverse(), RIGHT_WINDOW);
|
|
152
152
|
const tiled = (values) => rows.flatMap(() => values);
|
|
153
|
-
const tensor = (values, width) =>
|
|
154
|
-
|
|
153
|
+
const tensor = (values, width) => ({
|
|
154
|
+
data: BigInt64Array.from(values, BigInt),
|
|
155
|
+
dims: [rows.length, width],
|
|
156
|
+
});
|
|
157
|
+
return this.options.model.run({
|
|
155
158
|
left: tensor(tiled(left), LEFT_WINDOW),
|
|
156
159
|
right: tensor(tiled(right), RIGHT_WINDOW),
|
|
157
160
|
candidates: tensor(rows.flatMap((row) => [...row, ...pad([], CANDIDATES - row.length)]), CANDIDATES),
|
|
158
161
|
});
|
|
159
|
-
return output.scores.data;
|
|
160
162
|
}
|
|
161
163
|
/** Short words and words with an apostrophe are left to the list of known mix-ups ("to" and "too", "it's" and "its"). */
|
|
162
164
|
hasKnownMixups(word, textBefore) {
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import * as ort from "onnxruntime-web";
|
|
2
1
|
import { Fixer, WITH_MODEL_WEIGHTS, parseWordList } from "./engine/fixer.js";
|
|
2
|
+
import { createModel } from "./engine/model.js";
|
|
3
|
+
import { loadModelInWorker } from "./engine/modelInWorker.js";
|
|
3
4
|
import { WordTrie } from "./engine/trie.js";
|
|
4
5
|
import { CAREFUL_WRONG_WORD, EAGER_WRONG_WORD, WrongWordChecker } from "./engine/wrongWord.js";
|
|
5
6
|
/** The revision of huggingface.co/paperweight/tinyfix this version of the package was tested with. */
|
|
@@ -55,6 +56,21 @@ function apply(text, fixes) {
|
|
|
55
56
|
}
|
|
56
57
|
return result;
|
|
57
58
|
}
|
|
59
|
+
/**
|
|
60
|
+
* On a page, the model runs in tinyfix's own worker, so starting it and checking words never block typing. Elsewhere,
|
|
61
|
+
* or if the worker can't start, it runs right here.
|
|
62
|
+
*/
|
|
63
|
+
async function startModel(bytes, wasmPaths) {
|
|
64
|
+
const runHere = async (modelBytes) => {
|
|
65
|
+
// The WebAssembly-only build of ONNX Runtime. The default build also carries WebGPU, which tinyfix doesn't use.
|
|
66
|
+
const ort = await import("onnxruntime-web/wasm");
|
|
67
|
+
return createModel(ort, modelBytes, wasmPaths);
|
|
68
|
+
};
|
|
69
|
+
if (typeof document === "undefined" || typeof Worker === "undefined")
|
|
70
|
+
return runHere(bytes);
|
|
71
|
+
// The worker is handed a copy, so the bytes are still here if it can't start.
|
|
72
|
+
return loadModelInWorker(bytes.slice(0), wasmPaths).catch(() => runHere(bytes));
|
|
73
|
+
}
|
|
58
74
|
/**
|
|
59
75
|
* Loads tinyfix: about 3.5 MB of model and word lists, from Hugging Face unless `baseUrl` says otherwise. Call it
|
|
60
76
|
* once and keep the result.
|
|
@@ -73,22 +89,14 @@ export async function loadTinyfix(options = {}) {
|
|
|
73
89
|
])
|
|
74
90
|
: null,
|
|
75
91
|
]);
|
|
76
|
-
let
|
|
92
|
+
let model = null;
|
|
77
93
|
let wrongWord = null;
|
|
78
94
|
if (modelFiles) {
|
|
79
|
-
const [
|
|
80
|
-
|
|
81
|
-
ort.env.wasm.numThreads = 1;
|
|
82
|
-
if (options.wasmPaths)
|
|
83
|
-
ort.env.wasm.wasmPaths = options.wasmPaths;
|
|
84
|
-
// On a page, ONNX Runtime runs the model in its own worker, so starting it doesn't block typing.
|
|
85
|
-
if (typeof document !== "undefined")
|
|
86
|
-
ort.env.wasm.proxy = true;
|
|
87
|
-
session = await ort.InferenceSession.create(new Uint8Array(model), { executionProviders: ["wasm"] });
|
|
95
|
+
const [bytes, vocabulary, confusions] = modelFiles;
|
|
96
|
+
model = await startModel(bytes, options.wasmPaths);
|
|
88
97
|
await nextTask();
|
|
89
98
|
wrongWord = new WrongWordChecker({
|
|
90
|
-
|
|
91
|
-
session,
|
|
99
|
+
model,
|
|
92
100
|
vocabulary: lines(vocabulary),
|
|
93
101
|
confusions: lines(confusions),
|
|
94
102
|
settings: options.mode === "careful" ? CAREFUL_WRONG_WORD : EAGER_WRONG_WORD,
|
|
@@ -157,7 +165,7 @@ export async function loadTinyfix(options = {}) {
|
|
|
157
165
|
fix: async (text, checkOptions) => apply(text, await check(text, checkOptions)),
|
|
158
166
|
ignore: (word) => fixer.rememberUndo(word),
|
|
159
167
|
dispose: async () => {
|
|
160
|
-
await
|
|
168
|
+
await model?.release();
|
|
161
169
|
},
|
|
162
170
|
};
|
|
163
171
|
}
|
package/package.json
CHANGED