forked from Azure-Samples/cognitive-services-speech-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
program.cs
139 lines (113 loc) · 5.9 KB
/
program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE.md file in the project root for full license information.
//
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
namespace BatchClient
{
class Program
{
// <batchdefinition>
// Replace with your subscription key
private const string SubscriptionKey = "<YourSubscriptionKey>";
// Update with your service region
private const string HostName = "<YourServiceRegion>.cris.ai";
private const int Port = 443;
// recordings and locale
private const string Locale = "en-US";
private const string RecordingsBlobUri = "<SAS URI pointing to an audio file stored in Azure Blob Storage>";
// For usage of baseline models, no acoustic and language model needs to be specified.
private static Guid[] modelList = new Guid[0];
// For use of specific acoustic and language models:
// - comment the previous line
// - uncomment the next lines to create an array containing the guids of your required model(s)
// private static Guid AdaptedAcousticId = new Guid("<id of the custom acoustic model>");
// private static Guid AdaptedLanguageId = new Guid("<id of the custom language model>");
// private static Guid[] modelList = new[] { AdaptedAcousticId, AdaptedLanguageId };
//name and description
private const string Name = "Simple transcription";
private const string Description = "Simple transcription description";
// </batchdefinition>
static void Main(string[] args)
{
TranscribeAsync().Wait();
}
static async Task TranscribeAsync()
{
Console.WriteLine("Starting transcriptions client...");
// create the client object and authenticate
var client = BatchClient.CreateApiV2Client(SubscriptionKey, HostName, Port);
// get all transcriptions for the subscription
var transcriptions = await client.GetTranscriptionsAsync().ConfigureAwait(false);
Console.WriteLine("Deleting all existing completed transcriptions.");
// delete all pre-existing completed transcriptions. If transcriptions are still running or not started, they will not be deleted
foreach (var item in transcriptions)
{
// delete a transcription
await client.DeleteTranscriptionAsync(item.Id).ConfigureAwait(false);
}
Console.WriteLine("Creating transcriptions.");
var transcriptionLocation = await client.PostTranscriptionAsync(Name, Description, Locale, new Uri(RecordingsBlobUri), modelList).ConfigureAwait(false);
// get the transcription Id from the location URI
var createdTranscriptions = new List<Guid>();
createdTranscriptions.Add(new Guid(transcriptionLocation.ToString().Split('/').LastOrDefault()));
Console.WriteLine("Checking status.");
// check for the status of our transcriptions every 30 sec. (can also be 1, 2, 5 min depending on usage)
int completed = 0, running = 0, notStarted = 0;
while (completed < 1)
{
// <batchstatus>
// get all transcriptions for the user
transcriptions = await client.GetTranscriptionsAsync().ConfigureAwait(false);
completed = 0; running = 0; notStarted = 0;
// for each transcription in the list we check the status
foreach (var transcription in transcriptions)
{
switch (transcription.Status)
{
case "Failed":
case "Succeeded":
// we check to see if it was one of the transcriptions we created from this client.
if (!createdTranscriptions.Contains(transcription.Id))
{
// not created form here, continue
continue;
}
completed++;
// if the transcription was successfull, check the results
if (transcription.Status == "Succeeded")
{
var resultsUri0 = transcription.ResultsUrls["channel_0"];
WebClient webClient = new WebClient();
var filename = Path.GetTempFileName();
webClient.DownloadFile(resultsUri0, filename);
var results0 = File.ReadAllText(filename);
var resultObject0 = JsonConvert.DeserializeObject<RootObject>(results0);
Console.WriteLine(results0);
Console.WriteLine("Transcription succeeded. Results: ");
Console.WriteLine(results0);
}
break;
case "Running":
running++;
break;
case "NotStarted":
notStarted++;
break;
}
}
// </batchstatus>
Console.WriteLine(string.Format("Transcriptions status: {0} completed, {1} running, {2} not started yet", completed, running, notStarted));
await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}
Console.WriteLine("Press any key...");
Console.ReadKey();
}
}
}