Transcribe entire files

This commit is contained in:
Tomkarho 2024-05-08 15:29:14 +03:00
parent 0ae2dae240
commit 20b9dae553
Signed by: tomkarho
GPG key ID: 8A4E9CBB072D6B19
4 changed files with 90 additions and 13 deletions

View file

@ -25,14 +25,24 @@
}
</ul>
<button @onclick="Transcribe" disabled="@TranscribeInProgress">
<button @onclick="() => Transcribe()" disabled="@TranscribeInProgress">
@if (TranscribeInProgress)
{
<span>Transcribing...</span>
}
else
{
<span>Transcribe</span>
<span>Transcribe snippet</span>
}
</button>
<button @onclick="() => Transcribe(true)" disabled="@TranscribeInProgress">
@if (TranscribeInProgress)
{
<span>Transcribing...</span>
}
else
{
<span>Transcribe full</span>
}
</button>
</div>

View file

@ -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);
}

View file

@ -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;
}

View file

@ -5,7 +5,8 @@ namespace AzureAi.Transcriber.Services;
public interface ITranscribeService
{
Task<SpeechRecognitionResult> Transcribe(string filePath);
Task<string> TranscribeSnippet(string filePath);
Task<string> TranscribeFull(string filePath);
}
public class TranscribeService: ITranscribeService
@ -25,11 +26,9 @@ public class TranscribeService: ITranscribeService
}
}
public async Task<SpeechRecognitionResult> Transcribe(string filePath)
public async Task<string> 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<string> 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<int>();
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();
}
}