Skip to content

Commit

Permalink
fix build errors
Browse files Browse the repository at this point in the history
  • Loading branch information
DamienDeepgram committed Jul 14, 2024
1 parent 0ae2ec5 commit 7114c84
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/text_to_speech_to_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async fn main() -> Result<(), DeepgramError> {

dg_client
.text_to_speech()
.speak(text, &options, &output_file)
.speak(text, &options, output_file)
.await?;

Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/speak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub mod rest;

/// Generate speech from text using Deepgram's text to speech api.
///
/// Constructed using [`Deepgram::speak`].
/// Constructed using [`Deepgram::text_to_speech`].
///
/// See the [Deepgram API Reference][api] for more info.
///
Expand Down
16 changes: 9 additions & 7 deletions src/speak/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ use std::fs::File;
use std::io::copy;
use std::path::Path;
use url::Url;
use serde_json::json;
use options::{Options, SerializableOptions};

use super::Speak;

pub mod options;

use options::{Options, SerializableOptions};

static DEEPGRAM_API_URL_SPEAK: &str = "v1/speak";

Expand All @@ -31,9 +32,10 @@ impl<'a> Speak<'a> {
.client
.post(self.speak_url())
.query(&SerializableOptions(options))
.json(&serde_json::json!({ "text": text }));
.json(&json!({ "text": text }));

self.send_and_translate_response(request_builder, output_file).await
self.send_and_translate_response(request_builder, output_file)
.await
}

async fn send_and_translate_response(
Expand All @@ -42,24 +44,24 @@ impl<'a> Speak<'a> {
output_file: &Path,
) -> crate::Result<()> {
let mut response = request_builder.send().await?;

// Ensure the request was successful
if response.status().is_success() {
// Create the output file
let mut file = File::create(output_file)?;

// Stream the response body to the file
while let Some(chunk) = response.chunk().await? {
copy(&mut chunk.as_ref(), &mut file)?;
}

println!("Audio saved to {:?}", output_file);
} else {
eprintln!("Failed to generate speech: {}", response.status());
let error_text = response.text().await?;
eprintln!("Error details: {}", error_text);
}

Ok(())
}

Expand Down

0 comments on commit 7114c84

Please sign in to comment.