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

feat: support zh-tw zh-hk zh-cn #6

Merged
merged 3 commits into from
Jun 10, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 3 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ jobs:
- name: install whisper.cpp deps
run: sudo apt-get update && sudo apt-get install libclang-dev -y

- name: install feature = zh deps
run: sudo apt install libopencc-dev opencc -y

- name: Build debug
run: cargo build --locked

Expand Down
42 changes: 42 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ tract-onnx = "0.17.9"
rubato = "0.13.0"
owo-colors = "3.5.0"
yt_tsu = "0.1.0"
opencc-rust = { version = "1.1.15", optional = true }

[profile.release]
opt-level = 'z' # Optimize for size
lto = true # Enable link-time optimization
codegen-units = 1 # Reduce number of codegen units to increase optimizations
panic = 'abort' # Abort on panic
strip = true # Strip symbols from binary*

[features]
default = []
zh = ["opencc-rust/static-dictionaries"]
19 changes: 18 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ mod audio;
mod speech;
mod util;
mod vad;
#[cfg(feature = "zh")]
mod zh;

type F32Consumer = Consumer<f32, Arc<SharedRb<f32, Vec<MaybeUninit<f32>>>>>;
type SegmentProducer =
Expand Down Expand Up @@ -224,6 +226,14 @@ fn evoke_whisper_thread(
thread::spawn(move || {
let mut state = ctx.create_state().expect("failed to create state");
let mut streaming_time = 0.0f64;
let lang = if args.lang.starts_with("zh") {
"zh"
} else {
&args.lang
};

#[cfg(feature = "zh")]
let zh_transformer = zh::ZHTransformer::from(&args.lang);

while let Ok(ThreadState::Sync) = rx.recv() {
if cons.is_empty() {
Expand All @@ -232,14 +242,21 @@ fn evoke_whisper_thread(
}

cons.pop_iter().for_each(|segment| {
let config = SpeechConfig::new(args.threads as c_int, Some(&args.lang));
let config = SpeechConfig::new(args.threads as c_int, Some(lang));
let mut payload: WhisperPayload = WhisperPayload::new(&segment.data, config);
let running_calc = Instant::now();

let segment_time = (streaming_time * 1000.0) as i64;
streaming_time += segment.duration as f64;

speech::process(&mut state, &mut payload, &mut |segment, start| {
#[cfg(feature = "zh")]
let segment = if let Ok(zh) = &zh_transformer {
zh.convert(segment)
} else {
segment.to_string()
};

println!(
"[{}] {}",
util::format_timestamp_to_time(segment_time + start).bright_yellow(),
Expand Down
35 changes: 35 additions & 0 deletions src/zh.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use opencc_rust::{DefaultConfig, OpenCC};

pub struct ZHTransformer {
convert: OpenCC,
}

impl ZHTransformer {
pub fn from(s: &str) -> Result<Self, &'static str> {
let s = s.to_string().to_lowercase();

if s == "zh" {
return Err("auto detection chinese language code");
}

if !s.starts_with("zh") {
return Err("is not chinese language code");
}

let config = if matches!(s.as_str(), "zh_tw" | "zh-tw") {
DefaultConfig::S2TW
} else if matches!(s.as_str(), "zh_hk" | "zh-hk") {
DefaultConfig::S2HK
} else {
DefaultConfig::T2S
};

Ok(Self {
convert: OpenCC::new(config)?,
})
}

pub fn convert<S: AsRef<str>>(&self, s: S) -> String {
self.convert.convert(s)
}
}