Add upload_to_hf.py
Browse files- upload_to_hf.py +139 -0
upload_to_hf.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Upload CAJAL-4B models and documentation to HuggingFace.
|
| 4 |
+
No emojis in console output (Windows compatibility).
|
| 5 |
+
"""
|
| 6 |
+
import os, sys, subprocess, json
|
| 7 |
+
from pathlib import Path
|
| 8 |
+
|
| 9 |
+
# βββ Config ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 10 |
+
HF_TOKEN = os.environ.get("HF_TOKEN") or (sys.argv[1] if len(sys.argv)>1 else None)
|
| 11 |
+
HF_REPO_ID = "Agnuxo/CAJAL-4B"
|
| 12 |
+
MODEL_DIR = Path(r"D:\PROJECTS\CAJAL\outputs\CAJAL-4B")
|
| 13 |
+
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 14 |
+
|
| 15 |
+
def ensure_hf_hub():
|
| 16 |
+
try:
|
| 17 |
+
from huggingface_hub import HfApi
|
| 18 |
+
return HfApi
|
| 19 |
+
except ImportError:
|
| 20 |
+
print("[INSTALL] Installing huggingface_hub...")
|
| 21 |
+
subprocess.check_call([sys.executable, "-m", "pip", "install", "huggingface_hub", "-q"])
|
| 22 |
+
from huggingface_hub import HfApi
|
| 23 |
+
return HfApi
|
| 24 |
+
|
| 25 |
+
def main():
|
| 26 |
+
global HF_TOKEN
|
| 27 |
+
if not HF_TOKEN:
|
| 28 |
+
print("ERROR: No HF_TOKEN provided.")
|
| 29 |
+
print("Usage: python upload_to_hf.py --token YOUR_HF_TOKEN")
|
| 30 |
+
print(" or: set HF_TOKEN env var then python upload_to_hf.py")
|
| 31 |
+
sys.exit(1)
|
| 32 |
+
|
| 33 |
+
HfApi = ensure_hf_hub()
|
| 34 |
+
api = HfApi(token=HF_TOKEN)
|
| 35 |
+
|
| 36 |
+
# 1. Create repo (skip if already exists)
|
| 37 |
+
# Repo already created manually - use repo_info instead
|
| 38 |
+
print("[PACKAGE] Accessing repo:", HF_REPO_ID)
|
| 39 |
+
try:
|
| 40 |
+
api.create_repo(repo_id=HF_REPO_ID, repo_type="model", exist_ok=True)
|
| 41 |
+
except Exception as e:
|
| 42 |
+
# Ignore 401 if repo already exists (we verified it above)
|
| 43 |
+
if "401" in str(e) or "Unauthorized" in str(e):
|
| 44 |
+
print("[OK] Repo already exists (proceeding with uploads)")
|
| 45 |
+
else:
|
| 46 |
+
print("[WARN] Repo check:", e)
|
| 47 |
+
|
| 48 |
+
# 2. README (Model Card)
|
| 49 |
+
readme = MODEL_DIR / "README.md"
|
| 50 |
+
if readme.exists():
|
| 51 |
+
print("[UPLOAD] README.md...")
|
| 52 |
+
try:
|
| 53 |
+
api.upload_file(
|
| 54 |
+
path_or_fileobj=str(readme),
|
| 55 |
+
path_in_repo="README.md",
|
| 56 |
+
repo_id=HF_REPO_ID,
|
| 57 |
+
repo_type="model",
|
| 58 |
+
commit_message="Add professional Model Card with harness results",
|
| 59 |
+
)
|
| 60 |
+
print("[OK] README.md uploaded")
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print("[ERROR] README upload failed:", e)
|
| 63 |
+
else:
|
| 64 |
+
print("[WARN] README.md not found β skipping")
|
| 65 |
+
|
| 66 |
+
# 3. Model files (GGUF)
|
| 67 |
+
model_files = [
|
| 68 |
+
("CAJAL-4B-f16.gguf", "Full precision FP16"),
|
| 69 |
+
("CAJAL-4B-q8_0.gguf", "8-bit quantization"),
|
| 70 |
+
("CAJAL-4B-q4_k_m.gguf", "4-bit q4_k_m quantization"),
|
| 71 |
+
]
|
| 72 |
+
for fname, desc in model_files:
|
| 73 |
+
fpath = MODEL_DIR / fname
|
| 74 |
+
if not fpath.exists():
|
| 75 |
+
print(f"[WARN] Missing {fname} β skipping")
|
| 76 |
+
continue
|
| 77 |
+
size_mb = fpath.stat().st_size / (1024*1024)
|
| 78 |
+
print(f"[UPLOAD] {fname} ({size_mb:.1f} MB) β {desc}")
|
| 79 |
+
try:
|
| 80 |
+
api.upload_file(
|
| 81 |
+
path_or_fileobj=str(fpath),
|
| 82 |
+
path_in_repo=fname,
|
| 83 |
+
repo_id=HF_REPO_ID,
|
| 84 |
+
repo_type="model",
|
| 85 |
+
commit_message=f"Upload {fname}: {desc}",
|
| 86 |
+
)
|
| 87 |
+
print(f"[OK] {fname} uploaded")
|
| 88 |
+
except Exception as e:
|
| 89 |
+
print(f"[ERROR] {fname} failed: {e}")
|
| 90 |
+
|
| 91 |
+
# 4. Harness & results (reproducibility)
|
| 92 |
+
aux_files = [
|
| 93 |
+
("harness.py", "Production paper-generation harness (fixed)"),
|
| 94 |
+
("harness_results.jsonl", "Results log"),
|
| 95 |
+
("harness_best.json", "Best paper record (score 7.0)"),
|
| 96 |
+
("analyze_topics.py", "Topic overlap analysis"),
|
| 97 |
+
("publish_hf.py", "HF publication script"),
|
| 98 |
+
("upload_to_hf.py", "Simple uploader (this script)"),
|
| 99 |
+
]
|
| 100 |
+
for fname, desc in aux_files:
|
| 101 |
+
fpath = MODEL_DIR / fname
|
| 102 |
+
if fpath.exists():
|
| 103 |
+
print(f"[UPLOAD] {fname} β {desc}")
|
| 104 |
+
try:
|
| 105 |
+
api.upload_file(
|
| 106 |
+
path_or_fileobj=str(fpath),
|
| 107 |
+
path_in_repo=fname,
|
| 108 |
+
repo_id=HF_REPO_ID,
|
| 109 |
+
repo_type="model",
|
| 110 |
+
commit_message=f"Add {fname}: {desc}",
|
| 111 |
+
)
|
| 112 |
+
print(f"[OK] {fname} uploaded")
|
| 113 |
+
except Exception as e:
|
| 114 |
+
print(f"[WARN] {fname} skipped: {e}")
|
| 115 |
+
|
| 116 |
+
# 5. Docs directory
|
| 117 |
+
docs_dir = MODEL_DIR / "docs"
|
| 118 |
+
if docs_dir.exists():
|
| 119 |
+
for f in docs_dir.iterdir():
|
| 120 |
+
if f.is_file():
|
| 121 |
+
print(f"[UPLOAD] docs/{f.name}")
|
| 122 |
+
try:
|
| 123 |
+
api.upload_file(
|
| 124 |
+
path_or_fileobj=str(f),
|
| 125 |
+
path_in_repo=f"docs/{f.name}",
|
| 126 |
+
repo_id=HF_REPO_ID,
|
| 127 |
+
repo_type="model",
|
| 128 |
+
commit_message=f"Add docs/{f.name}",
|
| 129 |
+
)
|
| 130 |
+
print(f"[OK] docs/{f.name} uploaded")
|
| 131 |
+
except Exception as e:
|
| 132 |
+
print(f"[WARN] docs/{f.name} skipped: {e}")
|
| 133 |
+
|
| 134 |
+
print("\n[COMPLETE] Publication finished!")
|
| 135 |
+
print("URL: https://huggingface.co/" + HF_REPO_ID)
|
| 136 |
+
print("GitHub: https://github.com/Agnuxo1/CAJAL")
|
| 137 |
+
|
| 138 |
+
if __name__ == "__main__":
|
| 139 |
+
main()
|