public async Task StartTranscriptionAsync(Message message) { if (message == null) { throw new ArgumentNullException(nameof(message)); } var busMessage = JsonConvert.DeserializeObject <ServiceBusMessage>(Encoding.UTF8.GetString(message.Body)); var audioFileName = StorageUtilities.GetFileNameFromUri(busMessage.Data.Url); var startDateTime = DateTime.UtcNow; // Check if language identification is required: var secondaryLocale = StartTranscriptionEnvironmentVariables.SecondaryLocale; if (!string.IsNullOrEmpty(secondaryLocale) && !secondaryLocale.Equals("None", StringComparison.OrdinalIgnoreCase) && !secondaryLocale.Equals(Locale, StringComparison.OrdinalIgnoreCase)) { secondaryLocale = secondaryLocale.Split('|')[0].Trim(); Logger.LogInformation($"Primary locale: {Locale}"); Logger.LogInformation($"Secondary locale: {secondaryLocale}"); var languageID = new LanguageIdentification(StartTranscriptionEnvironmentVariables.AzureSpeechServicesKey, StartTranscriptionEnvironmentVariables.AzureSpeechServicesRegion); var fileExtension = Path.GetExtension(audioFileName); var sasUrl = await StorageUtilities.CreateSASAsync(StartTranscriptionEnvironmentVariables.AzureWebJobsStorage, busMessage.Data.Url, Logger).ConfigureAwait(false); var byteArray = await StorageUtilities.DownloadFileFromSAS(sasUrl).ConfigureAwait(false); var identifiedLocale = await languageID.DetectLanguage(byteArray, fileExtension, Locale, secondaryLocale).ConfigureAwait(false); Logger.LogInformation($"Identified locale: {identifiedLocale}"); Locale = identifiedLocale; } await StartBatchTranscriptionJobAsync(new[] { message }, audioFileName, startDateTime).ConfigureAwait(false); }
private async Task StartBatchTranscriptionJobAsync(IEnumerable <Message> messages, string jobName, DateTime startDateTime) { if (messages == null || !messages.Any()) { Logger.LogError("Invalid service bus message(s)."); return; } var locationString = string.Empty; var serviceBusMessages = messages.Select(message => JsonConvert.DeserializeObject <ServiceBusMessage>(Encoding.UTF8.GetString(message.Body))); var modelIds = new List <Guid>(); try { var properties = GetTranscriptionPropertyBag(); modelIds = GetModelIds(); var audioFileUrls = new List <string>(); foreach (var serviceBusMessage in serviceBusMessages) { var audioFileUrl = await StorageUtilities.CreateSASAsync(StartTranscriptionEnvironmentVariables.AzureWebJobsStorage, serviceBusMessage.Data.Url, Logger).ConfigureAwait(false); audioFileUrls.Add(audioFileUrl); } var client = new BatchClient(Subscription.SubscriptionKey, Subscription.LocationUri.AbsoluteUri, Logger); var transcriptionLocation = await client.PostTranscriptionAsync( jobName, "StartByTimerTranscription", Locale, properties, audioFileUrls, modelIds).ConfigureAwait(false); Logger.LogInformation($"Location: {transcriptionLocation}"); locationString = transcriptionLocation.ToString(); } catch (WebException e) { if (BatchClient.IsThrottledOrTimeoutStatusCode(((HttpWebResponse)e.Response).StatusCode)) { Logger.LogError($"Throttled or timeout while creating post, re-enqueueing transcription start. Message: {e.Message}"); var startTranscriptionSBConnectionString = StartTranscriptionEnvironmentVariables.StartTranscriptionServiceBusConnectionString; foreach (var message in messages) { ServiceBusUtilities.SendServiceBusMessageAsync(startTranscriptionSBConnectionString, message, Logger, 2).GetAwaiter().GetResult(); } return; } else { Logger.LogError($"Failed with Webexception. Write message for job with name {jobName} to report file."); var errorTxtName = jobName + ".txt"; var exceptionMessage = e.Message; using (var reader = new StreamReader(e.Response.GetResponseStream())) { var responseMessage = await reader.ReadToEndAsync().ConfigureAwait(false); exceptionMessage += "\n" + responseMessage; } await WriteFailedJobLogToStorageAsync(serviceBusMessages, exceptionMessage, jobName).ConfigureAwait(false); return; } } catch (TimeoutException e) { Logger.LogError($"Timeout while creating post, re-enqueueing transcription start. Message: {e.Message}"); var startTranscriptionSBConnectionString = StartTranscriptionEnvironmentVariables.StartTranscriptionServiceBusConnectionString; foreach (var message in messages) { ServiceBusUtilities.SendServiceBusMessageAsync(startTranscriptionSBConnectionString, message, Logger, 2).GetAwaiter().GetResult(); } return; } catch (Exception e) { Logger.LogError($"Failed with Exception {e} and message {e.Message}. Write message for job with name {jobName} to report file."); await WriteFailedJobLogToStorageAsync(serviceBusMessages, e.Message, jobName).ConfigureAwait(false); return; } var reenqueueingTimeInSeconds = MapDatasetSizeToReenqueueingTimeInSeconds(StartTranscriptionEnvironmentVariables.AudioDatasetSize); var transcriptionMessage = new PostTranscriptionServiceBusMessage( Subscription, locationString, jobName, startDateTime.ToString(CultureInfo.InvariantCulture), Locale, modelIds.Any(), StartTranscriptionEnvironmentVariables.AddSentimentAnalysis, StartTranscriptionEnvironmentVariables.AddEntityRedaction, 0, reenqueueingTimeInSeconds, reenqueueingTimeInSeconds); var fetchTranscriptionSBConnectionString = StartTranscriptionEnvironmentVariables.FetchTranscriptionServiceBusConnectionString; Logger.LogInformation($"FetchTranscriptionServiceBusConnectionString from settings: {fetchTranscriptionSBConnectionString}"); try { ServiceBusUtilities.SendServiceBusMessageAsync(fetchTranscriptionSBConnectionString, transcriptionMessage.CreateMessageString(), Logger, reenqueueingTimeInSeconds).GetAwaiter().GetResult(); } catch (Exception e) { Logger.LogError($"Failed with Exception {e} and message {e.Message}. Write message for job with name {jobName} to report file."); await WriteFailedJobLogToStorageAsync(serviceBusMessages, e.Message, jobName).ConfigureAwait(false); } Logger.LogInformation($"Fetch transcription queue informed about job at: {jobName}"); }