Transcribe entire files
This commit is contained in:
parent
0ae2dae240
commit
20b9dae553
4 changed files with 90 additions and 13 deletions
|
@ -25,14 +25,24 @@
|
||||||
}
|
}
|
||||||
</ul>
|
</ul>
|
||||||
|
|
||||||
<button @onclick="Transcribe" disabled="@TranscribeInProgress">
|
<button @onclick="() => Transcribe()" disabled="@TranscribeInProgress">
|
||||||
@if (TranscribeInProgress)
|
@if (TranscribeInProgress)
|
||||||
{
|
{
|
||||||
<span>Transcribing...</span>
|
<span>Transcribing...</span>
|
||||||
}
|
}
|
||||||
else
|
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>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -46,7 +46,7 @@ public class HomeBase: ComponentBase
|
||||||
InvokeAsync(StateHasChanged);
|
InvokeAsync(StateHasChanged);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected async void Transcribe()
|
protected async void Transcribe(bool full = false)
|
||||||
{
|
{
|
||||||
if (string.IsNullOrWhiteSpace(SelectedFile))
|
if (string.IsNullOrWhiteSpace(SelectedFile))
|
||||||
{
|
{
|
||||||
|
@ -59,10 +59,12 @@ public class HomeBase: ComponentBase
|
||||||
Logger.LogInformation("Beginning transcription of: {path}", SelectedFile);
|
Logger.LogInformation("Beginning transcription of: {path}", SelectedFile);
|
||||||
|
|
||||||
TranscribeInProgress = true;
|
TranscribeInProgress = true;
|
||||||
|
Transcription = string.Empty;
|
||||||
await InvokeAsync(StateHasChanged);
|
await InvokeAsync(StateHasChanged);
|
||||||
|
|
||||||
var result = await TranscribeService.Transcribe(SelectedFile);
|
Transcription = full
|
||||||
Transcription = result.Text;
|
? await TranscribeService.TranscribeFull(SelectedFile)
|
||||||
|
: await TranscribeService.TranscribeSnippet(SelectedFile);
|
||||||
|
|
||||||
Logger.LogInformation("Transcription complete for: {path}", SelectedFile);
|
Logger.LogInformation("Transcription complete for: {path}", SelectedFile);
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,6 +56,21 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
#results {
|
#results {
|
||||||
padding: 0.5em;
|
|
||||||
width: 84vw;
|
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;
|
||||||
}
|
}
|
|
@ -5,7 +5,8 @@ namespace AzureAi.Transcriber.Services;
|
||||||
|
|
||||||
public interface ITranscribeService
|
public interface ITranscribeService
|
||||||
{
|
{
|
||||||
Task<SpeechRecognitionResult> Transcribe(string filePath);
|
Task<string> TranscribeSnippet(string filePath);
|
||||||
|
Task<string> TranscribeFull(string filePath);
|
||||||
}
|
}
|
||||||
|
|
||||||
public class TranscribeService: ITranscribeService
|
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);
|
Logger.LogInformation("Transcribing snippet of {filePath}", filePath);
|
||||||
await Task.Delay(3000);
|
|
||||||
|
|
||||||
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);
|
var speechConfig = SpeechConfig.FromSubscription(_speechKey, _speechRegion);
|
||||||
speechConfig.SpeechRecognitionLanguage = "en-US";
|
speechConfig.SpeechRecognitionLanguage = "en-US";
|
||||||
|
|
||||||
|
@ -38,6 +37,57 @@ public class TranscribeService: ITranscribeService
|
||||||
|
|
||||||
var result = await recognizer.RecognizeOnceAsync();
|
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();
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue