mingyang91 commited on
Commit
98c4dba
·
verified ·
1 Parent(s): b371670

engine option

Browse files
Files changed (2) hide show
  1. src/lesson.rs +27 -24
  2. src/main.rs +1 -0
src/lesson.rs CHANGED
@@ -10,7 +10,7 @@ use std::io::BufRead;
10
  use std::ops::Deref;
11
  use std::sync::{Arc, Weak};
12
  use tokio::sync::RwLock;
13
- use tracing::warn;
14
 
15
  use tokio::select;
16
  use crate::asr::{Event, aws::AWS_ASR, ASR};
@@ -43,6 +43,11 @@ impl Deref for LessonsManager {
43
  }
44
  }
45
 
 
 
 
 
 
46
  impl LessonsManager {
47
  pub(crate) fn new(sdk_config: &SdkConfig) -> Self {
48
  let translate_client = aws_sdk_translate::Client::new(sdk_config);
@@ -55,9 +60,9 @@ impl LessonsManager {
55
  LessonsManager { inner: Arc::new(inner) }
56
  }
57
 
58
- pub(crate) async fn create_lesson(&self, id: u32, speaker_lang: LanguageCode) -> Lesson {
59
  let mut map = self.lessons.write().await;
60
- let lesson: Lesson = InnerLesson::new(self.clone(), speaker_lang).await.into();
61
  map.insert(id, lesson.clone());
62
  lesson
63
  }
@@ -129,39 +134,37 @@ pub(crate) struct InnerLesson {
129
  }
130
 
131
  impl InnerLesson {
132
- async fn new(parent: LessonsManager, speaker_lang: LanguageCode) -> InnerLesson {
133
  let (speaker_transcript, _) = tokio::sync::broadcast::channel::<Event>(128);
 
134
  let (speaker_voice_channel, mut speaker_voice_rx) = tokio::sync::mpsc::channel::<Vec<i16>>(128);
135
  let (drop_handler, drop_rx) = tokio::sync::oneshot::channel::<Signal>();
136
- let mut aws_asr = AWS_ASR::from_env(LanguageCode::EnGb)
137
- .await
138
- .expect("Failed to initialize AWS ASR");
139
- #[cfg(feature = "whisper")]
140
- let mut whisper_asr = Whisper_ASR::from_config()
141
- .await
142
- .expect("Failed to initialize Whisper ASR");
143
 
144
- tokio::spawn(async move {
145
- let fut = async {
 
146
  #[cfg(not(feature = "whisper"))]
147
- let mut transcribe = aws_asr.subscribe();
148
  #[cfg(feature = "whisper")]
149
- let mut transcribe = whisper_asr.subscribe();
 
 
 
 
 
 
150
  loop {
151
  select! {
152
- msg = speaker_voice_rx.recv() => {
153
- match msg {
154
  Some(frame) => {
155
- #[cfg(not(feature = "whisper"))]
156
- let res = aws_asr.frame(frame).await?;
157
- #[cfg(feature = "whisper")]
158
- let res = whisper_asr.frame(frame).await?;
159
  },
160
  None => break,
161
  }
162
  },
163
- a = transcribe.recv() => {
164
- todo!()
165
  }
166
  }
167
  }
@@ -171,7 +174,7 @@ impl InnerLesson {
171
  select! {
172
  res = fut => {
173
  if let Err(e) = res {
174
- warn!("Error: {:?}", e);
175
  }
176
  }
177
  _ = drop_rx => {}
 
10
  use std::ops::Deref;
11
  use std::sync::{Arc, Weak};
12
  use tokio::sync::RwLock;
13
+ use tracing::{error, warn};
14
 
15
  use tokio::select;
16
  use crate::asr::{Event, aws::AWS_ASR, ASR};
 
43
  }
44
  }
45
 
46
+ pub(crate) enum ASR_Engine {
47
+ AWS,
48
+ Whisper,
49
+ }
50
+
51
  impl LessonsManager {
52
  pub(crate) fn new(sdk_config: &SdkConfig) -> Self {
53
  let translate_client = aws_sdk_translate::Client::new(sdk_config);
 
60
  LessonsManager { inner: Arc::new(inner) }
61
  }
62
 
63
+ pub(crate) async fn create_lesson(&self, id: u32, engine: ASR_Engine, speaker_lang: LanguageCode) -> Lesson {
64
  let mut map = self.lessons.write().await;
65
+ let lesson: Lesson = InnerLesson::new(self.clone(), engine, speaker_lang).await.into();
66
  map.insert(id, lesson.clone());
67
  lesson
68
  }
 
134
  }
135
 
136
  impl InnerLesson {
137
+ async fn new(parent: LessonsManager, engine: ASR_Engine, speaker_lang: LanguageCode) -> InnerLesson {
138
  let (speaker_transcript, _) = tokio::sync::broadcast::channel::<Event>(128);
139
+ let shared_speaker_transcript = speaker_transcript.clone();
140
  let (speaker_voice_channel, mut speaker_voice_rx) = tokio::sync::mpsc::channel::<Vec<i16>>(128);
141
  let (drop_handler, drop_rx) = tokio::sync::oneshot::channel::<Signal>();
 
 
 
 
 
 
 
142
 
143
+ let mut asr: Box<dyn ASR + Send> = match engine {
144
+ ASR_Engine::AWS => Box::new(AWS_ASR::from_env(speaker_lang.clone()).await.expect("Failed to initialize AWS ASR")),
145
+ ASR_Engine::Whisper => {
146
  #[cfg(not(feature = "whisper"))]
147
+ unimplemented!("Whisper ASR is not enabled");
148
  #[cfg(feature = "whisper")]
149
+ Box::new(Whisper_ASR::from_config().await.expect("Failed to initialize Whisper ASR"))
150
+ },
151
+ };
152
+
153
+ tokio::spawn(async move {
154
+ let fut = async {
155
+ let mut transcribe = asr.subscribe();
156
  loop {
157
  select! {
158
+ msg_opt = speaker_voice_rx.recv() => {
159
+ match msg_opt {
160
  Some(frame) => {
161
+ asr.frame(frame).await?;
 
 
 
162
  },
163
  None => break,
164
  }
165
  },
166
+ evt_poll = transcribe.recv() => {
167
+ shared_speaker_transcript.send(evt_poll?)?;
168
  }
169
  }
170
  }
 
174
  select! {
175
  res = fut => {
176
  if let Err(e) = res {
177
+ error!("Error: {:?}", e);
178
  }
179
  }
180
  _ = drop_rx => {}
src/main.rs CHANGED
@@ -97,6 +97,7 @@ async fn stream_speaker(
97
  .lessons_manager
98
  .create_lesson(
99
  query.id,
 
100
  query.lang.clone().parse().expect("Not supported lang"),
101
  )
102
  .await;
 
97
  .lessons_manager
98
  .create_lesson(
99
  query.id,
100
+ ASR_Engine::AWS,
101
  query.lang.clone().parse().expect("Not supported lang"),
102
  )
103
  .await;