Skip to content

Commit 407e81f

Browse files
committed
feat: load from env
1 parent b6d0793 commit 407e81f

File tree

5 files changed

+29
-7
lines changed

5 files changed

+29
-7
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use std::path::PathBuf;
2+
3+
use anyhow::Result;
4+
5+
pub fn create_hf_api() -> Result<hf_hub::api::sync::Api> {
6+
let mut builder = hf_hub::api::sync::ApiBuilder::new();
7+
8+
// Read HF_MIRROR environment variable for endpoint
9+
if let Ok(mirror_url) = std::env::var("HF_MIRROR") {
10+
builder = builder.with_endpoint(mirror_url);
11+
}
12+
13+
// Read HF_HOME environment variable for cache directory
14+
if let Ok(cache_dir) = std::env::var("HF_HOME") {
15+
builder = builder.with_cache_dir(PathBuf::from(cache_dir));
16+
}
17+
18+
Ok(builder.build()?)
19+
}

apps/whisper-api/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ mod audio_manager;
2323
mod router;
2424
mod vad;
2525
mod whisper;
26+
mod huggingface;
27+
2628

2729
// Application state with dynamic model loading
2830
struct AppState {

apps/whisper-api/src/vad.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use anyhow::Result;
44
use candle_core::{DType, Device, Tensor};
55
use candle_onnx::simple_eval;
66

7+
use crate::huggingface;
8+
79
pub struct VADProcessor {
810
model: candle_onnx::onnx::ModelProto,
911
frame_size: usize,
@@ -20,9 +22,7 @@ impl VADProcessor {
2022
device: Device,
2123
threshold: f32,
2224
) -> Result<Self> {
23-
let api = hf_hub::api::sync::ApiBuilder::new()
24-
.with_endpoint("https://hf-mirror.com".to_string())
25-
.build()?;
25+
let api = huggingface::create_hf_api()?;
2626
let model_path = api
2727
.model("onnx-community/silero-vad".into())
2828
.get("onnx/model.onnx")?;

apps/whisper-api/src/whisper.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@ use candle_core::{Device, IndexOp, Tensor};
44
use candle_nn::VarBuilder;
55
use candle_transformers::models::whisper::{self as whisper_model, Config, audio};
66
use clap::ValueEnum;
7-
use hf_hub::{Repo, RepoType, api::sync::Api};
7+
use hf_hub::{Repo, RepoType};
88
use tokenizers::Tokenizer;
99

10+
use crate::huggingface;
11+
1012
pub enum WhisperModel {
1113
Normal(whisper_model::model::Whisper),
1214
}
@@ -102,9 +104,7 @@ impl WhisperProcessor {
102104
device: Device,
103105
) -> Result<Self> {
104106
// Load the Whisper model based on the provided model type
105-
let api = hf_hub::api::sync::ApiBuilder::new()
106-
.with_endpoint("https://hf-mirror.com".to_string())
107-
.build()?;
107+
let api = huggingface::create_hf_api()?;
108108
let (model_id, revision) = model.model_and_revision();
109109
let repo = api.repo(Repo::with_revision(model_id.to_string(), RepoType::Model, revision.to_string()));
110110

cspell.config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ words:
1111
- dtolnay
1212
- DTYPE
1313
- gguf
14+
- huggingface
1415
- logits
1516
- logprob
1617
- melfilters

0 commit comments

Comments
 (0)