From d16f0f0ad06aed2f5f620a47aaa5526712a4a2d3 Mon Sep 17 00:00:00 2001 From: rerender2021 Date: Sat, 1 Apr 2023 07:46:10 +0800 Subject: [PATCH 1/3] fix typo --- src/whisper/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/whisper/index.ts b/src/whisper/index.ts index 14effad..19dc311 100644 --- a/src/whisper/index.ts +++ b/src/whisper/index.ts @@ -56,7 +56,7 @@ export class Whisper { const exePath = path.resolve(dir, "./Whisper-API.exe"); if (fs.existsSync(dir) && fs.existsSync(exePath)) { return new Promise((resolve, reject) => { - console.log("asrDir exists, start asr server", dir); + console.log("whisper dir exists, start whisper server", dir); const name = getModel(); console.log("start whisper server with model: ", name); From 82e55fc22f95f3e8f35465515e2e003c1ebb1c8a Mon Sep 17 00:00:00 2001 From: rerender2021 Date: Sat, 1 Apr 2023 08:15:22 +0800 Subject: [PATCH 2/3] impl batch --- src/app.tsx | 43 ++++++++++++++++++++++++++++------------ src/hooks/useDragDrop.ts | 31 +++++++++++++++++------------ src/layout/index.ts | 2 +- 3 files changed, 49 insertions(+), 27 deletions(-) diff --git a/src/app.tsx b/src/app.tsx index 91688cd..daeb595 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -3,7 +3,7 @@ import { AveRenderer, Grid, Window, getAppContext, IIconResource, IWindowCompone import { App, ThemePredefined_Dark } from "ave-ui"; import { containerLayout, controlLayout } from "./layout"; import { iconResource } from "./resource"; -import { safe } from "./common"; +import { safe, sleep } from "./common"; import axios from "axios"; import { ProgressType, Whisper } from "./whisper"; import { useDragDrop } from "./hooks"; @@ -23,9 +23,10 @@ function initTheme() { themeDark.SetStyle(themeImage, 0); } -enum ButtonText { +enum Text { GenerateSubtitle = "生成字幕", - OpenFile = "选择文件" + OpenFile = "选择文件", + ProgressLabel = "进度", } const modelOptions = getModelList().map((each, index) => { @@ -47,11 +48,18 @@ export function Heard() { const [title, setTitle] = useState("Heard"); const [whisperReady, setwhisperReady] = useState(false); const [src, setSrc] = useState(""); + const [srcDesc, setSrcDesc] = useState(""); + const [srcList, setSrcList] = useState>([""]); const promptRef = useRef(""); - useDragDrop((path) => setSrc(path)); + useDragDrop((pathList) => { + setSrcList(pathList); + setSrcDesc(pathList.length === 1 ? pathList[0] : `${pathList.length} files`); + setProgressLabelText(`${Text.ProgressLabel}: 0/${pathList.length}`); + }); const [progress, setProgress] = useState(ProgressType.None); const [progressText, setProgressText] = useState(""); + const [progressLabelText, setProgressLabelText] = useState(Text.ProgressLabel); const [defaultSelectedKey, setDefaultSelectedKey] = useState("1"); const [hintText, setHintText] = useState("初始化中..."); @@ -65,6 +73,7 @@ export function Heard() { if (filePath) { setSrc(filePath); + setProgressLabelText(Text.ProgressLabel); } }), [] @@ -75,8 +84,13 @@ export function Heard() { if (progress !== ProgressType.None) { return; } - whisper.transcribe(src, promptRef.current).then( - safe((response) => { + + for (let i = 0; i < srcList.length; ++i) { + try { + const src = srcList[i]; + setSrc(src); + setProgressLabelText(`${Text.ProgressLabel}: ${i + 1}/${srcList.length}: ${src}`); + const response = await whisper.transcribe(src, promptRef.current); const data = response.data; const dirName = path.dirname(src); const fileName = path.basename(src); @@ -84,10 +98,13 @@ export function Heard() { const outPath = path.resolve(dirName, `${fileName}.subtitle.json`); fs.writeFileSync(outPath, JSON.stringify(data, null, 4), "utf8"); - }) - ); + await sleep(1500); + } catch (error) { + console.error(error?.message); + } + } }), - [src] + [srcList] ); const startWhisper = safe(() => { @@ -174,10 +191,10 @@ export function Heard() { - + - + {whisperReady ? ( <> @@ -191,10 +208,10 @@ export function Heard() { - + - + diff --git a/src/hooks/useDragDrop.ts b/src/hooks/useDragDrop.ts index 6411361..75fedcc 100644 --- a/src/hooks/useDragDrop.ts +++ b/src/hooks/useDragDrop.ts @@ -1,27 +1,32 @@ import { getAppContext } from "ave-react"; import { useEffect, useState } from "react"; import { DragDropImage, DropBehavior } from "ave-ui"; +import { safe } from "../common"; -export function useDragDrop(onPathChange?: (path: string) => void) { - const [path, setPath] = useState(""); +export function useDragDrop(onPathChange?: (pathList: Array) => void) { + const [pathList, setPathList] = useState([""]); useEffect(() => { const context = getAppContext(); const window = context.getWindow(); - window.OnDragMove((sender, dc) => { - if (1 === dc.FileGetCount()) { - dc.SetDropTip(DragDropImage.Copy, "打开此文件"); + window.OnDragMove( + safe((sender, dc) => { + const fileCount = dc.FileGetCount(); + dc.SetDropTip(DragDropImage.Copy, fileCount === 1 ? "选择此文件" : `选择文件x${fileCount}`); dc.SetDropBehavior(DropBehavior.Copy); - } - }); + }) + ); - window.OnDragDrop((sender, dc) => { - const filePath = dc.FileGet()[0]; - setPath(path); - onPathChange?.(filePath ?? ""); - }); + window.OnDragDrop( + safe((sender, dc) => { + const filePaths = dc.FileGet(); + console.log(`on drap drop, file paths:`, { filePaths }); + setPathList(filePaths); + onPathChange?.(filePaths ?? [""]); + }) + ); }, []); - return { path }; + return { pathList }; } diff --git a/src/layout/index.ts b/src/layout/index.ts index 906af38..4dddbfb 100644 --- a/src/layout/index.ts +++ b/src/layout/index.ts @@ -27,7 +27,7 @@ export const controlLayout = { selectModel: { row: 5, column: 0, columnSpan: 3 }, generate: { row: 5, column: 3, columnSpan: 5 }, - progressLabel: { row: 7, column: 0, columnSpan: 4 }, + progressLabel: { row: 7, column: 0, columnSpan: 8 }, progress: { row: 9, column: 0, columnSpan: 8 } }, }; From f770cd0f61f05289d4600a28f59729ead86b37a5 Mon Sep 17 00:00:00 2001 From: rerender2021 Date: Sat, 1 Apr 2023 08:22:53 +0800 Subject: [PATCH 3/3] fix open file --- src/app.tsx | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/app.tsx b/src/app.tsx index daeb595..d91aa1b 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -72,7 +72,8 @@ export function Heard() { console.log(`open file: ${filePath}`); if (filePath) { - setSrc(filePath); + setSrcList([filePath]); + setSrcDesc(filePath); setProgressLabelText(Text.ProgressLabel); } }),