Skip to content

Commit

Permalink
update batch transcription samples for API version 2024-11-15 (#2665)
Browse files Browse the repository at this point in the history
* update sample
  • Loading branch information
chlandsi authored Nov 21, 2024
1 parent 82b6b52 commit fe4797e
Show file tree
Hide file tree
Showing 11 changed files with 30 additions and 47 deletions.
4 changes: 2 additions & 2 deletions samples/batch/csharp/batchclient/BatchClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.AspNet.WebApi.Client" version="5.2.9" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" version="13.0.2" PrivateAssets="All" />
<PackageReference Include="Microsoft.AspNet.WebApi.Client" version="6.0.0" PrivateAssets="All" />
<PackageReference Include="Newtonsoft.Json" version="13.0.3" PrivateAssets="All" />
<PackageReference Include="Polly" version="7.2.3" PrivateAssets="All" />
</ItemGroup>
</Project>
10 changes: 6 additions & 4 deletions samples/batch/csharp/batchclient/batchclient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public partial class BatchClient : IDisposable

private readonly HttpClient client;
private readonly string speechToTextBasePath;
private readonly string apiVersion;

private static AsyncRetryPolicy<HttpResponseMessage> transientFailureRetryingPolicy = Policy
.Handle<HttpRequestException>()
Expand All @@ -30,21 +31,22 @@ public partial class BatchClient : IDisposable
Console.WriteLine($"Request failed with {result.Exception?.ToString() ?? result.Result?.StatusCode.ToString()}. Waiting {timeSpan} before next retry. Retry attempt {retryCount}");
});

private BatchClient(HttpClient client)
private BatchClient(HttpClient client, string apiVersion)
{
this.client = client;
speechToTextBasePath = "speechtotext/v3.2/";
this.speechToTextBasePath = "speechtotext/";
this.apiVersion = apiVersion;
}

public static BatchClient CreateApiV3Client(string key, string hostName)
public static BatchClient CreateApiClient(string key, string hostName, string apiVersion)
{
var client = new HttpClient();
client.Timeout = TimeSpan.FromMinutes(25);
client.BaseAddress = new UriBuilder(Uri.UriSchemeHttps, hostName).Uri;

client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", key);

return new BatchClient(client);
return new BatchClient(client, apiVersion);
}

private async Task<TResponse> PostAsJsonAsync<TPayload, TResponse>(string path, TPayload payload)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public partial class BatchClient
{
public Task<PaginatedTranscriptions> GetTranscriptionsAsync()
{
var path = $"{this.speechToTextBasePath}transcriptions";
var path = $"{this.speechToTextBasePath}transcriptions?api-version={this.apiVersion}";
return this.GetAsync<PaginatedTranscriptions>(path);
}

Expand Down Expand Up @@ -69,7 +69,7 @@ public Task<Transcription> CreateTranscriptionAsync(Transcription transcription)
throw new ArgumentNullException(nameof(transcription));
}

var path = $"{this.speechToTextBasePath}transcriptions/";
var path = $"{this.speechToTextBasePath}transcriptions:submit?api-version={this.apiVersion}";

return this.PostAsJsonAsync<Transcription, Transcription>(path, transcription);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace BatchClient
{
public class DiarizationProperties
{
public DiarizationSpeakersProperties Speakers { get; set; }
public int MaxSpeakers { get; set; }

public bool Enabled { get; set; }
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ namespace BatchClient

public class TranscriptionProperties
{
[JsonProperty(PropertyName = "diarizationEnabled")]
public bool IsDiarizationEnabled { get; set; }

[JsonProperty(PropertyName = "wordLevelTimestampsEnabled")]
public bool IsWordLevelTimestampsEnabled { get; set; }

Expand All @@ -33,8 +30,7 @@ public class TranscriptionProperties

public Uri DestinationContainerUrl { get; set; }

[JsonConverter(typeof(TimeSpanConverter))]
public TimeSpan TimeToLive { get; set; }
public int TimeToLiveHours { get; set; }

public DiarizationProperties Diarization { get; set; }

Expand Down
12 changes: 4 additions & 8 deletions samples/batch/csharp/batchclient/program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static void Main(string[] args)
private static async Task RunAsync()
{
// create the client object and authenticate
using (var client = BatchClient.CreateApiV3Client(SubscriptionKey, $"{Region}.api.cognitive.microsoft.com"))
using (var client = BatchClient.CreateApiClient(SubscriptionKey, $"{Region}.api.cognitive.microsoft.com", "2024-11-15"))
{
// uncomment next line when using web hooks
// await SetupWebHookAsync(client).ConfigureAwait(false);
Expand Down Expand Up @@ -96,19 +96,15 @@ private async static Task TranscribeAsync(BatchClient client)
Model = CustomModel,
Properties = new TranscriptionProperties
{
TimeToLive = TimeSpan.FromDays(1),
TimeToLiveHours = 6,
IsWordLevelTimestampsEnabled = true,
IsDisplayFormWordLevelTimestampsEnabled = false,

// uncomment the following block to enable and configure speaker separation
// IsDiarizationEnabled = true,
// Diarization = new DiarizationProperties
// {
// Speakers = new DiarizationSpeakersProperties
// {
// MinCount = 1,
// MaxCount = 5
// }
// Enabled = true,
// MaxSpeakers = 5
// },

// // uncomment the following block to enable and configure language identification prior to transcription. Available modes are "Single" and "Continuous".
Expand Down
2 changes: 1 addition & 1 deletion samples/batch/csharp/webhookreceiver/webhookreceiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public static async Task<IActionResult> Run(
// invocationId can be used for deduplication, it's unique per notification event
logger.LogInformation($"Processing notification {webHookNotification.InvocationId}.");

using (var client = BatchClient.CreateApiV3Client(Program.SubscriptionKey, $"{Program.Region}.api.cognitive.microsoft.com"))
using (var client = BatchClient.CreateApiClient(Program.SubscriptionKey, $"{Program.Region}.api.cognitive.microsoft.com", "2024-11-15"))
{
if (eventKind == WebHookEventKind.TranscriptionCompletion)
{
Expand Down
4 changes: 2 additions & 2 deletions samples/batch/csharp/webhookreceiver/webhookreceiver.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
<RootNamespace>WebHookReceiver</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.2" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.6.0" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\batchclient\batchclient.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion samples/batch/python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Follow these steps for the installation:

1. Go to https://editor.swagger.io.
1. Click **File**, then click **Import URL**.
1. Enter the Swagger URL for the Speech Services API: `https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/Speech/SpeechToText/stable/v3.2/speechtotext.json`.
1. Enter the Swagger URL for the Speech Services API: `https://raw.githubusercontent.com/Azure/azure-rest-api-specs/main/specification/cognitiveservices/data-plane/Speech/SpeechToText/stable/2024-11-15/speechtotext.json`.
1. Click **Generate Client** and select **Python**.
1. Save the client library.
1. Extract the downloaded python-client-generated.zip somewhere in your file system.
Expand Down
15 changes: 8 additions & 7 deletions samples/batch/python/python-client/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
logging.basicConfig(stream=sys.stdout, level=logging.DEBUG,
format="%(asctime)s %(message)s", datefmt="%m/%d/%Y %I:%M:%S %p %Z")

API_VERSION = "2024-11-15"

# Your subscription key and region for the speech service
SUBSCRIPTION_KEY = "YourSubscriptionKey"
SERVICE_REGION = "YourServiceRegion"
Expand Down Expand Up @@ -132,7 +134,7 @@ def transcribe():
# configure API key authorization: subscription_key
configuration = swagger_client.Configuration()
configuration.api_key["Ocp-Apim-Subscription-Key"] = SUBSCRIPTION_KEY
configuration.host = f"https://{SERVICE_REGION}.api.cognitive.microsoft.com/speechtotext/v3.2"
configuration.host = f"https://{SERVICE_REGION}.api.cognitive.microsoft.com/speechtotext"

# create the client object and authenticate
client = swagger_client.ApiClient(configuration)
Expand All @@ -143,13 +145,12 @@ def transcribe():
# Specify transcription properties by passing a dict to the properties parameter. See
# https://learn.microsoft.com/azure/cognitive-services/speech-service/batch-transcription-create?pivots=rest-api#request-configuration-options
# for supported parameters.
properties = swagger_client.TranscriptionProperties()
properties = swagger_client.TranscriptionProperties(time_to_live_hours=6)
# properties.word_level_timestamps_enabled = True
# properties.display_form_word_level_timestamps_enabled = True
# properties.punctuation_mode = "DictatedAndAutomatic"
# properties.profanity_filter_mode = "Masked"
# properties.destination_container_url = "<SAS Uri with at least write (w) permissions for an Azure Storage blob container that results should be written to>"
# properties.time_to_live = "PT1H"

# uncomment the following block to enable and configure speaker separation
# properties.diarization_enabled = True
Expand All @@ -169,10 +170,10 @@ def transcribe():
# Uncomment this block to transcribe all files from a container.
# transcription_definition = transcribe_from_container(RECORDINGS_CONTAINER_URI, properties)

created_transcription, status, headers = api.transcriptions_create_with_http_info(transcription=transcription_definition)
created_transcription, status, headers = api.transcriptions_submit_with_http_info(body=transcription_definition, api_version=API_VERSION)

# get the transcription Id from the location URI
transcription_id = headers["location"].split("/")[-1]
transcription_id = headers["location"].split("/")[-1].split("?")[0]

# Log information about the created transcription. If you should ask for support, please
# include this information.
Expand All @@ -186,7 +187,7 @@ def transcribe():
# wait for 5 seconds before refreshing the transcription status
time.sleep(5)

transcription = api.transcriptions_get(transcription_id)
transcription = api.transcriptions_get(transcription_id, api_version=API_VERSION)
logging.info(f"Transcriptions status: {transcription.status}")

if transcription.status in ("Failed", "Succeeded"):
Expand All @@ -197,7 +198,7 @@ def transcribe():
logging.info("Transcription succeeded. Results are located in your Azure Blob Storage.")
break

pag_files = api.transcriptions_list_files(transcription_id)
pag_files = api.transcriptions_list_files(transcription_id, api_version=API_VERSION)
for file_data in _paginate(api, pag_files):
if file_data.kind != "Transcription":
continue
Expand Down

0 comments on commit fe4797e

Please sign in to comment.