Skip to content

Commit

Permalink
Merge pull request #153 from solaoi/feature_fugumt
Browse files Browse the repository at this point in the history
翻訳パック(日本語:速度優先)を追加
  • Loading branch information
solaoi authored May 25, 2024
2 parents 5ec6663 + e16d92a commit ffcdc58
Show file tree
Hide file tree
Showing 16 changed files with 921 additions and 51 deletions.
446 changes: 410 additions & 36 deletions src-tauri/Cargo.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ core-graphics = "0.23.1"
objc = "0.2"
objc-foundation = "0.1"
objc_id = "0.1"
ct2rs = {version = "0.7.3", features = ["accelerate"] }

[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 @@ -141,4 +141,6 @@ VALUES("small-hi-0.22", "vosk");
INSERT INTO models(model_name, model_type)
VALUES("small-cs-0.4-rhasspy", "vosk");
INSERT INTO models(model_name, model_type)
VALUES("small-pl-0.22", "vosk");
VALUES("small-pl-0.22", "vosk");
INSERT INTO models(model_name, model_type)
VALUES("fugumt-en-ja", "fugumt");
29 changes: 23 additions & 6 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,19 @@ use module::{
chat_online::ChatOnline,
deleter::NoteDeleter,
device::{self, Device},
downloader::{vosk::VoskModelDownloader, whisper::WhisperModelDownloader},
downloader::{
fugumt::FugumtModelDownloader, vosk::VoskModelDownloader, whisper::WhisperModelDownloader,
},
model_type_vosk::ModelTypeVosk,
model_type_whisper::ModelTypeWhisper,
permissions,
record::Record,
record_desktop::RecordDesktop,
screenshot::{self, AppWindow},
transcription::{TraceCompletion, Transcription},
transcription_online::TranscriptionOnline,
transcription_amivoice::TranscriptionAmivoice,
transcription_online::TranscriptionOnline,
translation_ja::TranslationJa,
};

struct RecordState(Arc<Mutex<Option<Sender<()>>>>);
Expand Down Expand Up @@ -66,6 +69,14 @@ fn download_vosk_model_command(window: Window, model: String) {
});
}

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

#[tauri::command]
fn list_devices_command() -> Vec<Device> {
device::list_devices()
Expand Down Expand Up @@ -191,14 +202,19 @@ fn start_trace_command(
);
transcription_online.start(stop_convert_rx, true);
} else if transcription_accuracy.starts_with("online-amivoice") {
let mut transcription_amivoice = TranscriptionAmivoice::new(
window.app_handle(),
note_id,
);
let mut transcription_amivoice =
TranscriptionAmivoice::new(window.app_handle(), note_id);
transcription_amivoice.start(stop_convert_rx, true);
} else if transcription_accuracy.starts_with("online-chat") {
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,
);
translation_ja.start(stop_convert_rx, true);
} else {
let mut transcription = Transcription::new(
window.app_handle(),
Expand Down Expand Up @@ -307,6 +323,7 @@ fn main() {
delete_note_command,
download_whisper_model_command,
download_vosk_model_command,
download_fugumt_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/fugumt.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 FugumtModelDownloader {
app_handle: AppHandle,
}
impl FugumtModelDownloader {
pub fn new(app_handle: AppHandle) -> Self {
Self { app_handle }
}

#[tokio::main]
pub async fn download(&self) {
let model_type = "fugumt-en-ja";
let path: &str = &self
.app_handle
.path_resolver()
.resolve_resource("resources/fugumt-en-ja.zip")
.unwrap()
.to_string_lossy()
.to_string();
let url = "https://object-storage.tyo1.conoha.io/v1/nc_b22de95e3cf1434da07499038766e2b7/lycoris/fugumt-en-ja.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(
"downloadFugumtProgress",
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(
"downloadFugumtProgress",
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(
"downloadFugumtProgress",
Progress {
model_type: model_type.to_string(),
rate: 0.0,
is_progress: false,
},
);
}
}
1 change: 1 addition & 0 deletions src-tauri/src/module/downloader/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod whisper;
pub mod vosk;
pub mod fugumt;
1 change: 1 addition & 0 deletions src-tauri/src/module/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ mod transcriber;
pub mod transcription;
pub mod transcription_amivoice;
pub mod transcription_online;
pub mod translation_ja;
mod writer;
pub mod screenshot;
13 changes: 12 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, writer::Writer,
transcription_online, translation_ja, writer::Writer,
};

pub struct Record {
Expand Down Expand Up @@ -218,6 +218,16 @@ impl Record {
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else if transcription_accuracy_clone.starts_with("fugumt-en-ja") {
translation_ja::initialize_translation_ja(
app_handle_clone,
speaker_language_clone,
note_id,
);
let mut lock = translation_ja::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 @@ -254,6 +264,7 @@ impl Record {
if !is_no_transcription {
stop_convert_tx.send(()).unwrap();
transcription::drop_transcription();
translation_ja::drop_translation_ja();
transcription_online::drop_transcription_online();
transcription_amivoice::drop_transcription_amivoice();
chat_online::drop_chat_online();
Expand Down
13 changes: 12 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, writer::Writer,
transcription_online, translation_ja, writer::Writer,
};

pub struct RecordDesktop {
Expand Down Expand Up @@ -250,6 +250,16 @@ impl RecordDesktop {
if let Some(singleton) = lock.as_mut() {
singleton.start(stop_convert_rx_clone, false);
}
} else if transcription_accuracy_clone.starts_with("fugumt-en-ja") {
translation_ja::initialize_translation_ja(
app_handle_clone,
speaker_language_clone,
note_id,
);
let mut lock = translation_ja::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 @@ -290,6 +300,7 @@ impl RecordDesktop {
if !is_no_transcription {
stop_convert_tx.send(()).unwrap();
transcription::drop_transcription();
translation_ja::drop_translation_ja();
transcription_online::drop_transcription_online();
transcription_amivoice::drop_transcription_amivoice();
chat_online::drop_chat_online();
Expand Down
Loading

0 comments on commit ffcdc58

Please sign in to comment.