Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 5 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@ categories = ["science", "text-processing"]
exclude = ["tests/*"]

[features]
default = ["onig"]
default = ["onig", "hf-hub"]
hf-hub = ["dep:hf-hub"]
onig = ["tokenizers/onig",
"tokenizers/progressbar",
"tokenizers/esaxx_fast",
"tokenizers/http"]
"tokenizers/esaxx_fast"]

fancy-regex = ["tokenizers/fancy-regex",
"tokenizers/progressbar",
"tokenizers/esaxx_fast",
"tokenizers/http"]
"tokenizers/esaxx_fast"]

[dependencies]
tokenizers = { version = "0.21", default-features = false }
safetensors = "0.5"
ndarray = "0.15"
hf-hub = { version = "0.4", default-features = false, features = ["ureq"] }
hf-hub = { version = "0.4", default-features = false, features = ["ureq"], optional = true }
clap = { version = "4.0", features = ["derive"] }
anyhow = "1.0"
serde_json = "1.0"
Expand Down
58 changes: 46 additions & 12 deletions src/model.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
use anyhow::{anyhow, Context, Result};
use half::f16;
#[cfg(feature = "hf-hub")]
use hf_hub::api::sync::Api;
use ndarray::{Array2, ArrayView2, CowArray, Ix2};
use safetensors::{tensor::Dtype, SafeTensors};
use serde_json::Value;
use std::borrow::Cow;
use std::{env, fs, path::Path};
#[cfg(feature = "hf-hub")]
use std::env;
use std::{fs, path::Path};
use tokenizers::Tokenizer;

/// Static embedding model for Model2Vec
Expand Down Expand Up @@ -379,9 +382,8 @@ fn resolve_model_files<P: AsRef<Path>>(
token: Option<&str>,
subfolder: Option<&str>,
) -> Result<ModelFiles> {
if let Some(tok) = token {
env::set_var("HF_HUB_TOKEN", tok);
}
#[cfg(not(feature = "hf-hub"))]
let _ = token;

let (tokenizer, model, config) = {
let base = repo_or_path.as_ref();
Expand All @@ -395,14 +397,17 @@ fn resolve_model_files<P: AsRef<Path>>(
}
(tokenizer, model, config)
} else {
let api = Api::new().context("hf-hub API init failed")?;
let repo = api.model(repo_or_path.as_ref().to_string_lossy().into_owned());
let prefix = subfolder.map(|s| format!("{s}/")).unwrap_or_default();
(
repo.get(&format!("{prefix}tokenizer.json"))?,
repo.get(&format!("{prefix}model.safetensors"))?,
repo.get(&format!("{prefix}config.json"))?,
)
#[cfg(feature = "hf-hub")]
{
let files = download_model_files(repo_or_path.as_ref().to_string_lossy().as_ref(), token, subfolder)?;
(files.tokenizer, files.model, files.config)
}
#[cfg(not(feature = "hf-hub"))]
{
return Err(anyhow!(
"remote model downloads require the `hf-hub` feature; pass a local model directory instead"
));
Comment on lines +405 to +409
Copy link

Copilot AI Apr 1, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new #[cfg(not(feature = "hf-hub"))] remote-path error is a user-facing behavior change, but there doesn't appear to be a test asserting this failure mode/message when building without hf-hub. Add an integration test that runs under --no-default-features and verifies from_pretrained("some/repo", ..) returns this error so the gating behavior stays stable.

Copilot uses AI. Check for mistakes.
}
}
};

Expand All @@ -412,3 +417,32 @@ fn resolve_model_files<P: AsRef<Path>>(
config,
})
}

#[cfg(feature = "hf-hub")]
fn download_model_files(repo_id: &str, token: Option<&str>, subfolder: Option<&str>) -> Result<ModelFiles> {
let previous = token.and_then(|_| env::var_os("HF_HUB_TOKEN"));
if let Some(tok) = token {
env::set_var("HF_HUB_TOKEN", tok);
}

let result = (|| {
let api = Api::new().context("hf-hub API init failed")?;
let repo = api.model(repo_id.to_owned());
let prefix = subfolder.map(|s| format!("{s}/")).unwrap_or_default();
Ok(ModelFiles {
tokenizer: repo.get(&format!("{prefix}tokenizer.json"))?,
model: repo.get(&format!("{prefix}model.safetensors"))?,
config: repo.get(&format!("{prefix}config.json"))?,
})
})();

if token.is_some() {
if let Some(value) = previous {
env::set_var("HF_HUB_TOKEN", value);
} else {
env::remove_var("HF_HUB_TOKEN");
}
}

result
}
10 changes: 10 additions & 0 deletions tests/test_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,13 @@ fn test_from_bytes_matches_from_pretrained_for_local_model() {
);
}
}

#[cfg(not(feature = "hf-hub"))]
#[test]
fn test_from_pretrained_remote_requires_hf_hub_feature() {
let err = StaticModel::from_pretrained("minishlab/potion-base-2M", None, None, None).unwrap_err();
assert!(
err.to_string().contains("hf-hub"),
"expected remote loading without hf-hub to mention the missing feature"
);
}
Loading