openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Audio/Example05_DiarizedTranscription.cs
63lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Audio; |
| 3 | using System; |
| 4 | using System.IO; |
| 5 | |
| 6 | namespace OpenAI.Examples; |
| 7 | |
| 8 | public partial class AudioExamples |
| 9 | { |
| 10 | [Test] |
| 11 | public void Example05_DiarizedTranscription() |
| 12 | { |
| 13 | AudioClient client = new("gpt-4o-transcribe-diarize", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); |
| 14 | |
| 15 | string audioFilePath = Path.Combine("Assets", "audio_meeting.wav"); |
| 16 | string speakerRefPath = Path.Combine("Assets", "audio_agent_reference.wav"); |
| 17 | |
| 18 | byte[] speakerRefBytes = File.ReadAllBytes(speakerRefPath); |
| 19 | string speakerRefBase64 = Convert.ToBase64String(speakerRefBytes); |
| 20 | |
| 21 | AudioTranscriptionOptions options = new() |
| 22 | { |
| 23 | ResponseFormat = AudioTranscriptionFormat.Diarized, |
| 24 | ChunkingStrategy = AudioTranscriptionDefaultChunkingStrategy.Auto, |
| 25 | KnownSpeakerNames = { "agent" }, |
| 26 | KnownSpeakerReferenceUris = { new Uri($"data:audio/wav;base64,{speakerRefBase64}") }, |
| 27 | }; |
| 28 | |
| 29 | DiarizedAudioTranscription transcription = client.TranscribeAudioDiarized(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 | } |
| 64 | |