2024-07-30 18:57:32 +00:00
|
|
|
using Whisper.net;
|
|
|
|
|
using Whisper.net.Ggml;
|
|
|
|
|
|
|
|
|
|
namespace BotSharp.Plugin.AudioHandler.Provider;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Native Whisper provider for speech to text conversion
|
|
|
|
|
/// </summary>
|
2024-08-28 16:08:12 +00:00
|
|
|
public class NativeWhisperProvider : IAudioCompletion
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
2024-08-26 22:24:07 +00:00
|
|
|
private static WhisperProcessor _whisperProcessor;
|
2024-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
private readonly IServiceProvider _services;
|
|
|
|
|
private readonly IFileStorageService _fileStorage;
|
2024-08-26 22:24:07 +00:00
|
|
|
private readonly ILogger<NativeWhisperProvider> _logger;
|
|
|
|
|
|
2024-08-18 00:01:10 +00:00
|
|
|
public string Provider => "native";
|
2024-07-30 18:57:32 +00:00
|
|
|
|
2024-08-11 16:26:57 +00:00
|
|
|
public NativeWhisperProvider(
|
2024-08-28 16:08:12 +00:00
|
|
|
BotSharpDatabaseSettings dbSettings,
|
|
|
|
|
IFileStorageService fileStorage,
|
|
|
|
|
IServiceProvider services,
|
2024-08-11 16:26:57 +00:00
|
|
|
ILogger<NativeWhisperProvider> logger)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
_fileStorage = fileStorage;
|
|
|
|
|
_services = services;
|
2024-08-11 16:26:57 +00:00
|
|
|
_logger = logger;
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
public async Task<string> GenerateTextFromAudioAsync(Stream audio, string audioFileName, string? text = null)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
|
|
|
|
var textResult = new List<SegmentData>();
|
2024-08-28 16:08:12 +00:00
|
|
|
|
|
|
|
|
using var stream = AudioHelper.Transform(audio, audioFileName);
|
2024-08-26 22:24:07 +00:00
|
|
|
await foreach (var result in _whisperProcessor.ProcessAsync(stream).ConfigureAwait(false))
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
|
|
|
|
textResult.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-26 22:24:07 +00:00
|
|
|
_whisperProcessor.Dispose();
|
2024-08-28 16:08:12 +00:00
|
|
|
stream.Close();
|
2024-08-17 23:34:31 +00:00
|
|
|
|
2024-07-30 18:57:32 +00:00
|
|
|
var audioOutput = new AudioOutput
|
|
|
|
|
{
|
|
|
|
|
Segments = textResult
|
|
|
|
|
};
|
|
|
|
|
return audioOutput.ToString();
|
|
|
|
|
}
|
2024-08-26 22:24:07 +00:00
|
|
|
|
2024-08-28 18:43:37 +00:00
|
|
|
public async Task<BinaryData> GenerateAudioFromTextAsync(string text)
|
2024-08-26 22:24:07 +00:00
|
|
|
{
|
|
|
|
|
throw new NotImplementedException();
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
public void SetModelName(string model)
|
2024-08-26 22:24:07 +00:00
|
|
|
{
|
|
|
|
|
if (Enum.TryParse(model, true, out GgmlType ggmlType))
|
|
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
LoadWhisperModel(ggmlType);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogWarning($"Unsupported model type: {model}. Use Tiny model instead!");
|
|
|
|
|
LoadWhisperModel(GgmlType.Tiny);
|
2024-08-26 22:24:07 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
private void LoadWhisperModel(GgmlType modelType)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
var modelDir = _fileStorage.BuildDirectory("models", "whisper");
|
|
|
|
|
var exist = _fileStorage.ExistDirectory(modelDir);
|
|
|
|
|
if (!exist)
|
2024-08-26 22:24:07 +00:00
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
_fileStorage.CreateDirectory(modelDir);
|
2024-08-26 22:24:07 +00:00
|
|
|
}
|
2024-07-30 18:57:32 +00:00
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
var files = _fileStorage.GetFiles("models/whisper", "*.bin");
|
|
|
|
|
var modelLoc = files.FirstOrDefault(x => Path.GetFileName(x) == BuildModelFile(modelType));
|
|
|
|
|
if (string.IsNullOrEmpty(modelLoc))
|
2024-08-17 23:34:31 +00:00
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
modelLoc = BuildModelPath(modelType);
|
|
|
|
|
DownloadModel(modelType, modelLoc);
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
2024-08-17 23:34:31 +00:00
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
var bytes = _fileStorage.GetFileBytes(modelLoc);
|
|
|
|
|
_whisperProcessor = WhisperFactory.FromBuffer(buffer: bytes).CreateBuilder().WithLanguage("auto").Build();
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2024-08-26 22:24:07 +00:00
|
|
|
var error = "Failed to load whisper model";
|
|
|
|
|
_logger.LogWarning($"${error}: {ex.Message}\r\n{ex.InnerException}");
|
|
|
|
|
throw new Exception($"{error}: {ex.Message}");
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
private void DownloadModel(GgmlType modelType, string modelDir)
|
|
|
|
|
{
|
|
|
|
|
using var modelStream = WhisperGgmlDownloader.GetGgmlModelAsync(modelType).ConfigureAwait(false).GetAwaiter().GetResult();
|
|
|
|
|
_fileStorage.SaveFileStreamToPath(modelDir, modelStream);
|
|
|
|
|
modelStream.Close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private string BuildModelPath(GgmlType modelType)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
return _fileStorage.BuildDirectory("models", "whisper", BuildModelFile(modelType));
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
2024-08-11 16:26:57 +00:00
|
|
|
|
2024-08-28 16:08:12 +00:00
|
|
|
private string BuildModelFile(GgmlType modelType)
|
2024-08-17 23:34:31 +00:00
|
|
|
{
|
2024-08-28 16:08:12 +00:00
|
|
|
return $"ggml-{modelType}.bin";
|
2024-08-17 23:34:31 +00:00
|
|
|
}
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|