generated from qber-soft/Ave-React-Template
-
Notifications
You must be signed in to change notification settings - Fork 9
/
helsinki-nlp.ts
104 lines (94 loc) · 3.01 KB
/
helsinki-nlp.ts
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import fs from "fs";
import path from "path";
import axios from "axios";
import childProcess from "child_process";
import { INlpEngine, INlpEngineOptions, ITranslateResult } from "./base";
import { inspectLog, ErrorEvent } from "../server";
export class HelsinkiNlpEngine implements INlpEngine {
private options: INlpEngineOptions;
private nlp: childProcess.ChildProcessWithoutNullStreams;
constructor(options: INlpEngineOptions) {
this.options = options;
}
getNlpPath() {
const gpu = path.resolve(process.cwd(), "nlp-gpu-server");
if (fs.existsSync(gpu)) {
console.log("nlp-gpu-server exists! use it");
return { nlpDir: gpu, exePath: path.resolve(gpu, "./NLP-GPU-API.exe") };
}
const cpu = path.resolve(process.cwd(), "nlp-server");
if (fs.existsSync(cpu)) {
console.log("use nlp-server");
return { nlpDir: cpu, exePath: path.resolve(cpu, "./NLP-API.exe") };
}
return { nlpDir: "", exePath: "" };
}
async init() {
console.log("try to init nlp engine");
const { nlpDir, exePath } = this.getNlpPath();
if (nlpDir && exePath) {
return new Promise((resolve, reject) => {
console.log("nlpDir exists, start nlp server", nlpDir);
const port = this.options.nlpPort;
const nlp = childProcess.spawn(exePath, [`--lang-from=en`, `--lang-to=zh`, `--model-dir=.\\model`, `--port=${port}`], { windowsHide: true, detached: false /** hide console */ });
this.nlp = nlp;
nlp.stdout.on("data", (data) => {
const log = data?.toString() ?? "";
const isError = inspectLog(log);
if(isError) {
reject(false);
}
if(log.endsWith("INFO") || log.endsWith("INFO : ") || log === "\r\n" || log.includes("length:") || log.includes("Request ContentType")) {
// ignore them
} else {
console.log(`stdout: ${log}`);
}
if (data.includes("has been started")) {
console.log("nlp server started");
resolve(true);
}
});
nlp.stderr.on("data", (data) => {
const isError = inspectLog(data?.toString());
if(isError) {
reject(false);
}
console.error(`stderr: ${data}`);
});
nlp.on("close", (code) => {
console.log(`nlp server exit: ${code}`);
reject(false);
});
});
} else {
console.log(ErrorEvent.NlpServerNotExist.log);
inspectLog(ErrorEvent.NlpServerNotExist.log);
}
}
async destroy() {
if (this.nlp) {
console.log("exit nlp server process");
this.nlp.kill();
process.kill(this.nlp?.pid);
process.exit();
}
}
async translate(text: string): Promise<ITranslateResult> {
try {
const timeout = this.options.timeout;
const port = this.options.nlpPort;
const translated = await axios.post(
`http://localhost:${port}/translate`,
{
text,
},
{ timeout }
);
const result = translated.data.result[0].translation_text;
return { text: result };
} catch (error) {
console.log(`translate failed: ${error.message}`);
return { text: "" };
}
}
}