Skip to content

Commit

Permalink
Added GetSupportedLanguages method
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrohanea committed Oct 30, 2024
1 parent 935b846 commit 00dfd98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
22 changes: 22 additions & 0 deletions Whisper.net/WhisperFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,28 @@ private WhisperFactory(IWhisperProcessorModelLoader loader, bool delayInit)
return systemInfoStr;
}

/// <summary>
/// Returns an enumerable of the supported languages.
/// </summary>
/// <returns></returns>
public IEnumerable<string> GetSupportedLanguages()
{
if (!libraryLoaded.Value.IsSuccess)
{
throw new Exception($"Failed to load native whisper library. Error: {libraryLoaded.Value.ErrorMessage}");
}

for (var i = 0; i < libraryLoaded.Value.NativeWhisper!.Whisper_Lang_Max_Id(); i++)
{
var languagePtr = libraryLoaded.Value.NativeWhisper!.Whisper_Lang_Str(i);
var language = Marshal.PtrToStringAnsi(languagePtr);
if (!string.IsNullOrEmpty(language))
{
yield return language;
}
}
}

/// <summary>
/// Creates a factory that uses the ggml model from a path in order to create <seealso cref="WhisperProcessorBuilder"/>.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions tests/Whisper.net.Tests/FactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ public FactoryTests(TinyModelFixture model)
this.model = model;
}

[Fact]
public void GetSupportedLanguages_ShouldReturnAll()
{
var languages = WhisperFactory.FromPath(model.ModelFile).GetSupportedLanguages().ToList();

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

[Fact]
public void CreateBuilder_WithNoModel_ShouldThrow()
{
Expand Down

0 comments on commit 00dfd98

Please sign in to comment.