File size: 2,683 Bytes
8c3e275
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from __future__ import annotations

import urllib.request
from pathlib import Path

MODELS_DIR = Path(__file__).resolve().parent.parent / "models"
MODELS_DIR.mkdir(exist_ok=True)

MODELS = {
    "qwen2.5-1.5b-instruct-q4_k_m.gguf": (
        "https://huggingface.co/Qwen/Qwen2.5-1.5B-Instruct-GGUF/resolve/main/qwen2.5-1.5b-instruct-q4_k_m.gguf"
    ),
    "tr_ocr_base_handwritten.onnx": (
        "https://huggingface.co/Xenova/trocr-base-handwritten/resolve/main/onnx/model.onnx"
    ),
    "tr_ocr_tokenizer.json": (
        "https://huggingface.co/Xenova/trocr-base-handwritten/resolve/main/tokenizer.json"
    ),
    "all-MiniLM-L6-v2.onnx": (
        "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/onnx/model.onnx"
    ),
    "tokenizer.json": (
        "https://huggingface.co/sentence-transformers/all-MiniLM-L6-v2/resolve/main/tokenizer.json"
    ),
    "ggml-tiny.en-q5_0.bin": (
        "https://huggingface.co/ggerganov/whisper.cpp/resolve/main/ggml-tiny.en-q5_0.bin"
    ),
}


def download(url: str, dest: Path) -> None:
    if dest.exists() and dest.stat().st_size > 1000:
        print(f"  Already exists: {dest.name} ({dest.stat().st_size // 1024 // 1024} MB)")
        return
    print(f"  Downloading {dest.name}...")
    req = urllib.request.Request(
        url, headers={"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36"}
    )
    try:
        with urllib.request.urlopen(req, timeout=30) as response, open(dest, "wb") as out_file:
            total = int(response.headers.get("Content-Length", 0))
            downloaded = 0
            while True:
                chunk = response.read(1024 * 1024)
                if not chunk:
                    break
                out_file.write(chunk)
                downloaded += len(chunk)
                if total:
                    pct = downloaded * 100 // total
                    print(f"\r  {dest.name}: {pct}% ({downloaded // 1024 // 1024}/{total // 1024 // 1024} MB)", end="")
            print(f"\r  Saved: {dest.name} ({dest.stat().st_size // 1024 // 1024} MB)")
    except Exception as e:
        print(f"  FAILED: {dest.name}{e}")


def check_updates() -> dict[str, bool]:
    results = {}
    for name, url in MODELS.items():
        dest = MODELS_DIR / name
        if dest.exists() and dest.stat().st_size > 1000:
            results[name] = True
        else:
            results[name] = False
    return results


def main() -> None:
    print("Fetching models...")
    for name, url in MODELS.items():
        download(url, MODELS_DIR / name)
    print("Done. Models cached in", MODELS_DIR)


if __name__ == "__main__":
    main()