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"; import { modelPath } from "./initial"; export const ALGServerRoot = "http://127.0.0.1:5000/root"; export const getAlgPath = (platform: "macos" | "windows"): string => { const algExecFile = { macos: "", windows: "main.exe", }; const execFilePath = path.join(modelPath, "v1", algExecFile[platform]); return execFilePath; }; 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); }); }); };