-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
79 lines (68 loc) · 1.82 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { Py } from "./src/py.js";
// https://huggingface.co/facebook/nllb-200-distilled-600M/blob/main/README.md?code=true
// https://github.com/facebookresearch/flores/blob/main/flores200/README.md
import LANGS from "./langs/flores.json" assert { type: "json" };
const MAIN_PATH = "main.py";
const py = new Py(".");
/**
* https://huggingface.co/facebook
*/
const MODELS = [
"facebook/nllb-200-distilled-600M",
"facebook/nllb-200-distilled-1.3B",
"facebook/nllb-200-1.3B",
"facebook/nllb-200-3.3B",
"facebook/nllb-moe-54b",
];
function isValidLanguage(str) {
return Object.keys(LANGS).indexOf(str) > -1;
}
function convertLanguage(str) {
return LANGS[str];
}
function isValidModel(str) {
return MODELS.indexOf(str) > -1;
}
/**
*
* @param {boolean} force
* @returns {Promise<void>}
*/
async function init(force) {
await py.init(force);
await py.install("torch", [
"--index-url",
"https://download.pytorch.org/whl/cu118",
]);
await py.install("transformers");
}
/**
*
* @param {string} text
* @param {string} from eng_Latn
* @param {string} to jpn_Jpan
* @param {string} model default "facebook/nllb-200-distilled-600M"
* @returns {Promise<string>}
*/
async function translate(text, from, to, model) {
if (!model) {
model = MODELS[0]; // facebook/nllb-200-distilled-600M
}
if (!isValidLanguage(from)) {
throw new Error(`${from} is invalid language.`);
}
if (!isValidLanguage(to)) {
throw new Error(`${to} is invalid language.`);
}
if (!isValidModel(model)) {
throw new Error(`${model} is invalid model.`);
}
from = convertLanguage(from);
to = convertLanguage(to);
const { stdout, stderr } = await py.exec(MAIN_PATH, [model, text, from, to]);
if (stdout === "" && stderr.length > 0) {
throw new Error(stderr);
}
return stdout;
}
export { MODELS, init, translate };