Skip to content

Commit

Permalink
dev (#418)
Browse files Browse the repository at this point in the history
* remove unused hints in pre build script

* update whisper.cpp

* misc

* update whisper-rs

* misc

* fix pre build

* feat: enable logs by default to file

* feat: keep files for one day and clean older on startup
  • Loading branch information
thewh1teagle authored Dec 1, 2024
1 parent cd05a7e commit 8f8c8a2
Show file tree
Hide file tree
Showing 19 changed files with 181 additions and 109 deletions.
16 changes: 9 additions & 7 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ path = "src/lib.rs"
# Fixed incorrect timestamps, better logs
whisper-rs = { git = "https://github.com/thewh1teagle/whisper-rs.git", features = [
"whisper-cpp-tracing",
], default-features = false, rev = "4585bb" }
], default-features = false, branch = "dev" }
serde = { version = "1.0.195", features = ["derive"] }
num = "0.4.1"
once_cell = "1.19.0"
Expand All @@ -21,6 +21,7 @@ eyre = { workspace = true }
hound = "3.5.1"
reqwest = { version = "0.11.23", features = ["stream"] }
tempfile = "3.9.0"
chrono = "0.4.38"
tokio = { version = "1.35.1", features = [
"tokio-macros",
"macros",
Expand Down
11 changes: 11 additions & 0 deletions core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,14 @@ pub mod transcript;

#[cfg(test)]
mod test;

pub fn get_vibe_temp_folder() -> std::path::PathBuf {
use chrono::Local;
let current_datetime = Local::now();
let formatted_datetime = current_datetime.format("%Y-%m-%d").to_string();
let dir = std::env::temp_dir().join(format!("vibe_temp_{}", formatted_datetime));
if let Ok(_) = std::fs::create_dir_all(&dir) {
return dir;
}
std::env::temp_dir()
}
19 changes: 7 additions & 12 deletions core/src/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::audio;
use crate::config::TranscribeOptions;
use crate::transcript::{Segment, Transcript};
use crate::{audio, get_vibe_temp_folder};
use eyre::{bail, eyre, Context, OptionExt, Result};
use hound::WavReader;
use std::collections::hash_map::DefaultHasher;
Expand Down Expand Up @@ -81,7 +81,7 @@ pub fn create_normalized_audio(source: PathBuf, additional_ffmpeg_args: Option<V
tracing::debug!("normalize {:?}", source.display());

let cache_key = generate_cache_key(&source, &additional_ffmpeg_args);
let out_path = std::env::temp_dir().join(format!("{:x}.wav", cache_key));
let out_path = get_vibe_temp_folder().join(format!("{:x}.wav", cache_key));
if out_path.exists() {
tracing::info!("Using cached normalized audio: {}", out_path.display());
return Ok(out_path);
Expand Down Expand Up @@ -193,7 +193,7 @@ pub fn transcribe(
}

// whisper compatible. segment indices
tracing::debug!("diarize segment: {} - {}", diarize_segment.start, diarize_segment.end);
tracing::trace!("diarize segment: {} - {}", diarize_segment.start, diarize_segment.end);

let mut samples = vec![0.0f32; diarize_segment.samples.len()];

Expand All @@ -210,7 +210,7 @@ pub fn transcribe(
Ok(result) => result.collect(),
Err(error) => {
tracing::error!("error: {:?}", error);
tracing::debug!(
tracing::trace!(
"start = {:.2}, end = {:.2}, speaker = ?",
diarize_segment.start,
diarize_segment.end
Expand Down Expand Up @@ -247,9 +247,9 @@ pub fn transcribe(
new_segment_callback(segment);
}
if let Some(ref progress_callback) = progress_callback {
tracing::debug!("progress: {} * {} / 100", i, diarize_segments.len());
tracing::trace!("progress: {} * {} / 100", i, diarize_segments.len());
let progress = ((i + 1) as f64 / diarize_segments.len() as f64 * 100.0) as i32;
tracing::debug!("progress diarize: {}", progress);
tracing::trace!("progress diarize: {}", progress);
progress_callback(progress);
}
}
Expand Down Expand Up @@ -283,7 +283,7 @@ pub fn transcribe(
if PROGRESS_CALLBACK.lock().map_err(|e| eyre!("{:?}", e))?.as_ref().is_some() {
params.set_progress_callback_safe(|progress| {
// using move here lead to crash
tracing::debug!("progress callback {}", progress);
tracing::trace!("progress callback {}", progress);
match PROGRESS_CALLBACK.lock() {
Ok(callback_guard) => {
if let Some(progress_callback) = callback_guard.as_ref() {
Expand Down Expand Up @@ -330,10 +330,5 @@ pub fn transcribe(
processing_time_sec: Instant::now().duration_since(st).as_secs(),
};

// cleanup
if out_path.starts_with(std::env::temp_dir()) {
std::fs::remove_file(out_path)?;
}

Ok(transcript)
}
1 change: 1 addition & 0 deletions desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ utoipa-swagger-ui = { version = "7.1.0", features = ["axum"], optional = true }
tracing = { version = "0.1.40", features = ["log"] }
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
glob = "0.3.1"

chrono = "0.4.38"
crash-handler = "0.6.2"
Expand Down
57 changes: 57 additions & 0 deletions desktop/src-tauri/src/cleaner.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use crate::utils::LogError;
use crate::{cmd::get_logs_folder, config, logging::get_log_path};
use eyre::{eyre, ContextCompat, Result};
use vibe_core::get_vibe_temp_folder;

pub fn clean_old_logs(app: &tauri::AppHandle) -> Result<()> {
tracing::debug!("clean old logs");
let current_log_path = get_log_path(&app.clone())?;

// Get logs folder
let logs_folder = get_logs_folder(app.to_owned())?;
let logs_folder = logs_folder.to_str().context("tostr")?;

// Remove suffix
let logs_folder = logs_folder.strip_suffix('/').unwrap_or(logs_folder);
let logs_folder = logs_folder.strip_suffix('\\').unwrap_or(logs_folder);
let pattern = format!(
"{}/{}*{}",
logs_folder,
config::LOG_FILENAME_PREFIX,
config::LOG_FILENAME_SUFFIX
);
tracing::debug!("searching old logs in {}", pattern);
for path in glob::glob(&pattern)? {
let path = path?;
if path == current_log_path {
tracing::debug!("Skip clean of current log path {}", path.display());
continue;
}
tracing::debug!("clean old log {}", path.display());
std::fs::remove_file(path)?;
}
Ok(())
}

pub fn clean_old_files() -> Result<()> {
let current_temp_dir = get_vibe_temp_folder();
let temp_dir = std::env::temp_dir();
let temp_dir = temp_dir.to_str().unwrap_or_default();
// Remove suffix
let temp_dir = temp_dir.strip_suffix('/').unwrap_or(temp_dir);
let temp_dir = temp_dir.strip_suffix('\\').unwrap_or(temp_dir);
let pattern = format!("{}/vibe_temp*", temp_dir);
tracing::debug!("searching old files in {}", pattern);
for path in glob::glob(&pattern)? {
let path = path?;
if path == current_temp_dir {
tracing::debug!("Skip deletion of {}", current_temp_dir.display());
continue;
}
tracing::debug!("Clean old folder {}", path.clone().display());
std::fs::remove_dir_all(path.clone())
.map_err(|e| eyre!("failed to delete {}: {:?}", path.display(), e))
.log_error();
}
Ok(())
}
17 changes: 9 additions & 8 deletions desktop/src-tauri/src/cmd/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::io::BufWriter;
use std::path::PathBuf;
use std::sync::{Arc, Mutex};
use tauri::{AppHandle, Emitter, Listener, Manager};
use vibe_core::get_vibe_temp_folder;

#[cfg(target_os = "macos")]
use crate::screen_capture_kit;
Expand Down Expand Up @@ -108,7 +109,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
};
let spec = wav_spec_from_config(&config);

let path = std::env::temp_dir().join(format!("{}.wav", random_string(10)));
let path = get_vibe_temp_folder().join(format!("{}.wav", random_string(10)));
tracing::debug!("WAV file path: {:?}", path);
wav_paths.push((path.clone(), 0));

Expand All @@ -125,7 +126,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
cpal::SampleFormat::I8 => device.build_input_stream(
&config.into(),
move |data, _: &_| {
tracing::debug!("Writing input data (I8)");
tracing::trace!("Writing input data (I8)");
write_input_data::<i8, i8>(data, &writer_2)
},
err_fn,
Expand All @@ -134,7 +135,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
cpal::SampleFormat::I16 => device.build_input_stream(
&config.into(),
move |data, _: &_| {
tracing::debug!("Writing input data (I16)");
tracing::trace!("Writing input data (I16)");
write_input_data::<i16, i16>(data, &writer_2)
},
err_fn,
Expand All @@ -143,7 +144,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
cpal::SampleFormat::I32 => device.build_input_stream(
&config.into(),
move |data, _: &_| {
tracing::debug!("Writing input data (I32)");
tracing::trace!("Writing input data (I32)");
write_input_data::<i32, i32>(data, &writer_2)
},
err_fn,
Expand All @@ -152,7 +153,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
cpal::SampleFormat::F32 => device.build_input_stream(
&config.into(),
move |data, _: &_| {
tracing::debug!("Writing input data (F32)");
tracing::trace!("Writing input data (F32)");
write_input_data::<f32, f32>(data, &writer_2)
},
err_fn,
Expand Down Expand Up @@ -194,7 +195,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
{
if let Some(stream) = screencapture_stream {
screen_capture_kit::stop_capture(&stream).map_err(|e| eyre!("{:?}", e)).log_error();
let output_path = std::env::temp_dir().join(format!("{}.wav", random_string(5)));
let output_path = get_vibe_temp_folder().join(format!("{}.wav", random_string(5)));
screen_capture_kit::screencapturekit_to_wav(output_path.clone()).map_err(|e| eyre!("{e:?}")).log_error();
tracing::debug!("output path is {}", output_path.display());
wav_paths.push((output_path, 1));
Expand All @@ -204,7 +205,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
let dst = if wav_paths.len() == 1 {
wav_paths[0].0.clone()
} else if wav_paths[0].1 > 0 && wav_paths[1].1 > 0 {
let dst = std::env::temp_dir().join(format!("{}.wav", random_string(10)));
let dst = get_vibe_temp_folder().join(format!("{}.wav", random_string(10)));
tracing::debug!("Merging WAV files");
vibe_core::audio::merge_wav_files(wav_paths[0].0.clone(), wav_paths[1].0.clone(), dst.clone()).map_err(|e| eyre!("{e:?}")).log_error();
dst
Expand All @@ -218,7 +219,7 @@ pub async fn start_record(app_handle: AppHandle, devices: Vec<AudioDevice>, stor
};

tracing::debug!("Emitting record_finish event");
let mut normalized = std::env::temp_dir().join(format!("{}.wav", get_local_time()));
let mut normalized = get_vibe_temp_folder().join(format!("{}.wav", get_local_time()));
vibe_core::audio::normalize(dst.clone(), normalized.clone(), None).map_err(|e| eyre!("{e:?}")).log_error();

if store_in_documents {
Expand Down
18 changes: 15 additions & 3 deletions desktop/src-tauri/src/cmd/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ pub async fn download_model(app_handle: tauri::AppHandle, url: Option<String>) -
// Update progress in background
tauri::async_runtime::spawn(async move {
let percentage = (current as f64 / total as f64) * 100.0;
tracing::debug!("percentage: {}", percentage);
tracing::trace!("percentage: {}", percentage);
if let Err(e) = set_progress_bar(&app_handle, Some(percentage)) {
tracing::error!("Failed to set progress bar: {}", e);
}
Expand Down Expand Up @@ -179,7 +179,7 @@ pub async fn download_file(app_handle: tauri::AppHandle, url: String, path: Stri
// Update progress in background
tauri::async_runtime::spawn(async move {
let percentage = (current as f64 / total as f64) * 100.0;
tracing::debug!("percentage: {}", percentage);
tracing::trace!("percentage: {}", percentage);
if let Some(window) = app_handle.get_webview_window("main") {
if let Err(e) = window.emit("download_progress", (current, total)) {
tracing::error!("Failed to emit download progress: {}", e);
Expand Down Expand Up @@ -276,7 +276,6 @@ pub async fn transcribe(
let app_handle_c = app_handle.clone();
let app_handle_c1 = app_handle.clone();
let progress_callback = move |progress: i32| {
// tracing::debug!("desktop progress is {}", progress);
let _ = set_progress_bar(&app_handle, Some(progress.into()));
};

Expand Down Expand Up @@ -489,6 +488,19 @@ pub fn get_logs_folder(app_handle: tauri::AppHandle) -> Result<PathBuf> {
Ok(config_path)
}

#[tauri::command]
pub async fn show_log_path(app_handle: tauri::AppHandle) -> Result<()> {
let log_path = crate::logging::get_log_path(&app_handle)?;
if log_path.exists() {
showfile::show_path_in_file_manager(log_path);
} else {
if let Some(parent) = log_path.parent() {
showfile::show_path_in_file_manager(parent);
}
}
Ok(())
}

#[tauri::command]
pub fn get_models_folder(app_handle: tauri::AppHandle) -> Result<PathBuf> {
let store = app_handle.store(STORE_FILENAME)?;
Expand Down
5 changes: 3 additions & 2 deletions desktop/src-tauri/src/cmd/ytdlp.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use eyre::{bail, Context, Result};
use tauri::{AppHandle, Manager};
use vibe_core::get_vibe_temp_folder;

use super::get_ffmpeg_path;

Expand All @@ -22,9 +23,9 @@ fn get_binary_name() -> &'static str {
#[tauri::command]
pub fn get_temp_path(app_handle: AppHandle, ext: String, in_documents: Option<bool>) -> String {
let mut base_path = if in_documents.unwrap_or_default() {
app_handle.path().document_dir().unwrap_or(std::env::temp_dir())
app_handle.path().document_dir().unwrap_or(get_vibe_temp_folder())
} else {
std::env::temp_dir()
get_vibe_temp_folder()
};

base_path.push(format!("{}.{}", crate::utils::get_local_time(), ext));
Expand Down
3 changes: 2 additions & 1 deletion desktop/src-tauri/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub const CRASH_LOG_FILENAME_PREFIX: &str = "crash";
pub const LOG_FILENAME_PREFIX: &str = "log";
pub const LOG_FILENAME_SUFFIX: &str = ".txt";
pub const DEFAULT_LOG_DIRECTIVE: &str = "vibe=DEBUG,vibe_core=DEBUG,whisper_rs=INFO";
pub const STORE_FILENAME: &str = "app_config.json";
pub const DEAFULT_MODEL_URL: &str = "https://github.com/thewh1teagle/vibe/releases/download/v0.0.1/ggml-medium.bin";
pub const DEAFULT_MODEL_FILENAME: &str = "ggml-medium.bin";
Expand Down
Loading

0 comments on commit 8f8c8a2

Please sign in to comment.