refactoring
partially implement tts for human interface
This commit is contained in:
+45
-12
@@ -1,11 +1,11 @@
|
||||
use std::path::Path;
|
||||
use crate::audio::models::{AudioServerStatus, TranscriptionResponse, VoiceRequest};
|
||||
use reqwest::Client;
|
||||
use reqwest::multipart::Form;
|
||||
use std::path::Path;
|
||||
use thiserror::Error;
|
||||
use crate::audio::models::{AudioServerStatus, TranscriptionResponse, VoiceRequest};
|
||||
use url::Url;
|
||||
|
||||
pub struct AudioClient{
|
||||
pub struct AudioClient {
|
||||
client: Client,
|
||||
authorization: Option<String>,
|
||||
base_url: Url,
|
||||
@@ -19,9 +19,12 @@ pub trait AudioClientTrait {
|
||||
|
||||
fn status(&self) -> impl Future<Output = Result<AudioServerStatus, AudioError>>;
|
||||
|
||||
fn transcribe(&self, audio_file_path: impl AsRef<Path>) -> impl Future<Output=AudioResult<TranscriptionResponse>>;
|
||||
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>>;
|
||||
fn tts(&self, voice_request: VoiceRequest) -> impl Future<Output = AudioResult<bytes::Bytes>>;
|
||||
}
|
||||
|
||||
#[derive(Debug, Error)]
|
||||
@@ -36,13 +39,27 @@ pub enum AudioError {
|
||||
|
||||
type AudioResult<T> = Result<T, AudioError>;
|
||||
|
||||
impl AudioClient {
|
||||
fn get(&self, url: Url) -> reqwest::RequestBuilder {
|
||||
self.client.get(url).header("Authorization", self.authorization.clone().unwrap_or(String::new()))
|
||||
}
|
||||
}
|
||||
|
||||
impl AudioClientTrait for AudioClient {
|
||||
fn new(base_url: Url) -> Self {
|
||||
AudioClient { client: Client::new(), authorization: None, base_url }
|
||||
AudioClient {
|
||||
client: Client::new(),
|
||||
authorization: None,
|
||||
base_url,
|
||||
}
|
||||
}
|
||||
|
||||
fn from_client(base_url: Url, client: Client) -> Self {
|
||||
AudioClient { client, authorization: None, base_url }
|
||||
AudioClient {
|
||||
client,
|
||||
authorization: None,
|
||||
base_url,
|
||||
}
|
||||
}
|
||||
|
||||
fn with_authorization(mut self, authorization: String) -> Self {
|
||||
@@ -51,20 +68,36 @@ impl AudioClientTrait for AudioClient {
|
||||
}
|
||||
|
||||
async fn status(&self) -> AudioResult<AudioServerStatus> {
|
||||
let response = self.client.get(self.base_url.clone()).send().await?.error_for_status()?;
|
||||
let response = self
|
||||
.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> {
|
||||
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?;
|
||||
let response = self
|
||||
.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()?;
|
||||
let response = self
|
||||
.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