From a6e5838d788ea333e2c7eb6d100fb0d754d83a02 Mon Sep 17 00:00:00 2001 From: tomkarho Date: Wed, 8 May 2024 13:14:57 +0300 Subject: [PATCH] Handle audio up to 30 seconds --- AzureAi.Transcriber/.gitignore | 3 +- .../AzureAi.Transcriber.csproj | 4 ++ AzureAi.Transcriber/Program.cs | 7 +++ .../Services/TranscribeService.cs | 43 +++++++++++++++++++ 4 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 AzureAi.Transcriber/Services/TranscribeService.cs diff --git a/AzureAi.Transcriber/.gitignore b/AzureAi.Transcriber/.gitignore index 8d4a6c0..d9a8393 100644 --- a/AzureAi.Transcriber/.gitignore +++ b/AzureAi.Transcriber/.gitignore @@ -1,2 +1,3 @@ bin -obj \ No newline at end of file +obj +Data/* \ No newline at end of file diff --git a/AzureAi.Transcriber/AzureAi.Transcriber.csproj b/AzureAi.Transcriber/AzureAi.Transcriber.csproj index 13d8127..d250f2e 100644 --- a/AzureAi.Transcriber/AzureAi.Transcriber.csproj +++ b/AzureAi.Transcriber/AzureAi.Transcriber.csproj @@ -6,4 +6,8 @@ enable + + + + diff --git a/AzureAi.Transcriber/Program.cs b/AzureAi.Transcriber/Program.cs index 2c67dd3..9c53f9c 100644 --- a/AzureAi.Transcriber/Program.cs +++ b/AzureAi.Transcriber/Program.cs @@ -1,4 +1,5 @@ using AzureAi.Transcriber.Components; +using AzureAi.Transcriber.Services; var builder = WebApplication.CreateBuilder(args); @@ -6,8 +7,14 @@ var builder = WebApplication.CreateBuilder(args); builder.Services.AddRazorComponents() .AddInteractiveServerComponents(); +builder.Services.AddSingleton(); +builder.Services.AddSingleton(); + var app = builder.Build(); +// Warmup +app.Services.GetRequiredService(); + // Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { diff --git a/AzureAi.Transcriber/Services/TranscribeService.cs b/AzureAi.Transcriber/Services/TranscribeService.cs new file mode 100644 index 0000000..07c829d --- /dev/null +++ b/AzureAi.Transcriber/Services/TranscribeService.cs @@ -0,0 +1,43 @@ +using Microsoft.CognitiveServices.Speech; +using Microsoft.CognitiveServices.Speech.Audio; + +namespace AzureAi.Transcriber.Services; + +public interface ITranscribeService +{ + Task Transcribe(string filePath); +} + +public class TranscribeService: ITranscribeService +{ + private static string _speechKey = Environment.GetEnvironmentVariable("SPEECH_KEY") ?? string.Empty; + private static string _speechRegion = Environment.GetEnvironmentVariable("SPEECH_REGION") ?? string.Empty; + + private ILogger Logger; + + public TranscribeService(ILogger logger) + { + Logger = logger; + + if(string.IsNullOrWhiteSpace(_speechKey) || string.IsNullOrWhiteSpace(_speechRegion)) + { + throw new InvalidOperationException("Speech key and region must be set in environment variables."); + } + } + + public async Task Transcribe(string filePath) + { + Logger.LogInformation("Transcribing {filePath}", filePath); + await Task.Delay(3000); + + var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion); + speechConfig.SpeechRecognitionLanguage = "en-US"; + + using var audioConfig = AudioConfig.FromWavFileInput(filePath); + using var recognizer = new SpeechRecognizer(speechConfig, audioConfig); + + var result = await recognizer.RecognizeOnceAsync(); + + return result; + } +} \ No newline at end of file