openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Audio/Example05_DiarizedTranscriptionAsync.cs
63lines · modeblame
d002b9c3ShivangiReja5 months ago | 1 | using NUnit.Framework; |
| 2 | using OpenAI.Audio; | |
| 3 | using System; | |
| 4 | using System.IO; | |
| 5 | using System.Threading.Tasks; | |
| 6 | | |
| 7 | namespace OpenAI.Examples; | |
| 8 | | |
| 9 | public partial class AudioExamples | |
| 10 | { | |
| 11 | [Test] | |
| 12 | public async Task Example05_DiarizedTranscriptionAsync() | |
| 13 | { | |
| 14 | AudioClient client = new("gpt-4o-transcribe-diarize", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | |
| 15 | | |
| 16 | string audioFilePath = Path.Combine("Assets", "audio_meeting.wav"); | |
| 17 | string speakerRefPath = Path.Combine("Assets", "audio_agent_reference.wav"); | |
| 18 | | |
| 19 | byte[] speakerRefBytes = File.ReadAllBytes(speakerRefPath); | |
| 20 | string speakerRefBase64 = Convert.ToBase64String(speakerRefBytes); | |
| 21 | | |
| 22 | AudioTranscriptionOptions options = new() | |
| 23 | { | |
| 24 | ResponseFormat = AudioTranscriptionFormat.Diarized, | |
| 25 | KnownSpeakerNames = { "agent" }, | |
| 26 | KnownSpeakerReferenceUris = { new Uri($"data:audio/wav;base64,{speakerRefBase64}") }, | |
| 27 | }; | |
| 28 | | |
| 29 | DiarizedAudioTranscription transcription = await client.TranscribeAudioDiarizedAsync(audioFilePath, options); | |
| 30 | | |
| 31 | Console.WriteLine("Transcription:"); | |
| 32 | Console.WriteLine($"{transcription.Text}"); | |
| 33 | | |
| 34 | Console.WriteLine(); | |
| 35 | Console.WriteLine($"Duration: {transcription.Duration.TotalSeconds:0.00}s"); | |
| 36 | | |
| 37 | Console.WriteLine(); | |
| 38 | Console.WriteLine($"Segments:"); | |
| 39 | foreach (DiarizedTranscriptionSegment segment in transcription.Segments) | |
| 40 | { | |
| 41 | Console.WriteLine($" [{segment.SpeakerLabel}] {segment.Text,90} : {segment.StartTime.TotalMilliseconds,5:0} - {segment.EndTime.TotalMilliseconds,5:0}"); | |
| 42 | } | |
| 43 | | |
| 44 | Console.WriteLine(); | |
| 45 | Console.WriteLine($"Usage:"); | |
| 46 | if (transcription.Usage is AudioTranscriptionTokenUsage tokenUsage) | |
| 47 | { | |
| 48 | Console.WriteLine($" Input tokens: {tokenUsage.InputTokenCount}"); | |
| 49 | Console.WriteLine($" Output tokens: {tokenUsage.OutputTokenCount}"); | |
| 50 | Console.WriteLine($" Total tokens: {tokenUsage.TotalTokenCount}"); | |
| 51 | if (tokenUsage.InputTokenDetails is not null) | |
| 52 | { | |
| 53 | Console.WriteLine($" Input token details:"); | |
| 54 | Console.WriteLine($" Text tokens: {tokenUsage.InputTokenDetails.TextTokenCount}"); | |
| 55 | Console.WriteLine($" Audio tokens: {tokenUsage.InputTokenDetails.AudioTokenCount}"); | |
| 56 | } | |
| 57 | } | |
| 58 | else if (transcription.Usage is AudioTranscriptionDurationUsage durationUsage) | |
| 59 | { | |
| 60 | Console.WriteLine($" Duration: {durationUsage.Duration.TotalSeconds:0.00}s"); | |
| 61 | } | |
| 62 | } | |
| 63 | } |