2024-09-20 15:55:17 +08:00
|
|
|
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";
|
2024-10-13 10:10:05 +08:00
|
|
|
import { modelPath } from "./initial";
|
2024-09-20 15:55:17 +08:00
|
|
|
|
|
|
|
export const ALGServerRoot = "http://127.0.0.1:5000/root";
|
|
|
|
|
2024-10-13 10:10:05 +08:00
|
|
|
export const getAlgPath = (platform: "macos" | "windows"): string => {
|
2024-09-29 17:01:21 +08:00
|
|
|
const algExecFile = {
|
|
|
|
macos: "",
|
|
|
|
windows: "main.exe",
|
|
|
|
};
|
2024-10-13 10:10:05 +08:00
|
|
|
const execFilePath = path.join(modelPath, "v1", algExecFile[platform]);
|
|
|
|
return execFilePath;
|
2024-09-20 15:55:17 +08:00
|
|
|
};
|
|
|
|
|
2024-09-29 17:01:21 +08:00
|
|
|
export const startALGServer = (entryPath: string) => {
|
2024-09-20 15:55:17 +08:00
|
|
|
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) => {
|
2024-09-20 16:48:32 +08:00
|
|
|
console.log("error", err);
|
2024-09-20 15:55:17 +08:00
|
|
|
reject(err);
|
|
|
|
});
|
|
|
|
})
|
2024-09-20 16:48:32 +08:00
|
|
|
.catch((error) => {
|
|
|
|
console.log("axios error", error);
|
|
|
|
reject(error);
|
|
|
|
});
|
2024-09-20 15:55:17 +08:00
|
|
|
});
|
|
|
|
};
|