2024-08-11 16:26:57 +00:00
|
|
|
using BotSharp.Core.Agents.Services;
|
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>
|
|
|
|
|
public class NativeWhisperProvider : ISpeechToText
|
|
|
|
|
{
|
2024-08-17 03:39:32 +00:00
|
|
|
public string Provider => "whisper";
|
2024-07-30 18:57:32 +00:00
|
|
|
private readonly IAudioProcessUtilities _audioProcessUtilities;
|
|
|
|
|
private static WhisperProcessor _processor;
|
2024-08-11 16:26:57 +00:00
|
|
|
private readonly ILogger _logger;
|
2024-07-30 18:57:32 +00:00
|
|
|
|
|
|
|
|
private string _modelName;
|
2024-08-11 16:26:57 +00:00
|
|
|
private GgmlType _modelType = GgmlType.Tiny;
|
2024-07-30 18:57:32 +00:00
|
|
|
|
2024-08-11 16:26:57 +00:00
|
|
|
public NativeWhisperProvider(
|
|
|
|
|
IAudioProcessUtilities audioProcessUtilities,
|
|
|
|
|
ILogger<NativeWhisperProvider> logger)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
|
|
|
|
_audioProcessUtilities = audioProcessUtilities;
|
2024-08-11 16:26:57 +00:00
|
|
|
_logger = logger;
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|
|
|
|
|
|
2024-08-17 03:39:32 +00:00
|
|
|
public async Task<string> GenerateTextFromAudioAsync(string filePath)
|
2024-07-30 18:57:32 +00:00
|
|
|
{
|
|
|
|
|
string fileExtension = Path.GetExtension(filePath);
|
|
|
|
|
if (!Enum.TryParse<AudioType>(fileExtension.TrimStart('.').ToLower(), out AudioType audioType))
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Unsupported audio type: {fileExtension}");
|
|
|
|
|
}
|
|
|
|
|
await InitModel();
|
|
|
|
|
// var _streamHandler = _audioHandlerFactory.CreateAudioHandler(audioType);
|
|
|
|
|
using var stream = _audioProcessUtilities.ConvertToStream(filePath);
|
|
|
|
|
|
|
|
|
|
if (stream == null)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Failed to convert {fileExtension} to stream");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var textResult = new List<SegmentData>();
|
|
|
|
|
|
|
|
|
|
await foreach (var result in _processor.ProcessAsync((Stream)stream).ConfigureAwait(false))
|
|
|
|
|
{
|
|
|
|
|
textResult.Add(result);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var audioOutput = new AudioOutput
|
|
|
|
|
{
|
|
|
|
|
Segments = textResult
|
|
|
|
|
};
|
|
|
|
|
return audioOutput.ToString();
|
|
|
|
|
}
|
|
|
|
|
private async Task LoadWhisperModel(GgmlType modelType)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_modelName = $"ggml-{modelType}.bin";
|
|
|
|
|
|
|
|
|
|
if (!File.Exists(_modelName))
|
|
|
|
|
{
|
|
|
|
|
using var modelStream = await WhisperGgmlDownloader.GetGgmlModelAsync(GgmlType.TinyEn);
|
|
|
|
|
using var fileWriter = File.OpenWrite(_modelName);
|
|
|
|
|
await modelStream.CopyToAsync(fileWriter);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
throw new Exception($"Failed to load whisper model: {ex.Message}");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private async Task InitModel(GgmlType modelType = GgmlType.TinyEn)
|
|
|
|
|
{
|
|
|
|
|
if (_processor == null)
|
|
|
|
|
{
|
|
|
|
|
await LoadWhisperModel(modelType);
|
|
|
|
|
_processor = WhisperFactory
|
|
|
|
|
.FromPath(_modelName)
|
|
|
|
|
.CreateBuilder()
|
2024-08-11 16:26:57 +00:00
|
|
|
.WithLanguage("auto")
|
2024-07-30 18:57:32 +00:00
|
|
|
.Build();
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-11 16:26:57 +00:00
|
|
|
|
2024-08-17 03:39:32 +00:00
|
|
|
public void SetModelName(string modelType)
|
2024-08-11 16:26:57 +00:00
|
|
|
{
|
|
|
|
|
if (Enum.TryParse<GgmlType>(modelType, true, out GgmlType ggmlType))
|
|
|
|
|
{
|
|
|
|
|
_modelType = ggmlType;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
_logger.LogWarning($"Unsupported model type: {modelType}");
|
|
|
|
|
}
|
2024-07-30 18:57:32 +00:00
|
|
|
}
|