Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
//! Given an audio file, the model will return its transcription.

use std::path::Path;

use super::{openai_post_multipart, ApiResponseOrError};
use derive_builder::Builder;
use serde::{Deserialize, Serialize};
use reqwest::multipart::{Form, Part};

#[derive(Deserialize, Clone)]
pub struct Transcription {
pub text: String,
}

#[derive(Serialize, Builder, Debug, Clone)]
#[builder(pattern = "owned")]
#[builder(name = "TranscriptionBuilder")]
#[builder(setter(strip_option, into))]
pub struct TranscriptionRequest {
/// ID of the model to use.
/// You can use the [List models](https://beta.openai.com/docs/api-reference/models/list)
/// API to see all of your available models,
/// or see our [Model overview](https://beta.openai.com/docs/models/overview)
/// for descriptions of them.
/// At time of writing, only "whisper-1" is allowed.
pub model: String,
pub file_name: String,
}

impl Transcription {
/// Creates a completion for the provided prompt and parameters
async fn create(request: &TranscriptionRequest) -> ApiResponseOrError<Self> {
let model = request.model.clone();
let upload_file_path = Path::new(request.file_name.as_str());
let upload_file_path = upload_file_path.canonicalize()?;
let simple_name = upload_file_path
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_string()
.clone();
let async_file = tokio::fs::File::open(upload_file_path).await?;
let file_part = Part::stream(async_file)
.file_name(simple_name)
.mime_str("audio/wav")?;
let form = Form::new()
.part("file", file_part)
.text("model", model);
openai_post_multipart("audio/transcriptions", form).await
}

pub fn builder(model: &str) -> TranscriptionBuilder {
TranscriptionBuilder::create_empty().model(model)
}
}

impl TranscriptionBuilder {
pub async fn create(self) -> ApiResponseOrError<Transcription> {
Transcription::create(&self.build().unwrap()).await
}
}
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reqwest::{header::AUTHORIZATION, Client, Method, RequestBuilder, Response};
use reqwest_eventsource::{CannotCloneRequestError, EventSource, RequestBuilderExt};
use serde::{de::DeserializeOwned, Deserialize, Serialize};

pub mod audio;
pub mod chat;
pub mod completions;
pub mod edits;
Expand Down