Skip to content

Commit

Permalink
Merge pull request #15 from rerender2021/feature/custom-port
Browse files Browse the repository at this point in the history
feature: custom port
  • Loading branch information
rerender2021 authored May 6, 2023
2 parents 693d7bd + 05f33e0 commit 13d8b51
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -34,14 +34,14 @@ export function Echo() {
const asrEngine = useMemo(
() =>
new VoskAsrEngine({
...getAsrConfig(),
...AsrConfig,
}),
[]
);
const nlpEngine = useMemo(
() =>
new HelsinkiNlpEngine({
...getNlpConfig(),
...NlpConfig,
}),
[]
);
Expand Down Expand Up @@ -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)");
Expand Down
21 changes: 17 additions & 4 deletions src/asr/asr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { postasr } from "./postasr";
enum AsrVersion {
v100,
v110,
v120,
}

export class VoskAsrEngine implements IAsrEngine {
Expand All @@ -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;
Expand All @@ -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}`);
Expand Down Expand Up @@ -78,14 +89,16 @@ export class VoskAsrEngine implements IAsrEngine {
}

private async asrApi(): Promise<string> {
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 || "";
Expand Down
2 changes: 2 additions & 0 deletions src/asr/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export interface ISentence {

export interface IAsrEngineOptions {
timeout: number
asrPort: number
asrSocketPort: number
onRecognize?: OnRecognize;
onError?: OnError;
}
Expand Down
4 changes: 3 additions & 1 deletion src/asr/postasr.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down
14 changes: 12 additions & 2 deletions src/config/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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();
1 change: 1 addition & 0 deletions src/nlp/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export interface ITranslateResult {

export interface INlpEngineOptions {
timeout: number
nlpPort: number
onTranslate?: OnTranslate;
onError?: OnError;
}
Expand Down
9 changes: 6 additions & 3 deletions src/nlp/helsinki-nlp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down Expand Up @@ -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,
},
Expand Down

0 comments on commit 13d8b51

Please sign in to comment.