Skip to content

Commit

Permalink
fix: implement Mochan's suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
yavens committed Feb 25, 2025
1 parent bfb13d3 commit ecf06aa
Showing 1 changed file with 19 additions and 11 deletions.
30 changes: 19 additions & 11 deletions rig-core/src/transcription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ pub struct TranscriptionRequest {
pub struct TranscriptionRequestBuilder<M: TranscriptionModel> {
model: M,
data: Vec<u8>,
filename: String,
filename: Option<String>,
language: String,
prompt: Option<String>,
temperature: Option<f64>,
Expand All @@ -151,33 +151,41 @@ impl<M: TranscriptionModel> TranscriptionRequestBuilder<M> {
TranscriptionRequestBuilder {
model,
data: vec![],
filename: "".to_string(),
filename: None,
language: "en".to_string(),
prompt: None,
temperature: None,
additional_params: None,
}
}

pub fn filename(mut self, filename: Option<String>) -> Self {
self.filename = filename;
self
}

/// Sets the data for the request
pub fn data(mut self, filename: &str, data: Vec<u8>) -> Self {
self.filename = filename.to_string();
pub fn data(mut self, data: Vec<u8>) -> Self {
self.data = data;
self
}

/// Load the specified file into data
pub fn load_file(self, path: &str) -> Self {
let path = Path::new(path);
pub fn load_file<P>(self, path: P) -> Self
where
P: AsRef<Path>,
{
let path = path.as_ref();
let data = fs::read(path).expect("Failed to load audio file, file did not exist");

self.data(
self.filename(Some(
path.file_name()
.expect("Path was not a file")
.to_str()
.expect("Failed to convert filename to ascii"),
data,
)
.expect("Failed to convert filename to ascii")
.to_string(),
))
.data(data)
}

/// Sets the output language for the transcription request
Expand Down Expand Up @@ -226,7 +234,7 @@ impl<M: TranscriptionModel> TranscriptionRequestBuilder<M> {

TranscriptionRequest {
data: self.data,
filename: self.filename,
filename: self.filename.unwrap_or("file".to_string()),
language: self.language,
prompt: self.prompt,
temperature: self.temperature,
Expand Down

0 comments on commit ecf06aa

Please sign in to comment.