64 lines
1.7 KiB
TypeScript
64 lines
1.7 KiB
TypeScript
import log from "electron-log";
|
|
import { spawn } from "node:child_process";
|
|
import path from "node:path";
|
|
import { InferReq } from "./alg.type";
|
|
import axios from "axios";
|
|
|
|
export const ALGServerRoot = "http://127.0.0.1:5000/root";
|
|
|
|
export const getAlgPath = (
|
|
platform: "macos" | "windows",
|
|
isDevelopment: boolean
|
|
): string => {
|
|
const algExecFile = {
|
|
macos: "",
|
|
windows: "main.exe",
|
|
};
|
|
const basePath = isDevelopment
|
|
? path.join(process.env.VITE_PUBLIC)
|
|
: path.join(process.resourcesPath, "lib", "alg");
|
|
return path.join(basePath, algExecFile[platform]);
|
|
};
|
|
|
|
export const startALGServer = (entryPath: string) => {
|
|
const child_process = spawn(entryPath);
|
|
child_process.on("message", (data) => console.log(data));
|
|
child_process.stdout.on("data", (data) => log.info(data.toString()));
|
|
child_process.stderr.on("data", (data) => log.error(data.toString()));
|
|
};
|
|
|
|
/**
|
|
* 执行推理任务
|
|
*/
|
|
export const executeInferTask = (
|
|
task: InferReq,
|
|
onData: (data: string) => void
|
|
) => {
|
|
return new Promise((resolve, reject) => {
|
|
axios
|
|
.post(ALGServerRoot, task, {
|
|
responseType: "stream",
|
|
headers: { "Content-Type": "application/json" },
|
|
})
|
|
.then((response) => {
|
|
response.data.on("data", (chunk: Buffer) => {
|
|
const data = chunk.toString();
|
|
onData(data); // 实时处理数据
|
|
});
|
|
|
|
response.data.on("end", () => {
|
|
resolve(task); // 数据流结束
|
|
});
|
|
|
|
response.data.on("error", (err: Error) => {
|
|
console.log("error", err);
|
|
reject(err);
|
|
});
|
|
})
|
|
.catch((error) => {
|
|
console.log("axios error", error);
|
|
reject(error);
|
|
});
|
|
});
|
|
};
|