AWSSDK.Transcribe 启动转录工作 完成

huangapple go评论77阅读模式
英文:

AWSSDK.Transcribe StartTranscriptionJob completion

问题

我写了以下的代码,使用AWSSDK.Transcribe来转录音频文件。代码执行并返回带有OK状态的响应,但是response.TranscriptionJob.Transcript对象为空。我想到的解释是它只表示作业的开始,一个同事告诉我我需要立即轮询结果,或者使用AWS Step Functions或AWS Lambda来监视作业完成... 有没有其他选项让我知道何时转录完成?

英文:

I wrote the following code that uses AWSSDK.Transcribe to transcribe audio files.
The code executes and returns the response with OK status, BUT the object response.TranscriptionJob.Transcript is null
The explanation I came up with is that it only indicated the start of the job and a colleage tells me that I need either poll now for results or use AWS Step Functions or AWS Lambda to monitor the job completion... Is there any other options for me to use in order to know when the transcription is done?

    public static string Transcribe(string inputAudioFilePath)
    {
        var awsCredentials = new BasicAWSCredentials(accessKey, secretKey);
        // North CA, for streaming "us-west-2"
        var config = new AmazonTranscribeServiceConfig { RegionEndpoint = RegionEndpoint.GetBySystemName("us-west-2") }; 

        AmazonTranscribeServiceClient client = new AmazonTranscribeServiceClient(awsCredentials, config);
        client.AfterResponseEvent += Client_AfterResponseEvent;

        var jobName = String.Format("AWSSTT-{0}", DateTime.Now.ToString("yyyy-MM-dd-hh-mm-t"));
        var inputLanguage = "en-US";

        #region Upload File to S3 Bucket

        string s3BucketName = "tts-experiment";
        string s3KeyName = inputAudioFilePath;


        mAmazonS3Client = new AmazonS3Client(accessKey, secretKey, mRegionEndpoint);
        string s3MediaUrl = UploadFileToS3(inputAudioFilePath, s3BucketName, s3KeyName);
        string ext = Path.GetExtension(inputAudioFilePath).ToUpper();
        MediaFormat mediaFormat = MediaFormat.Wav;
        if (String.Compare(ext, ".MP3", true) == 0)
            mediaFormat = MediaFormat.Mp3;

        #endregion

        var media = new Media() { MediaFileUri = s3MediaUrl };
        var request = new StartTranscriptionJobRequest
        {
            TranscriptionJobName = jobName,
            LanguageCode = inputLanguage,
            Media = media,
            MediaFormat = MediaFormat.Mp3,
        };

        try
        {
            string text = "";
            StartTranscriptionJobResponse response = client.StartTranscriptionJob(request);
            if (response.HttpStatusCode == System.Net.HttpStatusCode.OK)
            {
                Console.WriteLine("Transcription completed successfully");

                if (response.TranscriptionJob.Transcript != null && response.TranscriptionJob.Transcript.TranscriptFileUri != null)
                {
                    string transcriptUrl = response.TranscriptionJob.Transcript.TranscriptFileUri.ToString();
                    WebClient webClient = new WebClient();

                    string targetDir = Path.Combine(Program.InstallDir, @"TTSExperiment\Transcribed");
                    if (!Directory.Exists(targetDir))
                    {
                        Directory.CreateDirectory(targetDir);
                    }
                    string localFilePath = Path.Combine(targetDir, Path.GetFileNameWithoutExtension(transcriptUrl) +".txt");
                
                    webClient.DownloadFile(transcriptUrl, localFilePath);

                    text = File.ReadAllText(localFilePath);

                    Console.WriteLine($"Transcription job {jobName} completed.");
                }
                else
                {
                    Console.WriteLine("No Transcript created");
                }
                return text;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine($"Error starting transcription job: {ex.Message}");
        }

        return "";
    }

答案1

得分: 1

您的同事是正确的,您确实需要轮询结果或使用其他方式进行检查,因为转录的时间可能会根据媒体的长度而非常长。AWS SDK示例存储库包含一个.NET示例,其中使用了一些简单的轮询逻辑,您可能会发现它有用。

Console.WriteLine("开始转录。");

var transcriptionName = $"ExampleTranscription_{DateTime.Now.Ticks}";

var transcriptionJob = await transcribeWrapper.StartTranscriptionJob(
    transcriptionName, transcriptionMediaLocation, MediaFormat.Mp3,
    LanguageCode.EnUS, null);

Console.WriteLine($"转录已启动:{transcriptionJob.TranscriptionJobName}," +
                  $"状态:{transcriptionJob.TranscriptionJobStatus}。");

Console.WriteLine($"等待转录 {transcriptionName}。");

while (transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.COMPLETED
       && transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.FAILED)
{
    transcriptionJob =
        await transcribeWrapper.GetTranscriptionJob(transcriptionName);
    Thread.Sleep(5000);
}

Console.WriteLine($"转录状态:{transcriptionJob.TranscriptionJobStatus}," +
                  $"完成时间:{transcriptionJob.CompletionTime}。");
英文:

Your colleague is correct, you do need to poll for results or check some other way, because transcriptions can take a very long time depending on the length of the media. The AWS SDK examples repository includes a .NET example that uses some simple polling logic that you may find useful.

Console.WriteLine("Start a transcription.");

    var transcriptionName = $"ExampleTranscription_{DateTime.Now.Ticks}";

    var transcriptionJob = await transcribeWrapper.StartTranscriptionJob(
        transcriptionName, transcriptionMediaLocation, MediaFormat.Mp3,
        LanguageCode.EnUS, null);

    Console.WriteLine($"Transcription started: {transcriptionJob.TranscriptionJobName}, " +
                      $"status: {transcriptionJob.TranscriptionJobStatus}.");

    Console.WriteLine($"Waiting for transcription {transcriptionName}.");

    while (transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.COMPLETED
           && transcriptionJob.TranscriptionJobStatus != TranscriptionJobStatus.FAILED)
    {
        transcriptionJob =
            await transcribeWrapper.GetTranscriptionJob(transcriptionName);
        Thread.Sleep(5000);
    }

    Console.WriteLine($"Transcription status:  {transcriptionJob.TranscriptionJobStatus}, " +
                      $"completed at: {transcriptionJob.CompletionTime}.");

huangapple
  • 本文由 发表于 2023年4月4日 07:51:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/75924518.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定