Skip to content

Commit 06f8fde

Browse files
Merge pull request #424 from alexwolfmsft/dotnet-whisper-quickstart
add dotnet whisper quickstart
2 parents a6f5c47 + da9b097 commit 06f8fde

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
services: ai-services
3+
author: mrbullwinkle
4+
ms.author: mbullwin
5+
ms.service: openai
6+
ms.topic: include
7+
ms.date: 3/19/2024
8+
---
9+
10+
## Prerequisites
11+
12+
- An Azure subscription. You can [create one for free](https://azure.microsoft.com/free/cognitive-services?azure-portal=true).
13+
- An Azure OpenAI resource with a Whisper model deployed in a [supported region](../concepts/models.md#whisper-models). For more information, see [Create a resource and deploy a model with Azure OpenAI](../how-to/create-resource.md).
14+
- [The .NET 8.0 SDK](https://dotnet.microsoft.com/en-us/download)
15+
16+
## Set up
17+
18+
### Retrieve key and endpoint
19+
20+
To successfully make a call against Azure OpenAI, you need an *endpoint* and a *key*.
21+
22+
|Variable name | Value |
23+
|--------------------------|-------------|
24+
| `AZURE_OPENAI_ENDPOINT` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. Alternatively, you can find the value in the **Azure OpenAI Studio** > **Playground** > **Code View**. An example endpoint is: `https://aoai-docs.openai.azure.com/`.|
25+
| `AZURE_OPENAI_API_KEY` | This value can be found in the **Keys & Endpoint** section when examining your resource from the Azure portal. You can use either `KEY1` or `KEY2`.|
26+
27+
Go to your resource in the Azure portal. The **Endpoint and Keys** can be found in the **Resource Management** section. Copy your endpoint and access key as you'll need both for authenticating your API calls. You can use either `KEY1` or `KEY2`. Always having two keys allows you to securely rotate and regenerate keys without causing a service disruption.
28+
29+
:::image type="content" source="../media/quickstarts/endpoint.png" alt-text="Screenshot of the overview UI for an Azure OpenAI resource in the Azure portal with the endpoint & access keys location circled in red." lightbox="../media/quickstarts/endpoint.png":::
30+
31+
## Create the .NET app
32+
33+
1. Create a .NET app using the `dotnet new` command:
34+
35+
```dotnetcli
36+
dotnet new console -n OpenAIWhisper
37+
```
38+
39+
1. Change into the directory of the new app:
40+
41+
```dotnetcli
42+
cd OpenAIWhisper
43+
```
44+
45+
1. Install the [`Azure.OpenAI`](https://www.nuget.org/packages/Azure.AI.OpenAI/) client library:
46+
47+
```dotnetcli
48+
dotnet add package Azure.AI.OpenAI
49+
```
50+
51+
## Passwordless authentication is recommended
52+
53+
Passwordless authentication is more secure than key-based alternatives and is the recommended approach for connecting to Azure services. If you choose to use Passwordless authentication, you'll need to complete the following:
54+
55+
1. Add the [`Azure.Identity`](https://www.nuget.org/packages/Azure.Identity) package.
56+
57+
```dotnetcli
58+
dotnet add package Azure.Identity
59+
```
60+
61+
1. Assign the `Cognitive Services User` role to your user account. This can be done in the Azure portal on your OpenAI resource under **Access control (IAM)** > **Add role assignment**.
62+
1. Sign-in to Azure using Visual Studio or the Azure CLI via `az login`.
63+
64+
## Update the app code
65+
66+
1. Replace the contents of `program.cs` with the following code and update the placeholder values with your own.
67+
68+
> [!NOTE]
69+
> You can get sample audio files, such as *wikipediaOcelot.wav*, from the [Azure AI Speech SDK repository at GitHub](https://github.com/Azure-Samples/cognitive-services-speech-sdk/tree/master/sampledata/audiofiles).
70+
71+
```csharp
72+
using Azure;
73+
using Azure.AI.OpenAI;
74+
using Azure.Identity; // Required for Passwordless auth
75+
76+
var endpoint = new Uri("YOUR_OPENAI_ENDPOINT");
77+
var credentials = new AzureKeyCredential("YOUR_OPENAI_KEY");
78+
// var credentials = new DefaultAzureCredential(); // Use this line for Passwordless auth
79+
var deploymentName = "whisper"; // Default deployment name, update with your own if necessary
80+
var audioFilePath = "YOUR_AUDIO_FILE_PATH";
81+
82+
var openAIClient = new AzureOpenAIClient(endpoint, credentials);
83+
84+
var audioClient = openAIClient.GetAudioClient(deploymentName);
85+
86+
var result = await audioClient.TranscribeAudioAsync(audioFilePath);
87+
88+
Console.WriteLine("Transcribed text:");
89+
foreach (var item in result.Value.Text)
90+
{
91+
Console.Write(item);
92+
}
93+
```
94+
95+
> [!IMPORTANT]
96+
> For production, store and access your credentials using a secure method, such as [Azure Key Vault](/azure/key-vault/general/overview). For more information about credential security, see [Azure AI services security](../../security-features.md).
97+
98+
1. Run the application using the `dotnet run` command or the run button at the top of Visual Studio:
99+
100+
```dotnetcli
101+
dotnet run
102+
```
103+
104+
If you are using the sample audio file, you should see the following text printed out in the console:
105+
106+
```text
107+
The ocelot, Lepardus paradalis, is a small wild cat native to the southwestern United States,
108+
Mexico, and Central and South America. This medium-sized cat is characterized by solid
109+
black spots and streaks on its coat, round ears...
110+
```
111+

articles/ai-services/openai/whisper-quickstart.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ ms.reviewer: v-baolianzou
1111
ms.author: eur
1212
author: eric-urban
1313
recommendations: false
14-
zone_pivot_groups: programming-languages-rest-ps-py-js
14+
zone_pivot_groups: programming-languages-rest-ps-py-js-cs
1515
---
1616

1717
# Quickstart: Speech to text with the Azure OpenAI Whisper model
@@ -32,6 +32,12 @@ The file size limit for the Whisper model is 25 MB. If you need to transcribe a
3232

3333
::: zone-end
3434

35+
::: zone pivot="programming-language-dotnet"
36+
37+
[!INCLUDE [.NET SDK quickstart](includes/whisper-dotnet.md)]
38+
39+
::: zone-end
40+
3541
::: zone pivot="programming-language-javascript"
3642

3743
[!INCLUDE [JavaScript quickstart](includes/whisper-javascript.md)]

zone-pivots/zone-pivot-groups.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,20 @@ groups:
251251
title: PowerShell
252252
- id: rest-api
253253
title: REST
254+
- id: programming-languages-rest-ps-py-js-cs
255+
title: Programming languages
256+
prompt: Choose a programming language
257+
pivots:
258+
- id: programming-language-javascript
259+
title: JavaScript
260+
- id: programming-language-python
261+
title: Python
262+
- id: programming-language-dotnet
263+
title: C#
264+
- id: programming-language-powershell
265+
title: PowerShell
266+
- id: rest-api
267+
title: REST
254268
# Owner: diberry
255269
- id: programming-languages-set-six
256270
title: Programming languages

0 commit comments

Comments
 (0)