use std::path::{Path, PathBuf};
pub const SAMPLE_IMAGE_REL: &str = "fixtures/sample.jpg";
pub fn crate_root() -> PathBuf {
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
}
pub fn sample_image_path() -> PathBuf {
crate_root().join(SAMPLE_IMAGE_REL)
}
pub fn probe_image_path() -> PathBuf {
std::env::var("RLX_LOCATEANYTHING_IMAGE")
.map(PathBuf::from)
.unwrap_or_else(|_| sample_image_path())
}
pub fn require_probe_image() -> Option<PathBuf> {
let path = probe_image_path();
path.is_file().then_some(path)
}
pub fn model_dir_has_tokenizer(dir: &Path) -> bool {
dir.join("tokenizer.json").is_file()
|| (dir.join("vocab.json").is_file() && dir.join("merges.txt").is_file())
}
pub fn require_model_dir() -> Option<PathBuf> {
crate::hub::default_model_dir()
.ok()
.filter(|dir| model_dir_has_tokenizer(dir))
}
pub fn resolve_model_dir_path(raw: &str) -> Option<PathBuf> {
let path = PathBuf::from(raw);
if path.join("config.json").is_file() {
return Some(path);
}
let rooted = crate_root().join("../../").join(raw);
if rooted.join("config.json").is_file() {
return Some(rooted.canonicalize().unwrap_or(rooted));
}
None
}
pub fn resolve_image_path(explicit: Option<impl AsRef<Path>>) -> PathBuf {
explicit
.map(|p| p.as_ref().to_path_buf())
.unwrap_or_else(probe_image_path)
}