openai/openai-dotnet

Public

mirrored from https://github.com/openai/openai-dotnetAvailable

CodeCommitsIssuesPull requestsActionsInsightsSecurity
OpenAI_2.2.0-beta.2

Branches

Tags

  • No tags available.
0Branches0Tags
Go to file
Add file
Code

Clone

HTTPS

Download ZIP

tests/Audio/TranslationTests.cs

120lines · modecode

1using NUnit.Framework;
2using OpenAI.Audio;
3using OpenAI.Tests.Utility;
4using System;
5using System.ClientModel;
6using System.IO;
7using System.Threading;
8using System.Threading.Tasks;
9using static OpenAI.Tests.TestHelpers;
10
11namespace OpenAI.Tests.Audio;
12
13[TestFixture(true)]
14[TestFixture(false)]
15[Parallelizable(ParallelScope.All)]
16[Category("Audio")]
17public partial class TranslationTests : SyncAsyncTestBase
18{
19 public TranslationTests(bool isAsync) : base(isAsync)
20 {
21 }
22
23 public enum AudioSourceKind
24 {
25 UsingStream,
26 UsingFilePath,
27 }
28
29 [Test]
30 [TestCase(AudioSourceKind.UsingStream)]
31 [TestCase(AudioSourceKind.UsingFilePath)]
32 public async Task TranslationWorks(AudioSourceKind audioSourceKind)
33 {
34 AudioClient client = GetTestClient<AudioClient>(TestScenario.Audio_Whisper);
35
36 string filename = "audio_french.wav";
37 string path = Path.Combine("Assets", filename);
38 AudioTranslation translation = null;
39
40 if (audioSourceKind == AudioSourceKind.UsingStream)
41 {
42 using FileStream audio = File.OpenRead(path);
43
44 translation = IsAsync
45 ? await client.TranslateAudioAsync(audio, filename)
46 : client.TranslateAudio(audio, filename);
47 }
48 else if (audioSourceKind == AudioSourceKind.UsingFilePath)
49 {
50 translation = IsAsync
51 ? await client.TranslateAudioAsync(path)
52 : client.TranslateAudio(path);
53 }
54 Assert.That(translation?.Text, Is.Not.Null);
55 Assert.That(translation.Text.ToLowerInvariant(), Contains.Substring("whisper"));
56 }
57
58 [Test]
59 [TestCase("text")]
60 [TestCase("json")]
61 [TestCase("verbose_json")]
62 [TestCase("srt")]
63 [TestCase("vtt")]
64 [TestCase(null)]
65 public async Task TranslationFormatsWork(string responseFormat)
66 {
67 AudioClient client = GetTestClient<AudioClient>(TestScenario.Audio_Whisper);
68 string path = Path.Combine("Assets", "audio_french.wav");
69
70 AudioTranslationOptions options = new()
71 {
72 ResponseFormat = responseFormat switch
73 {
74 "text" => AudioTranslationFormat.Text,
75 "json" => AudioTranslationFormat.Simple,
76 "verbose_json" => AudioTranslationFormat.Verbose,
77 "srt" => AudioTranslationFormat.Srt,
78 "vtt" => AudioTranslationFormat.Vtt,
79 _ => (AudioTranslationFormat?)null
80 }
81 };
82
83 AudioTranslation translation = IsAsync
84 ? await client.TranslateAudioAsync(path, options)
85 : client.TranslateAudio(path, options);
86
87 Assert.That(translation?.Text?.ToLowerInvariant(), Does.Contain("recognition"));
88
89 if (options.ResponseFormat == AudioTranslationFormat.Verbose)
90 {
91 Assert.That(translation.Language, Is.EqualTo("english"));
92 Assert.That(translation.Duration, Is.GreaterThan(TimeSpan.Zero));
93 Assert.That(translation.Segments, Is.Not.Empty);
94
95 for (int i = 0; i < translation.Segments.Count; i++)
96 {
97 TranscribedSegment segment = translation.Segments[i];
98
99 if (i > 0)
100 {
101 Assert.That(segment.StartTime, Is.GreaterThanOrEqualTo(translation.Segments[i - 1].EndTime));
102 }
103
104 Assert.That(segment.Id, Is.EqualTo(i));
105 Assert.That(segment.EndTime, Is.GreaterThanOrEqualTo(segment.StartTime));
106 Assert.That(segment.TokenIds.Span.Length, Is.GreaterThan(0));
107
108 Assert.That(segment.AverageLogProbability, Is.LessThan(-0.001f).Or.GreaterThan(0.001f));
109 Assert.That(segment.CompressionRatio, Is.LessThan(-0.001f).Or.GreaterThan(0.001f));
110 Assert.That(segment.NoSpeechProbability, Is.LessThan(-0.001f).Or.GreaterThan(0.001f));
111 }
112 }
113 else
114 {
115 Assert.That(translation.Duration, Is.Null);
116 Assert.That(translation.Language, Is.Null);
117 Assert.That(translation.Segments, Is.Not.Null.And.Empty);
118 }
119 }
120}
121