Skip to content

Commit ca5deb2

Browse files
authored
Language format conversion improvements (#709)
1 parent 36e5cd5 commit ca5deb2

File tree

7 files changed

+161
-27
lines changed

7 files changed

+161
-27
lines changed

crates/language/src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use serde::{ser::Serializer, Serialize};
33
#[derive(Debug, thiserror::Error)]
44
pub enum Error {
55
#[error("Not supported language: {0}")]
6-
NotSupportedLanguage(codes_iso_639::part_1::LanguageCode),
6+
NotSupportedLanguage(String),
77
}
88

99
impl Serialize for Error {

crates/language/src/lib.rs

Lines changed: 125 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,11 @@ impl std::ops::Deref for Language {
2828
}
2929
}
3030

31-
impl Language {
32-
pub fn iso639(&self) -> ISO639 {
33-
self.iso639
34-
}
31+
#[cfg(feature = "whisper")]
32+
impl TryInto<hypr_whisper::Language> for Language {
33+
type Error = Error;
3534

36-
#[cfg(feature = "whisper")]
37-
pub fn for_whisper(self) -> Result<hypr_whisper::Language, Error> {
35+
fn try_into(self) -> Result<hypr_whisper::Language, Self::Error> {
3836
use hypr_whisper::Language as WL;
3937

4038
match self.iso639 {
@@ -135,9 +133,127 @@ impl Language {
135133
ISO639::Yi => Ok(WL::Yi),
136134
ISO639::Yo => Ok(WL::Yo),
137135
ISO639::Zh => Ok(WL::Zh),
138-
_ => Err(Error::NotSupportedLanguage(self.iso639)),
136+
_ => Err(Error::NotSupportedLanguage(self.to_string())),
139137
}
140138
}
139+
}
140+
141+
#[cfg(feature = "whisper")]
142+
impl TryInto<Language> for hypr_whisper::Language {
143+
type Error = Error;
144+
145+
fn try_into(self) -> Result<Language, Self::Error> {
146+
use hypr_whisper::Language as WL;
147+
148+
let iso639 = match self {
149+
WL::Af => ISO639::Af,
150+
WL::Am => ISO639::Am,
151+
WL::Ar => ISO639::Ar,
152+
WL::As => ISO639::As,
153+
WL::Az => ISO639::Az,
154+
WL::Ba => ISO639::Ba,
155+
WL::Be => ISO639::Be,
156+
WL::Bg => ISO639::Bg,
157+
WL::Bn => ISO639::Bn,
158+
WL::Bo => ISO639::Bo,
159+
WL::Br => ISO639::Br,
160+
WL::Bs => ISO639::Bs,
161+
WL::Ca => ISO639::Ca,
162+
WL::Cs => ISO639::Cs,
163+
WL::Cy => ISO639::Cy,
164+
WL::Da => ISO639::Da,
165+
WL::De => ISO639::De,
166+
WL::El => ISO639::El,
167+
WL::En => ISO639::En,
168+
WL::Es => ISO639::Es,
169+
WL::Et => ISO639::Et,
170+
WL::Eu => ISO639::Eu,
171+
WL::Fa => ISO639::Fa,
172+
WL::Fi => ISO639::Fi,
173+
WL::Fo => ISO639::Fo,
174+
WL::Fr => ISO639::Fr,
175+
WL::Gl => ISO639::Gl,
176+
WL::Gu => ISO639::Gu,
177+
WL::Ha => ISO639::Ha,
178+
WL::He => ISO639::He,
179+
WL::Hi => ISO639::Hi,
180+
WL::Hr => ISO639::Hr,
181+
WL::Ht => ISO639::Ht,
182+
WL::Hu => ISO639::Hu,
183+
WL::Hy => ISO639::Hy,
184+
WL::Id => ISO639::Id,
185+
WL::Is => ISO639::Is,
186+
WL::It => ISO639::It,
187+
WL::Ja => ISO639::Ja,
188+
WL::Jw => ISO639::Jv,
189+
WL::Ka => ISO639::Ka,
190+
WL::Kk => ISO639::Kk,
191+
WL::Km => ISO639::Km,
192+
WL::Kn => ISO639::Kn,
193+
WL::Ko => ISO639::Ko,
194+
WL::La => ISO639::La,
195+
WL::Lb => ISO639::Lb,
196+
WL::Lo => ISO639::Lo,
197+
WL::Lt => ISO639::Lt,
198+
WL::Lv => ISO639::Lv,
199+
WL::Mg => ISO639::Mg,
200+
WL::Mi => ISO639::Mi,
201+
WL::Mk => ISO639::Mk,
202+
WL::Ml => ISO639::Ml,
203+
WL::Mn => ISO639::Mn,
204+
WL::Mr => ISO639::Mr,
205+
WL::Ms => ISO639::Ms,
206+
WL::Mt => ISO639::Mt,
207+
WL::My => ISO639::My,
208+
WL::Ne => ISO639::Ne,
209+
WL::Nl => ISO639::Nl,
210+
WL::Nn => ISO639::Nn,
211+
WL::No => ISO639::No,
212+
WL::Oc => ISO639::Oc,
213+
WL::Pa => ISO639::Pa,
214+
WL::Pl => ISO639::Pl,
215+
WL::Ps => ISO639::Ps,
216+
WL::Pt => ISO639::Pt,
217+
WL::Ro => ISO639::Ro,
218+
WL::Ru => ISO639::Ru,
219+
WL::Sa => ISO639::Sa,
220+
WL::Sd => ISO639::Sd,
221+
WL::Si => ISO639::Si,
222+
WL::Sk => ISO639::Sk,
223+
WL::Sl => ISO639::Sl,
224+
WL::Sn => ISO639::Sn,
225+
WL::So => ISO639::So,
226+
WL::Sq => ISO639::Sq,
227+
WL::Sr => ISO639::Sr,
228+
WL::Su => ISO639::Su,
229+
WL::Sv => ISO639::Sv,
230+
WL::Sw => ISO639::Sw,
231+
WL::Ta => ISO639::Ta,
232+
WL::Te => ISO639::Te,
233+
WL::Tg => ISO639::Tg,
234+
WL::Th => ISO639::Th,
235+
WL::Tk => ISO639::Tk,
236+
WL::Tl => ISO639::Tl,
237+
WL::Tr => ISO639::Tr,
238+
WL::Tt => ISO639::Tt,
239+
WL::Uk => ISO639::Uk,
240+
WL::Ur => ISO639::Ur,
241+
WL::Uz => ISO639::Uz,
242+
WL::Vi => ISO639::Vi,
243+
WL::Yi => ISO639::Yi,
244+
WL::Yo => ISO639::Yo,
245+
WL::Zh => ISO639::Zh,
246+
_ => return Err(Error::NotSupportedLanguage(self.to_string())),
247+
};
248+
249+
Ok(Language { iso639 })
250+
}
251+
}
252+
253+
impl Language {
254+
pub fn iso639(&self) -> ISO639 {
255+
self.iso639
256+
}
141257

142258
#[cfg(feature = "deepgram")]
143259
pub fn for_deepgram(self) -> Result<deepgram::common::options::Language, Error> {
@@ -178,7 +294,7 @@ impl Language {
178294
ISO639::Uk => Ok(DG::uk),
179295
ISO639::Vi => Ok(DG::vi),
180296
ISO639::Zh => Ok(DG::zh),
181-
_ => Err(Error::NotSupportedLanguage(self.iso639)),
297+
_ => Err(Error::NotSupportedLanguage(self.to_string())),
182298
}
183299
}
184300

@@ -218,7 +334,7 @@ impl Language {
218334
ISO639::Uk => Ok(String::from("стенограма")),
219335
ISO639::Vi => Ok(String::from("bản ghi")),
220336
ISO639::Zh => Ok(String::from("文字记录")),
221-
_ => Err(Error::NotSupportedLanguage(self.iso639)),
337+
_ => Err(Error::NotSupportedLanguage(self.to_string())),
222338
}
223339
}
224340
}

crates/stt/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ local = []
1313
hypr-audio-utils = { workspace = true }
1414
hypr-clova = { path = "../clova", package = "clova" }
1515
hypr-db-user = { workspace = true }
16-
hypr-language = { workspace = true, features = ["deepgram"] }
16+
hypr-language = { workspace = true, features = ["deepgram", "whisper"] }
1717
hypr-listener-interface = { workspace = true }
1818
hypr-rtzr = { path = "../rtzr", package = "rtzr" }
1919
hypr-whisper = { workspace = true, features = ["cloud"] }

crates/stt/src/realtime/mod.rs

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ impl ClientBuilder {
6464

6565
#[derive(Debug)]
6666
pub enum MultiClient {
67-
Deepgram(DeepgramClient),
6867
Clova(hypr_clova::realtime::Client),
68+
Deepgram(DeepgramClient),
69+
Whisper(hypr_whisper::cloud::WhisperClient),
6970
}
7071

7172
#[derive(Debug, Clone)]
@@ -81,20 +82,29 @@ impl Client {
8182

8283
pub async fn for_language(&self, language: hypr_language::Language) -> MultiClient {
8384
match language.iso639() {
85+
// hypr_language::ISO639::Ko => {
86+
// let clova = hypr_clova::realtime::Client::builder()
87+
// .api_key(self.clova_api_key.as_ref().unwrap())
88+
// .keywords(vec!["하이퍼노트".to_string()])
89+
// .build()
90+
// .await
91+
// .unwrap();
92+
// MultiClient::Clova(clova)
93+
// }
8494
hypr_language::ISO639::Ko => {
85-
let clova = hypr_clova::realtime::Client::builder()
86-
.api_key(self.clova_api_key.as_ref().unwrap())
87-
.keywords(vec!["하이퍼노트".to_string()])
88-
.build()
89-
.await
90-
.unwrap();
91-
MultiClient::Clova(clova)
95+
let whisper = hypr_whisper::cloud::WhisperClient::builder()
96+
.api_base(std::env::var("WHISPER_API_BASE").unwrap())
97+
.api_key(std::env::var("WHISPER_API_KEY").unwrap())
98+
.language(language.try_into().unwrap())
99+
.build();
100+
101+
MultiClient::Whisper(whisper)
92102
}
93103
hypr_language::ISO639::En => {
94104
let deepgram = DeepgramClient::builder()
95105
.api_key(self.deepgram_api_key.as_ref().unwrap())
96106
.keywords(vec!["Hyprnote".to_string()])
97-
.language(language.into())
107+
.language(language)
98108
.build()
99109
.unwrap();
100110

@@ -118,8 +128,9 @@ where
118128
crate::Error,
119129
> {
120130
match self {
121-
MultiClient::Deepgram(client) => Ok(Box::new(client.transcribe(stream).await?)),
122131
MultiClient::Clova(client) => Ok(Box::new(client.transcribe(stream).await?)),
132+
MultiClient::Deepgram(client) => Ok(Box::new(client.transcribe(stream).await?)),
133+
MultiClient::Whisper(client) => Ok(Box::new(client.transcribe(stream).await?)),
123134
}
124135
}
125136
}

crates/ws/src/client.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,21 @@ impl WebSocketClient {
6868

6969
let output_stream = async_stream::stream! {
7070
while let Some(msg_result) = ws_receiver.next().await {
71-
if let Ok(msg) = msg_result {
72-
match msg {
73-
Message::Text(_) | Message::Binary(_) => {
71+
match msg_result {
72+
Ok(msg) => {
73+
match msg {
74+
Message::Text(_) | Message::Binary(_) => {
7475
if let Some(output) = T::from_message(msg) {
7576
yield output;
7677
}
7778
},
7879
Message::Ping(_) | Message::Pong(_) | Message::Frame(_) => continue,
79-
Message::Close(_) => break,
80+
Message::Close(_) => break,
81+
}
82+
}
83+
Err(e) => {
84+
tracing::error!("ws_receiver_failed: {:?}", e);
85+
break;
8086
}
8187
}
8288
}

plugins/listener/src/fsm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ impl Session {
221221
wav.write_sample(sample).unwrap();
222222
wav.write_sample(sample).unwrap();
223223
if let Err(e) = mixed_tx.send(sample).await {
224-
tracing::error!("mixed_tx_send_error: {:?}", e);
224+
tracing::error!("mixed_tx_send_error: {:?}", e.0);
225225
}
226226
}
227227
}

plugins/local-stt/src/server.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ async fn listen(
119119
.ok_or(StatusCode::TOO_MANY_REQUESTS)?;
120120

121121
let model_path = state.model_type.model_path(&state.model_cache_dir);
122+
let language = params.language.try_into().unwrap();
122123

123124
let model = hypr_whisper::local::Whisper::builder()
124125
.model_path(model_path.to_str().unwrap())
125-
.language(params.language.for_whisper().unwrap())
126+
.language(language)
126127
.static_prompt(&params.static_prompt)
127128
.dynamic_prompt(&params.dynamic_prompt)
128129
.build();

0 commit comments

Comments
 (0)