diff --git a/src/app.tsx b/src/app.tsx index 6f1f078..24c4ac8 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -6,7 +6,7 @@ import { HelsinkiNlpEngine } from "./nlp"; import { containerLayout, controlLayout } from "./layout"; import { iconResource } from "./resource"; import { onMeasure, onTranslate, safe, shadowRelated } from "./shadow"; -import { getAsrConfig, getNlpConfig } from "./config"; +import { AsrConfig, NlpConfig } from "./config"; import axios from "axios"; function onInit(app: App) { @@ -34,14 +34,14 @@ export function Echo() { const asrEngine = useMemo( () => new VoskAsrEngine({ - ...getAsrConfig(), + ...AsrConfig, }), [] ); const nlpEngine = useMemo( () => new HelsinkiNlpEngine({ - ...getNlpConfig(), + ...NlpConfig, }), [] ); @@ -126,7 +126,8 @@ export function Echo() { ); nlpEngine.init().then( safe(async () => { - const response = await axios.get("http://localhost:8100/gpu"); + const port = NlpConfig.nlpPort; + const response = await axios.get(`http://localhost:${port}/gpu`); if (response.data.gpu === "True") { console.log("great! use gpu"); setTitle("Echo (GPU)"); diff --git a/src/asr/asr.ts b/src/asr/asr.ts index 2c40a09..4b284e2 100644 --- a/src/asr/asr.ts +++ b/src/asr/asr.ts @@ -9,6 +9,7 @@ import { postasr } from "./postasr"; enum AsrVersion { v100, v110, + v120, } export class VoskAsrEngine implements IAsrEngine { @@ -22,6 +23,16 @@ export class VoskAsrEngine implements IAsrEngine { } getAsrPath() { + const port = this.options.asrPort; + const voskPort = this.options.asrSocketPort; + + const v120 = path.resolve(process.cwd(), "asr-server-v1.2.0"); + if (fs.existsSync(v120)) { + this.version = AsrVersion.v120; + console.log("use asr-server-v1.2.0"); + return { asrDir: v120, exePath: path.resolve(v120, "./ASR-API.exe"), args: [`--port=${port}`, `--vosk-port=${voskPort}`] }; + } + const v110 = path.resolve(process.cwd(), "asr-server-v1.1.0"); if (fs.existsSync(v110)) { this.version = AsrVersion.v110; @@ -40,12 +51,12 @@ export class VoskAsrEngine implements IAsrEngine { async init() { console.log("try to init vosk asr engine"); - const { asrDir, exePath } = this.getAsrPath(); + const { asrDir, exePath, args = [] } = this.getAsrPath(); if (asrDir && exePath) { return new Promise((resolve, reject) => { console.log("asrDir exists, start asr server", asrDir); - const asr = childProcess.spawn(exePath, [], { windowsHide: true, detached: false /** hide console */ }); + const asr = childProcess.spawn(exePath, args, { windowsHide: true, detached: false /** hide console */ }); this.asr = asr; asr.stdout.on("data", (data) => { console.log(`stdout: ${data}`); @@ -78,14 +89,16 @@ export class VoskAsrEngine implements IAsrEngine { } private async asrApi(): Promise { + const port = this.options.asrPort; + if (this.version === AsrVersion.v100) { - const response = await axios.post("http://localhost:8200/asr", {}, { timeout: 2000 }); + const response = await axios.post(`http://localhost:${port}/asr`, {}, { timeout: 2000 }); const result = response?.data?.result; const data = JSON.parse(result || "{}"); const asrText = data.partial || ""; return asrText; } else { - const response = await axios.post("http://localhost:8200/asr_queue", {}, { timeout: 1000 }); + const response = await axios.post(`http://localhost:${port}/asr_queue`, {}, { timeout: 1000 }); const result = response?.data?.result; const data = JSON.parse(result[result.length - 1] || "{}"); const asrText = data.partial || ""; diff --git a/src/asr/base.ts b/src/asr/base.ts index 5602808..a5e3354 100644 --- a/src/asr/base.ts +++ b/src/asr/base.ts @@ -5,6 +5,8 @@ export interface ISentence { export interface IAsrEngineOptions { timeout: number + asrPort: number + asrSocketPort: number onRecognize?: OnRecognize; onError?: OnError; } diff --git a/src/asr/postasr.ts b/src/asr/postasr.ts index 07d3fdd..af7ddb2 100644 --- a/src/asr/postasr.ts +++ b/src/asr/postasr.ts @@ -1,5 +1,6 @@ import axios from "axios"; import { split } from "sentence-splitter"; +import { AsrConfig } from "../config"; class SessionManager { private prevTextLength: number = Number.MAX_SAFE_INTEGER; @@ -20,7 +21,8 @@ let tokenIndex = 0; async function getTextToPunct(asrText: string) { if (asrText.length >= maxTextLength) { - const punctResponse = await axios.post("http://localhost:8200/punct", { text: asrText }, { timeout: 1000 }); + const port = AsrConfig.asrPort; + const punctResponse = await axios.post(`http://localhost:${port}/punct`, { text: asrText }, { timeout: 1000 }); const withPunct = punctResponse?.data?.text || ""; return withPunct; } else { diff --git a/src/config/index.ts b/src/config/index.ts index 9d7399d..2bdcf9c 100644 --- a/src/config/index.ts +++ b/src/config/index.ts @@ -6,6 +6,9 @@ import { INlpEngineOptions } from "../nlp/base"; const defaultConfig = { /** timeout for asr and translate api call*/ timeout: 3500, + asrPort: 8200, + asrSocketPort: 8210, + nlpPort: 8100 }; export function getConfig() { @@ -25,16 +28,23 @@ export function getConfig() { } } -export function getAsrConfig(): IAsrEngineOptions { +function getAsrConfig(): IAsrEngineOptions { const config = getConfig(); return { timeout: config?.timeout || defaultConfig.timeout, + asrPort: config?.asrPort || defaultConfig.asrPort, + asrSocketPort: config?.asrSocketPort || defaultConfig.asrSocketPort }; } -export function getNlpConfig(): INlpEngineOptions { +export const AsrConfig: IAsrEngineOptions = getAsrConfig(); + +function getNlpConfig(): INlpEngineOptions { const config = getConfig(); return { timeout: config?.timeout || defaultConfig.timeout, + nlpPort: config?.nlpPort || defaultConfig.nlpPort }; } + +export const NlpConfig: INlpEngineOptions = getNlpConfig(); \ No newline at end of file diff --git a/src/nlp/base.ts b/src/nlp/base.ts index cfd896d..47edb9a 100644 --- a/src/nlp/base.ts +++ b/src/nlp/base.ts @@ -4,6 +4,7 @@ export interface ITranslateResult { export interface INlpEngineOptions { timeout: number + nlpPort: number onTranslate?: OnTranslate; onError?: OnError; } diff --git a/src/nlp/helsinki-nlp.ts b/src/nlp/helsinki-nlp.ts index f7e3143..3093818 100644 --- a/src/nlp/helsinki-nlp.ts +++ b/src/nlp/helsinki-nlp.ts @@ -35,7 +35,9 @@ export class HelsinkiNlpEngine implements INlpEngine { if (nlpDir && exePath) { return new Promise((resolve, reject) => { console.log("nlpDir exists, start nlp server", nlpDir); - const nlp = childProcess.spawn(exePath, [`--lang-from=en`, `--lang-to=zh`, `--model-dir=.\\model`], { windowsHide: true, detached: false /** hide console */ }); + + 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) => { console.log(`stdout: ${data}`); @@ -72,9 +74,10 @@ export class HelsinkiNlpEngine implements INlpEngine { if (this.cache[text]) { return { text: this.cache[text] }; } - const timeout = this.options?.timeout || 1000; + const timeout = this.options.timeout; + const port = this.options.nlpPort; const translated = await axios.post( - "http://localhost:8100/translate", + `http://localhost:${port}/translate`, { text, },