andyz321 commited on
Commit
befa31b
·
verified ·
1 Parent(s): e120038

Upload scripts/write_modelfile.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/write_modelfile.py +86 -0
scripts/write_modelfile.py ADDED
@@ -0,0 +1,86 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """Write Modelfile as UTF-8 without BOM (PS 5.1 Set-Content corrupts CJK).
3
+
4
+ Single source of truth for the ollama model definition.
5
+
6
+ ollama 0.40 has no `DESCRIPTION` Modelfile command (only: from, license,
7
+ template, system, adapter, draft, renderer, parser, parameter, message,
8
+ requires, capability). The manifest's only free-text slot is the
9
+ `ollama.image.license` layer, so --license-mode decides what goes in it:
10
+
11
+ apache : license text only -> Modelfile
12
+ card : full model card -> Modelfile.card
13
+
14
+ Usage:
15
+ py -3 scripts/write_modelfile.py [--license-mode apache|card]
16
+ """
17
+ import argparse
18
+ import sys
19
+ from pathlib import Path
20
+
21
+ ROOT = Path(r"E:\AI edge\bio-enhance")
22
+ sys.path.insert(0, str(ROOT / "scripts"))
23
+ from prompt import SYSTEM_PROMPT # noqa: E402
24
+
25
+ Q = '"' * 3
26
+
27
+ # Preferred GGUF (round3 / r3). It may be a symlink into a detached volume,
28
+ # so fall back to the byte-identical blob in the ollama blob store.
29
+ CANDIDATES = [
30
+ ROOT / "output" / "bio-expert-q8-r3.gguf",
31
+ Path(r"D:\ollama_data\blobs\sha256-569155cf3edc5c144201fb78352380dfa9a6b3904c4609bb48ade1a16c7920bd"),
32
+ ]
33
+
34
+ APACHE = "Apache-2.0"
35
+
36
+
37
+ def pick_gguf():
38
+ for c in CANDIDATES:
39
+ try:
40
+ if c.exists() and c.stat().st_size > 1_000_000_000:
41
+ with open(c, "rb") as fh:
42
+ if fh.read(4) == b"GGUF":
43
+ return c
44
+ except OSError:
45
+ continue
46
+ raise SystemExit("no usable GGUF found in: "
47
+ + ", ".join(str(c) for c in CANDIDATES))
48
+
49
+
50
+ def build(gguf, license_text, out_path):
51
+ card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()
52
+ mf = "\n".join([
53
+ "FROM " + str(gguf),
54
+ "",
55
+ "SYSTEM " + Q + SYSTEM_PROMPT + Q,
56
+ "",
57
+ "LICENSE " + Q + license_text + Q,
58
+ "",
59
+ "PARAMETER temperature 0.7",
60
+ "PARAMETER top_p 0.9",
61
+ "PARAMETER num_ctx 32768",
62
+ "PARAMETER repeat_penalty 1.1",
63
+ "",
64
+ ])
65
+ out_path.write_text(mf, encoding="utf-8") # no BOM
66
+ raw = out_path.read_bytes()
67
+ print(f"wrote {out_path}")
68
+ print(" bytes:", len(raw), " BOM:", raw[:3] == b"\xef\xbb\xbf")
69
+ print(" FROM:", gguf)
70
+ print(" license chars:", len(license_text))
71
+ print(" CJK ok:", "合曜生物" in raw.decode("utf-8"))
72
+ return card
73
+
74
+
75
+ if __name__ == "__main__":
76
+ ap = argparse.ArgumentParser()
77
+ ap.add_argument("--license-mode", choices=["apache", "card"], default="apache")
78
+ args = ap.parse_args()
79
+
80
+ gguf = pick_gguf()
81
+ card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()
82
+
83
+ if args.license_mode == "card":
84
+ build(gguf, card, ROOT / "Modelfile.card")
85
+ else:
86
+ build(gguf, APACHE, ROOT / "Modelfile")