use dirs::home_dir;
pub fn get_token() -> anyhow::Result<String> {
if let Ok(token) = std::env::var("HF_TOKEN") {
return Ok(token);
};
if let Some(home) = home_dir() {
let file = home.join(".cache/huggingface/token");
if file.exists() {
if let Ok(content) = std::fs::read_to_string(file) {
if !content.is_empty() {
return Ok(content);
}
}
}
};
anyhow::bail!(
"The Hugging Face authentication token is not set, and is required to send requests to the Hugging Face Hub API, so please make sure that you either provide the token via environment variable as `HF_TOKEN` or run `huggingface-cli login` to authenticate yourself."
)
}