Skip to content

YC (S26) | Open Computer History | Continuously record your company computer work, map your workflows, help you find work worth automating, and power your agents' context

refactor: download silero just once, and use cache dir

Louis Beaumont0c0f4c403c27
diff --git a/.github/workflows/release-app.yml b/.github/workflows/release-app.yml
index 25ca89b54..76de4e1af 100644
--- a/.github/workflows/release-app.yml
+++ b/.github/workflows/release-app.yml
@@ -36,14 +36,14 @@ jobs:
- platform: "macos-latest" # for Arm based macs (M1 and above).
args: "--target aarch64-apple-darwin --features metal"
target: aarch64-apple-darwin
- # - platform: "macos-latest" # for Intel based macs.
- # args: "--target x86_64-apple-darwin --features metal"
- # target: x86_64-apple-darwin
- # - platform: "ubuntu-22.04" # Ubuntu x86_64
- # args: "" # TODO CUDA, mkl
- # - platform: "windows-latest" # Windows x86_64
- # args: "--target x86_64-pc-windows-msvc" # TODO CUDA, mkl? --features "openblas"
- # pre-build-args: "" # --openblas
+ - platform: "macos-latest" # for Intel based macs.
+ args: "--target x86_64-apple-darwin --features metal"
+ target: x86_64-apple-darwin
+ - platform: "ubuntu-22.04" # Ubuntu x86_64
+ args: "" # TODO CUDA, mkl
+ - platform: "windows-latest" # Windows x86_64
+ args: "--target x86_64-pc-windows-msvc" # TODO CUDA, mkl? --features "openblas"
+ pre-build-args: "" # --openblas
# windows arm: https://github.com/ahqsoftwares/tauri-ahq-store/blob/2fbc2103c222662b3c6ee0cd71fcde664824f0ef/.github/workflows/publish.yml#L136
runs-on: ${{ matrix.platform }}
@@ -150,7 +150,7 @@ jobs:
export PKG_CONFIG_PATH="/usr/local/opt/ffmpeg/lib/pkgconfig:$PKG_CONFIG_PATH"
export PKG_CONFIG_ALLOW_CROSS=1
export RUSTFLAGS="-C link-arg=-Wl,-rpath,@executable_path/../Frameworks -C link-arg=-Wl,-rpath,@loader_path/../Frameworks -C link-arg=-Wl,-install_name,@rpath/libscreenpipe.dylib"
- # export V8_FLAGS="--max-old-space-size=4096" # https://github.com/denoland/deno_core/issues/916
+ export V8_FLAGS="--max-old-space-size=8192" # https://github.com/denoland/deno_core/issues/916
fi
# cargo build --release ${{ matrix.args }}
# tmp https://github.com/denoland/deno_core/issues/916
diff --git a/screenpipe-app-tauri/src-tauri/Cargo.toml b/screenpipe-app-tauri/src-tauri/Cargo.toml
index 8883938a6..555b01966 100644
--- a/screenpipe-app-tauri/src-tauri/Cargo.toml
+++ b/screenpipe-app-tauri/src-tauri/Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "screenpipe-app"
-version = "0.2.90-beta"
+version = "0.2.91"
description = ""
authors = ["you"]
license = ""
diff --git a/screenpipe-audio/Cargo.toml b/screenpipe-audio/Cargo.toml
index 069fed786..80f9ad1bd 100644
--- a/screenpipe-audio/Cargo.toml
+++ b/screenpipe-audio/Cargo.toml
@@ -66,6 +66,12 @@ screenpipe-core = { path = "../screenpipe-core" }
# crossbeam
crossbeam = { workspace = true }
+# Directories
+dirs = "5.0.1"
+
+lazy_static = { version = "1.4.0" }
+
+
[target.'cfg(target_os = "windows")'.dependencies]
ort = { version = "2.0.0-rc.5", features = ["download-binaries", "copy-dylibs", "directml", "cuda"] }
esaxx-rs = "0.1.10"
diff --git a/screenpipe-audio/src/vad_engine.rs b/screenpipe-audio/src/vad_engine.rs
index c6f1fee8a..b8a61c515 100644
--- a/screenpipe-audio/src/vad_engine.rs
+++ b/screenpipe-audio/src/vad_engine.rs
@@ -1,8 +1,12 @@
use anyhow;
+use dirs;
+use lazy_static::lazy_static;
use log::debug;
use std::collections::VecDeque;
-use std::io::Write;
use std::path::PathBuf;
+use std::sync::Once;
+use tokio::sync::Mutex;
+use tracing::info;
use vad_rs::{Vad, VadStatus};
#[derive(Clone, Copy, Debug)]
@@ -83,6 +87,12 @@ const SPEECH_THRESHOLD: f32 = 0.5;
const SILENCE_THRESHOLD: f32 = 0.35;
const SPEECH_FRAME_THRESHOLD: usize = 3; // Minimum number of frames above SPEECH_THRESHOLD to consider as speech
+lazy_static! {
+ static ref MODEL_PATH: Mutex<Option<PathBuf>> = Mutex::new(None);
+}
+
+static DOWNLOAD_ONCE: Once = Once::new();
+
pub struct SileroVad {
vad: Vad,
prob_history: VecDeque<f32>,
@@ -91,9 +101,9 @@ pub struct SileroVad {
impl SileroVad {
pub async fn new() -> anyhow::Result<Self> {
- debug!("Initializing SileroVad...");
- let model_path = Self::download_model().await?;
- debug!("SileroVad Model downloaded to: {:?}", model_path);
+ info!("Initializing SileroVad...");
+ let model_path = Self::get_or_download_model().await?;
+ info!("SileroVad Model downloaded to: {:?}", model_path);
let vad = Vad::new(model_path, 16000).map_err(|e| {
debug!("SileroVad Error creating Vad: {}", e);
anyhow::anyhow!("Vad creation error: {}", e)
@@ -106,19 +116,59 @@ impl SileroVad {
})
}
- async fn download_model() -> anyhow::Result<PathBuf> {
- debug!("Downloading SileroVAD model...");
+ async fn get_or_download_model() -> anyhow::Result<PathBuf> {
+ let mut model_path = MODEL_PATH.lock().await;
+ if let Some(path) = model_path.as_ref() {
+ return Ok(path.clone());
+ }
+
+ let cache_dir = Self::get_cache_dir()?;
+ let path = cache_dir.join("silero_vad.onnx");
+
+ if path.exists() {
+ *model_path = Some(path.clone());
+ return Ok(path);
+ }
+
+ DOWNLOAD_ONCE.call_once(|| {
+ tokio::spawn(async move {
+ if let Err(e) = Self::download_model().await {
+ debug!("error downloading silerovad model: {}", e);
+ }
+ });
+ });
+
+ // wait for download to complete
+ while !path.exists() {
+ tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
+ }
+
+ *model_path = Some(path.clone());
+ Ok(path)
+ }
+
+ async fn download_model() -> anyhow::Result<()> {
+ debug!("downloading silerovad model...");
let url =
"https://github.com/k2-fsa/sherpa-onnx/releases/download/asr-models/silero_vad.onnx";
let response = reqwest::get(url).await?;
let model_data = response.bytes().await?;
- let path = std::env::temp_dir().join("silero_vad.onnx");
- let mut file = std::fs::File::create(&path)?;
- file.write_all(&model_data)?;
- debug!("SileroVad Model downloaded and saved to: {:?}", path);
+ let cache_dir = Self::get_cache_dir()?;
+ tokio::fs::create_dir_all(&cache_dir).await?;
+ let path = cache_dir.join("silero_vad.onnx");
- Ok(path)
+ let mut file = tokio::fs::File::create(&path).await?;
+ tokio::io::AsyncWriteExt::write_all(&mut file, &model_data).await?;
+ debug!("silerovad model downloaded and saved to: {:?}", path);
+
+ Ok(())
+ }
+
+ fn get_cache_dir() -> anyhow::Result<PathBuf> {
+ let proj_dirs =
+ dirs::cache_dir().ok_or_else(|| anyhow::anyhow!("failed to get cache dir"))?;
+ Ok(proj_dirs.join("screenpipe").join("vad"))
}
fn update_status(&mut self, prob: f32) -> VadStatus {
diff --git a/screenpipe-core/src/pipes.rs b/screenpipe-core/src/pipes.rs
index b24298365..26ae35150 100644
--- a/screenpipe-core/src/pipes.rs
+++ b/screenpipe-core/src/pipes.rs
@@ -8,11 +8,8 @@ mod pipes {
use deno_core::extension;
use deno_core::op2;
use deno_core::v8;
- use deno_core::v8::Platform;
- use deno_core::v8::SharedRef;
use deno_core::ModuleLoadResponse;
use deno_core::ModuleSourceCode;
- use deno_core::RuntimeOptions;
use lettre::message::header::ContentType;
use regex::Regex;
use reqwest::header::HeaderMap;
@@ -27,7 +24,6 @@ mod pipes {
use reqwest::Client;
use serde_json::Value;
- use deno_core::v8::CreateParams;
use lettre::transport::smtp::authentication::Credentials;
use lettre::{Message, SmtpTransport, Transport};
use reqwest;