Skip to content

Commit

Permalink
Batch samples: allow to configure region (#371)
Browse files Browse the repository at this point in the history
- output transcription id in batch samples
- allow to configure region in python sample
- unify placeholder names
  • Loading branch information
chlandsi authored Aug 30, 2019
1 parent 68e88f0 commit 4928043
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
15 changes: 9 additions & 6 deletions samples/batch/csharp/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class Program
{
// <batchdefinition>
// Replace with your subscription key
private const string SubscriptionKey = "<YourSubscriptionKey>";
private const string SubscriptionKey = "YourSubscriptionKey";

// Update with your service region
private const string HostName = "<YourServiceRegion>.cris.ai";
private const string Region = "YourServiceRegion";
private const int Port = 443;

// recordings and locale
Expand Down Expand Up @@ -52,7 +52,7 @@ static async Task TranscribeAsync()
Console.WriteLine("Starting transcriptions client...");

// create the client object and authenticate
var client = BatchClient.CreateApiV2Client(SubscriptionKey, HostName, Port);
var client = BatchClient.CreateApiV2Client(SubscriptionKey, $"{Region}.cris.ai", Port);

// get all transcriptions for the subscription
var transcriptions = await client.GetTranscriptionsAsync().ConfigureAwait(false);
Expand All @@ -65,16 +65,18 @@ static async Task TranscribeAsync()
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);
var thisTranscriptionGuid = new Guid(transcriptionLocation.ToString().Split('/').LastOrDefault());

// get the transcription Id from the location URI
var createdTranscriptions = new List<Guid>();
createdTranscriptions.Add(new Guid(transcriptionLocation.ToString().Split('/').LastOrDefault()));
createdTranscriptions.Add(thisTranscriptionGuid);

Console.WriteLine($"Created transcription with id {thisTranscriptionGuid}.");

Console.WriteLine("Checking status.");

// check for the status of our transcriptions every 30 sec. (can also be 1, 2, 5 min depending on usage)
// check for the status of our transcriptions periodically
int completed = 0, running = 0, notStarted = 0;
while (completed < 1)
{
Expand Down Expand Up @@ -132,6 +134,7 @@ static async Task TranscribeAsync()
// </batchstatus>

Console.WriteLine(string.Format("Transcriptions status: {0} completed, {1} running, {2} not started yet", completed, running, notStarted));
// check again after 5 seconds (can also be 1, 2, 5 min depending on usage).
await Task.Delay(TimeSpan.FromSeconds(5)).ConfigureAwait(false);
}

Expand Down
3 changes: 2 additions & 1 deletion samples/batch/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ pip install requests
The sample code itself is [main.py](python-client/main.py) and can be run using Python 3.5 or higher.
You will need to adapt the following information to run the sample:
1. Your subscription key. This must a subscription key for the same region you [downloaded the client](#download-and-install-the-api-client-library) for; it won't work for any other region.
1. Your subscription key and region.
1. The URI of an audio recording in blob storage.
1. (Optional:) The model IDs of both an adapted acoustic and language model, if you want to use a custom model.
You can use a development environment like PyCharm to edit, debug, and execute the sample.
15 changes: 9 additions & 6 deletions samples/batch/python/python-client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@

logging.basicConfig(stream=sys.stdout, level=logging.DEBUG, format="%(message)s")

# The subscription key must be for the region that you generated the Swagger
# client library for (see ../README.md for detailed instructions).
SUBSCRIPTION_KEY = "<your subscription key>"
# Your subscription key and region for the speech service
SUBSCRIPTION_KEY = "YourSubscriptionKey"
SERVICE_REGION = "YourServiceRegion"

NAME = "Simple transcription"
DESCRIPTION = "Simple transcription description"
Expand All @@ -36,6 +36,7 @@ def transcribe():
# configure API key authorization: subscription_key
configuration = cris_client.Configuration()
configuration.api_key['Ocp-Apim-Subscription-Key'] = SUBSCRIPTION_KEY
configuration.host = "https://{}.cris.ai".format(SERVICE_REGION)

# create the client object and authenticate
client = cris_client.ApiClient(configuration)
Expand All @@ -57,8 +58,6 @@ def transcribe():
# ignore swagger error on empty response message body: https://github.com/swagger-api/swagger-core/issues/2446
pass

logging.info("Creating transcriptions.")

# Use base models for transcription. Comment this block if you are using a custom model.
# Note: you can specify additional transcription properties by passing a
# dictionary in the properties parameter. See
Expand All @@ -85,6 +84,8 @@ def transcribe():
# get the transcription Id from the location URI
created_transcription: str = transcription_location.split('/')[-1]

logging.info("Created new transcription with id {}".format(created_transcription))

logging.info("Checking status.")

completed = False
Expand All @@ -98,7 +99,7 @@ def transcribe():
# for each transcription in the list we check the status
for transcription in transcriptions:
if transcription.status in ("Failed", "Succeeded"):
# we check to see if it was one of the transcriptions we created from this client
# we check to see if it was the transcription we created from this client
if created_transcription != transcription.id:
continue

Expand All @@ -111,6 +112,8 @@ def transcribe():
logging.info(results.content.decode("utf-8"))
else:
logging.info("Transcription failed :{}.".format(transcription.status_message))

break
elif transcription.status == "Running":
running += 1
elif transcription.status == "NotStarted":
Expand Down

0 comments on commit 4928043

Please sign in to comment.