From 275406258aca21fe7753cf0724a65f06fd464eea Mon Sep 17 00:00:00 2001 From: Alex Evgrashin Date: Sun, 25 Aug 2024 22:45:08 +0200 Subject: [PATCH] Fixed loading model from path with non-English letters (#98) * Remove whisper_init_from_file_with_params * Minor changes --- .../Runtime/Native/WhisperNative.cs | 6 +-- .../Runtime/Utils/FileUtils.cs | 10 +++++ .../Runtime/WhisperWrapper.cs | 43 ++++--------------- 3 files changed, 19 insertions(+), 40 deletions(-) diff --git a/Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs b/Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs index adc6e3f..3c42886 100644 --- a/Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs +++ b/Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs @@ -44,11 +44,7 @@ static WhisperNative() Environment.SetEnvironmentVariable("GGML_METAL_PATH_RESOURCES",path); #endif } - - [DllImport(LibraryName)] - public static extern whisper_context_ptr whisper_init_from_file_with_params(string path_model, - WhisperNativeContextParams @params); - + [DllImport(LibraryName)] public static extern whisper_context_ptr whisper_init_from_buffer_with_params(IntPtr buffer, UIntPtr buffer_size, WhisperNativeContextParams @params); diff --git a/Packages/com.whisper.unity/Runtime/Utils/FileUtils.cs b/Packages/com.whisper.unity/Runtime/Utils/FileUtils.cs index 961ce2f..e444280 100644 --- a/Packages/com.whisper.unity/Runtime/Utils/FileUtils.cs +++ b/Packages/com.whisper.unity/Runtime/Utils/FileUtils.cs @@ -15,6 +15,11 @@ public static byte[] ReadFile(string path) #if UNITY_ANDROID && !UNITY_EDITOR return ReadFileWebRequest(path); #else + if (!File.Exists(path)) + { + LogUtils.Error($"Path {path} doesn't exist!"); + return null; + } return File.ReadAllBytes(path); #endif } @@ -27,6 +32,11 @@ public static async Task ReadFileAsync(string path) #if UNITY_ANDROID && !UNITY_EDITOR return await ReadFileWebRequestAsync(path); #else + if (!File.Exists(path)) + { + LogUtils.Error($"File: '{path}' doesn't exist!"); + return null; + } return await ReadAllBytesAsync(path); #endif } diff --git a/Packages/com.whisper.unity/Runtime/WhisperWrapper.cs b/Packages/com.whisper.unity/Runtime/WhisperWrapper.cs index 089152c..ddc5e59 100644 --- a/Packages/com.whisper.unity/Runtime/WhisperWrapper.cs +++ b/Packages/com.whisper.unity/Runtime/WhisperWrapper.cs @@ -271,40 +271,14 @@ public static WhisperWrapper InitFromFile(string modelPath) /// Loaded whisper model. Null if loading failed. public static WhisperWrapper InitFromFile(string modelPath, WhisperContextParams contextParams) { -#if UNITY_ANDROID && !UNITY_EDITOR - var buffer = FileUtils.ReadFile(modelPath); - var res = InitFromBuffer(buffer, contextParams); - return res; -#else // load model weights LogUtils.Log($"Trying to load Whisper model from {modelPath}..."); - - // some sanity checks - if (string.IsNullOrEmpty(modelPath)) - { - LogUtils.Error("Whisper model path is null or empty!"); - return null; - } - if (!File.Exists(modelPath)) - { - LogUtils.Error($"Whisper model path {modelPath} doesn't exist!"); - return null; - } - - // actually loading model - var sw = new Stopwatch(); - sw.Start(); - - var ctx = WhisperNative.whisper_init_from_file_with_params(modelPath, contextParams.NativeParams); - if (ctx == IntPtr.Zero) - { - LogUtils.Error("Failed to load Whisper model!"); + var buffer = FileUtils.ReadFile(modelPath); + if (buffer == null) return null; - } - LogUtils.Log($"Whisper model is loaded, total time: {sw.ElapsedMilliseconds} ms."); - return new WhisperWrapper(ctx); -#endif + var res = InitFromBuffer(buffer, contextParams); + return res; } /// @@ -326,14 +300,13 @@ public static async Task InitFromFileAsync(string modelPath) /// Loaded whisper model. Null if loading failed. public static async Task InitFromFileAsync(string modelPath, WhisperContextParams contextParams) { -#if UNITY_ANDROID && !UNITY_EDITOR + LogUtils.Log($"Trying to load Whisper model from {modelPath}..."); var buffer = await FileUtils.ReadFileAsync(modelPath); + if (buffer == null) + return null; + var res = await InitFromBufferAsync(buffer, contextParams); return res; -#else - var asyncTask = Task.Factory.StartNew(() => InitFromFile(modelPath, contextParams)); - return await asyncTask; -#endif } ///