Skip to content

Commit

Permalink
Merge pull request #157 from solaoi/feature_translation-ja-high
Browse files Browse the repository at this point in the history
翻訳パック(日本語)を追加
  • Loading branch information
solaoi authored Jun 17, 2024
2 parents a6cf455 + 7ef50c8 commit c7348e7
Show file tree
Hide file tree
Showing 16 changed files with 2,368 additions and 686 deletions.
2,455 changes: 1,787 additions & 668 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ tauri-build = { version = "1.2.1", features = [] }
[dependencies]
serde_json = "1.0"
serde = { version = "1.0", features = ["derive"] }
tauri = { version = "1.2.5", features = ["api-all"] }
tauri = { version = "1.6.7", features = ["api-all"] }
vosk = "0.1.0"
cpal = "0.14.0"
dasp = "0.11"
Expand All @@ -43,7 +43,10 @@ core-graphics = "0.23.1"
objc = "0.2"
objc-foundation = "0.1"
objc_id = "0.1"
ct2rs = {version = "0.7.3", features = ["accelerate"] }
ct2rs = { version = "0.7.3", features = ["accelerate"] }
mistralrs = { git = "https://github.com/EricLBuehler/mistral.rs.git", tag = "v0.1.18", features = [
"metal",
] }

[target.'cfg(target_arch = "x86_64")'.dependencies]
whisper-rs = { version = "0.11.1", features = ["metal"] }
Expand Down
4 changes: 3 additions & 1 deletion src-tauri/migrations/001.sql
Original file line number Diff line number Diff line change
Expand Up @@ -143,4 +143,6 @@ VALUES("small-cs-0.4-rhasspy", "vosk");
INSERT INTO models(model_name, model_type)
VALUES("small-pl-0.22", "vosk");
INSERT INTO models(model_name, model_type)
VALUES("fugumt-en-ja", "fugumt");
VALUES("fugumt-en-ja", "fugumt");
INSERT INTO models(model_name, model_type)
VALUES("honyaku13b-q4-0", "honyaku13b");
24 changes: 18 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ use module::{
deleter::NoteDeleter,
device::{self, Device},
downloader::{
fugumt::FugumtModelDownloader, vosk::VoskModelDownloader, whisper::WhisperModelDownloader,
fugumt::FugumtModelDownloader, honyaku13b::Honyaku13BModelDownloader,
vosk::VoskModelDownloader, whisper::WhisperModelDownloader,
},
model_type_vosk::ModelTypeVosk,
model_type_whisper::ModelTypeWhisper,
Expand All @@ -39,6 +40,7 @@ use module::{
transcription_amivoice::TranscriptionAmivoice,
transcription_online::TranscriptionOnline,
translation_ja::TranslationJa,
translation_ja_high::TranslationJaHigh,
};

struct RecordState(Arc<Mutex<Option<Sender<()>>>>);
Expand Down Expand Up @@ -77,6 +79,14 @@ fn download_fugumt_model_command(window: Window) {
});
}

#[tauri::command]
fn download_honyaku13b_model_command(window: Window) {
std::thread::spawn(move || {
let dl = Honyaku13BModelDownloader::new(window.app_handle().clone());
dl.download()
});
}

#[tauri::command]
fn list_devices_command() -> Vec<Device> {
device::list_devices()
Expand Down Expand Up @@ -209,12 +219,13 @@ fn start_trace_command(
let mut chat_online = ChatOnline::new(window.app_handle(), speaker_language, note_id);
chat_online.start(stop_convert_rx, true);
} else if transcription_accuracy.starts_with("fugumt-en-ja") {
let mut translation_ja = TranslationJa::new(
window.app_handle(),
speaker_language,
note_id,
);
let mut translation_ja =
TranslationJa::new(window.app_handle(), speaker_language, note_id);
translation_ja.start(stop_convert_rx, true);
} else if transcription_accuracy.starts_with("honyaku13b-q4-0") {
let mut translation_ja_high =
TranslationJaHigh::new(window.app_handle(), speaker_language, note_id);
translation_ja_high.start(stop_convert_rx, true);
} else {
let mut transcription = Transcription::new(
window.app_handle(),
Expand Down Expand Up @@ -324,6 +335,7 @@ fn main() {
download_whisper_model_command,
download_vosk_model_command,
download_fugumt_model_command,
download_honyaku13b_model_command,
list_devices_command,
list_apps_command,
list_app_windows_command,
Expand Down
119 changes: 119 additions & 0 deletions src-tauri/src/module/downloader/honyaku13b.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
use tauri::{AppHandle, Manager};

use futures_util::StreamExt;
use std::cmp::min;
use std::fs::File;
use std::io::Write;

use crate::module::sqlite::Sqlite;

#[derive(Debug, Clone, serde::Serialize)]
pub struct Progress {
pub model_type: String,
pub rate: f64,
pub is_progress: bool,
}

pub struct Honyaku13BModelDownloader {
app_handle: AppHandle,
}
impl Honyaku13BModelDownloader {
pub fn new(app_handle: AppHandle) -> Self {
Self { app_handle }
}

#[tokio::main]
pub async fn download(&self) {
let model_type = "honyaku13b-q4-0";
let path: &str = &self
.app_handle
.path_resolver()
.resolve_resource("resources/honyaku13b-q4-0.zip")
.unwrap()
.to_string_lossy()
.to_string();
let url = "https://object-storage.tyo1.conoha.io/v1/nc_b22de95e3cf1434da07499038766e2b7/lycoris/honyaku13b-q4-0.zip";
let res = reqwest::get(url).await.unwrap();
let total_size = res
.content_length()
.ok_or(format!("Failed to get content length from '{}'", url))
.unwrap();

let _ = &self.app_handle.emit_all(
"downloadHonyaku13BProgress",
Progress {
model_type: model_type.to_string(),
rate: 0.0,
is_progress: true,
},
);

let mut file;
let mut downloaded: u64 = 0;
let mut stream = res.bytes_stream();

println!("Seeking in file.");
if std::path::Path::new(&path).exists() {
println!("File exists. Removig...");
let _ = std::fs::remove_file(&path);
}
file = File::create(&path)
.or(Err(format!("Failed to create file '{}'", &path)))
.unwrap();

println!("Commencing transfer");
let mut rate = 0.0;
while let Some(item) = stream.next().await {
let chunk = item
.or(Err(format!("Error while downloading file")))
.unwrap();
file.write(&chunk)
.or(Err(format!("Error while writing to file")))
.unwrap();
let new = min(downloaded + (chunk.len() as u64), total_size);
downloaded = new;

let current_rate = ((new as f64 * 100.0) / total_size as f64).round();
if rate != current_rate {
let _ = &self.app_handle.emit_all(
"downloadHonyaku13BProgress",
Progress {
model_type: model_type.to_string(),
rate: current_rate,
is_progress: true,
},
);
rate = current_rate
}
}

let dir: &str = &self
.app_handle
.path_resolver()
.resolve_resource("resources")
.unwrap()
.to_string_lossy()
.to_string();
let _ = std::process::Command::new("sh")
.arg("-c")
.arg(format!("unzip {} -d {}", path, dir))
.output()
.expect("failed");
let _ = std::process::Command::new("sh")
.arg("-c")
.arg(format!("rm {}", path))
.output()
.expect("failed");

let _ = Sqlite::new().update_model_is_downloaded(model_type.to_string(), 1);

let _ = &self.app_handle.emit_all(
"downloadHonyaku13BProgress",
Progress {
model_type: model_type.to_string(),
rate: 0.0,
is_progress: false,
},
);
}
}
3 changes: 2 additions & 1 deletion src-tauri/src/module/downloader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod whisper;
pub mod vosk;
pub mod fugumt;
pub mod fugumt;
pub mod honyaku13b;
1 change: 1 addition & 0 deletions src-tauri/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ pub mod transcription;
pub mod transcription_amivoice;
pub mod transcription_online;
pub mod translation_ja;
pub mod translation_ja_high;
mod writer;
pub mod screenshot;
14 changes: 13 additions & 1 deletion src-tauri/src/module/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use tauri::{api::path::data_dir, AppHandle, Manager};

use super::{
chat_online, recognizer::MyRecognizer, sqlite::Sqlite, transcription, transcription_amivoice,
transcription_online, translation_ja, writer::Writer,
transcription_online, translation_ja, translation_ja_high, writer::Writer,
};

pub struct Record {
Expand Down Expand Up @@ -228,6 +228,17 @@ impl Record {
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else if transcription_accuracy_clone.starts_with("honyaku13b-q4-0") {
translation_ja_high::initialize_translation_ja_high(
app_handle_clone,
speaker_language_clone,
note_id,
);
let mut lock =
translation_ja_high::SINGLETON_INSTANCE.lock().unwrap();
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else {
transcription::initialize_transcription(
app_handle_clone,
Expand Down Expand Up @@ -265,6 +276,7 @@ impl Record {
stop_convert_tx.send(()).unwrap();
transcription::drop_transcription();
translation_ja::drop_translation_ja();
translation_ja_high::drop_translation_ja_high();
transcription_online::drop_transcription_online();
transcription_amivoice::drop_transcription_amivoice();
chat_online::drop_chat_online();
Expand Down
14 changes: 13 additions & 1 deletion src-tauri/src/module/record_desktop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use vosk::Recognizer;

use super::{
chat_online, recognizer::MyRecognizer, sqlite::Sqlite, transcription, transcription_amivoice,
transcription_online, translation_ja, writer::Writer,
transcription_online, translation_ja, translation_ja_high, writer::Writer,
};

pub struct RecordDesktop {
Expand Down Expand Up @@ -260,6 +260,17 @@ impl RecordDesktop {
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else if transcription_accuracy_clone.starts_with("honyaku13b-q4-0") {
translation_ja_high::initialize_translation_ja_high(
app_handle_clone,
speaker_language_clone,
note_id,
);
let mut lock =
translation_ja_high::SINGLETON_INSTANCE.lock().unwrap();
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else {
transcription::initialize_transcription(
app_handle_clone,
Expand Down Expand Up @@ -301,6 +312,7 @@ impl RecordDesktop {
stop_convert_tx.send(()).unwrap();
transcription::drop_transcription();
translation_ja::drop_translation_ja();
translation_ja_high::drop_translation_ja_high();
transcription_online::drop_transcription_online();
transcription_amivoice::drop_transcription_amivoice();
chat_online::drop_chat_online();
Expand Down
Loading

0 comments on commit c7348e7

Please sign in to comment.