openai/openai-dotnet
Publicmirrored from https://github.com/openai/openai-dotnetAvailable
examples/Audio/Example01_SimpleTextToSpeechAsync.cs
27lines · modeblame
8cc6643fJose Arriaga Maldonado2 years 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 Example01_SimpleTextToSpeechAsync() | |
| 13 | { | |
| 14 | AudioClient client = new("tts-1", Environment.GetEnvironmentVariable("OPENAI_API_KEY")); | |
| 15 | | |
| 16 | string input = "Overwatering is a common issue for those taking care of houseplants. To prevent it, it is" | |
| 17 | + " crucial to allow the soil to dry out between waterings. Instead of watering on a fixed schedule," | |
| 18 | + " consider using a moisture meter to accurately gauge the soil’s wetness. Should the soil retain" | |
| 19 | + " moisture, it is wise to postpone watering for a couple more days. When in doubt, it is often safer" | |
| 20 | + " to water sparingly and maintain a less-is-more approach."; | |
| 21 | | |
d84bf54dTravis Wilson1 years ago | 22 | BinaryData speech = await client.GenerateSpeechAsync(input, GeneratedSpeechVoice.Alloy); |
8cc6643fJose Arriaga Maldonado2 years ago | 23 | |
| 24 | using FileStream stream = File.OpenWrite($"{Guid.NewGuid()}.mp3"); | |
| 25 | speech.ToStream().CopyTo(stream); | |
| 26 | } | |
| 27 | } |