Skip to content

Commit

Permalink
Changed GetSupportedLanguages to be static and added CheckLibraryLoaded
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrohanea committed Oct 30, 2024
1 parent 00dfd98 commit 2efee05
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
25 changes: 12 additions & 13 deletions Whisper.net/WhisperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ public sealed class WhisperFactory : IDisposable

private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}
CheckLibraryLoaded();

this.loader = loader;
if (!delayInit)
Expand All @@ -57,10 +54,7 @@ private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
/// <exception cref="Exception"></exception>
public static string? GetRuntimeInfo()
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}
CheckLibraryLoaded();

var systemInfoPtr = libraryLoaded.Value.NativeWhisper!.WhisperPrintSystemInfo();
var systemInfoStr = Marshal.PtrToStringAnsi(systemInfoPtr);
Expand All @@ -72,12 +66,9 @@ private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
/// Returns an enumerable of the supported languages.
/// </summary>
/// <returns></returns>
public IEnumerable<string> GetSupportedLanguages()
public static IEnumerable<string> GetSupportedLanguages()
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}
CheckLibraryLoaded();

for (var i = 0; i < libraryLoaded.Value.NativeWhisper!.Whisper_Lang_Max_Id(); i++)
{
Expand Down Expand Up @@ -153,4 +144,12 @@ public void Dispose()
loader.Dispose();
wasDisposed = true;
}

private static void CheckLibraryLoaded()
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}
}
}
2 changes: 1 addition & 1 deletion tests/Whisper.net.Tests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public FactoryTests(TinyModelFixture model)
[Fact]
public void GetSupportedLanguages_ShouldReturnAll()
{
var languages = WhisperFactory.FromPath(model.ModelFile).GetSupportedLanguages().ToList();
var languages = WhisperFactory.GetSupportedLanguages().ToList();

languages.Should().HaveCount(99);
}
Expand Down

0 comments on commit 2efee05

Please sign in to comment.