Skip to content

Commit

Permalink
feat: import model (#40)
Browse files Browse the repository at this point in the history
Co-authored-by: linxiaodong <[email protected]>
  • Loading branch information
buxuku and linxiaodong authored Sep 29, 2024
1 parent c7be724 commit 22b450d
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
59 changes: 46 additions & 13 deletions main/helpers/systemInfoManager.ts
Original file line number Diff line number Diff line change
@@ -1,50 +1,83 @@
import { ipcMain, BrowserWindow } from 'electron';
import { checkWhisperInstalled, getModelsInstalled, getPath, deleteModel, downloadModelSync, install, makeWhisper } from './whisper';
import { ipcMain, BrowserWindow, dialog } from 'electron';
import {
checkWhisperInstalled,
getModelsInstalled,
getPath,
deleteModel,
downloadModelSync,
install,
makeWhisper,
} from './whisper';
import fs from 'fs';
import path from 'path';

let downloadingModels = new Set<string>();

export function setupSystemInfoManager(mainWindow: BrowserWindow) {
ipcMain.handle("getSystemInfo", async () => {
ipcMain.handle('getSystemInfo', async () => {
return {
whisperInstalled: checkWhisperInstalled(),
modelsInstalled: getModelsInstalled(),
modelsPath: getPath("modelsPath"),
modelsPath: getPath('modelsPath'),
downloadingModels: Array.from(downloadingModels),
};
});

ipcMain.handle("deleteModel", async (event, modelName) => {
ipcMain.handle('deleteModel', async (event, modelName) => {
await deleteModel(modelName);
return true;
});

ipcMain.handle("downloadModel", async (event, { model, source }) => {
ipcMain.handle('downloadModel', async (event, { model, source }) => {
downloadingModels.add(model);
const onProcess = (data) => {
const match = data?.match(/(\d+)%/);
if (match) {
event.sender.send("downloadProgress", model, +match[1]);
event.sender.send('downloadProgress', model, +match[1]);
}
if(data?.includes("Done") || data?.includes("main")) {
event.sender.send("downloadProgress", model, 100);
if (data?.includes('Done') || data?.includes('main')) {
event.sender.send('downloadProgress', model, 100);
}
};
try {
await downloadModelSync(model?.toLowerCase(), source, onProcess);
downloadingModels.delete(model);
} catch (error) {
event.sender.send("message", "下载失败,请切换下载源重试");
event.sender.send('message', '下载失败,请切换下载源重试');
downloadingModels.delete(model);
return false;
}
return true;
});

ipcMain.on("installWhisper", (event, source) => {
ipcMain.on('installWhisper', (event, source) => {
install(event, source);
});

ipcMain.on("makeWhisper", (event) => {
ipcMain.on('makeWhisper', (event) => {
makeWhisper(event);
});
}

ipcMain.handle('importModel', async (event) => {
const result = await dialog.showOpenDialog(mainWindow, {
properties: ['openFile'],
filters: [{ name: 'Model Files', extensions: ['bin'] }],
});

if (!result.canceled && result.filePaths.length > 0) {
const sourcePath = result.filePaths[0];
const fileName = path.basename(sourcePath);
const destPath = path.join(getPath('modelsPath'), fileName);

try {
await fs.promises.copyFile(sourcePath, destPath);
return true;
} catch (error) {
console.error('导入模型失败:', error);
return false;
}
}

return false;
});
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"private": true,
"name": "video-subtitle-master",
"description": "视频转字幕,字幕翻译软件",
"version": "1.0.19",
"version": "1.0.20",
"author": "buxuku <[email protected]>",
"main": "app/background.js",
"scripts": {
Expand Down
48 changes: 33 additions & 15 deletions renderer/pages/modelsControl.tsx
Original file line number Diff line number Diff line change
@@ -1,60 +1,75 @@
import React, { useEffect, useRef, useState } from "react";
import React, { useEffect, useRef, useState } from 'react';
import {
Card,
CardContent,
CardDescription,
CardHeader,
CardTitle,
} from "@/components/ui/card";
} from '@/components/ui/card';
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
} from '@/components/ui/table';
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/components/ui/select";
import { models } from "lib/utils";
import { Button } from "@/components/ui/button";
import { ISystemInfo } from "../types";
import DeleteModel from "@/components/DeleteModel";
import DownModel from "@/components/DownModel";
import DownModelButton from "@/components/DownModelButton";
} from '@/components/ui/select';
import { models } from 'lib/utils';
import { Button } from '@/components/ui/button';
import { ISystemInfo } from '../types';
import DeleteModel from '@/components/DeleteModel';
import DownModel from '@/components/DownModel';
import DownModelButton from '@/components/DownModelButton';
import { Upload } from 'lucide-react'; // 导入上传图标

const ModelsControl = () => {
const [systemInfo, setSystemInfo] = React.useState<ISystemInfo>({
whisperInstalled: true,
modelsInstalled: [],
downloadingModels: [],
});
const [downSource, setDownSource] = useState("hf-mirror");
const [downSource, setDownSource] = useState('hf-mirror');
useEffect(() => {
updateSystemInfo();
}, []);
const updateSystemInfo = async () => {
const systemInfoRes = await window?.ipc?.invoke("getSystemInfo", null);
const systemInfoRes = await window?.ipc?.invoke('getSystemInfo', null);
setSystemInfo(systemInfoRes);
};
const isInstalledModel = (name) =>
systemInfo?.modelsInstalled?.includes(name.toLowerCase());
const handleDownSource = (value: string) => {
setDownSource(value);
};
const handleImportModel = async () => {
try {
const result = await window?.ipc?.invoke('importModel');
if (result) {
updateSystemInfo();
}
} catch (error) {
console.error('导入模型失败:', error);
// 这里可以添加一个错误提示
}
};

return (
<Card>
<CardHeader>
<CardTitle>模型管理</CardTitle>
<CardDescription>
您可以在这里管理你的模型,下载,删除 <br />
模型保存位置: {systemInfo?.modelsPath}
<span className="float-right -mt-4 flex items-center">
您可以在这里管理你的模型,下载,删除,或导入 <br />
模型保存位置:
<br />
{systemInfo?.modelsPath}
<span className="float-right mt-4 flex items-center">
<span>切换下载源:</span>
<Select onValueChange={handleDownSource} value={downSource}>
<SelectTrigger className="w-[250px]">
Expand All @@ -67,6 +82,9 @@ const ModelsControl = () => {
</SelectItem>
</SelectContent>
</Select>
<Button onClick={handleImportModel} className="ml-4">
<Upload className="mr-2 h-4 w-4" /> 导入模型
</Button>
</span>
</CardDescription>
</CardHeader>
Expand Down

0 comments on commit 22b450d

Please sign in to comment.