Skip to content

Commit 6413834

Browse files
committed
experimental batch command in cli
Signed-off-by: Yujong Lee <yujonglee.dev@gmail.com>
1 parent 9c71cfc commit 6413834

File tree

5 files changed

+103
-1
lines changed

5 files changed

+103
-1
lines changed

apps/cli/src/commands/batch.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
use std::sync::Arc;
2+
3+
use hypr_listener2_core::{BatchEvent, BatchParams, BatchProvider, BatchRuntime};
4+
5+
pub struct Args {
6+
pub file: String,
7+
pub provider: BatchProvider,
8+
pub base_url: String,
9+
pub api_key: String,
10+
pub model: Option<String>,
11+
pub language: String,
12+
pub keywords: Vec<String>,
13+
}
14+
15+
struct CliBatchRuntime;
16+
17+
impl BatchRuntime for CliBatchRuntime {
18+
fn emit(&self, event: BatchEvent) {
19+
match &event {
20+
BatchEvent::BatchStarted { .. } => {
21+
eprintln!("Transcribing...");
22+
}
23+
BatchEvent::BatchResponseStreamed { percentage, .. } => {
24+
eprintln!("Progress: {:.0}%", percentage * 100.0);
25+
}
26+
BatchEvent::BatchResponse { response, .. } => {
27+
for channel in &response.results.channels {
28+
for alt in &channel.alternatives {
29+
if !alt.transcript.is_empty() {
30+
println!("{}", alt.transcript);
31+
}
32+
}
33+
}
34+
}
35+
BatchEvent::BatchFailed { error, .. } => {
36+
eprintln!("Error: {error}");
37+
}
38+
}
39+
}
40+
}
41+
42+
pub async fn run(args: Args) {
43+
let languages = vec![
44+
args.language
45+
.parse::<hypr_language::Language>()
46+
.expect("invalid language code"),
47+
];
48+
49+
let session_id = uuid::Uuid::new_v4().to_string();
50+
let runtime = Arc::new(CliBatchRuntime);
51+
52+
let params = BatchParams {
53+
session_id,
54+
provider: args.provider,
55+
file_path: args.file,
56+
model: args.model,
57+
base_url: args.base_url,
58+
api_key: args.api_key,
59+
languages,
60+
keywords: args.keywords,
61+
};
62+
63+
if let Err(e) = hypr_listener2_core::run_batch(runtime, params).await {
64+
eprintln!("Batch transcription failed: {e}");
65+
std::process::exit(1);
66+
}
67+
}

apps/cli/src/commands/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
pub mod auth;
2+
pub mod batch;
23
pub mod tui;

apps/cli/src/main.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ struct Cli {
3131
#[derive(Subcommand)]
3232
enum Commands {
3333
Auth,
34+
Batch {
35+
#[arg(long)]
36+
file: String,
37+
#[arg(long)]
38+
provider: String,
39+
},
3440
}
3541

3642
#[tokio::main]
@@ -39,6 +45,32 @@ async fn main() {
3945

4046
match cli.command {
4147
Some(Commands::Auth) => commands::auth::run(),
48+
Some(Commands::Batch { file, provider }) => {
49+
let base_url = cli.base_url.unwrap_or_else(|| {
50+
eprintln!("error: --base-url (or CHAR_BASE_URL) is required");
51+
std::process::exit(1);
52+
});
53+
54+
let provider = provider.parse().unwrap_or_else(|_| {
55+
eprintln!("error: unknown provider '{provider}'. expected: deepgram, soniox, assemblyai, am, cactus");
56+
std::process::exit(1);
57+
});
58+
59+
commands::batch::run(commands::batch::Args {
60+
file,
61+
provider,
62+
base_url,
63+
api_key: cli.api_key,
64+
model: if cli.model.is_empty() {
65+
None
66+
} else {
67+
Some(cli.model)
68+
},
69+
language: cli.language,
70+
keywords: vec![],
71+
})
72+
.await;
73+
}
4274
None => {
4375
let base_url = cli.base_url.unwrap_or_else(|| {
4476
eprintln!("error: --base-url (or CHAR_BASE_URL) is required");

crates/listener2-core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ owhisper-interface = { workspace = true }
2020

2121
serde = { workspace = true }
2222
specta = { workspace = true, optional = true }
23+
strum = { workspace = true, features = ["derive"] }
2324
thiserror = { workspace = true }
2425

2526
ractor = { workspace = true, features = ["async-trait"] }

crates/listener2-core/src/batch.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,10 @@ const DEFAULT_CHUNK_MS: u64 = 500;
2121
const DEFAULT_DELAY_MS: u64 = 20;
2222
const DEVICE_FINGERPRINT_HEADER: &str = "x-device-fingerprint";
2323

24-
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
24+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, strum::Display, strum::EnumString)]
2525
#[cfg_attr(feature = "specta", derive(specta::Type))]
2626
#[serde(rename_all = "lowercase")]
27+
#[strum(serialize_all = "lowercase")]
2728
pub enum BatchProvider {
2829
Deepgram,
2930
Soniox,

0 commit comments

Comments
 (0)