Spaces:
Sleeping
Sleeping
File size: 5,011 Bytes
3569cbd 9ce120f 3569cbd 2bb7b57 b5d8a79 65f3e54 ee7230e 3569cbd a4dee07 819a8c6 b5d8a79 819a8c6 b5d8a79 819a8c6 b0c3f88 819a8c6 b5d8a79 a4dee07 ee7230e 539e871 ee7230e b5d8a79 ee7230e b5d8a79 ee7230e b5d8a79 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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
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: usize,
pub(crate) length_ms: usize,
pub(crate) keep_ms: usize,
pub(crate) model: String,
pub(crate) max_prompt_tokens: usize,
pub(crate) context_confidence_threshold: f32,
}
#[allow(dead_code)]
#[derive(Debug, Deserialize, Clone)]
pub(crate) struct WhisperParams {
pub(crate) n_threads: Option<usize>,
pub(crate) max_tokens: Option<u32>,
pub(crate) audio_ctx: Option<u32>,
pub(crate) speed_up: Option<bool>,
pub(crate) translate: Option<bool>,
pub(crate) no_context: Option<bool>,
pub(crate) print_special: Option<bool>,
pub(crate) print_realtime: Option<bool>,
pub(crate) print_progress: Option<bool>,
pub(crate) token_timestamps: Option<bool>,
pub(crate) no_timestamps: Option<bool>,
pub(crate) temperature_inc: Option<f32>,
pub(crate) entropy_threshold: Option<f32>,
pub(crate) single_segment: Option<bool>,
pub(crate) suppress_non_speech_tokens: Option<bool>,
pub(crate) n_max_text_ctx: Option<usize>,
// 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());
if let Some(print_progress) = self.print_progress.as_ref() {
param.set_print_progress(*print_progress);
}
if let Some(print_special) = self.print_special.as_ref() {
param.set_print_special(*print_special);
}
if let Some(print_realtime) = self.print_realtime.as_ref() {
param.set_print_realtime(*print_realtime);
}
if let Some(single_segment) = self.single_segment.as_ref() {
param.set_single_segment(*single_segment);
}
if let Some(no_timestamps) = self.no_timestamps.as_ref() {
param.set_print_timestamps(!no_timestamps);
}
if let Some(token_timestamps) = self.token_timestamps.as_ref() {
param.set_token_timestamps(*token_timestamps);
}
if let Some(translate) = self.translate.as_ref() {
param.set_translate(*translate);
}
if let Some(max_tokens) = self.max_tokens.as_ref() {
param.set_max_tokens(*max_tokens as i32);
}
param.set_language(self.language.as_deref());
if let Some(n_threads) = self.n_threads.as_ref() {
param.set_n_threads(*n_threads as i32);
}
if let Some(audio_ctx) = self.audio_ctx.as_ref() {
param.set_audio_ctx(*audio_ctx as i32);
}
if let Some(speed_up) = self.speed_up.as_ref() {
param.set_speed_up(*speed_up);
}
// param.set_tdrz_enable(self.tinydiarize);
if let Some(temperature_inc) = self.temperature_inc.as_ref() {
param.set_temperature_inc(*temperature_inc);
}
if let Some(suppress_non_speech_tokens) = self.suppress_non_speech_tokens.as_ref() {
param.set_suppress_non_speech_tokens(*suppress_non_speech_tokens);
}
if let Some(no_context) = self.no_context.as_ref() {
param.set_no_context(*no_context);
}
if let Some(entropy_threshold) = self.entropy_threshold.as_ref() {
param.set_entropy_thold(*entropy_threshold);
}
if let Some(n_max_text_ctx) = self.n_max_text_ctx.as_ref() {
param.set_n_max_text_ctx(*n_max_text_ctx as i32);
}
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);
}
}
|