add logging config to audio server
add audio client change delimiter for mcp tools from "::" to ":" to save tokens
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
use std::io::ErrorKind;
|
||||
use std::path::Path;
|
||||
use reqwest::Client;
|
||||
use reqwest::multipart::Form;
|
||||
use thiserror::Error;
|
||||
use crate::audio::models::{AudioServerStatus, TranscriptionResponse, VoiceRequest};
|
||||
use url::Url;
|
||||
|
||||
pub struct AudioClient{
|
||||
client: Client,
|
||||
authorization: Option<String>,
|
||||
base_url: Url,
|
||||
}
|
||||
|
||||
pub trait AudioClientTrait {
|
||||
fn new(base_url: Url) -> Self;
|
||||
fn from_client(base_url: Url, client: Client) -> Self;
|
||||
|
||||
fn with_authorization(self, authorization: String) -> Self;
|
||||
|
||||
fn status(&self) -> impl Future<Output = Result<AudioServerStatus, AudioError>>;
|
||||
|
||||
fn transcribe(&self, audio_file_path: impl AsRef<Path>) -> impl Future<Output=AudioResult<TranscriptionResponse>>;
|
||||
|
||||
fn tts(&self, voice_request: VoiceRequest) -> impl Future<Output=AudioResult<bytes::Bytes>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
pub enum AudioError {
|
||||
#[error(transparent)]
|
||||
RequestError(#[from] reqwest::Error),
|
||||
#[error(transparent)]
|
||||
UrlParseError(#[from] url::ParseError),
|
||||
#[error(transparent)]
|
||||
IOError(#[from] std::io::Error),
|
||||
}
|
||||
|
||||
type AudioResult<T> = Result<T, AudioError>;
|
||||
|
||||
impl AudioClientTrait for AudioClient {
|
||||
fn new(base_url: Url) -> Self {
|
||||
AudioClient { client: Client::new(), authorization: None, base_url }
|
||||
}
|
||||
|
||||
fn from_client(base_url: Url, client: Client) -> Self {
|
||||
AudioClient { client, authorization: None, base_url }
|
||||
}
|
||||
|
||||
fn with_authorization(mut self, authorization: String) -> Self {
|
||||
self.authorization = Some(authorization);
|
||||
self
|
||||
}
|
||||
|
||||
async fn status(&self) -> AudioResult<AudioServerStatus> {
|
||||
let response = self.client.get(self.base_url.clone()).send().await?.error_for_status()?;
|
||||
Ok(response.json().await?)
|
||||
}
|
||||
|
||||
async fn transcribe(&self, audio_file_path: impl AsRef<Path>) -> AudioResult<TranscriptionResponse> {
|
||||
let form = Form::new().file("audio_file", audio_file_path).await?;
|
||||
|
||||
let response = self.client.get(self.base_url.join("transcribe")?).multipart(form).send().await?;
|
||||
Ok(response.json().await?)
|
||||
}
|
||||
|
||||
async fn tts(&self, voice_request: VoiceRequest) -> AudioResult<bytes::Bytes> {
|
||||
let response = self.client.get(self.base_url.join("tts")?).json(&voice_request).send().await?.error_for_status()?;
|
||||
|
||||
Ok(response.bytes().await?)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user