From 92d3edc3de743f54e08560f3dae867aed90c5188 Mon Sep 17 00:00:00 2001 From: niksedk Date: Thu, 16 Nov 2023 18:34:16 +0100 Subject: [PATCH] Add Whisper CPP cuBLAS --- src/libse/AudioToText/WhisperChoice.cs | 1 + src/libse/AudioToText/WhisperHelper.cs | 35 +++++++++--- .../Forms/AudioToText/WhisperAudioToText.cs | 57 +++++++++++++++++-- src/ui/Forms/AudioToText/WhisperDownload.cs | 30 +++++++++- 4 files changed, 109 insertions(+), 14 deletions(-) diff --git a/src/libse/AudioToText/WhisperChoice.cs b/src/libse/AudioToText/WhisperChoice.cs index 417a950e3..8048dfc39 100644 --- a/src/libse/AudioToText/WhisperChoice.cs +++ b/src/libse/AudioToText/WhisperChoice.cs @@ -4,6 +4,7 @@ { public const string OpenAi = "OpenAI"; public const string Cpp = "CPP"; + public const string CppCuBlas = "CPP cuBLAS"; public const string WhisperX = "WhisperX"; public const string ConstMe = "Const-me"; public const string CTranslate2 = "CTranslate2"; diff --git a/src/libse/AudioToText/WhisperHelper.cs b/src/libse/AudioToText/WhisperHelper.cs index 0ecb611f7..23d17470c 100644 --- a/src/libse/AudioToText/WhisperHelper.cs +++ b/src/libse/AudioToText/WhisperHelper.cs @@ -8,7 +8,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText { public static IWhisperModel GetWhisperModel() { - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp) + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return new WhisperCppModel(); } @@ -33,7 +33,8 @@ namespace Nikse.SubtitleEdit.Core.AudioToText public static string ModelExtension() { - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || + Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe) { return ".bin"; @@ -50,7 +51,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText public static string GetWebSiteUrl() { - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp) + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return "https://github.com/ggerganov/whisper.cpp"; } @@ -106,6 +107,12 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return Directory.Exists(path) ? path : null; } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) + { + var path = Path.Combine(Configuration.DataDirectory, "Whisper", WhisperChoice.CppCuBlas); + return Directory.Exists(path) ? path : null; + } + if (!Configuration.IsRunningOnWindows) { return null; @@ -173,6 +180,12 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return Directory.Exists(path) ? path : null; } + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) + { + var path = Path.Combine(Configuration.DataDirectory, "Whisper", WhisperChoice.CppCuBlas); + return Directory.Exists(path) ? path : null; + } + if (whisperChoice == WhisperChoice.WhisperX && !string.IsNullOrEmpty(Configuration.Settings.Tools.WhisperXLocation)) { if (Configuration.Settings.Tools.WhisperXLocation.EndsWith("whisperx.exe", StringComparison.InvariantCultureIgnoreCase) && File.Exists(Configuration.Settings.Tools.WhisperXLocation)) @@ -288,7 +301,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText var whisperFolder = GetWhisperFolder(whisperChoice); if (string.IsNullOrEmpty(whisperFolder)) { - if (whisperChoice == WhisperChoice.Cpp) + if (whisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return "main"; } @@ -323,7 +336,12 @@ namespace Nikse.SubtitleEdit.Core.AudioToText if (Configuration.IsRunningOnWindows) { - if (whisperChoice == WhisperChoice.Cpp) + if (whisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) + { + return "main.exe"; + } + + if (whisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return "main.exe"; } @@ -356,7 +374,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText return "whisper.exe"; } - if (Configuration.IsRunningOnLinux && whisperChoice == WhisperChoice.Cpp) + if (Configuration.IsRunningOnLinux && whisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return "main"; } @@ -398,7 +416,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText if (Configuration.IsRunningOnWindows) { - if (whisperChoice == WhisperChoice.Cpp) + if (whisperChoice == WhisperChoice.Cpp || whisperChoice == WhisperChoice.CppCuBlas) { var f = Path.Combine(whisperFolder, fileNameOnly); if (File.Exists(f)) @@ -469,7 +487,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText public static string GetWhisperModelForCmdLine(string model) { - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp) + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { return Path.Combine(GetWhisperModel().ModelFolder, model + ModelExtension()); } @@ -485,6 +503,7 @@ namespace Nikse.SubtitleEdit.Core.AudioToText public static string GetWhisperTranslateParameter() { return Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || + Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe ? "--translate " : "--task translate "; diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.cs index e4dac7bc2..3e213b6b5 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.cs @@ -146,9 +146,14 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText if (Configuration.IsRunningOnWindows) { engines.Add(WhisperChoice.PurfviewFasterWhisper); + engines.Add(WhisperChoice.Cpp); + engines.Add(WhisperChoice.CppCuBlas); engines.Add(WhisperChoice.ConstMe); } - engines.Add(WhisperChoice.Cpp); + else + { + engines.Add(WhisperChoice.Cpp); + } engines.Add(WhisperChoice.CTranslate2); engines.Add(WhisperChoice.StableTs); engines.Add(WhisperChoice.WhisperX); @@ -285,6 +290,28 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } } + if (comboBoxWhisperEngine.Text == WhisperChoice.CppCuBlas) + { + var fileName = WhisperHelper.GetWhisperPathAndFileName(WhisperChoice.CppCuBlas); + if (!File.Exists(fileName)) + { + if (MessageBox.Show(string.Format(LanguageSettings.Current.Settings.DownloadX, "Whisper " + WhisperChoice.CppCuBlas), "Subtitle Edit", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + using (var downloadForm = new WhisperDownload(WhisperChoice.CppCuBlas)) + { + if (downloadForm.ShowDialog(this) != DialogResult.OK) + { + return; + } + } + } + else + { + return; + } + } + } + if (comboBoxWhisperEngine.Text == WhisperChoice.PurfviewFasterWhisper) { var fileName = WhisperHelper.GetWhisperPathAndFileName(WhisperChoice.PurfviewFasterWhisper); @@ -1055,7 +1082,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText translateToEnglish = string.Empty; } - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp) + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas) { if (!Configuration.Settings.Tools.WhisperExtraSettings.Contains("--print-progress")) { @@ -1065,7 +1092,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText var outputSrt = string.Empty; var postParams = string.Empty; - if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || + if (Configuration.Settings.Tools.WhisperChoice == WhisperChoice.Cpp || + Configuration.Settings.Tools.WhisperChoice == WhisperChoice.CppCuBlas || Configuration.Settings.Tools.WhisperChoice == WhisperChoice.ConstMe) { outputSrt = "--output-srt "; @@ -1106,7 +1134,8 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } if (Configuration.Settings.Tools.WhisperChoice != WhisperChoice.Cpp && - Configuration.Settings.Tools.WhisperChoice != WhisperChoice.ConstMe) + Configuration.Settings.Tools.WhisperChoice != WhisperChoice.CppCuBlas && + Configuration.Settings.Tools.WhisperChoice != WhisperChoice.ConstMe) { process.StartInfo.EnvironmentVariables["PYTHONIOENCODING"] = "utf-8"; process.StartInfo.EnvironmentVariables["PYTHONUTF8"] = "1"; @@ -1834,6 +1863,26 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText Init(); } + else if (comboBoxWhisperEngine.Text == WhisperChoice.CppCuBlas) + { + Configuration.Settings.Tools.WhisperChoice = WhisperChoice.CppCuBlas; + var fileName = WhisperHelper.GetWhisperPathAndFileName(); + if (!File.Exists(fileName) || WhisperDownload.IsOld(fileName, WhisperChoice.CppCuBlas)) + { + if (MessageBox.Show(string.Format(LanguageSettings.Current.Settings.DownloadX, "Whisper " + WhisperChoice.CppCuBlas), "Subtitle Edit", MessageBoxButtons.YesNoCancel) == DialogResult.Yes) + { + using (var downloadForm = new WhisperDownload(WhisperChoice.CppCuBlas)) + { + if (downloadForm.ShowDialog(this) != DialogResult.OK) + { + return; + } + } + } + } + + Init(); + } else if (comboBoxWhisperEngine.Text == WhisperChoice.ConstMe) { whisperConstMeToolStripMenuItem_Click(null, null); diff --git a/src/ui/Forms/AudioToText/WhisperDownload.cs b/src/ui/Forms/AudioToText/WhisperDownload.cs index 51cdf63a9..26a3262b4 100644 --- a/src/ui/Forms/AudioToText/WhisperDownload.cs +++ b/src/ui/Forms/AudioToText/WhisperDownload.cs @@ -46,6 +46,14 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText "2a9e10f746a1ebe05dffa86e9f66cd20848faa6e849f3300c2281051c1a17b0fc35c60dc435f07f5974aa1191000aaf2866a4f03a5fe35ecffd4ae0919778e63", // SSE2 32-bit }; + + private const string DownloadUrl64CppCuBlas = "https://github.com/ggerganov/whisper.cpp/releases/download/v1.5.0/whisper-cublas-bin-x64.zip"; + + private static readonly string[] Sha512HashesCppCuBlas = + { + "de5b6fe7487f4cdc5e883ef6825dd0ecfe3ce4f9c914b0e02ba19b89c138e47e76584ae221a75eb7aed1a96893d4764401e065e196e0455a2e72050209252780", // 1.5.0 + }; + private const string DownloadUrlConstMe = "https://github.com/Const-me/Whisper/releases/download/1.12.0/cli.zip"; private static readonly string[] Sha512HashesConstMe = @@ -113,7 +121,11 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText private void WhisperDownload_Shown(object sender, EventArgs e) { var downloadUrl = IntPtr.Size * 8 == 32 ? DownloadUrl32Cpp : DownloadUrl64Cpp; - if (_whisperChoice == WhisperChoice.ConstMe) + if (_whisperChoice == WhisperChoice.CppCuBlas) + { + downloadUrl = DownloadUrl64CppCuBlas; + } + else if (_whisperChoice == WhisperChoice.ConstMe) { downloadUrl = DownloadUrlConstMe; } @@ -173,7 +185,11 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText downloadStream.Position = 0; var hash = Utilities.GetSha512Hash(downloadStream.ToArray()); string[] hashes; - if (_whisperChoice == WhisperChoice.ConstMe) + if (_whisperChoice == WhisperChoice.CppCuBlas) + { + hashes = Sha512HashesCppCuBlas; + } + else if (_whisperChoice == WhisperChoice.ConstMe) { hashes = Sha512HashesConstMe; } @@ -214,6 +230,16 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText } } + if (_whisperChoice == WhisperChoice.CppCuBlas) + { + folder = Path.Combine(folder, WhisperChoice.CppCuBlas); + + if (!Directory.Exists(folder)) + { + Directory.CreateDirectory(folder); + } + } + if (_whisperChoice == WhisperChoice.ConstMe) { folder = Path.Combine(folder, "Const-me");