Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed loading model from path with non-English letters #98

Merged
merged 2 commits into from
Aug 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading