Skip to content

Commit 3a76240

Browse files
authored
Read Hugging Face Hub token from cache if not provided (#814)
1 parent d5ab32e commit 3a76240

4 files changed

Lines changed: 19 additions & 8 deletions

File tree

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ Options:
249249
[env: DENSE_PATH=]
250250

251251
--hf-token <HF_TOKEN>
252-
Your Hugging Face Hub token
252+
Your Hugging Face Hub token. If neither `--hf-token` nor `HF_TOKEN` is set, the token will be read from the `$HF_HOME/token` path, if it exists. This ensures access to private or gated models, and allows for a more permissive rate limiting
253253

254254
[env: HF_TOKEN=]
255255

@@ -297,6 +297,8 @@ Options:
297297
[env: JSON_OUTPUT=]
298298

299299
--disable-spans
300+
Whether or not to include the log trace through spans
301+
300302
[env: DISABLE_SPANS=]
301303

302304
--otlp-endpoint <OTLP_ENDPOINT>

docs/source/en/cli_arguments.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ Options:
134134
[env: DENSE_PATH=]
135135

136136
--hf-token <HF_TOKEN>
137-
Your Hugging Face Hub token
137+
Your Hugging Face Hub token. If neither `--hf-token` nor `HF_TOKEN` is set, the token will be read from the `$HF_HOME/token` path, if it exists. This ensures access to private or gated models, and allows for a more permissive rate limiting
138138

139139
[env: HF_TOKEN=]
140140

@@ -182,6 +182,8 @@ Options:
182182
[env: JSON_OUTPUT=]
183183

184184
--disable-spans
185+
Whether or not to include the log trace through spans
186+
185187
[env: DISABLE_SPANS=]
186188

187189
--otlp-endpoint <OTLP_ENDPOINT>

router/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,18 @@ pub async fn run(
7373
// Using a local model
7474
(model_id_path.to_path_buf(), None)
7575
} else {
76-
let mut builder = ApiBuilder::from_env()
77-
.with_progress(false)
78-
.with_token(hf_token);
76+
let mut builder = ApiBuilder::from_env().with_progress(false);
7977

8078
if let Some(cache_dir) = huggingface_hub_cache {
8179
builder = builder.with_cache_dir(cache_dir.into());
8280
}
8381

82+
// NOTE: Only set the `token` if it's not None, otherwise leave it as default so that the
83+
// token from the cache location is pulled instead, if exists
84+
if hf_token.is_some() {
85+
builder = builder.with_token(hf_token);
86+
}
87+
8488
if let Ok(origin) = std::env::var("HF_HUB_USER_AGENT_ORIGIN") {
8589
builder = builder.with_user_agent("origin", origin.as_str());
8690
}

router/src/main.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use anyhow::Result;
22
use clap::Parser;
33
use opentelemetry::global;
4-
use text_embeddings_backend::DType;
54
use veil::Redact;
65

6+
use text_embeddings_backend::DType;
7+
78
#[cfg(not(target_os = "linux"))]
89
#[global_allocator]
910
static GLOBAL: mimalloc::MiMalloc = mimalloc::MiMalloc;
@@ -132,7 +133,9 @@ struct Args {
132133
#[redact(partial)]
133134
hf_api_token: Option<String>,
134135

135-
/// Your Hugging Face Hub token
136+
/// Your Hugging Face Hub token. If neither `--hf-token` nor `HF_TOKEN` is set, the token
137+
/// will be read from the `$HF_HOME/token` path, if it exists. This ensures access to private
138+
/// or gated models, and allows for a more permissive rate limiting.
136139
#[clap(long, env, conflicts_with = "hf_api_token")]
137140
#[redact(partial)]
138141
hf_token: Option<String>,
@@ -171,7 +174,7 @@ struct Args {
171174
#[clap(long, env)]
172175
json_output: bool,
173176

174-
// Whether or not to include the log trace through spans
177+
/// Whether or not to include the log trace through spans
175178
#[clap(long, env)]
176179
disable_spans: bool,
177180

0 commit comments

Comments
 (0)