use std::path::{Path, PathBuf};
use anyhow::Context;
use hf_hub::api::tokio::ApiBuilder;
const HF_TOKEN_ENV: &str = "HF_TOKEN";
pub async fn resolve_model_config_dir(source: &str) -> anyhow::Result<PathBuf> {
let path = Path::new(source);
if path.join("config.json").exists() {
return Ok(path.to_path_buf());
}
let mut builder = ApiBuilder::from_env().with_progress(false);
if let Ok(token) = std::env::var(HF_TOKEN_ENV) {
if !token.is_empty() {
builder = builder.with_token(Some(token));
}
}
let api = builder
.build()
.context("Failed to build HuggingFace API client")?;
let repo = api.model(source.to_string());
let config_path = repo
.get("config.json")
.await
.with_context(|| format!("Failed to download config.json for model '{source}'"))?;
let _ = repo.get("preprocessor_config.json").await;
config_path
.parent()
.map(|p| p.to_path_buf())
.ok_or_else(|| anyhow::anyhow!("Invalid HF cache path for model '{source}'"))
}