Spaces:
Sleeping
Sleeping
File size: 3,278 Bytes
3569cbd 9ce120f 3569cbd 2bb7b57 ee7230e 65f3e54 ee7230e 3569cbd a4dee07 819a8c6 5cb1ef7 a4dee07 819a8c6 b0c3f88 819a8c6 a4dee07 ee7230e 819a8c6 ee7230e 819a8c6 a4dee07 ee7230e 539e871 ee7230e 819a8c6 ee7230e 819a8c6 ee7230e 819a8c6 a4dee07 3569cbd ee7230e 2bb7b57 ee7230e a4dee07 ee7230e 819a8c6 539e871 ee7230e 9ce120f 819a8c6 ee7230e 9ce120f 65f3e54 9ce120f 5b9ecd0 9ce120f 5b9ecd0 2bb7b57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
use std::{env, ffi::c_int, net::IpAddr};
use config::{Config, Environment, File};
use once_cell::sync::Lazy;
use serde::Deserialize;
use whisper_rs::FullParams;
use tracing::debug;
pub(crate) static SETTINGS: Lazy<Settings> =
Lazy::new(|| Settings::new().expect("Failed to initialize settings"));
#[derive(Debug, Deserialize, Clone)]
pub(crate) struct WhisperConfig {
pub(crate) params: WhisperParams,
pub(crate) step_ms: u32,
pub(crate) length_ms: u32,
pub(crate) keep_ms: u32,
pub(crate) model: String,
pub(crate) max_prompt_tokens: usize,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize, Clone)]
pub(crate) struct WhisperParams {
pub(crate) n_threads: Option<usize>,
pub(crate) max_tokens: u32,
pub(crate) audio_ctx: u32,
pub(crate) speed_up: bool,
pub(crate) translate: bool,
pub(crate) no_fallback: bool,
pub(crate) print_special: bool,
pub(crate) print_realtime: bool,
pub(crate) print_progress: bool,
pub(crate) no_timestamps: bool,
pub(crate) temperature_inc: f32,
pub(crate) single_segment: bool,
// pub(crate) tinydiarize: bool,
pub(crate) language: Option<String>,
}
impl WhisperParams {
pub(crate) fn to_full_params<'a, 'b>(&'a self, tokens: &'b [c_int]) -> FullParams<'a, 'b> {
let mut param = FullParams::new(Default::default());
param.set_print_progress(self.print_progress);
param.set_print_special(self.print_special);
param.set_print_realtime(self.print_realtime);
param.set_print_timestamps(!self.no_timestamps);
param.set_translate(self.translate);
param.set_single_segment(false);
param.set_max_tokens(self.max_tokens as i32);
let lang = self.language.as_deref();
param.set_language(lang);
let num_cpus = std::thread::available_parallelism()
.map(|c| c.get())
.unwrap_or(4);
param.set_n_threads(self.n_threads.unwrap_or(num_cpus) as c_int);
param.set_audio_ctx(self.audio_ctx as i32);
param.set_speed_up(self.speed_up);
// param.set_tdrz_enable(self.tinydiarize);
param.set_temperature_inc(self.temperature_inc);
param.set_tokens(tokens);
param
}
}
#[derive(Debug, Deserialize)]
pub(crate) struct Server {
pub(crate) port: u16,
pub(crate) host: IpAddr,
}
#[derive(Debug, Deserialize)]
pub struct Settings {
pub(crate) whisper: WhisperConfig,
pub(crate) server: Server,
}
impl Settings {
pub(crate) fn new() -> Result<Self, anyhow::Error> {
let run_mode = env::var("APP_RUN_MODE").unwrap_or("dev".into());
let config = Config::builder()
.add_source(File::with_name(&format!("config/{run_mode}.yaml")).required(false))
.add_source(Environment::with_prefix("APP").separator("-"))
.build()
.map_err(anyhow::Error::from)?;
config.try_deserialize::<Self>().map_err(Into::into)
.map(|settings| {
debug!("Settings: {settings:?}");
settings
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn load_dev_settings_should_success() {
let settings = Settings::new().unwrap();
println!("{:?}", settings);
}
}
|