-
Notifications
You must be signed in to change notification settings - Fork 176
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Audio APIs from updated spec (#202)
* Implement CreateTranscriptRequest::response_granularities This PR adds support for `AudioResponseFormat::VerboseJson` and `TimestampGranularity`, including updated example code. These were defined as types before, but not fully implemented. Implements #201. * Modify transcription API to be more like spec - Rename `CreateTranscriptionRespose` to `CreateTranscriptionResponseJson` (to match API spec) - Add `CreateTranscriptionResponseVerboseJson` and `transcribe_verbose_json` - Add `transcribe_raw` for SRT output - Add `post_form_raw` - Update example code
- Loading branch information
Showing
5 changed files
with
186 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,68 @@ | ||
use async_openai::{types::CreateTranscriptionRequestArgs, Client}; | ||
use async_openai::{ | ||
types::{AudioResponseFormat, CreateTranscriptionRequestArgs, TimestampGranularity}, | ||
Client | ||
}; | ||
use std::error::Error; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn Error>> { | ||
transcribe_json().await?; | ||
transcribe_verbose_json().await?; | ||
transcribe_srt().await?; | ||
Ok(()) | ||
} | ||
|
||
async fn transcribe_json() -> Result<(), Box<dyn Error>> { | ||
let client = Client::new(); | ||
// Credits and Source for audio: https://www.youtube.com/watch?v=oQnDVqGIv4s | ||
let request = CreateTranscriptionRequestArgs::default() | ||
.file( | ||
"./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", | ||
) | ||
.model("whisper-1") | ||
.response_format(AudioResponseFormat::Json) | ||
.build()?; | ||
|
||
let response = client.audio().transcribe(request).await?; | ||
println!("{}", response.text); | ||
Ok(()) | ||
} | ||
|
||
async fn transcribe_verbose_json() -> Result<(), Box<dyn Error>> { | ||
let client = Client::new(); | ||
let request = CreateTranscriptionRequestArgs::default() | ||
.file( | ||
"./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", | ||
) | ||
.model("whisper-1") | ||
.response_format(AudioResponseFormat::VerboseJson) | ||
.timestamp_granularities(vec![TimestampGranularity::Word, TimestampGranularity::Segment]) | ||
.build()?; | ||
|
||
let response = client.audio().transcribe_verbose_json(request).await?; | ||
|
||
println!("{}", response.text); | ||
if let Some(words) = &response.words { | ||
println!("- {} words", words.len()); | ||
} | ||
if let Some(segments) = &response.segments { | ||
println!("- {} segments", segments.len()); | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn transcribe_srt() -> Result<(), Box<dyn Error>> { | ||
let client = Client::new(); | ||
let request = CreateTranscriptionRequestArgs::default() | ||
.file( | ||
"./audio/A Message From Sir David Attenborough A Perfect Planet BBC Earth_320kbps.mp3", | ||
) | ||
.model("whisper-1") | ||
.response_format(AudioResponseFormat::Srt) | ||
.build()?; | ||
|
||
let response = client.audio().transcribe_raw(request).await?; | ||
println!("{}", String::from_utf8_lossy(response.as_ref())); | ||
Ok(()) | ||
} |