use hf_hub::api::sync::Api;
use hf_hub::{Repo, RepoType};
use crate::config::TokenizerConfig;
use crate::error::Error;
pub(crate) fn fetch_config(
repo_id: &str,
revision: Option<&str>,
) -> Result<TokenizerConfig, Error> {
let api = Api::new().map_err(|e| Error::Hub(e.to_string()))?;
let repo = match revision {
Some(rev) => api.repo(Repo::with_revision(
repo_id.to_owned(),
RepoType::Model,
rev.to_owned(),
)),
None => api.model(repo_id.to_owned()),
};
let path = repo
.get("tokenizer_config.json")
.map_err(|e| Error::Hub(e.to_string()))?;
let bytes = std::fs::read(&path).map_err(|e| Error::Hub(e.to_string()))?;
serde_json::from_slice(&bytes).map_err(|e| Error::Config(e.to_string()))
}