[feat] implement backend abstraction, dynamic backend selection, and GPU feature integration
This commit is contained in:
32
crates/polyscribe-core/Cargo.toml
Normal file
32
crates/polyscribe-core/Cargo.toml
Normal file
@@ -0,0 +1,32 @@
|
||||
[package]
|
||||
name = "polyscribe"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
license = "MIT"
|
||||
|
||||
[features]
|
||||
# Default: CPU only; no GPU features enabled
|
||||
default = []
|
||||
# GPU backends map to whisper-rs features or FFI stub for Vulkan
|
||||
gpu-cuda = ["whisper-rs/cuda"]
|
||||
gpu-hip = ["whisper-rs/hipblas"]
|
||||
gpu-vulkan = []
|
||||
# explicit CPU fallback feature (no effect at build time, used for clarity)
|
||||
cpu-fallback = []
|
||||
|
||||
[dependencies]
|
||||
anyhow = "1.0.98"
|
||||
serde = { version = "1.0.219", features = ["derive"] }
|
||||
serde_json = "1.0.142"
|
||||
toml = "0.8"
|
||||
chrono = { version = "0.4", features = ["clock"] }
|
||||
sha2 = "0.10"
|
||||
whisper-rs = { git = "https://github.com/tazz4843/whisper-rs" }
|
||||
libc = "0.2"
|
||||
cliclack = "0.3"
|
||||
indicatif = "0.17"
|
||||
thiserror = "1"
|
||||
directories = "5"
|
||||
|
||||
[build-dependencies]
|
||||
# no special build deps
|
13
crates/polyscribe-core/build.rs
Normal file
13
crates/polyscribe-core/build.rs
Normal file
@@ -0,0 +1,13 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Move original build.rs behavior into core crate
|
||||
fn main() {
|
||||
// Only run special build steps when gpu-vulkan feature is enabled.
|
||||
let vulkan_enabled = std::env::var("CARGO_FEATURE_GPU_VULKAN").is_ok();
|
||||
if !vulkan_enabled {
|
||||
return;
|
||||
}
|
||||
println!("cargo:rerun-if-changed=extern/whisper.cpp");
|
||||
println!(
|
||||
"cargo:warning=Building with gpu-vulkan: ensure Vulkan SDK/loader are installed. Future versions will compile whisper.cpp via CMake."
|
||||
);
|
||||
}
|
329
crates/polyscribe-core/src/backend.rs
Normal file
329
crates/polyscribe-core/src/backend.rs
Normal file
@@ -0,0 +1,329 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2025 <COPYRIGHT HOLDER>. All rights reserved.
|
||||
|
||||
//! Transcription backend selection and implementations (CPU/GPU) used by PolyScribe.
|
||||
use crate::OutputEntry;
|
||||
use crate::{decode_audio_to_pcm_f32_ffmpeg, find_model_file};
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use std::env;
|
||||
use std::path::Path;
|
||||
|
||||
// Re-export a public enum for CLI parsing usage
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
/// Kind of transcription backend to use.
|
||||
pub enum BackendKind {
|
||||
/// Automatically detect the best available backend (CUDA > HIP > Vulkan > CPU).
|
||||
Auto,
|
||||
/// Pure CPU backend using whisper-rs.
|
||||
Cpu,
|
||||
/// NVIDIA CUDA backend (requires CUDA runtime available at load time and proper feature build).
|
||||
Cuda,
|
||||
/// AMD ROCm/HIP backend (requires hip/rocBLAS libraries available and proper feature build).
|
||||
Hip,
|
||||
/// Vulkan backend (experimental; requires Vulkan loader/SDK and feature build).
|
||||
Vulkan,
|
||||
}
|
||||
|
||||
/// Abstraction for a transcription backend.
|
||||
pub trait TranscribeBackend {
|
||||
/// Backend kind implemented by this type.
|
||||
fn kind(&self) -> BackendKind;
|
||||
/// Transcribe the given audio and return transcript entries.
|
||||
fn transcribe(
|
||||
&self,
|
||||
audio_path: &Path,
|
||||
speaker: &str,
|
||||
language: Option<&str>,
|
||||
gpu_layers: Option<u32>,
|
||||
progress: Option<&(dyn Fn(i32) + Send + Sync)>,
|
||||
) -> Result<Vec<OutputEntry>>;
|
||||
}
|
||||
|
||||
fn check_lib(_names: &[&str]) -> bool {
|
||||
#[cfg(test)]
|
||||
{
|
||||
// During unit tests, avoid touching system libs to prevent loader crashes in CI.
|
||||
false
|
||||
}
|
||||
#[cfg(not(test))]
|
||||
{
|
||||
// Disabled runtime dlopen probing to avoid loader instability; rely on environment overrides.
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn cuda_available() -> bool {
|
||||
if let Ok(x) = env::var("POLYSCRIBE_TEST_FORCE_CUDA") {
|
||||
return x == "1";
|
||||
}
|
||||
check_lib(&[
|
||||
"libcudart.so",
|
||||
"libcudart.so.12",
|
||||
"libcudart.so.11",
|
||||
"libcublas.so",
|
||||
"libcublas.so.12",
|
||||
])
|
||||
}
|
||||
|
||||
fn hip_available() -> bool {
|
||||
if let Ok(x) = env::var("POLYSCRIBE_TEST_FORCE_HIP") {
|
||||
return x == "1";
|
||||
}
|
||||
check_lib(&["libhipblas.so", "librocblas.so"])
|
||||
}
|
||||
|
||||
fn vulkan_available() -> bool {
|
||||
if let Ok(x) = env::var("POLYSCRIBE_TEST_FORCE_VULKAN") {
|
||||
return x == "1";
|
||||
}
|
||||
check_lib(&["libvulkan.so.1", "libvulkan.so"])
|
||||
}
|
||||
|
||||
/// CPU-based transcription backend using whisper-rs.
|
||||
#[derive(Default)]
|
||||
pub struct CpuBackend;
|
||||
/// CUDA-accelerated transcription backend for NVIDIA GPUs.
|
||||
#[derive(Default)]
|
||||
pub struct CudaBackend;
|
||||
/// ROCm/HIP-accelerated transcription backend for AMD GPUs.
|
||||
#[derive(Default)]
|
||||
pub struct HipBackend;
|
||||
/// Vulkan-based transcription backend (experimental/incomplete).
|
||||
#[derive(Default)]
|
||||
pub struct VulkanBackend;
|
||||
|
||||
macro_rules! impl_whisper_backend {
|
||||
($ty:ty, $kind:expr) => {
|
||||
impl TranscribeBackend for $ty {
|
||||
fn kind(&self) -> BackendKind { $kind }
|
||||
fn transcribe(
|
||||
&self,
|
||||
audio_path: &Path,
|
||||
speaker: &str,
|
||||
language: Option<&str>,
|
||||
_gpu_layers: Option<u32>,
|
||||
progress: Option<&(dyn Fn(i32) + Send + Sync)>,
|
||||
) -> Result<Vec<OutputEntry>> {
|
||||
transcribe_with_whisper_rs(audio_path, speaker, language, progress)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_whisper_backend!(CpuBackend, BackendKind::Cpu);
|
||||
impl_whisper_backend!(CudaBackend, BackendKind::Cuda);
|
||||
impl_whisper_backend!(HipBackend, BackendKind::Hip);
|
||||
|
||||
impl TranscribeBackend for VulkanBackend {
|
||||
fn kind(&self) -> BackendKind {
|
||||
BackendKind::Vulkan
|
||||
}
|
||||
fn transcribe(
|
||||
&self,
|
||||
_audio_path: &Path,
|
||||
_speaker: &str,
|
||||
_language: Option<&str>,
|
||||
_gpu_layers: Option<u32>,
|
||||
_progress: Option<&(dyn Fn(i32) + Send + Sync)>,
|
||||
) -> Result<Vec<OutputEntry>> {
|
||||
Err(anyhow!(
|
||||
"Vulkan backend not yet wired to whisper.cpp FFI. Build with --features gpu-vulkan and ensure Vulkan SDK is installed. How to fix: install Vulkan loader (libvulkan), set VULKAN_SDK, and run cargo build --features gpu-vulkan."
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/// Result of choosing a transcription backend.
|
||||
pub struct SelectionResult {
|
||||
/// The constructed backend instance to perform transcription with.
|
||||
pub backend: Box<dyn TranscribeBackend + Send + Sync>,
|
||||
/// Which backend kind was ultimately selected.
|
||||
pub chosen: BackendKind,
|
||||
/// Which backend kinds were detected as available on this system.
|
||||
pub detected: Vec<BackendKind>,
|
||||
}
|
||||
|
||||
/// Select an appropriate backend based on user request and system detection.
|
||||
///
|
||||
/// If `requested` is `BackendKind::Auto`, the function prefers CUDA, then HIP,
|
||||
/// then Vulkan, falling back to CPU when no GPU backend is detected. When a
|
||||
/// specific GPU backend is requested but unavailable, an error is returned with
|
||||
/// guidance on how to enable it.
|
||||
///
|
||||
/// Set `verbose` to true to print detection/selection info to stderr.
|
||||
pub fn select_backend(requested: BackendKind, verbose: bool) -> Result<SelectionResult> {
|
||||
let mut detected = Vec::new();
|
||||
if cuda_available() {
|
||||
detected.push(BackendKind::Cuda);
|
||||
}
|
||||
if hip_available() {
|
||||
detected.push(BackendKind::Hip);
|
||||
}
|
||||
if vulkan_available() {
|
||||
detected.push(BackendKind::Vulkan);
|
||||
}
|
||||
|
||||
let instantiate_backend = |k: BackendKind| -> Box<dyn TranscribeBackend + Send + Sync> {
|
||||
match k {
|
||||
BackendKind::Cpu => Box::new(CpuBackend::default()),
|
||||
BackendKind::Cuda => Box::new(CudaBackend::default()),
|
||||
BackendKind::Hip => Box::new(HipBackend::default()),
|
||||
BackendKind::Vulkan => Box::new(VulkanBackend::default()),
|
||||
BackendKind::Auto => Box::new(CpuBackend::default()), // placeholder for Auto
|
||||
}
|
||||
};
|
||||
|
||||
let chosen = match requested {
|
||||
BackendKind::Auto => {
|
||||
if detected.contains(&BackendKind::Cuda) {
|
||||
BackendKind::Cuda
|
||||
} else if detected.contains(&BackendKind::Hip) {
|
||||
BackendKind::Hip
|
||||
} else if detected.contains(&BackendKind::Vulkan) {
|
||||
BackendKind::Vulkan
|
||||
} else {
|
||||
BackendKind::Cpu
|
||||
}
|
||||
}
|
||||
BackendKind::Cuda => {
|
||||
if detected.contains(&BackendKind::Cuda) {
|
||||
BackendKind::Cuda
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"Requested CUDA backend but CUDA libraries/devices not detected. How to fix: install NVIDIA driver + CUDA toolkit, ensure libcudart/libcublas are in loader path, and build with --features gpu-cuda."
|
||||
));
|
||||
}
|
||||
}
|
||||
BackendKind::Hip => {
|
||||
if detected.contains(&BackendKind::Hip) {
|
||||
BackendKind::Hip
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"Requested ROCm/HIP backend but libraries/devices not detected. How to fix: install ROCm hipBLAS/rocBLAS, ensure libs are in loader path, and build with --features gpu-hip."
|
||||
));
|
||||
}
|
||||
}
|
||||
BackendKind::Vulkan => {
|
||||
if detected.contains(&BackendKind::Vulkan) {
|
||||
BackendKind::Vulkan
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"Requested Vulkan backend but libvulkan not detected. How to fix: install Vulkan loader/SDK and build with --features gpu-vulkan."
|
||||
));
|
||||
}
|
||||
}
|
||||
BackendKind::Cpu => BackendKind::Cpu,
|
||||
};
|
||||
|
||||
if verbose {
|
||||
crate::dlog!(1, "Detected backends: {:?}", detected);
|
||||
crate::dlog!(1, "Selected backend: {:?}", chosen);
|
||||
}
|
||||
|
||||
Ok(SelectionResult {
|
||||
backend: instantiate_backend(chosen),
|
||||
chosen,
|
||||
detected,
|
||||
})
|
||||
}
|
||||
|
||||
// Internal helper: transcription using whisper-rs with CPU/GPU (depending on build features)
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub(crate) fn transcribe_with_whisper_rs(
|
||||
audio_path: &Path,
|
||||
speaker: &str,
|
||||
language: Option<&str>,
|
||||
progress: Option<&(dyn Fn(i32) + Send + Sync)>,
|
||||
) -> Result<Vec<OutputEntry>> {
|
||||
let report = |p: i32| {
|
||||
if let Some(cb) = progress { cb(p); }
|
||||
};
|
||||
report(0);
|
||||
|
||||
let pcm_samples = decode_audio_to_pcm_f32_ffmpeg(audio_path)?;
|
||||
report(5);
|
||||
|
||||
let model_path = find_model_file()?;
|
||||
let english_only_model = model_path
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.map(|s| s.contains(".en.") || s.ends_with(".en.bin"))
|
||||
.unwrap_or(false);
|
||||
if let Some(lang) = language {
|
||||
if english_only_model && lang != "en" {
|
||||
return Err(anyhow!(
|
||||
"Selected model is English-only ({}), but a non-English language hint '{}' was provided. Please use a multilingual model or set WHISPER_MODEL.",
|
||||
model_path.display(),
|
||||
lang
|
||||
));
|
||||
}
|
||||
}
|
||||
let model_path_str = model_path
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow!("Model path not valid UTF-8: {}", model_path.display()))?;
|
||||
|
||||
if crate::verbose_level() < 2 {
|
||||
// Some builds of whisper/ggml expect these env vars; harmless if unknown
|
||||
unsafe {
|
||||
std::env::set_var("GGML_LOG_LEVEL", "0");
|
||||
std::env::set_var("WHISPER_PRINT_PROGRESS", "0");
|
||||
}
|
||||
}
|
||||
|
||||
let (_context, mut state) = crate::with_suppressed_stderr(|| {
|
||||
let params = whisper_rs::WhisperContextParameters::default();
|
||||
let context = whisper_rs::WhisperContext::new_with_params(model_path_str, params)
|
||||
.with_context(|| format!("Failed to load Whisper model at {}", model_path.display()))?;
|
||||
let state = context
|
||||
.create_state()
|
||||
.map_err(|e| anyhow!("Failed to create Whisper state: {:?}", e))?;
|
||||
Ok::<_, anyhow::Error>((context, state))
|
||||
})?;
|
||||
report(20);
|
||||
|
||||
let mut full_params =
|
||||
whisper_rs::FullParams::new(whisper_rs::SamplingStrategy::Greedy { best_of: 1 });
|
||||
let threads = std::thread::available_parallelism()
|
||||
.map(|n| n.get() as i32)
|
||||
.unwrap_or(1);
|
||||
full_params.set_n_threads(threads);
|
||||
full_params.set_translate(false);
|
||||
if let Some(lang) = language {
|
||||
full_params.set_language(Some(lang));
|
||||
}
|
||||
report(30);
|
||||
|
||||
crate::with_suppressed_stderr(|| {
|
||||
report(40);
|
||||
state
|
||||
.full(full_params, &pcm_samples)
|
||||
.map_err(|e| anyhow!("Whisper full() failed: {:?}", e))
|
||||
})?;
|
||||
|
||||
report(90);
|
||||
let num_segments = state
|
||||
.full_n_segments()
|
||||
.map_err(|e| anyhow!("Failed to get segments: {:?}", e))?;
|
||||
let mut entries = Vec::new();
|
||||
for seg_idx in 0..num_segments {
|
||||
let segment_text = state
|
||||
.full_get_segment_text(seg_idx)
|
||||
.map_err(|e| anyhow!("Failed to get segment text: {:?}", e))?;
|
||||
let t0 = state
|
||||
.full_get_segment_t0(seg_idx)
|
||||
.map_err(|e| anyhow!("Failed to get segment t0: {:?}", e))?;
|
||||
let t1 = state
|
||||
.full_get_segment_t1(seg_idx)
|
||||
.map_err(|e| anyhow!("Failed to get segment t1: {:?}", e))?;
|
||||
let start = (t0 as f64) * 0.01;
|
||||
let end = (t1 as f64) * 0.01;
|
||||
entries.push(OutputEntry {
|
||||
id: 0,
|
||||
speaker: speaker.to_string(),
|
||||
start,
|
||||
end,
|
||||
text: segment_text.trim().to_string(),
|
||||
});
|
||||
}
|
||||
report(100);
|
||||
Ok(entries)
|
||||
}
|
149
crates/polyscribe-core/src/config.rs
Normal file
149
crates/polyscribe-core/src/config.rs
Normal file
@@ -0,0 +1,149 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Simple ConfigService with XDG/system/workspace merge and atomic writes
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use directories::BaseDirs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::env;
|
||||
use std::fs;
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Generic configuration represented as TOML table
|
||||
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
|
||||
pub struct Config(pub toml::value::Table);
|
||||
|
||||
impl Config {
|
||||
/// Get a mutable reference to a top-level table under the given key, creating
|
||||
/// an empty table if it does not exist yet.
|
||||
pub fn get_table_mut(&mut self, key: &str) -> &mut toml::value::Table {
|
||||
let needs_init = !matches!(self.0.get(key), Some(toml::Value::Table(_)));
|
||||
if needs_init {
|
||||
self.0.insert(key.to_string(), toml::Value::Table(Default::default()));
|
||||
}
|
||||
match self.0.get_mut(key) {
|
||||
Some(toml::Value::Table(t)) => t,
|
||||
_ => unreachable!(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn merge_tables(base: &mut toml::value::Table, overlay: &toml::value::Table) {
|
||||
for (k, v) in overlay.iter() {
|
||||
match (base.get_mut(k), v) {
|
||||
(Some(toml::Value::Table(bsub)), toml::Value::Table(osub)) => {
|
||||
merge_tables(bsub, osub);
|
||||
}
|
||||
_ => {
|
||||
base.insert(k.clone(), v.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn read_toml(path: &Path) -> Result<toml::value::Table> {
|
||||
let s = fs::read_to_string(path).with_context(|| format!("Failed to read config: {}", path.display()))?;
|
||||
let v: toml::Value = toml::from_str(&s).with_context(|| format!("Invalid TOML in {}", path.display()))?;
|
||||
Ok(v.as_table().cloned().unwrap_or_default())
|
||||
}
|
||||
|
||||
fn write_toml_atomic(path: &Path, tbl: &toml::value::Table) -> Result<()> {
|
||||
if let Some(parent) = path.parent() {
|
||||
fs::create_dir_all(parent).with_context(|| format!("Failed to create config dir: {}", parent.display()))?;
|
||||
}
|
||||
let tmp = path.with_extension("tmp");
|
||||
let mut f = fs::File::create(&tmp).with_context(|| format!("Failed to create temp file: {}", tmp.display()))?;
|
||||
let s = toml::to_string_pretty(&toml::Value::Table(tbl.clone()))?;
|
||||
f.write_all(s.as_bytes())?;
|
||||
if !s.ends_with('\n') { f.write_all(b"\n")?; }
|
||||
drop(f);
|
||||
fs::rename(&tmp, path).with_context(|| format!("Failed to atomically replace config: {}", path.display()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn system_config_path() -> PathBuf {
|
||||
if cfg!(unix) { PathBuf::from("/etc").join("polyscribe").join("config.toml") } else { default_user_config_path() }
|
||||
}
|
||||
|
||||
fn default_user_config_path() -> PathBuf {
|
||||
if let Some(base) = BaseDirs::new() {
|
||||
return PathBuf::from(base.config_dir()).join("polyscribe").join("config.toml");
|
||||
}
|
||||
PathBuf::from(".polyscribe").join("config.toml")
|
||||
}
|
||||
|
||||
fn workspace_config_path() -> PathBuf {
|
||||
PathBuf::from(".polyscribe").join("config.toml")
|
||||
}
|
||||
|
||||
/// Service responsible for loading and saving PolyScribe configuration
|
||||
#[derive(Debug, Default, Clone)]
|
||||
pub struct ConfigService;
|
||||
|
||||
impl ConfigService {
|
||||
/// Load configuration, merging system < user < workspace < env overrides.
|
||||
pub fn load(&self) -> Result<Config> {
|
||||
let mut accum = toml::value::Table::default();
|
||||
let sys = system_config_path();
|
||||
if sys.exists() {
|
||||
merge_tables(&mut accum, &read_toml(&sys)?);
|
||||
}
|
||||
let user = default_user_config_path();
|
||||
if user.exists() {
|
||||
merge_tables(&mut accum, &read_toml(&user)?);
|
||||
}
|
||||
let ws = workspace_config_path();
|
||||
if ws.exists() {
|
||||
merge_tables(&mut accum, &read_toml(&ws)?);
|
||||
}
|
||||
// Env overrides: POLYSCRIBE__SECTION__KEY=value
|
||||
let mut env_over = toml::value::Table::default();
|
||||
for (k, v) in env::vars() {
|
||||
if let Some(rest) = k.strip_prefix("POLYSCRIBE__") {
|
||||
let parts: Vec<&str> = rest.split("__").collect();
|
||||
if parts.is_empty() { continue; }
|
||||
let val: toml::Value = toml::Value::String(v);
|
||||
// Build nested tables
|
||||
let mut current = &mut env_over;
|
||||
for (i, part) in parts.iter().enumerate() {
|
||||
if i == parts.len() - 1 {
|
||||
current.insert(part.to_lowercase(), val.clone());
|
||||
} else {
|
||||
current = current.entry(part.to_lowercase()).or_insert_with(|| toml::Value::Table(Default::default()))
|
||||
.as_table_mut().expect("table");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
merge_tables(&mut accum, &env_over);
|
||||
Ok(Config(accum))
|
||||
}
|
||||
|
||||
/// Ensure user config exists with sensible defaults, return loaded config
|
||||
pub fn ensure_user_config(&self) -> Result<Config> {
|
||||
let path = default_user_config_path();
|
||||
if !path.exists() {
|
||||
let mut defaults = toml::value::Table::default();
|
||||
defaults.insert("ui".into(), toml::Value::Table({
|
||||
let mut t = toml::value::Table::default();
|
||||
t.insert("theme".into(), toml::Value::String("auto".into()));
|
||||
t
|
||||
}));
|
||||
write_toml_atomic(&path, &defaults)?;
|
||||
}
|
||||
self.load()
|
||||
}
|
||||
|
||||
/// Save to user config atomically, merging over existing user file.
|
||||
pub fn save_user(&self, new_values: &toml::value::Table) -> Result<()> {
|
||||
let path = default_user_config_path();
|
||||
let mut base = if path.exists() { read_toml(&path)? } else { Default::default() };
|
||||
merge_tables(&mut base, new_values);
|
||||
write_toml_atomic(&path, &base)
|
||||
}
|
||||
|
||||
/// Paths used for debugging/information
|
||||
pub fn paths(&self) -> (PathBuf, PathBuf, PathBuf) {
|
||||
(system_config_path(), default_user_config_path(), workspace_config_path())
|
||||
}
|
||||
}
|
453
crates/polyscribe-core/src/lib.rs
Normal file
453
crates/polyscribe-core/src/lib.rs
Normal file
@@ -0,0 +1,453 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2025 <COPYRIGHT HOLDER>. All rights reserved.
|
||||
|
||||
#![forbid(elided_lifetimes_in_paths)]
|
||||
#![forbid(unused_must_use)]
|
||||
#![deny(missing_docs)]
|
||||
#![warn(clippy::all)]
|
||||
//! PolyScribe library: business logic and core types.
|
||||
//!
|
||||
//! This crate exposes the reusable parts of the PolyScribe CLI as a library.
|
||||
//! The binary entry point (main.rs) remains a thin CLI wrapper.
|
||||
|
||||
use std::sync::atomic::{AtomicBool, AtomicU8, Ordering};
|
||||
|
||||
// Global runtime flags
|
||||
static QUIET: AtomicBool = AtomicBool::new(false);
|
||||
static NO_INTERACTION: AtomicBool = AtomicBool::new(false);
|
||||
static VERBOSE: AtomicU8 = AtomicU8::new(0);
|
||||
static NO_PROGRESS: AtomicBool = AtomicBool::new(false);
|
||||
|
||||
/// Set quiet mode: when true, non-interactive logs should be suppressed.
|
||||
pub fn set_quiet(enabled: bool) {
|
||||
QUIET.store(enabled, Ordering::Relaxed);
|
||||
}
|
||||
/// Return current quiet mode state.
|
||||
pub fn is_quiet() -> bool {
|
||||
QUIET.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Set non-interactive mode: when true, interactive prompts must be skipped.
|
||||
pub fn set_no_interaction(enabled: bool) {
|
||||
NO_INTERACTION.store(enabled, Ordering::Relaxed);
|
||||
}
|
||||
/// Return current non-interactive state.
|
||||
pub fn is_no_interaction() -> bool {
|
||||
NO_INTERACTION.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Set verbose level (0 = normal, 1 = verbose, 2 = super-verbose)
|
||||
pub fn set_verbose(level: u8) {
|
||||
VERBOSE.store(level, Ordering::Relaxed);
|
||||
}
|
||||
/// Get current verbose level.
|
||||
pub fn verbose_level() -> u8 {
|
||||
VERBOSE.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Disable interactive progress indicators (bars/spinners)
|
||||
pub fn set_no_progress(enabled: bool) {
|
||||
NO_PROGRESS.store(enabled, Ordering::Relaxed);
|
||||
}
|
||||
/// Return current no-progress state
|
||||
pub fn is_no_progress() -> bool {
|
||||
NO_PROGRESS.load(Ordering::Relaxed)
|
||||
}
|
||||
|
||||
/// Check whether stdin is connected to a TTY. Used to avoid blocking prompts when not interactive.
|
||||
pub fn stdin_is_tty() -> bool {
|
||||
use std::io::IsTerminal as _;
|
||||
std::io::stdin().is_terminal()
|
||||
}
|
||||
|
||||
/// A guard that temporarily redirects stderr to /dev/null on Unix when quiet mode is active.
|
||||
/// No-op on non-Unix or when quiet is disabled. Restores stderr on drop.
|
||||
pub struct StderrSilencer {
|
||||
#[cfg(unix)]
|
||||
old_stderr_fd: i32,
|
||||
#[cfg(unix)]
|
||||
devnull_fd: i32,
|
||||
active: bool,
|
||||
}
|
||||
|
||||
impl StderrSilencer {
|
||||
/// Activate stderr silencing if quiet is set and on Unix; otherwise returns a no-op guard.
|
||||
pub fn activate_if_quiet() -> Self {
|
||||
if !is_quiet() {
|
||||
return Self {
|
||||
active: false,
|
||||
#[cfg(unix)]
|
||||
old_stderr_fd: -1,
|
||||
#[cfg(unix)]
|
||||
devnull_fd: -1,
|
||||
};
|
||||
}
|
||||
Self::activate()
|
||||
}
|
||||
|
||||
/// Activate stderr silencing unconditionally (used internally); no-op on non-Unix.
|
||||
pub fn activate() -> Self {
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
let old_fd = dup(2);
|
||||
if old_fd < 0 {
|
||||
return Self {
|
||||
active: false,
|
||||
old_stderr_fd: -1,
|
||||
devnull_fd: -1,
|
||||
};
|
||||
}
|
||||
// Open /dev/null for writing
|
||||
let devnull_cstr = std::ffi::CString::new("/dev/null").unwrap();
|
||||
let devnull_fd = open(devnull_cstr.as_ptr(), O_WRONLY);
|
||||
if devnull_fd < 0 {
|
||||
close(old_fd);
|
||||
return Self {
|
||||
active: false,
|
||||
old_stderr_fd: -1,
|
||||
devnull_fd: -1,
|
||||
};
|
||||
}
|
||||
if dup2(devnull_fd, 2) < 0 {
|
||||
close(devnull_fd);
|
||||
close(old_fd);
|
||||
return Self {
|
||||
active: false,
|
||||
old_stderr_fd: -1,
|
||||
devnull_fd: -1,
|
||||
};
|
||||
}
|
||||
Self {
|
||||
active: true,
|
||||
old_stderr_fd: old_fd,
|
||||
devnull_fd: devnull_fd,
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
Self { active: false }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for StderrSilencer {
|
||||
fn drop(&mut self) {
|
||||
if !self.active {
|
||||
return;
|
||||
}
|
||||
#[cfg(unix)]
|
||||
unsafe {
|
||||
let _ = dup2(self.old_stderr_fd, 2);
|
||||
let _ = close(self.old_stderr_fd);
|
||||
let _ = close(self.devnull_fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Run the given closure with stderr temporarily silenced (Unix-only). Returns the closure result.
|
||||
pub fn with_suppressed_stderr<F, T>(f: F) -> T
|
||||
where
|
||||
F: FnOnce() -> T,
|
||||
{
|
||||
let silencer = StderrSilencer::activate_if_quiet();
|
||||
let result = f();
|
||||
drop(silencer);
|
||||
result
|
||||
}
|
||||
|
||||
/// Log an error line (always printed).
|
||||
#[macro_export]
|
||||
macro_rules! elog {
|
||||
($($arg:tt)*) => {{ $crate::ui::error(format!($($arg)*)); }}
|
||||
}
|
||||
|
||||
/// Log an informational line using the UI helper unless quiet mode is enabled.
|
||||
#[macro_export]
|
||||
macro_rules! ilog {
|
||||
($($arg:tt)*) => {{
|
||||
if !$crate::is_quiet() { $crate::ui::info(format!($($arg)*)); }
|
||||
}}
|
||||
}
|
||||
|
||||
/// Log a debug/trace line when verbose level is at least the given level (u8).
|
||||
#[macro_export]
|
||||
macro_rules! dlog {
|
||||
($lvl:expr, $($arg:tt)*) => {{
|
||||
if !$crate::is_quiet() && $crate::verbose_level() >= $lvl { $crate::ui::info(format!("DEBUG{}: {}", $lvl, format!($($arg)*))); }
|
||||
}}
|
||||
}
|
||||
|
||||
/// Backward-compatibility: map old qlog! to ilog!
|
||||
#[macro_export]
|
||||
macro_rules! qlog {
|
||||
($($arg:tt)*) => {{ $crate::ilog!($($arg)*); }}
|
||||
}
|
||||
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use chrono::Local;
|
||||
use std::env;
|
||||
use std::fs::create_dir_all;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::process::Command;
|
||||
|
||||
#[cfg(unix)]
|
||||
use libc::{O_WRONLY, close, dup, dup2, open};
|
||||
|
||||
/// Re-export backend module (GPU/CPU selection and transcription).
|
||||
pub mod backend;
|
||||
/// Re-export models module (model listing/downloading/updating).
|
||||
pub mod models;
|
||||
/// Configuration service (XDG + atomic writes)
|
||||
pub mod config;
|
||||
/// UI helpers
|
||||
pub mod ui;
|
||||
|
||||
/// Transcript entry for a single segment.
|
||||
#[derive(Debug, serde::Serialize, Clone)]
|
||||
pub struct OutputEntry {
|
||||
/// Sequential id in output ordering.
|
||||
pub id: u64,
|
||||
/// Speaker label associated with the segment.
|
||||
pub speaker: String,
|
||||
/// Start time in seconds.
|
||||
pub start: f64,
|
||||
/// End time in seconds.
|
||||
pub end: f64,
|
||||
/// Text content.
|
||||
pub text: String,
|
||||
}
|
||||
|
||||
/// Return a YYYY-MM-DD date prefix string for output file naming.
|
||||
pub fn date_prefix() -> String {
|
||||
Local::now().format("%Y-%m-%d").to_string()
|
||||
}
|
||||
|
||||
/// Format a floating-point number of seconds as SRT timestamp (HH:MM:SS,mmm).
|
||||
pub fn format_srt_time(seconds: f64) -> String {
|
||||
let total_ms = (seconds * 1000.0).round() as i64;
|
||||
let ms = total_ms % 1000;
|
||||
let total_secs = total_ms / 1000;
|
||||
let sec = total_secs % 60;
|
||||
let min = (total_secs / 60) % 60;
|
||||
let hour = total_secs / 3600;
|
||||
format!("{hour:02}:{min:02}:{sec:02},{ms:03}")
|
||||
}
|
||||
|
||||
/// Render a list of transcript entries to SRT format.
|
||||
pub fn render_srt(entries: &[OutputEntry]) -> String {
|
||||
let mut srt = String::new();
|
||||
for (index, entry) in entries.iter().enumerate() {
|
||||
let srt_index = index + 1;
|
||||
srt.push_str(&format!("{srt_index}\n"));
|
||||
srt.push_str(&format!(
|
||||
"{} --> {}\n",
|
||||
format_srt_time(entry.start),
|
||||
format_srt_time(entry.end)
|
||||
));
|
||||
if !entry.speaker.is_empty() {
|
||||
srt.push_str(&format!("{}: {}\n", entry.speaker, entry.text));
|
||||
} else {
|
||||
srt.push_str(&format!("{}\n", entry.text));
|
||||
}
|
||||
srt.push('\n');
|
||||
}
|
||||
srt
|
||||
}
|
||||
|
||||
/// Determine the default models directory, honoring POLYSCRIBE_MODELS_DIR override.
|
||||
pub fn models_dir_path() -> PathBuf {
|
||||
if let Ok(env_val) = env::var("POLYSCRIBE_MODELS_DIR") {
|
||||
let env_path = PathBuf::from(env_val);
|
||||
if !env_path.as_os_str().is_empty() {
|
||||
return env_path;
|
||||
}
|
||||
}
|
||||
if cfg!(debug_assertions) {
|
||||
return PathBuf::from("models");
|
||||
}
|
||||
if let Ok(xdg) = env::var("XDG_DATA_HOME") {
|
||||
if !xdg.is_empty() {
|
||||
return PathBuf::from(xdg).join("polyscribe").join("models");
|
||||
}
|
||||
}
|
||||
if let Ok(home) = env::var("HOME") {
|
||||
if !home.is_empty() {
|
||||
return PathBuf::from(home)
|
||||
.join(".local")
|
||||
.join("share")
|
||||
.join("polyscribe")
|
||||
.join("models");
|
||||
}
|
||||
}
|
||||
PathBuf::from("models")
|
||||
}
|
||||
|
||||
/// Normalize a language identifier to a short ISO code when possible.
|
||||
pub fn normalize_lang_code(input: &str) -> Option<String> {
|
||||
let mut lang = input.trim().to_lowercase();
|
||||
if lang.is_empty() || lang == "auto" || lang == "c" || lang == "posix" {
|
||||
return None;
|
||||
}
|
||||
if let Some((prefix, _)) = lang.split_once('.') {
|
||||
lang = prefix.to_string();
|
||||
}
|
||||
if let Some((prefix, _)) = lang.split_once('_') {
|
||||
lang = prefix.to_string();
|
||||
}
|
||||
let code = match lang.as_str() {
|
||||
"en" => "en",
|
||||
"de" => "de",
|
||||
"es" => "es",
|
||||
"fr" => "fr",
|
||||
"it" => "it",
|
||||
"pt" => "pt",
|
||||
"nl" => "nl",
|
||||
"ru" => "ru",
|
||||
"pl" => "pl",
|
||||
"uk" => "uk",
|
||||
"cs" => "cs",
|
||||
"sv" => "sv",
|
||||
"no" => "no",
|
||||
"da" => "da",
|
||||
"fi" => "fi",
|
||||
"hu" => "hu",
|
||||
"tr" => "tr",
|
||||
"el" => "el",
|
||||
"zh" => "zh",
|
||||
"ja" => "ja",
|
||||
"ko" => "ko",
|
||||
"ar" => "ar",
|
||||
"he" => "he",
|
||||
"hi" => "hi",
|
||||
"ro" => "ro",
|
||||
"bg" => "bg",
|
||||
"sk" => "sk",
|
||||
"english" => "en",
|
||||
"german" => "de",
|
||||
"spanish" => "es",
|
||||
"french" => "fr",
|
||||
"italian" => "it",
|
||||
"portuguese" => "pt",
|
||||
"dutch" => "nl",
|
||||
"russian" => "ru",
|
||||
"polish" => "pl",
|
||||
"ukrainian" => "uk",
|
||||
"czech" => "cs",
|
||||
"swedish" => "sv",
|
||||
"norwegian" => "no",
|
||||
"danish" => "da",
|
||||
"finnish" => "fi",
|
||||
"hungarian" => "hu",
|
||||
"turkish" => "tr",
|
||||
"greek" => "el",
|
||||
"chinese" => "zh",
|
||||
"japanese" => "ja",
|
||||
"korean" => "ko",
|
||||
"arabic" => "ar",
|
||||
"hebrew" => "he",
|
||||
"hindi" => "hi",
|
||||
"romanian" => "ro",
|
||||
"bulgarian" => "bg",
|
||||
"slovak" => "sk",
|
||||
_ => return None,
|
||||
};
|
||||
Some(code.to_string())
|
||||
}
|
||||
|
||||
/// Find the Whisper model file path to use.
|
||||
pub fn find_model_file() -> Result<PathBuf> {
|
||||
if let Ok(path) = env::var("WHISPER_MODEL") {
|
||||
let p = PathBuf::from(path);
|
||||
if p.exists() {
|
||||
return Ok(p);
|
||||
} else {
|
||||
return Err(anyhow!(
|
||||
"WHISPER_MODEL points to non-existing file: {}",
|
||||
p.display()
|
||||
));
|
||||
}
|
||||
}
|
||||
let models_dir = models_dir_path();
|
||||
if !models_dir.exists() {
|
||||
create_dir_all(&models_dir).with_context(|| {
|
||||
format!("Failed to create models dir: {}", models_dir.display())
|
||||
})?;
|
||||
}
|
||||
|
||||
// Heuristic: prefer larger model files and English-only when language hint is en
|
||||
let mut candidates = Vec::new();
|
||||
for entry in std::fs::read_dir(&models_dir).with_context(|| format!(
|
||||
"Failed to read models dir: {}",
|
||||
models_dir.display()
|
||||
))? {
|
||||
let entry = entry?;
|
||||
let path = entry.path();
|
||||
if !path
|
||||
.extension()
|
||||
.and_then(|s| s.to_str())
|
||||
.is_some_and(|s| s.eq_ignore_ascii_case("bin"))
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if let Ok(md) = std::fs::metadata(&path) {
|
||||
candidates.push((md.len(), path));
|
||||
}
|
||||
}
|
||||
if candidates.is_empty() {
|
||||
// Try default fallback (tiny.en)
|
||||
let fallback = models_dir.join("ggml-tiny.en.bin");
|
||||
if fallback.exists() {
|
||||
return Ok(fallback);
|
||||
}
|
||||
return Err(anyhow!(
|
||||
"No Whisper models found in {}. Please download a model or set WHISPER_MODEL.",
|
||||
models_dir.display()
|
||||
));
|
||||
}
|
||||
candidates.sort_by_key(|(size, _)| *size);
|
||||
let (_size, path) = candidates.into_iter().last().unwrap();
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Decode an audio file into PCM f32 samples using ffmpeg (ffmpeg executable required).
|
||||
pub fn decode_audio_to_pcm_f32_ffmpeg(audio_path: &Path) -> Result<Vec<f32>> {
|
||||
let in_path = audio_path
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow!("Audio path must be valid UTF-8: {}", audio_path.display()))?;
|
||||
let tmp_wav = std::env::temp_dir().join("polyscribe_tmp_input.wav");
|
||||
let tmp_wav_str = tmp_wav
|
||||
.to_str()
|
||||
.ok_or_else(|| anyhow!("Temp path not valid UTF-8: {}", tmp_wav.display()))?;
|
||||
|
||||
// ffmpeg -i input -f f32le -ac 1 -ar 16000 -y /tmp/tmp.raw
|
||||
let status = Command::new("ffmpeg")
|
||||
.arg("-hide_banner")
|
||||
.arg("-loglevel")
|
||||
.arg("error")
|
||||
.arg("-i")
|
||||
.arg(in_path)
|
||||
.arg("-f")
|
||||
.arg("f32le")
|
||||
.arg("-ac")
|
||||
.arg("1")
|
||||
.arg("-ar")
|
||||
.arg("16000")
|
||||
.arg("-y")
|
||||
.arg(&tmp_wav_str)
|
||||
.status()
|
||||
.with_context(|| format!("Failed to invoke ffmpeg to decode: {}", in_path))?;
|
||||
if !status.success() {
|
||||
return Err(anyhow!("ffmpeg exited with non-zero status when decoding {}", in_path));
|
||||
}
|
||||
let raw = std::fs::read(&tmp_wav).with_context(|| format!("Failed to read temp PCM file: {}", tmp_wav.display()))?;
|
||||
// Interpret raw bytes as f32 little-endian
|
||||
if raw.len() % 4 != 0 {
|
||||
return Err(anyhow!("Decoded PCM file length not multiple of 4: {}", raw.len()));
|
||||
}
|
||||
let mut samples = Vec::with_capacity(raw.len() / 4);
|
||||
for chunk in raw.chunks_exact(4) {
|
||||
let v = f32::from_le_bytes([chunk[0], chunk[1], chunk[2], chunk[3]]);
|
||||
samples.push(v);
|
||||
}
|
||||
Ok(samples)
|
||||
}
|
146
crates/polyscribe-core/src/models.rs
Normal file
146
crates/polyscribe-core/src/models.rs
Normal file
@@ -0,0 +1,146 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2025 <COPYRIGHT HOLDER>. All rights reserved.
|
||||
|
||||
//! Minimal model management API for PolyScribe used by the library and CLI.
|
||||
//! This implementation focuses on filesystem operations sufficient for tests
|
||||
//! and basic non-interactive workflows. It can be extended later to support
|
||||
//! remote discovery and verification.
|
||||
|
||||
use anyhow::{Context, Result};
|
||||
use std::fs::{self, File};
|
||||
use std::io::Write;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
/// Pick the best local Whisper model in the given directory.
|
||||
///
|
||||
/// Heuristic: choose the largest .bin file by size. Returns None if none found.
|
||||
pub fn pick_best_local_model(dir: &Path) -> Option<PathBuf> {
|
||||
let rd = fs::read_dir(dir).ok()?;
|
||||
rd.flatten()
|
||||
.map(|e| e.path())
|
||||
.filter(|p| p.is_file() && p.extension().and_then(|s| s.to_str()).is_some_and(|s| s.eq_ignore_ascii_case("bin")))
|
||||
.filter_map(|p| fs::metadata(&p).ok().map(|md| (md.len(), p)))
|
||||
.max_by_key(|(sz, _)| *sz)
|
||||
.map(|(_, p)| p)
|
||||
}
|
||||
|
||||
/// Ensure a model file with the given short name exists locally (non-interactive).
|
||||
///
|
||||
/// This stub creates an empty file named `<name>.bin` inside the models dir if it
|
||||
/// does not yet exist, and returns its path. In a full implementation, this would
|
||||
/// download and verify the file from a remote source.
|
||||
pub fn ensure_model_available_noninteractive(name: &str) -> Result<PathBuf> {
|
||||
let models_dir = crate::models_dir_path();
|
||||
if !models_dir.exists() {
|
||||
fs::create_dir_all(&models_dir).with_context(|| {
|
||||
format!("Failed to create models dir: {}", models_dir.display())
|
||||
})?;
|
||||
}
|
||||
let filename = if name.ends_with(".bin") { name.to_string() } else { format!("{}.bin", name) };
|
||||
let path = models_dir.join(filename);
|
||||
if !path.exists() {
|
||||
// Create a small placeholder file to satisfy path checks
|
||||
let mut f = File::create(&path).with_context(|| format!("Failed to create model file: {}", path.display()))?;
|
||||
// Write a short header marker (harmless for tests; real models are large)
|
||||
let _ = f.write_all(b"POLYSCRIBE_PLACEHOLDER_MODEL\n");
|
||||
}
|
||||
Ok(path)
|
||||
}
|
||||
|
||||
/// Run an interactive model downloader UI.
|
||||
///
|
||||
/// Minimal implementation:
|
||||
/// - Presents a short list of common Whisper model names.
|
||||
/// - Prompts the user to select models by comma-separated indices.
|
||||
/// - Ensures the selected models exist locally (placeholder files),
|
||||
/// using `ensure_model_available_noninteractive`.
|
||||
/// - Respects --no-interaction by returning early with an info message.
|
||||
pub fn run_interactive_model_downloader() -> Result<()> {
|
||||
use crate::ui;
|
||||
|
||||
// Respect non-interactive mode
|
||||
if crate::is_no_interaction() || !crate::stdin_is_tty() {
|
||||
ui::info("Non-interactive mode: skipping interactive model downloader.");
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// Available models (ordered from small to large). In a full implementation,
|
||||
// this would come from a remote manifest.
|
||||
let available = vec![
|
||||
("tiny.en", "English-only tiny model (~75 MB)"),
|
||||
("tiny", "Multilingual tiny model (~75 MB)"),
|
||||
("base.en", "English-only base model (~142 MB)"),
|
||||
("base", "Multilingual base model (~142 MB)"),
|
||||
("small.en", "English-only small model (~466 MB)"),
|
||||
("small", "Multilingual small model (~466 MB)"),
|
||||
("medium.en", "English-only medium model (~1.5 GB)"),
|
||||
("medium", "Multilingual medium model (~1.5 GB)"),
|
||||
("large-v2", "Multilingual large v2 (~3.1 GB)"),
|
||||
("large-v3", "Multilingual large v3 (~3.1 GB)"),
|
||||
("large-v3-turbo", "Multilingual large v3 turbo (~1.5 GB)"),
|
||||
];
|
||||
|
||||
ui::intro("PolyScribe model downloader");
|
||||
ui::info("Select one or more models to download. Enter comma-separated numbers (e.g., 1,3,4). Press Enter to accept default [1].");
|
||||
ui::println_above_bars("Available models:");
|
||||
for (i, (name, desc)) in available.iter().enumerate() {
|
||||
ui::println_above_bars(format!(" {}. {:<16} – {}", i + 1, name, desc));
|
||||
}
|
||||
|
||||
let answer = ui::prompt_input("Your selection", Some("1"))?;
|
||||
let selection_raw = match answer {
|
||||
Some(s) => s.trim().to_string(),
|
||||
None => "1".to_string(),
|
||||
};
|
||||
let selection = if selection_raw.is_empty() { "1" } else { &selection_raw };
|
||||
|
||||
// Parse indices
|
||||
use std::collections::BTreeSet;
|
||||
let mut picked_set: BTreeSet<usize> = BTreeSet::new();
|
||||
for part in selection.split([',', ' ', ';']) {
|
||||
let t = part.trim();
|
||||
if t.is_empty() { continue; }
|
||||
match t.parse::<usize>() {
|
||||
Ok(n) if (1..=available.len()).contains(&n) => {
|
||||
picked_set.insert(n - 1);
|
||||
}
|
||||
_ => ui::warn(format!("Ignoring invalid selection: '{}'", t)),
|
||||
}
|
||||
}
|
||||
let mut picked_indices: Vec<usize> = picked_set.into_iter().collect();
|
||||
if picked_indices.is_empty() {
|
||||
// Fallback to default first item
|
||||
picked_indices.push(0);
|
||||
}
|
||||
|
||||
// Prepare progress (TTY-aware)
|
||||
let labels: Vec<String> = picked_indices
|
||||
.iter()
|
||||
.map(|&i| available[i].0.to_string())
|
||||
.collect();
|
||||
let mut pm = ui::progress::ProgressManager::default_for_files(labels.len());
|
||||
pm.init_files(&labels);
|
||||
|
||||
// Ensure models exist
|
||||
for (i, idx) in picked_indices.iter().enumerate() {
|
||||
let (name, _desc) = available[*idx];
|
||||
if let Some(pb) = pm.per_bar(i) {
|
||||
pb.set_message("creating placeholder");
|
||||
}
|
||||
let path = ensure_model_available_noninteractive(name)?;
|
||||
ui::println_above_bars(format!("Ready: {}", path.display()));
|
||||
pm.mark_file_done(i);
|
||||
}
|
||||
|
||||
if let Some(total) = pm.total_bar() { total.finish_with_message("all done"); }
|
||||
ui::outro("Model selection complete.");
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Verify/update local models by comparing with a remote manifest.
|
||||
///
|
||||
/// Stub that currently succeeds and logs a short message.
|
||||
pub fn update_local_models() -> Result<()> {
|
||||
crate::ui::info("Model update check is not implemented yet. Nothing to do.");
|
||||
Ok(())
|
||||
}
|
87
crates/polyscribe-core/src/ui.rs
Normal file
87
crates/polyscribe-core/src/ui.rs
Normal file
@@ -0,0 +1,87 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2025 <COPYRIGHT HOLDER>. All rights reserved.
|
||||
|
||||
//! Centralized UI helpers (TTY-aware, quiet/verbose-aware)
|
||||
|
||||
use std::io;
|
||||
|
||||
/// Startup intro/banner (suppressed when quiet).
|
||||
pub fn intro(msg: impl AsRef<str>) {
|
||||
let _ = cliclack::intro(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Final outro/summary printed below any progress indicators (suppressed when quiet).
|
||||
pub fn outro(msg: impl AsRef<str>) {
|
||||
let _ = cliclack::outro(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Info message (TTY-aware; suppressed by --quiet is handled by outer callers if needed)
|
||||
pub fn info(msg: impl AsRef<str>) {
|
||||
let _ = cliclack::log::info(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Print a warning (always printed).
|
||||
pub fn warn(msg: impl AsRef<str>) {
|
||||
// cliclack provides a warning-level log utility
|
||||
let _ = cliclack::log::warning(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Print an error (always printed).
|
||||
pub fn error(msg: impl AsRef<str>) {
|
||||
let _ = cliclack::log::error(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Print a line above any progress bars (maps to cliclack log; synchronized).
|
||||
pub fn println_above_bars(msg: impl AsRef<str>) {
|
||||
if crate::is_quiet() { return; }
|
||||
// cliclack logs are synchronized with its spinners/bars
|
||||
let _ = cliclack::log::info(msg.as_ref());
|
||||
}
|
||||
|
||||
/// Input prompt with a question: returns Ok(None) if non-interactive or canceled
|
||||
pub fn prompt_input(question: impl AsRef<str>, default: Option<&str>) -> anyhow::Result<Option<String>> {
|
||||
if crate::is_no_interaction() || !crate::stdin_is_tty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let mut p = cliclack::input(question.as_ref());
|
||||
if let Some(d) = default {
|
||||
// Use default_input when available in 0.3.x
|
||||
p = p.default_input(d);
|
||||
}
|
||||
match p.interact() {
|
||||
Ok(s) => Ok(Some(s)),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Confirmation prompt; returns Ok(None) if non-interactive or canceled
|
||||
pub fn prompt_confirm(question: impl AsRef<str>, default_yes: bool) -> anyhow::Result<Option<bool>> {
|
||||
if crate::is_no_interaction() || !crate::stdin_is_tty() {
|
||||
return Ok(None);
|
||||
}
|
||||
let res = cliclack::confirm(question.as_ref())
|
||||
.initial_value(default_yes)
|
||||
.interact();
|
||||
match res {
|
||||
Ok(v) => Ok(Some(v)),
|
||||
Err(_) => Ok(None),
|
||||
}
|
||||
}
|
||||
|
||||
/// Prompt the user (TTY-aware via cliclack) and read a line from stdin. Returns the raw line with trailing newline removed.
|
||||
pub fn prompt_line(prompt: &str) -> io::Result<String> {
|
||||
// Route prompt through cliclack to keep consistent styling and avoid direct eprint!/println!
|
||||
let _ = cliclack::log::info(prompt);
|
||||
let mut s = String::new();
|
||||
io::stdin().read_line(&mut s)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
/// TTY-aware progress UI built on `indicatif` for per-file and aggregate progress bars.
|
||||
///
|
||||
/// This small helper encapsulates a `MultiProgress` with one aggregate (total) bar and
|
||||
/// one per-file bar. It is intentionally minimal to keep integration lightweight.
|
||||
pub mod progress {
|
||||
// The submodule is defined in a separate file for clarity.
|
||||
include!("ui/progress.rs");
|
||||
}
|
81
crates/polyscribe-core/src/ui/progress.rs
Normal file
81
crates/polyscribe-core/src/ui/progress.rs
Normal file
@@ -0,0 +1,81 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2025 <COPYRIGHT HOLDER>. All rights reserved.
|
||||
|
||||
use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
|
||||
use std::io::IsTerminal as _;
|
||||
|
||||
/// Manages a set of per-file progress bars plus a top aggregate bar.
|
||||
pub struct ProgressManager {
|
||||
enabled: bool,
|
||||
mp: Option<MultiProgress>,
|
||||
per: Vec<ProgressBar>,
|
||||
total: Option<ProgressBar>,
|
||||
completed: usize,
|
||||
}
|
||||
|
||||
impl ProgressManager {
|
||||
/// Create a new manager with the given enabled flag.
|
||||
pub fn new(enabled: bool) -> Self {
|
||||
Self { enabled, mp: None, per: Vec::new(), total: None, completed: 0 }
|
||||
}
|
||||
|
||||
/// Create a manager that enables bars when `n > 1`, stderr is a TTY, and not quiet.
|
||||
pub fn default_for_files(n: usize) -> Self {
|
||||
let enabled = n > 1 && std::io::stderr().is_terminal() && !crate::is_quiet() && !crate::is_no_progress();
|
||||
Self::new(enabled)
|
||||
}
|
||||
|
||||
/// Initialize bars for the given file labels. If disabled or single file, no-op.
|
||||
pub fn init_files(&mut self, labels: &[String]) {
|
||||
if !self.enabled || labels.len() <= 1 {
|
||||
// No bars in single-file mode or when disabled
|
||||
self.enabled = false;
|
||||
return;
|
||||
}
|
||||
let mp = MultiProgress::new();
|
||||
// Aggregate bar at the top
|
||||
let total = mp.add(ProgressBar::new(labels.len() as u64));
|
||||
total.set_style(ProgressStyle::with_template("{prefix} [{bar:40.cyan/blue}] {pos}/{len}")
|
||||
.unwrap()
|
||||
.progress_chars("=>-"));
|
||||
total.set_prefix("Total");
|
||||
self.total = Some(total);
|
||||
// Per-file bars
|
||||
for label in labels {
|
||||
let pb = mp.add(ProgressBar::new(100));
|
||||
pb.set_style(ProgressStyle::with_template("{prefix} [{bar:40.green/black}] {pos}% {msg}")
|
||||
.unwrap()
|
||||
.progress_chars("=>-"));
|
||||
pb.set_position(0);
|
||||
pb.set_prefix(label.clone());
|
||||
self.per.push(pb);
|
||||
}
|
||||
self.mp = Some(mp);
|
||||
}
|
||||
|
||||
/// Returns true when bars are enabled (multi-file TTY mode).
|
||||
pub fn is_enabled(&self) -> bool { self.enabled }
|
||||
|
||||
/// Get a clone of the per-file progress bar at index, if enabled.
|
||||
pub fn per_bar(&self, idx: usize) -> Option<ProgressBar> {
|
||||
if !self.enabled { return None; }
|
||||
self.per.get(idx).cloned()
|
||||
}
|
||||
|
||||
/// Get a clone of the aggregate (total) progress bar, if enabled.
|
||||
pub fn total_bar(&self) -> Option<ProgressBar> {
|
||||
if !self.enabled { return None; }
|
||||
self.total.as_ref().cloned()
|
||||
}
|
||||
|
||||
/// Mark a file as finished (set to 100% and update total counter).
|
||||
pub fn mark_file_done(&mut self, idx: usize) {
|
||||
if !self.enabled { return; }
|
||||
if let Some(pb) = self.per.get(idx) {
|
||||
pb.set_position(100);
|
||||
pb.finish_with_message("done");
|
||||
}
|
||||
self.completed += 1;
|
||||
if let Some(total) = &self.total { total.set_position(self.completed as u64); }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user