-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathopenai_tts_example.rs
More file actions
34 lines (27 loc) · 1.03 KB
/
openai_tts_example.rs
File metadata and controls
34 lines (27 loc) · 1.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//! Example demonstrating text-to-speech synthesis using OpenAI
//!
//! This example shows how to:
//! 1. Initialize the OpenAI text-to-speech provider
//! 2. Generate speech from text
//! 3. Save the audio output to a file
use llm::builder::{LLMBackend, LLMBuilder};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Get API key from environment variable or use test key
let api_key = std::env::var("OPENAI_API_KEY").unwrap_or("test_key".into());
// Initialize OpenAI text-to-speech provider
let tts = LLMBuilder::new()
.backend(LLMBackend::OpenAI)
.api_key(api_key)
.model("tts-1")
.voice("ash")
.build()?;
// Text to convert to speech
let text = "Hello! This is an example of text-to-speech synthesis using OpenAI.";
// Generate speech
let audio_data = tts.speech(text).await?;
// Save the audio to a file
std::fs::write("output-speech.mp3", audio_data)?;
println!("Audio file generated successfully: output-speech.mp3");
Ok(())
}