Skip to main content

weirdnet_model/
catalog.rs

1//! Which model files Weirdnet knows how to fetch.
2
3use serde::{Deserialize, Serialize};
4
5/// A downloadable GGUF.
6#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
7pub struct ModelSpec {
8    /// Short id, such as `gemma-4-e4b`.
9    pub id: String,
10    pub label: String,
11    /// Hugging Face repo (`owner/name`).
12    pub repo: String,
13    pub file: String,
14    pub size: u64,
15    /// Lower-case hex SHA-256 of the file, when known.
16    pub sha256: Option<String>,
17}
18
19/// Device class, which picks the default model size.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum Device {
22    Desktop,
23    Phone,
24}
25
26pub const GEMMA_4_E2B: &str = "gemma-4-e2b";
27pub const GEMMA_4_E4B: &str = "gemma-4-e4b";
28
29/// Pinned files: ggml-org's Q4_0 conversions, sizes and SHA-256 from the Hugging Face API (2026-11-18).
30pub fn catalog() -> Vec<ModelSpec> {
31    vec![
32        ModelSpec {
33            id: GEMMA_4_E2B.into(),
34            label: "Gemma 4 E2B instruct (Q4_0)".into(),
35            repo: "ggml-org/gemma-4-E2B-it-GGUF".into(),
36            file: "gemma-4-E2B-it-Q4_0.gguf".into(),
37            size: 2_841_481_184,
38            sha256: Some("8e30dff3ac4c8434c49a7036fa15564bdbb6044e42bf04550bf1a096ad7e6a52".into()),
39        },
40        ModelSpec {
41            id: GEMMA_4_E4B.into(),
42            label: "Gemma 4 E4B instruct (Q4_0)".into(),
43            repo: "ggml-org/gemma-4-E4B-it-GGUF".into(),
44            file: "gemma-4-E4B-it-Q4_0.gguf".into(),
45            size: 4_590_807_392,
46            sha256: Some("a555b900214b477d8880e7832e0b8925e139b0159640036b09fe472b6f2097f2".into()),
47        },
48    ]
49}
50
51/// The catalog entry with this id.
52pub fn find(id: &str) -> Option<ModelSpec> {
53    catalog().into_iter().find(|m| m.id == id)
54}
55
56/// E4B on desktop, E2B on phones.
57pub fn default_for(device: Device) -> ModelSpec {
58    find(match device {
59        Device::Desktop => GEMMA_4_E4B,
60        Device::Phone => GEMMA_4_E2B,
61    })
62    .expect("the default models are in the catalog")
63}
64
65impl ModelSpec {
66    /// Where the file is downloaded from, on the Hub at `hub`.
67    pub fn url(&self, hub: &str) -> String {
68        format!("{}/{}/resolve/main/{}", hub.trim_end_matches('/'), self.repo, self.file)
69    }
70
71    /// What a consent prompt shows.
72    pub fn consent_text(&self, hub: &str) -> Vec<String> {
73        vec![
74            format!("Download {} ({}, {} bytes)", self.label, human(self.size), self.size),
75            format!("from {}/{}", hub.trim_end_matches('/'), self.repo),
76            "Nothing is downloaded without your OK; the file is checked (SHA-256) when done.".into(),
77        ]
78    }
79}
80
81/// `2.84 GB` (decimal, like the download pages).
82pub fn human(b: u64) -> String {
83    if b >= 1_000_000_000 {
84        format!("{:.2} GB", b as f64 / 1e9)
85    } else if b >= 1_000_000 {
86        format!("{:.1} MB", b as f64 / 1e6)
87    } else {
88        format!("{b} bytes")
89    }
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn defaults_follow_the_device() {
98        assert_eq!(default_for(Device::Desktop).id, "gemma-4-e4b");
99        assert_eq!(default_for(Device::Phone).id, "gemma-4-e2b");
100    }
101
102    #[test]
103    fn entries_are_pinned_and_urls_point_at_the_hub() {
104        for m in catalog() {
105            assert_eq!(m.sha256.as_deref().map(str::len), Some(64), "{}", m.id);
106            assert!(m.file.ends_with(".gguf"));
107        }
108        let e2b = find("gemma-4-e2b").unwrap();
109        assert_eq!(e2b.url("https://huggingface.co/"), "https://huggingface.co/ggml-org/gemma-4-E2B-it-GGUF/resolve/main/gemma-4-E2B-it-Q4_0.gguf");
110        assert!(find("nope").is_none());
111    }
112
113    #[test]
114    fn sizes_read_like_download_pages() {
115        assert_eq!(human(2_841_481_184), "2.84 GB");
116        assert_eq!(human(557_368_096), "557.4 MB");
117        assert_eq!(human(12), "12 bytes");
118        assert!(default_for(Device::Desktop).consent_text("https://h").iter().any(|l| l.contains("4.59 GB")));
119    }
120}