openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
tests/Audio/TranslationTests.cs
76lines · modecode
| 1 | using NUnit.Framework; |
| 2 | using OpenAI.Audio; |
| 3 | using OpenAI.Tests.Utility; |
| 4 | using System.IO; |
| 5 | using System.Threading.Tasks; |
| 6 | using static OpenAI.Tests.TestHelpers; |
| 7 | |
| 8 | namespace OpenAI.Tests.Audio; |
| 9 | |
| 10 | [TestFixture(true)] |
| 11 | [TestFixture(false)] |
| 12 | [Parallelizable(ParallelScope.All)] |
| 13 | [Category("Audio")] |
| 14 | public partial class TranslationTests : SyncAsyncTestBase |
| 15 | { |
| 16 | public TranslationTests(bool isAsync) : base(isAsync) |
| 17 | { |
| 18 | } |
| 19 | |
| 20 | public enum AudioSourceKind |
| 21 | { |
| 22 | UsingStream, |
| 23 | UsingFilePath, |
| 24 | } |
| 25 | |
| 26 | [Test] |
| 27 | [TestCase(AudioSourceKind.UsingStream)] |
| 28 | [TestCase(AudioSourceKind.UsingFilePath)] |
| 29 | public async Task TranslationWorks(AudioSourceKind audioSourceKind) |
| 30 | { |
| 31 | AudioClient client = GetTestClient<AudioClient>(TestScenario.Audio_Whisper); |
| 32 | |
| 33 | string filename = "audio_french.wav"; |
| 34 | string path = Path.Combine("Assets", filename); |
| 35 | AudioTranslation translation = null; |
| 36 | |
| 37 | if (audioSourceKind == AudioSourceKind.UsingStream) |
| 38 | { |
| 39 | using FileStream audio = File.OpenRead(path); |
| 40 | |
| 41 | translation = IsAsync |
| 42 | ? await client.TranslateAudioAsync(audio, filename) |
| 43 | : client.TranslateAudio(audio, filename); |
| 44 | } |
| 45 | else if (audioSourceKind == AudioSourceKind.UsingFilePath) |
| 46 | { |
| 47 | translation = IsAsync |
| 48 | ? await client.TranslateAudioAsync(path) |
| 49 | : client.TranslateAudio(path); |
| 50 | } |
| 51 | Assert.That(translation?.Text, Is.Not.Null); |
| 52 | Assert.That(translation.Text.ToLowerInvariant(), Contains.Substring("whisper")); |
| 53 | } |
| 54 | |
| 55 | [Test] |
| 56 | [TestCase(AudioTranslationFormat.Simple)] |
| 57 | [TestCase(AudioTranslationFormat.Verbose)] |
| 58 | [TestCase(AudioTranslationFormat.Srt)] |
| 59 | [TestCase(AudioTranslationFormat.Vtt)] |
| 60 | public async Task TranslationFormatsWork(AudioTranslationFormat formatToTest) |
| 61 | { |
| 62 | AudioClient client = GetTestClient<AudioClient>(TestScenario.Audio_Whisper); |
| 63 | string path = Path.Combine("Assets", "audio_french.wav"); |
| 64 | |
| 65 | AudioTranslationOptions options = new() |
| 66 | { |
| 67 | ResponseFormat = formatToTest, |
| 68 | }; |
| 69 | |
| 70 | AudioTranslation translation = IsAsync |
| 71 | ? await client.TranslateAudioAsync(path, options) |
| 72 | : client.TranslateAudio(path, options); |
| 73 | |
| 74 | Assert.That(translation?.Text?.ToLowerInvariant(), Does.Contain("recognition")); |
| 75 | } |
| 76 | } |