From 20b9dae553ca3033a934d56dede0c141f8910400 Mon Sep 17 00:00:00 2001 From: tomkarho Date: Wed, 8 May 2024 15:29:14 +0300 Subject: [PATCH] Transcribe entire files --- .../Components/Pages/Home.razor | 14 ++++- .../Components/Pages/Home.razor.cs | 10 +-- .../Components/Pages/Home.razor.css | 17 ++++- .../Services/TranscribeService.cs | 62 +++++++++++++++++-- 4 files changed, 90 insertions(+), 13 deletions(-) diff --git a/AzureAi.Transcriber/Components/Pages/Home.razor b/AzureAi.Transcriber/Components/Pages/Home.razor index f5bfb0f..5407a9e 100644 --- a/AzureAi.Transcriber/Components/Pages/Home.razor +++ b/AzureAi.Transcriber/Components/Pages/Home.razor @@ -25,14 +25,24 @@ } - + diff --git a/AzureAi.Transcriber/Components/Pages/Home.razor.cs b/AzureAi.Transcriber/Components/Pages/Home.razor.cs index d99c6aa..7ed5cb0 100644 --- a/AzureAi.Transcriber/Components/Pages/Home.razor.cs +++ b/AzureAi.Transcriber/Components/Pages/Home.razor.cs @@ -46,7 +46,7 @@ public class HomeBase: ComponentBase InvokeAsync(StateHasChanged); } - protected async void Transcribe() + protected async void Transcribe(bool full = false) { if (string.IsNullOrWhiteSpace(SelectedFile)) { @@ -59,10 +59,12 @@ public class HomeBase: ComponentBase Logger.LogInformation("Beginning transcription of: {path}", SelectedFile); TranscribeInProgress = true; + Transcription = string.Empty; await InvokeAsync(StateHasChanged); - - var result = await TranscribeService.Transcribe(SelectedFile); - Transcription = result.Text; + + Transcription = full + ? await TranscribeService.TranscribeFull(SelectedFile) + : await TranscribeService.TranscribeSnippet(SelectedFile); Logger.LogInformation("Transcription complete for: {path}", SelectedFile); } diff --git a/AzureAi.Transcriber/Components/Pages/Home.razor.css b/AzureAi.Transcriber/Components/Pages/Home.razor.css index 5e82292..950507e 100644 --- a/AzureAi.Transcriber/Components/Pages/Home.razor.css +++ b/AzureAi.Transcriber/Components/Pages/Home.razor.css @@ -56,6 +56,21 @@ } #results { - padding: 0.5em; width: 84vw; +} + +#results h3 { + margin: 0; + padding: 0; + font-size: 1.35em; + height: 1.5em; + display: flex; + align-items: center; +} + +#transcription { + height: 96vh; + background: rgba(0, 0, 0, 0.1); + overflow-y: auto; + padding: 0.25em; } \ No newline at end of file diff --git a/AzureAi.Transcriber/Services/TranscribeService.cs b/AzureAi.Transcriber/Services/TranscribeService.cs index 07c829d..6cd9494 100644 --- a/AzureAi.Transcriber/Services/TranscribeService.cs +++ b/AzureAi.Transcriber/Services/TranscribeService.cs @@ -5,7 +5,8 @@ namespace AzureAi.Transcriber.Services; public interface ITranscribeService { - Task Transcribe(string filePath); + Task TranscribeSnippet(string filePath); + Task TranscribeFull(string filePath); } public class TranscribeService: ITranscribeService @@ -25,11 +26,9 @@ public class TranscribeService: ITranscribeService } } - public async Task Transcribe(string filePath) + public async Task TranscribeSnippet(string filePath) { - Logger.LogInformation("Transcribing {filePath}", filePath); - await Task.Delay(3000); - + Logger.LogInformation("Transcribing snippet of {filePath}", filePath); var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion); speechConfig.SpeechRecognitionLanguage = "en-US"; @@ -38,6 +37,57 @@ public class TranscribeService: ITranscribeService var result = await recognizer.RecognizeOnceAsync(); - return result; + return result.Text; + } + + public async Task TranscribeFull(string filePath) + { + Logger.LogInformation("Transcribing full length of {filePath}", filePath); + var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion); + using var audioConfig = AudioConfig.FromWavFileInput(filePath); + using var recognizer = new SpeechRecognizer(speechConfig, audioConfig); + + var stopRecognition = new TaskCompletionSource(); + + var result = new System.Text.StringBuilder(); + + recognizer.Recognizing += (s, e) => + { + Logger.LogTrace("Recognizing: {text}", e.Result.Text); + }; + + recognizer.Recognized += (s, e) => + { + if (e.Result.Reason == ResultReason.RecognizedSpeech) + { + Logger.LogTrace("Recognized: {text}", e.Result.Text); + result.AppendLine(e.Result.Text); + } + else if (e.Result.Reason == ResultReason.NoMatch) + { + Logger.LogWarning("No match found."); + } + }; + + recognizer.Canceled += (s, e) => + { + Logger.LogWarning("Canceled: {reason}", e.Reason); + if (e.Reason == CancellationReason.Error) + { + Logger.LogError("Error: {error}", e.ErrorDetails); + } + stopRecognition.TrySetResult(0); + }; + + recognizer.SessionStopped += (s, e) => + { + Logger.LogInformation("Session stopped."); + stopRecognition.TrySetResult(0); + }; + + await recognizer.StartContinuousRecognitionAsync(); + Task.WaitAny([stopRecognition.Task]); + + return result.ToString(); } } \ No newline at end of file