Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming callbacks #85

Merged
merged 5 commits into from
Aug 22, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ name = "simple_stream"
path = "examples/transcription/websocket/simple_stream.rs"
required-features = ["listen"]

[[example]]
name = "callback_stream"
path = "examples/transcription/websocket/callback_stream.rs"
required-features = ["listen"]

[[example]]
name = "microphone_stream"
path = "examples/transcription/websocket/microphone_stream.rs"
Expand Down
53 changes: 53 additions & 0 deletions examples/transcription/websocket/callback_stream.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use std::env;
bd-g marked this conversation as resolved.
Show resolved Hide resolved
use std::time::Duration;

use futures::stream::StreamExt;

use deepgram::{
common::options::{Encoding, Endpointing, Language, Options},
Deepgram, DeepgramError,
};

static PATH_TO_FILE: &str = "examples/audio/bueller.wav";
static AUDIO_CHUNK_SIZE: usize = 3174;
static FRAME_DELAY: Duration = Duration::from_millis(16);

#[tokio::main]
async fn main() -> Result<(), DeepgramError> {
let deepgram_api_key =
env::var("DEEPGRAM_API_KEY").expect("DEEPGRAM_API_KEY environmental variable");

let dg_client = Deepgram::new(&deepgram_api_key)?;

let options = Options::builder()
.smart_format(true)
.language(Language::en_US)
.build();

let callback_url = env::var("DEEPGRAM_CALLBACK_URL")
.expect("DEEPGRAM_CALLBACK_URL environmental variable")
.parse()
.expect("DEEPGRAM_CALLBACK_URL not a valid URL");

let mut results = dg_client
.transcription()
.stream_request_with_options(options)
.keep_alive()
.encoding(Encoding::Linear16)
.sample_rate(44100)
.channels(2)
.endpointing(Endpointing::CustomDurationMs(300))
.interim_results(true)
.utterance_end_ms(1000)
.vad_events(true)
.no_delay(true)
.callback(callback_url)
bd-g marked this conversation as resolved.
Show resolved Hide resolved
.file(PATH_TO_FILE, AUDIO_CHUNK_SIZE, FRAME_DELAY)
.await?;

while let Some(result) = results.next().await {
println!("got: {:?}", result);
}

Ok(())
}
5 changes: 5 additions & 0 deletions src/common/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1887,7 +1887,12 @@ impl OptionsBuilder {
///
/// See the [Deepgram Callback Method feature docs][docs] for more info.
///
/// Note that modifying the callback method is only available for pre-recorded audio.
/// See the [Deepgram Callback feature docs for streaming][streaming-docs] for details
/// on streaming callbacks.
bd-g marked this conversation as resolved.
Show resolved Hide resolved
///
/// [docs]: https://developers.deepgram.com/docs/callback#pre-recorded-audio
/// [streaming-docs]: https://developers.deepgram.com/docs/callback#streaming-audio
///
/// # Examples
///
Expand Down
12 changes: 12 additions & 0 deletions src/listen/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ pub struct WebsocketBuilder<'a> {
vad_events: Option<bool>,
stream_url: Url,
keep_alive: Option<bool>,
callback: Option<Url>,
}

impl Transcription<'_> {
Expand Down Expand Up @@ -143,6 +144,7 @@ impl Transcription<'_> {
vad_events: None,
stream_url: self.listen_stream_url(),
keep_alive: None,
callback: None,
}
}

Expand Down Expand Up @@ -214,6 +216,7 @@ impl<'a> WebsocketBuilder<'a> {
no_delay,
vad_events,
stream_url,
callback,
} = self;

let mut url = stream_url.clone();
Expand Down Expand Up @@ -257,6 +260,9 @@ impl<'a> WebsocketBuilder<'a> {
if let Some(vad_events) = vad_events {
pairs.append_pair("vad_events", &vad_events.to_string());
}
if let Some(callback) = callback {
pairs.append_pair("callback", callback.as_ref());
}
}

Ok(url)
Expand Down Expand Up @@ -315,6 +321,12 @@ impl<'a> WebsocketBuilder<'a> {

self
}

pub fn callback(mut self, callback: Url) -> Self {
self.callback = Some(callback);

self
}
}

impl<'a> WebsocketBuilder<'a> {
Expand Down
Loading