From 2ab84fc0679e92dd09d7399cde8bb5c0cdb86dde Mon Sep 17 00:00:00 2001 From: an-lee Date: Wed, 21 Aug 2024 15:05:39 +0800 Subject: [PATCH] Refactor whisper notification (#1003) * update notification when whisper failed * fix style --- enjoy/src/i18n/en.json | 1 + enjoy/src/i18n/zh-CN.json | 1 + enjoy/src/main/whisper.ts | 12 ++++++++++-- .../src/renderer/components/audios/audios-table.tsx | 6 ++---- .../src/renderer/components/videos/videos-table.tsx | 6 ++---- enjoy/src/renderer/hooks/use-transcribe.tsx | 4 ++++ 6 files changed, 20 insertions(+), 10 deletions(-) diff --git a/enjoy/src/i18n/en.json b/enjoy/src/i18n/en.json index 55a00b71..722d91ba 100644 --- a/enjoy/src/i18n/en.json +++ b/enjoy/src/i18n/en.json @@ -411,6 +411,7 @@ "pleaseDownloadWhisperModelFirst": "Please download whisper model first", "whisperIsWorkingGood": "Whisper is working good", "whisperIsNotWorking": "Whisper is not working", + "whisperTranscribeFailed": "Whisper transcribe failed. Maybe your machine is not compatbile with it. Please use other STT service. {{error}}", "checkingWhisperModel": "Checking whisper model", "whisperModelIsWorkingGood": "Whisper model is working good", "whisperModelIsNotWorking": "Whisper model is not working", diff --git a/enjoy/src/i18n/zh-CN.json b/enjoy/src/i18n/zh-CN.json index c86bdfa5..1940de12 100644 --- a/enjoy/src/i18n/zh-CN.json +++ b/enjoy/src/i18n/zh-CN.json @@ -411,6 +411,7 @@ "pleaseDownloadWhisperModelFirst": "请先下载 Whisper 模型", "whisperIsWorkingGood": "Whisper 正常工作", "whisperIsNotWorking": "Whisper 无法正常工作,请尝试更换模型后重试,或联系开发者", + "whisperTranscribeFailed": "Whisper 语音转文本失败。您的设备可能不兼容该功能,请选择其他语音转文本服务。{{error}}", "checkingWhisperModel": "正在检查 Whisper 模型", "whisperModelIsWorkingGood": "Whisper 模型正常工作", "whisperModelIsNotWorking": "Whisper 模型无法正常工作,请尝试更换模型后重试,或联系开发者", diff --git a/enjoy/src/main/whisper.ts b/enjoy/src/main/whisper.ts index 6ebf6803..a501f7fb 100644 --- a/enjoy/src/main/whisper.ts +++ b/enjoy/src/main/whisper.ts @@ -7,6 +7,7 @@ import fs from "fs-extra"; import log from "@main/logger"; import url from "url"; import { enjoyUrlToPath } from "./utils"; +import { t } from "i18next"; const __filename = url.fileURLToPath(import.meta.url); /* @@ -299,12 +300,19 @@ class Whipser { }); ipcMain.handle("whisper-transcribe", async (event, params, options) => { - return await this.transcribe(params, { + return this.transcribe(params, { ...options, onProgress: (progress) => { event.sender.send("whisper-on-progress", progress); }, - }); + }) + .then((result) => { + return result; + }) + .catch((err) => { + logger.error(err); + throw t("whisperTranscribeFailed", { error: err.message }); + }); }); ipcMain.handle("whisper-abort", async (_event) => { diff --git a/enjoy/src/renderer/components/audios/audios-table.tsx b/enjoy/src/renderer/components/audios/audios-table.tsx index 49c63aa0..9f9801b6 100644 --- a/enjoy/src/renderer/components/audios/audios-table.tsx +++ b/enjoy/src/renderer/components/audios/audios-table.tsx @@ -96,12 +96,10 @@ export const AudiosTable = (props: { {dayjs(audio.createdAt).format("YYYY-MM-DD HH:mm")} - {audio.transcribing ? ( - - ) : audio.transcribed ? ( + {audio.transcribed ? ( ) : ( - + )} diff --git a/enjoy/src/renderer/components/videos/videos-table.tsx b/enjoy/src/renderer/components/videos/videos-table.tsx index bc222b8a..f6827a65 100644 --- a/enjoy/src/renderer/components/videos/videos-table.tsx +++ b/enjoy/src/renderer/components/videos/videos-table.tsx @@ -96,12 +96,10 @@ export const VideosTable = (props: { {dayjs(video.createdAt).format("YYYY-MM-DD HH:mm")} - {video.transcribing ? ( - - ) : video.transcribed ? ( + {video.transcribed ? ( ) : ( - + )} diff --git a/enjoy/src/renderer/hooks/use-transcribe.tsx b/enjoy/src/renderer/hooks/use-transcribe.tsx index 90e6fdf1..5db04258 100644 --- a/enjoy/src/renderer/hooks/use-transcribe.tsx +++ b/enjoy/src/renderer/hooks/use-transcribe.tsx @@ -222,6 +222,10 @@ export const useTranscribe = () => { } ); + if (!res) { + throw new Error(t("whisperTranscribeFailed", { error: "" })); + } + const timeline: TimelineEntry[] = res.transcription .map((segment) => { // ignore the word if it is empty or in the format of `[xxx]` or `(xxx)`