Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Katukagloria/fast #2603

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions samples/fast-transcription/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Examples to use Fast Transcription

The Azure Speech Fast Transcription API is used to transcribe audio files with returning results synchronously and much faster than real-time audio.
This functionality is exposed through a REST API and is easy to access from many programming languages. The samples here do **NOT** require the installation of the Cognitive Service Speech SDK, but use the REST API directly instead.

For a detailed explanation see the [fast transcription documentation](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/fast-transcription-create) and the `README.md` in the language specific subdirectories.

Available samples:

| Language | Directory | Description |
| ---------- | -------- | ----------- |
| C# | [csharp](csharp_client) | C# calling fast transcription REST API through System.Net.Http |
| Python | [python](python_client) | Python client calling fast transcription REST API |
| Node.js | [js/node](js_client) | Node.js client calling fast transcription REST API |
92 changes: 92 additions & 0 deletions samples/fast-transcription/csharp_client/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Net.Http;
using System.Net.Http.Json;
using System.Runtime.InteropServices;
using System.Text.Json.Serialization;
using Newtonsoft.Json;

namespace fastTranscriptionClient
{
public class FastTranscriptionOptions
{
public string[] Locales { get; set; } = { "en-US" };
public string ProfanityFilterMode { get; set; } = "masked";
public int[] Channels { get; set; } = { 0 };
public Dictionary<string, int> DiarizationSettings { get; set; } = new Dictionary<string, int>{
{ "minSpeakers", 1 },
{ "maxSpeakers", 4 }
};

}

public class Program
{
//Replace with your subscription key
public const string SubscriptionKey = "YourSubscriptionKey"; //Update with your subscription key

// Update with your service region
public const string Region = "YourServiceRegion"; //Update with your service region

// locale and endpoint URL
private const string Locale = "en-US";

//local storage location for testing
private const string DisplayName = "Fast transcription";


private static async Task Main(string[] args)
{
var audio_path = @"YourAudio_FilePath"; //- update to your audio file location
var transcription_path = @"transcription_response.json"; // path to store your transcription output
try
{
await TranscribeAsync(audio_path, transcription_path).ConfigureAwait(false);
}
catch (Exception ex)
{
Console.WriteLine($"An error occurred: {ex.Message}");
}
}

public static async Task TranscribeAsync(string audio_filepath, string transcriptions_filepath)
{
var fastTranscriptionEndpointPath = $"https://{Region}.api.cognitive.microsoft.com//speechtotext/transcriptions:transcribe?api-version=2024-05-15-preview";

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, fastTranscriptionEndpointPath);

request.Headers.Add("Ocp-apim-subscription-key", SubscriptionKey);
request.Headers.Add("Accept", "application/json");


var content = new MultipartFormDataContent();

// Add the audio file
content.Add(new StreamContent(File.OpenRead(audio_filepath)), "audio", "test_call_audio.wav");

// Add definition
//var transcriptionDefinition = new StringContent("{\"locales\": [\"en-us\"],\"profanityfiltermode\": \"masked\", \"channels\": [0, 1]}", System.Text.Encoding.UTF8, "application/json");
var fastTranscriptionOptions = new FastTranscriptionOptions();
var transcriptionDefinition = JsonContent.Create(fastTranscriptionOptions);
content.Add(transcriptionDefinition, "definition");

request.Content = content;

// Make the API request
var response = await client.SendAsync(request).ConfigureAwait(false);
response.EnsureSuccessStatusCode();

var response_content = await response.Content.ReadAsStringAsync();
Console.WriteLine(response_content);

var response_content_json = JsonConvert.SerializeObject(JsonConvert.DeserializeObject(response_content), Formatting.Indented);

//save transcriptions to the transcriptions file location
await File.WriteAllTextAsync(transcriptions_filepath, response_content_json);
Console.WriteLine($"Response saved to {transcriptions_filepath}");

}
}
}
14 changes: 14 additions & 0 deletions samples/fast-transcription/csharp_client/csharp_client.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

</Project>
25 changes: 25 additions & 0 deletions samples/fast-transcription/csharp_client/csharp_client.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.11.35327.3
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "csharp_client", "csharp_client.csproj", "{7965ED0C-5A0E-449A-A11E-8ADFB20F5474}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{7965ED0C-5A0E-449A-A11E-8ADFB20F5474}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7965ED0C-5A0E-449A-A11E-8ADFB20F5474}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7965ED0C-5A0E-449A-A11E-8ADFB20F5474}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7965ED0C-5A0E-449A-A11E-8ADFB20F5474}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {CCC58C19-0005-4B05-A56B-0016ECEEC7C5}
EndGlobalSection
EndGlobal
40 changes: 40 additions & 0 deletions samples/fast-transcription/csharp_client/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# C# Console app for .NET Core

This sample demonstrates audio transcription using the Fast Transcription API for C#. It runs under .NET 8.0 or later.

## Prerequisites

1. An Azure AI Speech resource in one of the regions where the fast transcription API is available. The supported regions are: Central India, East US, Southeast Asia, and West Europe. For more information and region identifier, see [Speech service regions](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/regions)
1. An audio file (less than 2 hours long and less than 200 MB in size) in one of the formats and codecs supported by the batch transcription API. For more information about supported audio formats, see [Supported audio formats](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/batch-transcription-audio-data?tabs=portal#supported-audio-formats-and-codecs)
1. For additional explanation, see [fast transcription documentation](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/fast-transcription-create)


## Build the sample

* To tailor the sample to your configuration, use search and replace across the whole solution (for example, in Visual Studio, via **Edit** \> **Find and Replace** \> **Quick Replace**) to update the following strings:

* `YourSubscriptionKey`: replace with your subscription key.
* `YourServiceRegion`: replace with the [region](https://aka.ms/csspeech/region) your subscription is associated with.
For example, `westus` or `northeurope`.


## Run the sample
You can run the sample using Microsoft Visual Studio 2022 on Windows or using a .NET Core CLI

### Using Visual Studio 2022

TODO


### Using the .NET Core CLI

Navigate to the directory (samples/fast-transcription/csharp_client) that contains the sample and Run the following command

```bash
cd samples/fast-transcription/csharp_client
dotnet run
```

## References

* [fast transcription documentation](https://learn.microsoft.com/en-us/azure/ai-services/speech-service/fast-transcription-create)
Binary file not shown.
Loading