use hf_hub::api::sync::{Api, ApiRepo};
use hf_hub::{Repo, RepoType};
use crate::config::TokenizerConfig;
use crate::error::Error;
const CHAT_TEMPLATE_FILE: &str = "chat_template.jinja";
pub(crate) fn fetch_template_material(
repo_id: &str,
revision: Option<&str>,
) -> Result<(TokenizerConfig, Option<String>), Error> {
let api = Api::new().map_err(|e| Error::Hub(e.to_string()))?;
let repo = repo_handle(&api, repo_id, revision);
let info = repo.info().map_err(|e| Error::Hub(e.to_string()))?;
let has_standalone = info
.siblings
.iter()
.any(|s| s.rfilename == CHAT_TEMPLATE_FILE);
let config = {
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()))?
};
let standalone = if has_standalone {
let path = repo
.get(CHAT_TEMPLATE_FILE)
.map_err(|e| Error::Hub(e.to_string()))?;
Some(std::fs::read_to_string(&path).map_err(|e| Error::Hub(e.to_string()))?)
} else {
None
};
Ok((config, standalone))
}
fn repo_handle(api: &Api, repo_id: &str, revision: Option<&str>) -> ApiRepo {
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()),
}
}