From 2d1b0225460f5db126681c9c9aa2c6a59e1103a8 Mon Sep 17 00:00:00 2001 From: thewh1teagle <61390950+thewh1teagle@users.noreply.github.com> Date: Tue, 19 Nov 2024 05:04:37 +0200 Subject: [PATCH] feat/server behind feature (#404) * feat: move server behind feature flag * don't include vulkan runtime by default * fix test --- .github/workflows/linux_special.yml | 2 +- .github/workflows/release.yml | 8 ++++---- .github/workflows/windows_special.yml | 4 ++-- core/Cargo.toml | 5 ++++- core/src/config.rs | 5 +++-- core/src/test.rs | 2 +- core/src/transcript.rs | 7 ++++--- desktop/src-tauri/Cargo.toml | 13 ++++++++++--- desktop/src-tauri/src/cli.rs | 6 +++--- desktop/src-tauri/src/cmd/mod.rs | 6 +++--- desktop/src-tauri/src/main.rs | 3 +++ desktop/src-tauri/src/server.rs | 4 ++-- desktop/src-tauri/tauri.windows.conf.json | 3 +-- 13 files changed, 41 insertions(+), 27 deletions(-) diff --git a/.github/workflows/linux_special.yml b/.github/workflows/linux_special.yml index 853b0609..bedf17b7 100644 --- a/.github/workflows/linux_special.yml +++ b/.github/workflows/linux_special.yml @@ -17,7 +17,7 @@ jobs: include: # Ubuntu 22.04 - platform: "ubuntu-22.04" - args: '--features "vulkan"' + args: '--features "vulkan" --features "server"' pre-build-args: "--vulkan" rust-version: "stable" name: "ubuntu-22.04" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 3dd7ecd5..a2a5f8f6 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -12,14 +12,14 @@ jobs: matrix: include: - platform: "macos-latest" # for Arm based macs (M1 and above). - args: "--target aarch64-apple-darwin" + args: '--target aarch64-apple-darwin --features "server"' - platform: "macos-latest" # for Intel based macs. - args: "--target x86_64-apple-darwin" + args: '--target x86_64-apple-darwin --features "server"' - platform: "ubuntu-22.04" # Ubuntu 22.04 x86_64 (Works on 24.04 as well) - args: '--features "vulkan"' + args: '--features "vulkan,server"' pre-build-args: "--vulkan" - platform: "windows-latest" # Windows x86_64 - args: '--target x86_64-pc-windows-msvc --features "vulkan"' + args: '--target x86_64-pc-windows-msvc --features "vulkan,server"' pre-build-args: "--vulkan" runs-on: ${{ matrix.platform }} diff --git a/.github/workflows/windows_special.yml b/.github/workflows/windows_special.yml index d9220ed8..f76300c7 100644 --- a/.github/workflows/windows_special.yml +++ b/.github/workflows/windows_special.yml @@ -22,14 +22,14 @@ jobs: include: # Windows no AVX2 - platform: "windows-latest" - args: '' + args: '--features "server"' pre-build-args: "--older-cpu" name: "older-cpu" rust-version: "stable" # Windows no AVX2 - platform: "windows-latest" - args: '--features="vulkan"' + args: '--features="vulkan,server"' pre-build-args: "--older-cpu --vulkan" name: "older-cpu-vulkan" rust-version: "stable" diff --git a/core/Cargo.toml b/core/Cargo.toml index 6cc4fd43..1088353c 100644 --- a/core/Cargo.toml +++ b/core/Cargo.toml @@ -16,7 +16,6 @@ serde = { version = "1.0.195", features = ["derive"] } num = "0.4.1" once_cell = "1.19.0" which = "6.0.1" -utoipa = { version = "4.2.3", features = ["axum_extras"] } tracing = { version = "0.1.40", features = ["log"] } eyre = { workspace = true } hound = "3.5.1" @@ -32,6 +31,9 @@ serde_json = { workspace = true } futures-util = "0.3.30" pyannote-rs = "0.2.7" +# Server +utoipa = { version = "4.2.3", features = ["axum_extras"], optional = true } + [dev-dependencies] serial_test = "3.1.1" @@ -49,3 +51,4 @@ openblas = ["whisper-rs/openblas"] metal = ["whisper-rs/metal"] rocm = ["whisper-rs/hipblas"] vulkan = ["whisper-rs/vulkan"] +server = ["dep:utoipa"] diff --git a/core/src/config.rs b/core/src/config.rs index 6cdbd232..62089af8 100644 --- a/core/src/config.rs +++ b/core/src/config.rs @@ -1,8 +1,9 @@ use core::fmt; use serde::{Deserialize, Serialize}; -use utoipa::ToSchema; -#[derive(Deserialize, Serialize, ToSchema)] +#[derive(Deserialize, Serialize)] +#[cfg_attr(feature = "server", derive(utoipa::ToSchema))] + pub struct TranscribeOptions { pub path: String, pub lang: Option, diff --git a/core/src/test.rs b/core/src/test.rs index 5319262c..aba2a25b 100644 --- a/core/src/test.rs +++ b/core/src/test.rs @@ -27,7 +27,7 @@ fn test_transcribe() { word_timestamps: None, }; let start = Instant::now(); - let result = crate::transcribe::transcribe(&ctx, options, None, None, None, None); + let result = crate::transcribe::transcribe(&ctx, options, None, None, None, None, None); println!("{:?}", result); println!( "Elapsed time: {:.2} seconds", diff --git a/core/src/transcript.rs b/core/src/transcript.rs index 214c51d9..4bb5c1e5 100644 --- a/core/src/transcript.rs +++ b/core/src/transcript.rs @@ -1,7 +1,6 @@ use eyre::Result; use num::integer::div_floor; use serde::{Deserialize, Serialize}; -use utoipa::ToSchema; pub fn format_timestamp(seconds: i64, always_include_hours: bool, decimal_marker: &str) -> String { assert!(seconds >= 0, "non-negative timestamp expected"); @@ -25,13 +24,15 @@ pub fn format_timestamp(seconds: i64, always_include_hours: bool, decimal_marker format!("{hours_marker}{minutes:02}:{seconds:02}{decimal_marker}{milliseconds:03}") } -#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "server", derive(utoipa::ToSchema))] pub struct Transcript { pub processing_time_sec: u64, pub segments: Vec, } -#[derive(Debug, Serialize, Deserialize, ToSchema, Clone)] +#[derive(Debug, Serialize, Deserialize, Clone)] +#[cfg_attr(feature = "server", derive(utoipa::ToSchema))] pub struct Segment { pub start: i64, pub stop: i64, diff --git a/desktop/src-tauri/Cargo.toml b/desktop/src-tauri/Cargo.toml index 82719b98..33ecd6ef 100644 --- a/desktop/src-tauri/Cargo.toml +++ b/desktop/src-tauri/Cargo.toml @@ -42,12 +42,17 @@ futures = "0.3.30" cpal = "0.15.3" hound = "3.5.1" rand = "0.8.5" -axum = "0.7.5" -utoipa = { version = "4.2.3", features = ["axum_extras"] } -utoipa-swagger-ui = { version = "7.1.0", features = ["axum"] } + +# Server +axum = { version = "0.7.5", optional = true } +utoipa = { version = "4.2.3", features = ["axum_extras"], optional = true } +utoipa-swagger-ui = { version = "7.1.0", features = ["axum"], optional = true } + +# Log tracing = { version = "0.1.40", features = ["log"] } tracing-log = "0.2.0" tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] } + chrono = "0.4.38" crash-handler = "0.6.2" urlencoding = "2.1.3" @@ -84,9 +89,11 @@ objc_id = "0.1" core-graphics-helmer-fork = "0.24.0" [features] +default = [] cuda = ["vibe_core/cuda"] coreml = ["vibe_core/coreml"] metal = ["vibe_core/metal"] openblas = ["vibe_core/openblas"] rocm = ["vibe_core/rocm"] vulkan = ["vibe_core/vulkan", "dep:ash"] +server = ["dep:utoipa", "dep:utoipa-swagger-ui", "dep:axum", "vibe_core/server"] diff --git a/desktop/src-tauri/src/cli.rs b/desktop/src-tauri/src/cli.rs index 209a268b..1c28b4ae 100644 --- a/desktop/src-tauri/src/cli.rs +++ b/desktop/src-tauri/src/cli.rs @@ -9,7 +9,6 @@ use vibe_core::config::TranscribeOptions; use vibe_core::transcribe; use crate::cmd::get_models_folder; -use crate::server; /// Attach to console if cli detected in Windows #[cfg(all(windows, not(debug_assertions)))] @@ -115,7 +114,7 @@ struct Args { diarize_speaker_id_model: Option, /// Run http server - #[arg(long)] + #[cfg_attr(feature = "server", clap(long))] server: bool, #[arg(long, default_value = "0.0.0.0")] @@ -180,8 +179,9 @@ pub async fn run(app_handle: &AppHandle) -> Result<()> { args.format = "json".into(); } + #[cfg(feature = "server")] if args.server { - server::run(app_handle.clone(), args.host, args.port).await?; + crate::server::run(app_handle.clone(), args.host, args.port).await?; } let lang = language_name_to_whisper_lang(&args.language)?; let options = TranscribeOptions { diff --git a/desktop/src-tauri/src/cmd/mod.rs b/desktop/src-tauri/src/cmd/mod.rs index cd4df822..7c25ee0c 100644 --- a/desktop/src-tauri/src/cmd/mod.rs +++ b/desktop/src-tauri/src/cmd/mod.rs @@ -228,14 +228,14 @@ impl Default for FfmpegOptions { } impl FfmpegOptions { - pub fn to_vec(&mut self) -> Vec { + pub fn to_vec(&self) -> Vec { let mut cmd = Vec::::new(); if let Some(custom_cmd) = &self.custom_command { cmd.extend(custom_cmd.split_whitespace().map(|s| s.to_string())); } else if self.normalize_loudness { cmd.extend(["-af".to_string(), "loudnorm=I=-16:TP=-1.5:LRA=11".to_string()]); } - return cmd; + cmd } } @@ -245,7 +245,7 @@ pub async fn transcribe( options: vibe_core::config::TranscribeOptions, model_context_state: State<'_, Mutex>>, diarize_options: DiarizeOptions, - mut ffmpeg_options: FfmpegOptions, + ffmpeg_options: FfmpegOptions, ) -> Result { let model_context = model_context_state.lock().await; if model_context.is_none() { diff --git a/desktop/src-tauri/src/main.rs b/desktop/src-tauri/src/main.rs index 91b6e1b9..82b6deae 100644 --- a/desktop/src-tauri/src/main.rs +++ b/desktop/src-tauri/src/main.rs @@ -5,7 +5,10 @@ mod cli; mod cmd; mod config; mod panic_hook; + +#[cfg(feature = "server")] mod server; + mod setup; mod utils; use tauri::{Emitter, Manager}; diff --git a/desktop/src-tauri/src/server.rs b/desktop/src-tauri/src/server.rs index e9e47a01..ce07c9f2 100644 --- a/desktop/src-tauri/src/server.rs +++ b/desktop/src-tauri/src/server.rs @@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use tauri::Manager; use tokio::sync::Mutex; -use utoipa::{OpenApi, ToSchema}; +use utoipa::OpenApi; use utoipa_swagger_ui::SwaggerUi; use vibe_core::config::TranscribeOptions; use vibe_core::transcript::{Segment, Transcript}; @@ -39,7 +39,7 @@ pub async fn run(app_handle: tauri::AppHandle, host: String, port: u16) -> eyre: Ok(()) } -#[derive(Deserialize, Serialize, ToSchema)] +#[derive(Deserialize, Serialize, utoipa::ToSchema)] struct LoadPayload { pub model_path: String, pub gpu_device: Option, diff --git a/desktop/src-tauri/tauri.windows.conf.json b/desktop/src-tauri/tauri.windows.conf.json index 6b5c9800..d2d4352e 100644 --- a/desktop/src-tauri/tauri.windows.conf.json +++ b/desktop/src-tauri/tauri.windows.conf.json @@ -9,8 +9,7 @@ "timestampUrl": "http://timestamp.digicert.com" }, "resources": { - "ffmpeg\\bin\\x64\\*": "./", - "vulkan_runtime\\x64\\*.dll": "./" + "ffmpeg\\bin\\x64\\*": "./" } } } \ No newline at end of file