openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
src/Custom/Audio/AudioClient.cs
327lines · modeblame
9f9f2936Jose Arriaga Maldonado2 years ago | 1 | using System; |
| 2 | using System.ClientModel; | |
| 3 | using System.ClientModel.Primitives; | |
| 4 | using System.IO; | |
19a65a0aKrzysztof Cwalina2 years ago | 5 | using System.Threading; |
9f9f2936Jose Arriaga Maldonado2 years ago | 6 | using System.Threading.Tasks; |
| 7 | | |
| 8 | namespace OpenAI.Audio; | |
| 9 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 10 | // CUSTOM: |
| 11 | // - Renamed. | |
| 12 | // - Suppressed constructor that takes endpoint parameter; endpoint is now a property in the options class. | |
| 13 | // - Suppressed methods that only take the options parameter. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 14 | /// <summary> The service client for OpenAI audio operations. </summary> |
| 15 | [CodeGenClient("Audio")] | |
| 16 | [CodeGenSuppress("AudioClient", typeof(ClientPipeline), typeof(ApiKeyCredential), typeof(Uri))] | |
e0fee603Jose Arriaga Maldonado1 years ago | 17 | [CodeGenSuppress("CreateSpeechAsync", typeof(SpeechGenerationOptions), typeof(CancellationToken))] |
| 18 | [CodeGenSuppress("CreateSpeech", typeof(SpeechGenerationOptions), typeof(CancellationToken))] | |
9f9f2936Jose Arriaga Maldonado2 years ago | 19 | public partial class AudioClient |
| 20 | { | |
| 21 | private readonly string _model; | |
| 22 | | |
2ab1a942Jose Arriaga Maldonado1 years ago | 23 | // CUSTOM: Added as a convenience. |
e0fee603Jose Arriaga Maldonado1 years ago | 24 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
2ab1a942Jose Arriaga Maldonado1 years ago | 25 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 26 | /// <param name="apiKey"> The API key to authenticate with the service. </param> | |
| 27 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="apiKey"/> is null. </exception> | |
| 28 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> | |
| 29 | public AudioClient(string model, string apiKey) : this(model, new ApiKeyCredential(apiKey), new OpenAIClientOptions()) | |
| 30 | { | |
| 31 | } | |
| 32 | | |
9f9f2936Jose Arriaga Maldonado2 years ago | 33 | // CUSTOM: |
| 34 | // - Added `model` parameter. | |
13a9c686Jose Arriaga Maldonado1 years ago | 35 | // - Used a custom pipeline. |
| 36 | // - Demoted the endpoint parameter to be a property in the options class. | |
e0fee603Jose Arriaga Maldonado1 years ago | 37 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
13a9c686Jose Arriaga Maldonado1 years ago | 38 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 39 | /// <param name="credential"> The API key to authenticate with the service. </param> | |
| 40 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception> | |
| 41 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> | |
| 42 | public AudioClient(string model, ApiKeyCredential credential) : this(model, credential, new OpenAIClientOptions()) | |
| 43 | { | |
| 44 | } | |
9f9f2936Jose Arriaga Maldonado2 years ago | 45 | |
| 46 | // CUSTOM: | |
| 47 | // - Added `model` parameter. | |
13a9c686Jose Arriaga Maldonado1 years ago | 48 | // - Used a custom pipeline. |
| 49 | // - Demoted the endpoint parameter to be a property in the options class. | |
e0fee603Jose Arriaga Maldonado1 years ago | 50 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
13a9c686Jose Arriaga Maldonado1 years ago | 51 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> |
| 52 | /// <param name="credential"> The API key to authenticate with the service. </param> | |
| 53 | /// <param name="options"> The options to configure the client. </param> | |
| 54 | /// <exception cref="ArgumentNullException"> <paramref name="model"/> or <paramref name="credential"/> is null. </exception> | |
| 55 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> | |
| 56 | public AudioClient(string model, ApiKeyCredential credential, OpenAIClientOptions options) | |
| 57 | { | |
| 58 | Argument.AssertNotNullOrEmpty(model, nameof(model)); | |
| 59 | Argument.AssertNotNull(credential, nameof(credential)); | |
| 60 | options ??= new OpenAIClientOptions(); | |
9f9f2936Jose Arriaga Maldonado2 years ago | 61 | |
13a9c686Jose Arriaga Maldonado1 years ago | 62 | _model = model; |
e0fee603Jose Arriaga Maldonado1 years ago | 63 | Pipeline = OpenAIClient.CreatePipeline(credential, options); |
13a9c686Jose Arriaga Maldonado1 years ago | 64 | _endpoint = OpenAIClient.GetEndpoint(options); |
| 65 | } | |
| 66 | | |
| 67 | // CUSTOM: | |
| 68 | // - Added `model` parameter. | |
| 69 | // - Used a custom pipeline. | |
| 70 | // - Demoted the endpoint parameter to be a property in the options class. | |
| 71 | // - Made protected. | |
e0fee603Jose Arriaga Maldonado1 years ago | 72 | /// <summary> Initializes a new instance of <see cref="AudioClient"/>. </summary> |
13a9c686Jose Arriaga Maldonado1 years ago | 73 | /// <param name="pipeline"> The HTTP pipeline to send and receive REST requests and responses. </param> |
| 74 | /// <param name="model"> The name of the model to use in requests sent to the service. To learn more about the available models, see <see href="https://platform.openai.com/docs/models"/>. </param> | |
| 75 | /// <param name="options"> The options to configure the client. </param> | |
| 76 | /// <exception cref="ArgumentNullException"> <paramref name="pipeline"/> or <paramref name="model"/> is null. </exception> | |
| 77 | /// <exception cref="ArgumentException"> <paramref name="model"/> is an empty string, and was expected to be non-empty. </exception> | |
| 78 | protected internal AudioClient(ClientPipeline pipeline, string model, OpenAIClientOptions options) | |
9f9f2936Jose Arriaga Maldonado2 years ago | 79 | { |
13a9c686Jose Arriaga Maldonado1 years ago | 80 | Argument.AssertNotNull(pipeline, nameof(pipeline)); |
9f9f2936Jose Arriaga Maldonado2 years ago | 81 | Argument.AssertNotNullOrEmpty(model, nameof(model)); |
13a9c686Jose Arriaga Maldonado1 years ago | 82 | options ??= new OpenAIClientOptions(); |
9f9f2936Jose Arriaga Maldonado2 years ago | 83 | |
| 84 | _model = model; | |
e0fee603Jose Arriaga Maldonado1 years ago | 85 | Pipeline = pipeline; |
13a9c686Jose Arriaga Maldonado1 years ago | 86 | _endpoint = OpenAIClient.GetEndpoint(options); |
9f9f2936Jose Arriaga Maldonado2 years ago | 87 | } |
| 88 | | |
| 89 | #region GenerateSpeech | |
| 90 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 91 | /// <summary> Generates a life-like, spoken audio recording of the input text. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 92 | /// <remarks> |
13a9c686Jose Arriaga Maldonado1 years ago | 93 | /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified |
| 94 | /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 95 | /// </remarks> |
13a9c686Jose Arriaga Maldonado1 years ago | 96 | /// <param name="text"> The text to generate audio for. </param> |
| 97 | /// <param name="voice"> The voice to use in the generated audio. </param> | |
| 98 | /// <param name="options"> The options to configure the audio generation. </param> | |
| 99 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
| 100 | /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 101 | /// <returns> The generated audio in the specified output format. </returns> |
d84bf54dTravis Wilson1 years ago | 102 | public virtual async Task<ClientResult<BinaryData>> GenerateSpeechAsync(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 103 | { |
| 104 | Argument.AssertNotNull(text, nameof(text)); | |
| 105 | | |
| 106 | options ??= new(); | |
| 107 | CreateSpeechGenerationOptions(text, voice, ref options); | |
| 108 | | |
e0fee603Jose Arriaga Maldonado1 years ago | 109 | using BinaryContent content = options; |
d84bf54dTravis Wilson1 years ago | 110 | ClientResult result = await GenerateSpeechAsync(content, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
9f9f2936Jose Arriaga Maldonado2 years ago | 111 | return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); |
| 112 | } | |
| 113 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 114 | /// <summary> Generates a life-like, spoken audio recording of the input text. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 115 | /// <remarks> |
13a9c686Jose Arriaga Maldonado1 years ago | 116 | /// The default format of the generated audio is <see cref="GeneratedSpeechFormat.Mp3"/> unless otherwise specified |
| 117 | /// via <see cref="SpeechGenerationOptions.ResponseFormat"/>. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 118 | /// </remarks> |
13a9c686Jose Arriaga Maldonado1 years ago | 119 | /// <param name="text"> The text to generate audio for. </param> |
| 120 | /// <param name="voice"> The voice to use in the generated audio. </param> | |
| 121 | /// <param name="options"> The options to configure the audio generation. </param> | |
| 122 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
| 123 | /// <exception cref="ArgumentNullException"> <paramref name="text"/> is null. </exception> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 124 | /// <returns> The generated audio in the specified output format. </returns> |
d84bf54dTravis Wilson1 years ago | 125 | public virtual ClientResult<BinaryData> GenerateSpeech(string text, GeneratedSpeechVoice voice, SpeechGenerationOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 126 | { |
| 127 | Argument.AssertNotNull(text, nameof(text)); | |
| 128 | | |
| 129 | options ??= new(); | |
| 130 | CreateSpeechGenerationOptions(text, voice, ref options); | |
| 131 | | |
e0fee603Jose Arriaga Maldonado1 years ago | 132 | using BinaryContent content = options; |
d84bf54dTravis Wilson1 years ago | 133 | ClientResult result = GenerateSpeech(content, cancellationToken.ToRequestOptions()); ; |
9f9f2936Jose Arriaga Maldonado2 years ago | 134 | return ClientResult.FromValue(result.GetRawResponse().Content, result.GetRawResponse()); |
| 135 | } | |
| 136 | | |
| 137 | #endregion | |
| 138 | | |
| 139 | #region TranscribeAudio | |
| 140 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 141 | /// <summary> Transcribes the input audio. </summary> |
| 142 | /// <param name="audio"> The audio stream to transcribe. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 143 | /// <param name="audioFilename"> |
13a9c686Jose Arriaga Maldonado1 years ago | 144 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 145 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual | |
| 146 | /// format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 147 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 148 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 149 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 150 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 151 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> | |
19a65a0aKrzysztof Cwalina2 years ago | 152 | public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 153 | { |
| 154 | Argument.AssertNotNull(audio, nameof(audio)); | |
| 155 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); | |
| 156 | | |
| 157 | options ??= new(); | |
| 158 | CreateAudioTranscriptionOptions(audio, audioFilename, ref options); | |
| 159 | | |
| 160 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); | |
19a65a0aKrzysztof Cwalina2 years ago | 161 | ClientResult result = await TranscribeAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
9f9f2936Jose Arriaga Maldonado2 years ago | 162 | return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 163 | } | |
| 164 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 165 | /// <summary> Transcribes the input audio. </summary> |
| 166 | /// <param name="audio"> The audio stream to transcribe. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 167 | /// <param name="audioFilename"> |
13a9c686Jose Arriaga Maldonado1 years ago | 168 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 169 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual | |
| 170 | /// format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 171 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 172 | /// <param name="options"> The options to configure the audio transcription. </param> |
| 173 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 174 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 175 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> | |
19a65a0aKrzysztof Cwalina2 years ago | 176 | public virtual ClientResult<AudioTranscription> TranscribeAudio(Stream audio, string audioFilename, AudioTranscriptionOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 177 | { |
| 178 | Argument.AssertNotNull(audio, nameof(audio)); | |
| 179 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); | |
| 180 | | |
| 181 | options ??= new(); | |
| 182 | CreateAudioTranscriptionOptions(audio, audioFilename, ref options); | |
| 183 | | |
| 184 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); | |
19a65a0aKrzysztof Cwalina2 years ago | 185 | ClientResult result = TranscribeAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); |
9f9f2936Jose Arriaga Maldonado2 years ago | 186 | return ClientResult.FromValue(AudioTranscription.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 187 | } | |
| 188 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 189 | /// <summary> Transcribes the input audio. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 190 | /// <param name="audioFilePath"> |
13a9c686Jose Arriaga Maldonado1 years ago | 191 | /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be |
| 192 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the | |
| 193 | /// actual format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 194 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 195 | /// <param name="options"> The options to configure the audio transcription. </param> |
9f9f2936Jose Arriaga Maldonado2 years ago | 196 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception> |
| 197 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> | |
| 198 | public virtual async Task<ClientResult<AudioTranscription>> TranscribeAudioAsync(string audioFilePath, AudioTranscriptionOptions options = null) | |
| 199 | { | |
| 200 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); | |
| 201 | | |
| 202 | using FileStream audioStream = File.OpenRead(audioFilePath); | |
| 203 | return await TranscribeAudioAsync(audioStream, audioFilePath, options).ConfigureAwait(false); | |
| 204 | } | |
| 205 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 206 | /// <summary> Transcribes the input audio. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 207 | /// <param name="audioFilePath"> |
13a9c686Jose Arriaga Maldonado1 years ago | 208 | /// The path of the audio file to transcribe. The provided file path's extension (for example: .mp3) will be |
| 209 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the | |
| 210 | /// actual format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 211 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 212 | /// <param name="options"> The options to configure the audio transcription. </param> |
9f9f2936Jose Arriaga Maldonado2 years ago | 213 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> is null. </exception> |
| 214 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> | |
| 215 | public virtual ClientResult<AudioTranscription> TranscribeAudio(string audioFilePath, AudioTranscriptionOptions options = null) | |
| 216 | { | |
| 217 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); | |
| 218 | | |
| 219 | using FileStream audioStream = File.OpenRead(audioFilePath); | |
| 220 | return TranscribeAudio(audioStream, audioFilePath, options); | |
| 221 | } | |
| 222 | | |
| 223 | #endregion | |
| 224 | | |
| 225 | #region TranslateAudio | |
| 226 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 227 | /// <summary> Translates the input audio into English. </summary> |
| 228 | /// <param name="audio"> The audio stream to translate. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 229 | /// <param name="audioFilename"> |
13a9c686Jose Arriaga Maldonado1 years ago | 230 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 231 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual | |
| 232 | /// format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 233 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 234 | /// <param name="options"> The options to configure the audio translation. </param> |
| 235 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 236 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 237 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> | |
19a65a0aKrzysztof Cwalina2 years ago | 238 | public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 239 | { |
| 240 | Argument.AssertNotNull(audio, nameof(audio)); | |
| 241 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); | |
| 242 | | |
| 243 | options ??= new(); | |
| 244 | CreateAudioTranslationOptions(audio, audioFilename, ref options); | |
| 245 | | |
| 246 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); | |
19a65a0aKrzysztof Cwalina2 years ago | 247 | ClientResult result = await TranslateAudioAsync(content, content.ContentType, cancellationToken.ToRequestOptions()).ConfigureAwait(false); |
9f9f2936Jose Arriaga Maldonado2 years ago | 248 | return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 249 | } | |
| 250 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 251 | /// <summary> Translates the input audio into English. </summary> |
| 252 | /// <param name="audio"> The audio stream to translate. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 253 | /// <param name="audioFilename"> |
13a9c686Jose Arriaga Maldonado1 years ago | 254 | /// The filename associated with the audio stream. The filename's extension (for example: .mp3) will be used to |
| 255 | /// validate the format of the input audio. The request may fail if the filename's extension and the actual | |
| 256 | /// format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 257 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 258 | /// <param name="options"> The options to configure the audio translation. </param> |
| 259 | /// <param name="cancellationToken"> A token that can be used to cancel this method call. </param> | |
9f9f2936Jose Arriaga Maldonado2 years ago | 260 | /// <exception cref="ArgumentNullException"> <paramref name="audio"/> or <paramref name="audioFilename"/> is null. </exception> |
| 261 | /// <exception cref="ArgumentException"> <paramref name="audioFilename"/> is an empty string, and was expected to be non-empty. </exception> | |
19a65a0aKrzysztof Cwalina2 years ago | 262 | public virtual ClientResult<AudioTranslation> TranslateAudio(Stream audio, string audioFilename, AudioTranslationOptions options = null, CancellationToken cancellationToken = default) |
9f9f2936Jose Arriaga Maldonado2 years ago | 263 | { |
| 264 | Argument.AssertNotNull(audio, nameof(audio)); | |
| 265 | Argument.AssertNotNullOrEmpty(audioFilename, nameof(audioFilename)); | |
| 266 | | |
| 267 | options ??= new(); | |
| 268 | CreateAudioTranslationOptions(audio, audioFilename, ref options); | |
| 269 | | |
| 270 | using MultipartFormDataBinaryContent content = options.ToMultipartContent(audio, audioFilename); | |
19a65a0aKrzysztof Cwalina2 years ago | 271 | ClientResult result = TranslateAudio(content, content.ContentType, cancellationToken.ToRequestOptions()); |
9f9f2936Jose Arriaga Maldonado2 years ago | 272 | return ClientResult.FromValue(AudioTranslation.FromResponse(result.GetRawResponse()), result.GetRawResponse()); |
| 273 | } | |
| 274 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 275 | /// <summary> Translates the input audio into English. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 276 | /// <param name="audioFilePath"> |
13a9c686Jose Arriaga Maldonado1 years ago | 277 | /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be |
| 278 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the | |
| 279 | /// actual format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 280 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 281 | /// <param name="options"> The options to configure the audio translation. </param> |
9f9f2936Jose Arriaga Maldonado2 years ago | 282 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception> |
| 283 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> | |
| 284 | public virtual ClientResult<AudioTranslation> TranslateAudio(string audioFilePath, AudioTranslationOptions options = null) | |
| 285 | { | |
| 286 | Argument.AssertNotNullOrEmpty(audioFilePath, nameof(audioFilePath)); | |
| 287 | | |
| 288 | using FileStream audioStream = File.OpenRead(audioFilePath); | |
| 289 | return TranslateAudio(audioStream, audioFilePath, options); | |
| 290 | } | |
| 291 | | |
13a9c686Jose Arriaga Maldonado1 years ago | 292 | /// <summary> Translates the input audio into English. </summary> |
9f9f2936Jose Arriaga Maldonado2 years ago | 293 | /// <param name="audioFilePath"> |
13a9c686Jose Arriaga Maldonado1 years ago | 294 | /// The path of the audio file to translate. The provided file path's extension (for example: .mp3) will be |
| 295 | /// used to validate the format of the input audio. The request may fail if the file path's extension and the | |
| 296 | /// actual format of the input audio do not match. | |
9f9f2936Jose Arriaga Maldonado2 years ago | 297 | /// </param> |
13a9c686Jose Arriaga Maldonado1 years ago | 298 | /// <param name="options"> The options to configure the audio translation. </param> |
9f9f2936Jose Arriaga Maldonado2 years ago | 299 | /// <exception cref="ArgumentNullException"> <paramref name="audioFilePath"/> was null. </exception> |
| 300 | /// <exception cref="ArgumentException"> <paramref name="audioFilePath"/> is an empty string, and was expected to be non-empty. </exception> | |
| 301 | public virtual async Task<ClientResult<AudioTranslation>> TranslateAudioAsync(string audioFilePath, AudioTranslationOptions options = null) | |
| 302 | { | |
| 303 | Argument.AssertNotNull(audioFilePath, nameof(audioFilePath)); | |
| 304 | | |
| 305 | using FileStream audioStream = File.OpenRead(audioFilePath); | |
| 306 | return await TranslateAudioAsync(audioStream, audioFilePath, options); | |
| 307 | } | |
| 308 | | |
| 309 | #endregion | |
79014abcShivangiReja1 years ago | 310 | |
9f9f2936Jose Arriaga Maldonado2 years ago | 311 | private void CreateSpeechGenerationOptions(string text, GeneratedSpeechVoice voice, ref SpeechGenerationOptions options) |
| 312 | { | |
| 313 | options.Input = text; | |
| 314 | options.Voice = voice; | |
| 315 | options.Model = _model; | |
| 316 | } | |
| 317 | | |
| 318 | private void CreateAudioTranscriptionOptions(Stream audio, string audioFilename, ref AudioTranscriptionOptions options) | |
| 319 | { | |
| 320 | options.Model = _model; | |
| 321 | } | |
| 322 | | |
| 323 | private void CreateAudioTranslationOptions(Stream audio, string audioFilename, ref AudioTranslationOptions options) | |
| 324 | { | |
| 325 | options.Model = _model; | |
| 326 | } | |
| 327 | } |