Пример #1
0
        public Task <Uri> PostTranscriptionAsync(string name, string description, string locale, Uri recordingsUrl)
        {
            var path = $"{this.speechToTextBasePath}Transcriptions/";
            var transcriptionDefinition = TranscriptionDefinition.Create(name, description, locale, recordingsUrl);

            return(this.PostAsJsonAsync <TranscriptionDefinition>(path, transcriptionDefinition));
        }
 public static TranscriptionDefinition Create(
     string name,
     string description,
     string locale,
     Uri recordingsUrl)
 {
     return(TranscriptionDefinition.Create(name, description, locale, recordingsUrl, null));
 }
Пример #3
0
        public Task <Uri> PostTranscriptionAsync(string name, string description, string locale, Uri recordingsUrl, IEnumerable <Guid> modelIds)
        {
            if (!modelIds.Any())
            {
                return(this.PostTranscriptionAsync(name, description, locale, recordingsUrl));
            }

            var models = modelIds.Select(m => ModelIdentity.Create(m)).ToList();
            var path   = $"{this.speechToTextBasePath}Transcriptions/";

            var transcriptionDefinition = TranscriptionDefinition.Create(name, description, locale, recordingsUrl, models);

            return(this.PostAsJsonAsync <TranscriptionDefinition>(path, transcriptionDefinition));
        }
        static async Task TranscribeAsync()
        {
            Console.WriteLine("Starting transcriptions client...");

            // Create the client object and authenticate
            var client = new HttpClient
            {
                Timeout               = TimeSpan.FromMinutes(25),
                BaseAddress           = new UriBuilder(Uri.UriSchemeHttps, $"{Region}.cris.ai", Port).Uri,
                DefaultRequestHeaders =
                {
                    { "Ocp-Apim-Subscription-Key", SubscriptionKey }
                }
            };

            var transcriptionDefinition =
                TranscriptionDefinition.Create(
                    Name,
                    Description,
                    Locale,
                    new Uri(RecordingsBlobUri));

            var res = JsonConvert.SerializeObject(transcriptionDefinition);
            var sc  = new StringContent(res);

            sc.Headers.ContentType = JsonMediaTypeFormatter.DefaultMediaType;

            Uri transcriptionLocation = null;

            using (var response = await client.PostAsync($"{SpeechToTextBasePath}Transcriptions/", sc))
            {
                if (!response.IsSuccessStatusCode)
                {
                    Console.WriteLine("Error {0} starting transcription.", response.StatusCode);
                    return;
                }

                transcriptionLocation = response.Headers.Location;
            }

            Console.WriteLine($"Created transcription at location {transcriptionLocation}.");
            Console.WriteLine("Checking status.");

            var completed = false;

            // Check for the status of our transcriptions periodically
            while (!completed)
            {
                Transcription transcription = null;

                // Get all transcriptions for the user
                using (var response = await client.GetAsync(transcriptionLocation.AbsolutePath))
                {
                    var contentType = response.Content.Headers.ContentType;
                    if (response.IsSuccessStatusCode &&
                        string.Equals(contentType.MediaType, "application/json", StringComparison.OrdinalIgnoreCase))
                    {
                        transcription = await response.Content.ReadAsAsync <Transcription>();
                    }
                    else
                    {
                        Console.WriteLine("Error with status {0} getting transcription result", response.StatusCode);
                        continue;
                    }
                }

                // For each transcription in the list we check the status
                switch (transcription.Status)
                {
                case "Failed":
                    completed = true;
                    Console.WriteLine("Transcription failed. Status: {0}", transcription.StatusMessage);
                    break;

                case "Succeeded":
                    completed = true;
                    var webClient = new WebClient();
                    var filename  = Path.GetTempFileName();
                    webClient.DownloadFile(transcription.ResultsUrls["channel_0"], filename);
                    var results = File.ReadAllText(filename);
                    Console.WriteLine($"Transcription succeeded. Results: {Environment.NewLine}{results}");
                    File.Delete(filename);
                    break;

                case "Running":
                    Console.WriteLine("Transcription is still running.");
                    break;

                case "NotStarted":
                    Console.WriteLine("Transcription has not started.");
                    break;
                }

                await Task.Delay(TimeSpan.FromSeconds(5));
            }

            Console.WriteLine("Press any key...");
            Console.ReadKey();
        }