Skip to content

Commit 7cd7855

Browse files
authored
Merge pull request #55 from pragmatrix/azure-transcribe-multi-language
Support multiple language detection in azure-transcribe.
2 parents 076a49f + 3b92af2 commit 7cd7855

10 files changed

Lines changed: 79 additions & 529 deletions

File tree

core/src/conversation.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -227,8 +227,12 @@ impl ConversationOutput {
227227
self.post(Output::ClearAudio)
228228
}
229229

230-
pub fn text(&self, is_final: bool, text: String) -> Result<()> {
231-
self.post(Output::Text { is_final, text })
230+
pub fn text(&self, is_final: bool, text: String, language: Option<String>) -> Result<()> {
231+
self.post(Output::Text {
232+
is_final,
233+
text,
234+
language,
235+
})
232236
}
233237

234238
pub fn request_completed(&self, request_id: Option<RequestId>) -> Result<()> {
@@ -318,6 +322,7 @@ pub enum Output {
318322
Text {
319323
is_final: bool,
320324
text: String,
325+
language: Option<String>,
321326
},
322327
RequestCompleted {
323328
request_id: Option<RequestId>,

examples/azure-transcribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use context_switch_core::{
1515
service::Service,
1616
};
1717

18-
const LANGUAGE: &str = "de-DE";
18+
const LANGUAGE: &str = "de-DE,en-US";
1919

2020
#[tokio::main]
2121
async fn main() -> Result<()> {

examples/azure-translate.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,12 @@ async fn setup_audio_playback(
178178
break;
179179
}
180180
}
181-
Output::Text { is_final, text } => {
182-
println!("Text: {text}, final: {is_final}")
181+
Output::Text {
182+
is_final,
183+
text,
184+
language,
185+
} => {
186+
println!("Text: {text}, final: {is_final}, language: {language:?}")
183187
}
184188
Output::RequestCompleted { .. } => {}
185189
Output::ClearAudio => {

services/aristech/src/transcribe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Service for AristechTranscribe {
152152

153153
// Instead of processing all alternatives, just take the first one
154154
if let Some(alternative) = chunk.alternatives.into_iter().next() {
155-
output.text(is_final, alternative.text)?;
155+
output.text(is_final, alternative.text, None)?;
156156
}
157157
}
158158
}

services/azure/src/transcribe.rs

Lines changed: 53 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::{error, info};
99
use crate::Host;
1010
use context_switch_core::{
1111
BillingRecord, Service,
12-
conversation::{BillingSchedule, Conversation, Input},
12+
conversation::{BillingSchedule, Conversation, ConversationOutput, Input},
1313
speech_gate::make_speech_gate_processor_soft_rms,
1414
};
1515

@@ -46,13 +46,35 @@ impl Service for AzureTranscribe {
4646
}
4747
};
4848

49+
let languages = params
50+
.language
51+
.split(',')
52+
.map(str::trim)
53+
.filter(|x| !x.is_empty())
54+
.map(str::to_owned)
55+
.collect::<Vec<_>>();
56+
57+
if languages.is_empty() {
58+
bail!("language must contain at least one locale code");
59+
}
60+
let include_detected_language = languages.len() > 1;
61+
4962
let config = recognizer::Config::default()
5063
// Disable profanity filter.
51-
.set_profanity(recognizer::Profanity::Raw)
52-
// short-circuit language filter.
53-
// TODO: may actually use the filter to check for supported languages?
54-
.set_language(recognizer::Language::Custom(params.language))
55-
.set_output_format(recognizer::OutputFormat::Detailed);
64+
.set_profanity(recognizer::Profanity::Raw);
65+
66+
let config = if languages.len() == 1 {
67+
config.set_language(recognizer::Language::Custom(languages[0].clone()))
68+
} else {
69+
config.set_detect_languages(
70+
languages
71+
.into_iter()
72+
.map(recognizer::Language::Custom)
73+
.collect(),
74+
recognizer::LanguageDetectMode::Continuous,
75+
)
76+
}
77+
.set_output_format(recognizer::OutputFormat::Detailed);
5678

5779
let client = recognizer::Client::connect(host.auth.clone(), config).await?;
5880

@@ -108,13 +130,36 @@ impl Service for AzureTranscribe {
108130
| Event::StartDetected(_, _)
109131
| Event::EndDetected(_, _) => {}
110132
Event::Recognizing(_, recognized, _, _, _) => {
111-
output.text(false, recognized.text)?
133+
output_recognized_text(&output, recognized, false, include_detected_language)?
134+
}
135+
Event::Recognized(_, recognized, _, _, _) => {
136+
output_recognized_text(&output, recognized, true, include_detected_language)?
112137
}
113-
Event::Recognized(_, recognized, _, _, _) => output.text(true, recognized.text)?,
114138
Event::UnMatch(_, _, _, _) => {}
115139
}
116140
}
117141

118142
Ok(())
119143
}
120144
}
145+
146+
fn output_recognized_text(
147+
output: &ConversationOutput,
148+
recognized: recognizer::Recognized,
149+
is_final: bool,
150+
include_detected_language: bool,
151+
) -> Result<()> {
152+
let recognizer::Recognized {
153+
text,
154+
primary_language,
155+
..
156+
} = recognized;
157+
158+
let language = if include_detected_language {
159+
primary_language.map(|x| x.language.to_string())
160+
} else {
161+
None
162+
};
163+
164+
output.text(is_final, text, language)
165+
}

services/azure/src/translate.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,12 +128,12 @@ impl Service for AzureTranslate {
128128
Event::EndDetected(_, _) => {}
129129
Event::Translating(_, text, _, _, _) => {
130130
if output_modalities.text {
131-
output.text(false, text)?;
131+
output.text(false, text, None)?;
132132
}
133133
}
134134
Event::Translated(_, text, _, _, _) => {
135135
if output_modalities.interim_text {
136-
output.text(true, text)?;
136+
output.text(true, text, None)?;
137137
}
138138
}
139139
Event::TranslationSynthesis(_, samples) => {

0 commit comments

Comments
 (0)