Add scripts/convert_and_push_gguf.py
Browse files
scripts/convert_and_push_gguf.py
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Convert zindango-slm to GGUF and push to Hugging Face.
|
| 4 |
+
|
| 5 |
+
Requires: llama.cpp cloned, gguf, sentencepiece
|
| 6 |
+
pip install gguf sentencepiece
|
| 7 |
+
git clone https://github.com/ggml-org/llama.cpp
|
| 8 |
+
|
| 9 |
+
Usage:
|
| 10 |
+
python scripts/convert_and_push_gguf.py [--model-dir PATH] [--quantize Q4_K_M]
|
| 11 |
+
"""
|
| 12 |
+
|
| 13 |
+
import argparse
|
| 14 |
+
import subprocess
|
| 15 |
+
import sys
|
| 16 |
+
from pathlib import Path
|
| 17 |
+
|
| 18 |
+
from huggingface_hub import HfApi, create_repo, upload_folder, upload_file
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
def main():
|
| 22 |
+
parser = argparse.ArgumentParser()
|
| 23 |
+
parser.add_argument("--model-dir", default="outputs/zindango-slm-20260215_124754")
|
| 24 |
+
parser.add_argument("--llama-cpp", default="/home/piren/projects/llama.cpp")
|
| 25 |
+
parser.add_argument("--quantize", choices=["q4_k_m", "q5_k_m", "q8_0", "none"], default="none")
|
| 26 |
+
parser.add_argument("--repo-id", default=None)
|
| 27 |
+
parser.add_argument("--skip-create", action="store_true")
|
| 28 |
+
parser.add_argument("--push-only", action="store_true", help="Skip conversion, only push existing GGUF")
|
| 29 |
+
args = parser.parse_args()
|
| 30 |
+
|
| 31 |
+
project_root = Path(__file__).resolve().parent.parent
|
| 32 |
+
model_dir = project_root / args.model_dir
|
| 33 |
+
out_dir = project_root / "outputs"
|
| 34 |
+
f16_gguf = out_dir / "zindango-slm-f16.gguf"
|
| 35 |
+
|
| 36 |
+
if not args.push_only:
|
| 37 |
+
llama_cpp = Path(args.llama_cpp)
|
| 38 |
+
if not (llama_cpp / "convert_hf_to_gguf.py").exists():
|
| 39 |
+
raise SystemExit(f"llama.cpp not found at {llama_cpp}. Clone it first.")
|
| 40 |
+
if not model_dir.exists():
|
| 41 |
+
raise SystemExit(f"Model not found: {model_dir}")
|
| 42 |
+
|
| 43 |
+
# Convert to F16 GGUF
|
| 44 |
+
cmd = [
|
| 45 |
+
sys.executable,
|
| 46 |
+
str(llama_cpp / "convert_hf_to_gguf.py"),
|
| 47 |
+
str(model_dir),
|
| 48 |
+
"--outtype", "f16",
|
| 49 |
+
"--outfile", str(f16_gguf),
|
| 50 |
+
]
|
| 51 |
+
print("Converting to GGUF f16...")
|
| 52 |
+
subprocess.run(cmd, check=True)
|
| 53 |
+
|
| 54 |
+
# Optionally quantize
|
| 55 |
+
if args.quantize != "none":
|
| 56 |
+
quant_bin = llama_cpp / "build" / "bin" / "llama-quantize"
|
| 57 |
+
if not quant_bin.exists():
|
| 58 |
+
quant_bin = llama_cpp / "bin" / "llama-quantize"
|
| 59 |
+
if quant_bin.exists():
|
| 60 |
+
q_gguf = out_dir / f"zindango-slm-{args.quantize}.gguf"
|
| 61 |
+
cmd = [str(quant_bin), str(f16_gguf), str(q_gguf), args.quantize.upper()]
|
| 62 |
+
print(f"Quantizing to {args.quantize}...")
|
| 63 |
+
subprocess.run(cmd, check=True)
|
| 64 |
+
else:
|
| 65 |
+
print("llama-quantize not found; skipping quantization")
|
| 66 |
+
|
| 67 |
+
# Push to Hub
|
| 68 |
+
api = HfApi()
|
| 69 |
+
user = api.whoami()
|
| 70 |
+
username = user["name"]
|
| 71 |
+
repo_id = args.repo_id or f"{username}/zindango-slm"
|
| 72 |
+
|
| 73 |
+
if not args.skip_create:
|
| 74 |
+
try:
|
| 75 |
+
create_repo(repo_id, repo_type="model", exist_ok=True)
|
| 76 |
+
except Exception as e:
|
| 77 |
+
if "403" in str(e).lower() or "forbidden" in str(e).lower():
|
| 78 |
+
print("Create repo failed. Run with --skip-create after creating manually.")
|
| 79 |
+
raise
|
| 80 |
+
|
| 81 |
+
# Upload GGUF file(s): f16 + any quantized (q4_k_m, q8_0, etc.)
|
| 82 |
+
quant_ggufs = list(out_dir.glob("zindango-slm-q*.gguf")) + list(out_dir.glob("zindango-slm-Q*.gguf"))
|
| 83 |
+
for gguf_path in [f16_gguf] + quant_ggufs:
|
| 84 |
+
if gguf_path.exists():
|
| 85 |
+
print(f"Uploading {gguf_path.name}...")
|
| 86 |
+
upload_file(
|
| 87 |
+
path_or_fileobj=str(gguf_path),
|
| 88 |
+
path_in_repo=gguf_path.name,
|
| 89 |
+
repo_id=repo_id,
|
| 90 |
+
repo_type="model",
|
| 91 |
+
commit_message=f"Add {gguf_path.name}",
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
print(f"Done. Model: https://huggingface.co/{repo_id}")
|
| 95 |
+
|
| 96 |
+
|
| 97 |
+
if __name__ == "__main__":
|
| 98 |
+
main()
|