Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rust code for https://client.camb.ai/apis/tts documentation #88

Open
Adesoji1 opened this issue Nov 29, 2024 · 4 comments
Open

Add Rust code for https://client.camb.ai/apis/tts documentation #88

Adesoji1 opened this issue Nov 29, 2024 · 4 comments

Comments

@Adesoji1
Copy link

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::Client;
use serde_json::json;
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Define the API endpoint
    let url = "https://client.camb.ai/apis/tts";

    // Define the payload
    let payload = json!({
        "voice_id": 20299,
        "text": "Hello world",
        "language": 40,
        "gender": 1, // Male
        "age": 43,
    });

    // Create the headers
    let mut headers = HeaderMap::new();
    headers.insert(
        HeaderName::from_static("x-api-key"),
        HeaderValue::from_static("xxxxxxx"), // Replace with your API key
    );
    headers.insert(
        HeaderName::from_static("content-type"),
        HeaderValue::from_static("application/json"),
    );

    // Create an HTTP client
    let client = Client::new();

    // Make the POST request
    let response = client
        .post(url)
        .headers(headers) // Attach the headers
        .json(&payload) // Attach the JSON payload
        .send()
        .await?;

    // Handle the response
    if response.status().is_success() {
        let response_text = response.text().await?;
        println!("Response: {}", response_text);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error: {}", error_text);
    }

    Ok(())
}

Screenshot from 2024-11-29 17-03-32
@pieterscholtz @victorchall

@Adesoji1
Copy link
Author

Adesoji1 commented Nov 29, 2024

For list Voices

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Define the API endpoint
    let url = "https://client.camb.ai/apis/list_voices";

    // Create the headers
    let mut headers = HeaderMap::new();
    headers.insert(
        HeaderName::from_static("x-api-key"),
        HeaderValue::from_static("xxxxxxxxxxxxxxxxxxxxxxx"), // Replace with your API key
    );

    // Create an HTTP client
    let client = reqwest::Client::new();

    // Make the GET request
    let response = client.get(url).headers(headers).send().await?;

    // Handle the response
    if response.status().is_success() {
        let response_text = response.text().await?;
        println!("Response: {}", response_text);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error: {}", error_text);
    }

    Ok(())
}

image

@Adesoji1
Copy link
Author

Create Transcription


use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use reqwest::multipart::{Form, Part};
use std::error::Error;
use std::fs;
use std::path::Path;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Define the API endpoint
    let url = "https://client.camb.ai/apis/create_transcription";

    // Create the headers
    let mut headers = HeaderMap::new();
    headers.insert(
        HeaderName::from_static("x-api-key"),
        HeaderValue::from_static("xxxxxxxxxxxxxxxx"), // Replace with your API key
    );

    // Path to the file
    let file_path = "/home/adesoji/Music/1.mp3"; // Replace with your file path

    // Validate file extension and determine MIME type
    let _allowed_extensions = ["wav", "mp3"];
    let file_extension = Path::new(file_path)
        .extension()
        .and_then(|ext| ext.to_str())
        .unwrap_or("")
        .to_lowercase();

    let mime_type = match file_extension.as_str() {
        "wav" => "audio/wav",
        "mp3" => "audio/mpeg",
        _ => {
            eprintln!("Error: Unsupported file type. Only .wav and .mp3 are allowed.");
            return Ok(());
        }
    };

    // Read the file and create a multipart `Part`
    let file_content = fs::read(file_path)?;
    let file_part = Part::bytes(file_content)
        .file_name(format!("file.{}", file_extension)) // Set the filename dynamically
        .mime_str(mime_type)?; // Set the MIME type dynamically

    // Create the multipart form
    let form = Form::new()
        .text("language", "5") // Replace "5" with the desired language code
        .part("file", file_part); // Attach the file

    // Create an HTTP client
    let client = reqwest::Client::new();

    // Make the POST request
    let response = client
        .post(url)
        .headers(headers)
        .multipart(form)
        .send()
        .await?;

    // Handle the response
    if response.status().is_success() {
        let response_text = response.text().await?;
        println!("Response: {}", response_text);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error: {}", error_text);
    }

    Ok(())
}


image

@Adesoji1
Copy link
Author

Adesoji1 commented Nov 29, 2024

Get All Target Languages

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Define the API endpoint
    let url = "https://client.camb.ai/apis/target_languages";

    // Create the headers
    let mut headers = HeaderMap::new();
    headers.insert(
        HeaderName::from_static("x-api-key"),
        HeaderValue::from_static("xxxxxxxxx"), // Replace with your API key
    );

    // Create an HTTP client
    let client = reqwest::Client::new();

    // Make the GET request
    let response = client.get(url).headers(headers).send().await?;

    // Handle the response
    if response.status().is_success() {
        let response_text = response.text().await?;
        println!("Response: {}", response_text);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error: {}", error_text);
    }

    Ok(())
}

image

@Adesoji1
Copy link
Author

Screenshot from 2024-11-29 17-31-34

Poll for TTS Status

use reqwest::header::{HeaderMap, HeaderName, HeaderValue};
use std::error::Error;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    // Define the API endpoint with a placeholder for the ID
    let base_url = "https://client.camb.ai/apis/tts/";
    let id = "40"; // Replace with the actual ID
    let url = format!("{}{}", base_url, id);

    // Create the headers
    let mut headers = HeaderMap::new();
    headers.insert(
        HeaderName::from_static("x-api-key"),
        HeaderValue::from_static("xxxxxx"), // Replace with your API key
    );

    // Create an HTTP client
    let client = reqwest::Client::new();

    // Make the GET request
    let response = client.get(&url).headers(headers).send().await?;

    // Handle the response
    if response.status().is_success() {
        let response_text = response.text().await?;
        println!("Response: {}", response_text);
    } else {
        let error_text = response.text().await?;
        eprintln!("Error: {}", error_text);
    }

    Ok(())
}
```
Cargo.toml 
[package]
name = "cambai_tts"
version = "0.1.0"
edition = "2021"
author = "Adesoji Alu"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
reqwest = { version = "0.11", features = ["json", "multipart"] }
tokio = { version = "1", features = ["full"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"



Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant