MCPcopy Create free account
hub / github.com/evilsocket/cake / find_cached_model

Function find_cached_model

cake-core/src/utils/hf.rs:22–83  ·  view source on GitHub ↗

Check the HuggingFace cache for an already-downloaded complete model. Returns the snapshot directory path if found with all shards present.

(repo_id: &str)

Source from the content-addressed store, hash-verified

20/// Check the HuggingFace cache for an already-downloaded complete model.
21/// Returns the snapshot directory path if found with all shards present.
22fn find_cached_model(repo_id: &str) -> Option<PathBuf> {
23 let hf_cache = hf_cache_dir()?;
24
25 // HF cache dirs look like "models--org--model-name"
26 let cache_dir_name = format!("models--{}", repo_id.replace('/', "--"));
27 let model_dir = hf_cache.join(&cache_dir_name);
28 let snapshots_dir = model_dir.join("snapshots");
29
30 if !snapshots_dir.exists() {
31 return None;
32 }
33
34 // Check each snapshot (usually just one, pick the newest)
35 let mut best: Option<(PathBuf, std::time::SystemTime)> = None;
36
37 for entry in std::fs::read_dir(&snapshots_dir).ok()?.flatten() {
38 let snap_path = entry.path();
39 if !snap_path.is_dir() {
40 continue;
41 }
42
43 // Must have config.json
44 if !snap_path.join("config.json").exists() {
45 continue;
46 }
47
48 // Check model completeness
49 let is_complete = if snap_path.join("model.safetensors").exists() {
50 true
51 } else if let Ok(index_data) =
52 std::fs::read_to_string(snap_path.join("model.safetensors.index.json"))
53 {
54 if let Ok(index_json) = serde_json::from_str::<serde_json::Value>(&index_data) {
55 if let Some(weight_map) = index_json.get("weight_map").and_then(|v| v.as_object())
56 {
57 let expected: HashSet<&str> =
58 weight_map.values().filter_map(|v| v.as_str()).collect();
59 expected.iter().all(|f| snap_path.join(f).exists())
60 } else {
61 false
62 }
63 } else {
64 false
65 }
66 } else {
67 false
68 };
69
70 if is_complete {
71 let mtime = entry
72 .metadata()
73 .ok()
74 .and_then(|m| m.modified().ok())
75 .unwrap_or(std::time::SystemTime::UNIX_EPOCH);
76 if best.as_ref().is_none_or(|(_, t)| mtime > *t) {
77 best = Some((snap_path, mtime));
78 }
79 }

Calls 2

hf_cache_dirFunction · 0.70
getMethod · 0.45