Skip to content

Commit

Permalink
Fixed loading model from path with non-English letters (#98)
Browse files Browse the repository at this point in the history
* Remove whisper_init_from_file_with_params

* Minor changes
  • Loading branch information
Macoron authored Aug 25, 2024
1 parent fb21679 commit 2754062
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 40 deletions.
6 changes: 1 addition & 5 deletions Packages/com.whisper.unity/Runtime/Native/WhisperNative.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions Packages/com.whisper.unity/Runtime/Utils/FileUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -27,6 +32,11 @@ public static async Task<byte[]> 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
}
Expand Down
43 changes: 8 additions & 35 deletions Packages/com.whisper.unity/Runtime/WhisperWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,40 +271,14 @@ public static WhisperWrapper InitFromFile(string modelPath)
/// <returns>Loaded whisper model. Null if loading failed.</returns>
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;
}

/// <summary>
Expand All @@ -326,14 +300,13 @@ public static async Task<WhisperWrapper> InitFromFileAsync(string modelPath)
/// <returns>Loaded whisper model. Null if loading failed.</returns>
public static async Task<WhisperWrapper> 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
}

/// <summary>
Expand Down

0 comments on commit 2754062

Please sign in to comment.