bio-expert / scripts /write_modelfile.py
andyz321's picture
Upload scripts/write_modelfile.py with huggingface_hub
befa31b verified
Raw History Blame Contribute Delete
2.77 kB
# -*- coding: utf-8 -*-
"""Write Modelfile as UTF-8 without BOM (PS 5.1 Set-Content corrupts CJK).
Single source of truth for the ollama model definition.
ollama 0.40 has no `DESCRIPTION` Modelfile command (only: from, license,
template, system, adapter, draft, renderer, parser, parameter, message,
requires, capability). The manifest's only free-text slot is the
`ollama.image.license` layer, so --license-mode decides what goes in it:
apache : license text only -> Modelfile
card : full model card -> Modelfile.card
Usage:
py -3 scripts/write_modelfile.py [--license-mode apache|card]
"""
import argparse
import sys
from pathlib import Path
ROOT = Path(r"E:\AI edge\bio-enhance")
sys.path.insert(0, str(ROOT / "scripts"))
from prompt import SYSTEM_PROMPT # noqa: E402
Q = '"' * 3
# Preferred GGUF (round3 / r3). It may be a symlink into a detached volume,
# so fall back to the byte-identical blob in the ollama blob store.
CANDIDATES = [
ROOT / "output" / "bio-expert-q8-r3.gguf",
Path(r"D:\ollama_data\blobs\sha256-569155cf3edc5c144201fb78352380dfa9a6b3904c4609bb48ade1a16c7920bd"),
]
APACHE = "Apache-2.0"
def pick_gguf():
for c in CANDIDATES:
try:
if c.exists() and c.stat().st_size > 1_000_000_000:
with open(c, "rb") as fh:
if fh.read(4) == b"GGUF":
return c
except OSError:
continue
raise SystemExit("no usable GGUF found in: "
+ ", ".join(str(c) for c in CANDIDATES))
def build(gguf, license_text, out_path):
card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()
mf = "\n".join([
"FROM " + str(gguf),
"",
"SYSTEM " + Q + SYSTEM_PROMPT + Q,
"",
"LICENSE " + Q + license_text + Q,
"",
"PARAMETER temperature 0.7",
"PARAMETER top_p 0.9",
"PARAMETER num_ctx 32768",
"PARAMETER repeat_penalty 1.1",
"",
])
out_path.write_text(mf, encoding="utf-8") # no BOM
raw = out_path.read_bytes()
print(f"wrote {out_path}")
print(" bytes:", len(raw), " BOM:", raw[:3] == b"\xef\xbb\xbf")
print(" FROM:", gguf)
print(" license chars:", len(license_text))
print(" CJK ok:", "合曜生物" in raw.decode("utf-8"))
return card
if __name__ == "__main__":
ap = argparse.ArgumentParser()
ap.add_argument("--license-mode", choices=["apache", "card"], default="apache")
args = ap.parse_args()
gguf = pick_gguf()
card = (ROOT / "model_card.md").read_text(encoding="utf-8").strip()
if args.license_mode == "card":
build(gguf, card, ROOT / "Modelfile.card")
else:
build(gguf, APACHE, ROOT / "Modelfile")