File size: 1,412 Bytes
2bcda37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2effc44
2bcda37
24d38de
 
 
 
 
 
2bcda37
 
 
 
24d38de
2bcda37
 
 
 
 
 
 
2effc44
 
2bcda37
 
 
24d38de
2bcda37
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from pathlib import Path
from fsspec.implementations.local import LocalFileSystem
from huggingface_hub import hf_hub_download

MODEL_REPO_ID = "Jackrong/Qwen3.5-4B-Neo-GGUF"
MODEL_FILENAME = "Qwen3.5-4B.Q4_K_S.gguf"


class UTF8LocalFileSystem(LocalFileSystem):
    """LocalFileSystem that forces UTF-8 encoding for text-mode opens."""

    def open(self, path, mode="rb", *args, **kwargs):  # type: ignore[override]
        if "b" not in mode:
            kwargs.setdefault("encoding", "utf-8")
        return super().open(path, mode, *args, **kwargs)


def resolve_gguf_model_path(status_callback=None) -> str:
    """Return local path to the GGUF model file, downloading from HF Hub if needed."""
    import os

    # Respect HF_HUB_CACHE env var (set in Dockerfile to /app/model)
    cache_dir = Path(os.environ.get("HF_HUB_CACHE", "./model"))

    direct = cache_dir / MODEL_FILENAME
    if direct.exists():
        return str(direct)

    snapshots_root = (
        cache_dir
        / f"models--{'--'.join(MODEL_REPO_ID.split('/'))}"
        / "snapshots"
    )
    match = next(snapshots_root.rglob(MODEL_FILENAME), None)
    if match:
        return str(match)

    if status_callback:
        status_callback(f"downloading model ({MODEL_FILENAME}) from HuggingFace…")
    return hf_hub_download(
        repo_id=MODEL_REPO_ID,
        filename=MODEL_FILENAME,
        cache_dir=str(cache_dir),
    )