Skip to content

Commit 7b95361

Browse files
authored
Add comprehensive samples for missing Azure.OpenAI APIs (#52137)
1 parent 588dd67 commit 7b95361

File tree

5 files changed

+1136
-0
lines changed

5 files changed

+1136
-0
lines changed
Lines changed: 202 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,202 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#nullable disable
5+
6+
using System;
7+
using System.IO;
8+
using Azure.Identity;
9+
using OpenAI.Audio;
10+
11+
namespace Azure.AI.OpenAI.Samples;
12+
13+
public partial class AzureOpenAISamples
14+
{
15+
public void AudioTranscription()
16+
{
17+
#region Snippet:AudioTranscription
18+
AzureOpenAIClient azureClient = new(
19+
new Uri("https://your-azure-openai-resource.com"),
20+
new DefaultAzureCredential());
21+
AudioClient audioClient = azureClient.GetAudioClient("my-whisper-deployment");
22+
23+
// Load an audio file from your local system
24+
string audioFilePath = "path/to/your/audio/file.wav";
25+
26+
// Transcribe the audio to text
27+
AudioTranscription transcription = audioClient.TranscribeAudio(audioFilePath);
28+
29+
Console.WriteLine($"Transcribed text: {transcription.Text}");
30+
#endregion
31+
}
32+
33+
public void AudioTranscriptionWithOptions()
34+
{
35+
AzureOpenAIClient azureClient = new(
36+
new Uri("https://your-azure-openai-resource.com"),
37+
new DefaultAzureCredential());
38+
AudioClient audioClient = azureClient.GetAudioClient("my-whisper-deployment");
39+
40+
#region Snippet:AudioTranscriptionWithOptions
41+
string audioFilePath = "path/to/your/audio/file.wav";
42+
43+
// Configure transcription options for enhanced output
44+
AudioTranscriptionOptions options = new()
45+
{
46+
ResponseFormat = AudioTranscriptionFormat.Verbose,
47+
Temperature = 0.2f,
48+
Language = "en", // Specify the language if known for better accuracy
49+
Prompt = "This is a recording about artificial intelligence and machine learning.", // Context hint
50+
};
51+
52+
AudioTranscription transcription = audioClient.TranscribeAudio(audioFilePath, options);
53+
54+
Console.WriteLine($"Transcribed text: {transcription.Text}");
55+
Console.WriteLine($"Language detected: {transcription.Language}");
56+
Console.WriteLine($"Duration: {transcription.Duration}");
57+
58+
// Access word-level timestamps if using verbose format
59+
if (transcription.Words != null)
60+
{
61+
Console.WriteLine("Word-level timestamps:");
62+
foreach (var word in transcription.Words)
63+
{
64+
Console.WriteLine($" {word.Word}: timestamps available");
65+
}
66+
}
67+
#endregion
68+
}
69+
70+
public void AudioTranslation()
71+
{
72+
AzureOpenAIClient azureClient = new(
73+
new Uri("https://your-azure-openai-resource.com"),
74+
new DefaultAzureCredential());
75+
AudioClient audioClient = azureClient.GetAudioClient("my-whisper-deployment");
76+
77+
#region Snippet:AudioTranslation
78+
// Load an audio file in any supported language
79+
string audioFilePath = "path/to/your/foreign/audio/file.wav";
80+
81+
// Translate the audio to English text
82+
AudioTranslation translation = audioClient.TranslateAudio(audioFilePath);
83+
84+
Console.WriteLine($"Translated to English: {translation.Text}");
85+
#endregion
86+
}
87+
88+
public void AudioTranslationWithOptions()
89+
{
90+
AzureOpenAIClient azureClient = new(
91+
new Uri("https://your-azure-openai-resource.com"),
92+
new DefaultAzureCredential());
93+
AudioClient audioClient = azureClient.GetAudioClient("my-whisper-deployment");
94+
95+
#region Snippet:AudioTranslationWithOptions
96+
string audioFilePath = "path/to/your/foreign/audio/file.wav";
97+
98+
// Configure translation options for enhanced output
99+
AudioTranslationOptions options = new()
100+
{
101+
ResponseFormat = AudioTranslationFormat.Verbose,
102+
Temperature = 0.1f, // Lower temperature for more deterministic translation
103+
Prompt = "This is a technical presentation about computer science.", // Context for better translation
104+
};
105+
106+
AudioTranslation translation = audioClient.TranslateAudio(audioFilePath, options);
107+
108+
Console.WriteLine($"Translated text: {translation.Text}");
109+
Console.WriteLine($"Duration: {translation.Duration}");
110+
#endregion
111+
}
112+
113+
#if !AZURE_OPENAI_GA
114+
public void TextToSpeech()
115+
{
116+
AzureOpenAIClient azureClient = new(
117+
new Uri("https://your-azure-openai-resource.com"),
118+
new DefaultAzureCredential());
119+
AudioClient audioClient = azureClient.GetAudioClient("my-tts-deployment");
120+
121+
#region Snippet:TextToSpeech
122+
string textToSpeak = "Hello! Welcome to Azure OpenAI text-to-speech. This technology can convert written text into natural-sounding speech.";
123+
124+
// Generate speech from text
125+
BinaryData speechData = audioClient.GenerateSpeech(textToSpeak, GeneratedSpeechVoice.Alloy);
126+
127+
// Save the audio to a file
128+
string outputPath = "generated_speech.mp3";
129+
File.WriteAllBytes(outputPath, speechData.ToArray());
130+
131+
Console.WriteLine($"Speech generated and saved to: {outputPath}");
132+
#endregion
133+
}
134+
135+
public void TextToSpeechWithOptions()
136+
{
137+
AzureOpenAIClient azureClient = new(
138+
new Uri("https://your-azure-openai-resource.com"),
139+
new DefaultAzureCredential());
140+
AudioClient audioClient = azureClient.GetAudioClient("my-tts-deployment");
141+
142+
#region Snippet:TextToSpeechWithOptions
143+
string textToSpeak = "This is a demonstration of advanced text-to-speech capabilities with customized voice settings.";
144+
145+
// Generate speech with specific voice
146+
BinaryData speechData = audioClient.GenerateSpeech(textToSpeak, GeneratedSpeechVoice.Nova);
147+
148+
// Save with descriptive filename
149+
string outputPath = $"speech_nova_{DateTime.Now:yyyyMMdd_HHmmss}.mp3";
150+
File.WriteAllBytes(outputPath, speechData.ToArray());
151+
152+
Console.WriteLine($"Speech generated with Nova voice");
153+
Console.WriteLine($"Saved to: {outputPath}");
154+
#endregion
155+
}
156+
157+
public void AudioWorkflowExample()
158+
{
159+
AzureOpenAIClient azureClient = new(
160+
new Uri("https://your-azure-openai-resource.com"),
161+
new DefaultAzureCredential());
162+
163+
AudioClient whisperClient = azureClient.GetAudioClient("my-whisper-deployment");
164+
AudioClient ttsClient = azureClient.GetAudioClient("my-tts-deployment");
165+
166+
#region Snippet:AudioWorkflowExample
167+
// Complete workflow: audio processing pipeline
168+
string inputAudioPath = "path/to/meeting/recording.wav";
169+
170+
// Step 1: Transcribe the meeting recording
171+
Console.WriteLine("Transcribing meeting recording...");
172+
AudioTranscriptionOptions transcriptionOptions = new()
173+
{
174+
ResponseFormat = AudioTranscriptionFormat.Verbose,
175+
Language = "en",
176+
Prompt = "This is a business meeting discussing project updates and next steps.",
177+
};
178+
179+
AudioTranscription transcription = whisperClient.TranscribeAudio(inputAudioPath, transcriptionOptions);
180+
Console.WriteLine($"Meeting transcribed: {transcription.Text.Length} characters");
181+
182+
// Step 2: Extract key points or summary (this would typically involve chat completion)
183+
string summary = "Key meeting outcomes: Project milestone achieved, budget approved, next review scheduled for next week.";
184+
185+
// Step 3: Convert summary back to speech for accessibility
186+
Console.WriteLine("Generating audio summary...");
187+
188+
BinaryData summaryAudio = ttsClient.GenerateSpeech(summary, GeneratedSpeechVoice.Echo);
189+
190+
// Step 4: Save outputs
191+
File.WriteAllText("meeting_transcript.txt", transcription.Text);
192+
File.WriteAllText("meeting_summary.txt", summary);
193+
File.WriteAllBytes("meeting_summary.mp3", summaryAudio.ToArray());
194+
195+
Console.WriteLine("Audio workflow completed:");
196+
Console.WriteLine("- meeting_transcript.txt: Full transcription");
197+
Console.WriteLine("- meeting_summary.txt: Key points summary");
198+
Console.WriteLine("- meeting_summary.mp3: Audio summary");
199+
#endregion
200+
}
201+
#endif
202+
}

0 commit comments

Comments
 (0)