Handle audio up to 30 seconds
This commit is contained in:
parent
24b726e82a
commit
a6e5838d78
4 changed files with 56 additions and 1 deletions
1
AzureAi.Transcriber/.gitignore
vendored
1
AzureAi.Transcriber/.gitignore
vendored
|
@ -1,2 +1,3 @@
|
||||||
bin
|
bin
|
||||||
obj
|
obj
|
||||||
|
Data/*
|
|
@ -6,4 +6,8 @@
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Microsoft.CognitiveServices.Speech" Version="1.37.0" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
</Project>
|
</Project>
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
using AzureAi.Transcriber.Components;
|
using AzureAi.Transcriber.Components;
|
||||||
|
using AzureAi.Transcriber.Services;
|
||||||
|
|
||||||
var builder = WebApplication.CreateBuilder(args);
|
var builder = WebApplication.CreateBuilder(args);
|
||||||
|
|
||||||
|
@ -6,8 +7,14 @@ var builder = WebApplication.CreateBuilder(args);
|
||||||
builder.Services.AddRazorComponents()
|
builder.Services.AddRazorComponents()
|
||||||
.AddInteractiveServerComponents();
|
.AddInteractiveServerComponents();
|
||||||
|
|
||||||
|
builder.Services.AddSingleton<IFileService, FileService>();
|
||||||
|
builder.Services.AddSingleton<ITranscribeService, TranscribeService>();
|
||||||
|
|
||||||
var app = builder.Build();
|
var app = builder.Build();
|
||||||
|
|
||||||
|
// Warmup
|
||||||
|
app.Services.GetRequiredService<ITranscribeService>();
|
||||||
|
|
||||||
// Configure the HTTP request pipeline.
|
// Configure the HTTP request pipeline.
|
||||||
if (!app.Environment.IsDevelopment())
|
if (!app.Environment.IsDevelopment())
|
||||||
{
|
{
|
||||||
|
|
43
AzureAi.Transcriber/Services/TranscribeService.cs
Normal file
43
AzureAi.Transcriber/Services/TranscribeService.cs
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
using Microsoft.CognitiveServices.Speech;
|
||||||
|
using Microsoft.CognitiveServices.Speech.Audio;
|
||||||
|
|
||||||
|
namespace AzureAi.Transcriber.Services;
|
||||||
|
|
||||||
|
public interface ITranscribeService
|
||||||
|
{
|
||||||
|
Task<SpeechRecognitionResult> 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<TranscribeService> Logger;
|
||||||
|
|
||||||
|
public TranscribeService(ILogger<TranscribeService> 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<SpeechRecognitionResult> 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;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue