Upload trained model
Browse files- hanuman_pkg/__init__.py +100 -0
- hanuman_pkg/quick_start.py +15 -0
hanuman_pkg/__init__.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""hanuman_pkg
|
| 2 |
+
|
| 3 |
+
Helper to load the custom Hanuman model directly from a Hugging Face repo.
|
| 4 |
+
|
| 5 |
+
Usage:
|
| 6 |
+
from hanuman_pkg import from_pretrained
|
| 7 |
+
model, tokenizer = from_pretrained("ZombitX64/GPT4All-Model")
|
| 8 |
+
|
| 9 |
+
This will download `modeling.py`, `config.json` and `pytorch_model.bin` (if present)
|
| 10 |
+
from the repo and dynamically import the Hanuman class.
|
| 11 |
+
"""
|
| 12 |
+
from __future__ import annotations
|
| 13 |
+
|
| 14 |
+
import importlib.util
|
| 15 |
+
import json
|
| 16 |
+
import os
|
| 17 |
+
import tempfile
|
| 18 |
+
from typing import Tuple
|
| 19 |
+
|
| 20 |
+
import torch
|
| 21 |
+
from huggingface_hub import hf_hub_download
|
| 22 |
+
from transformers import AutoTokenizer
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
def _download_file(repo_id: str, filename: str) -> str:
|
| 26 |
+
"""Try to download `filename` from repo_id. Return local path or raise."""
|
| 27 |
+
try:
|
| 28 |
+
return hf_hub_download(repo_id, filename)
|
| 29 |
+
except Exception:
|
| 30 |
+
# try with common subfolder used by this repo
|
| 31 |
+
try:
|
| 32 |
+
return hf_hub_download(repo_id, os.path.join("out_run1", "epoch-3", filename))
|
| 33 |
+
except Exception as e:
|
| 34 |
+
raise RuntimeError(f"Failed to download {filename} from repo {repo_id}: {e}")
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
def _load_module_from_path(path: str, module_name: str):
|
| 38 |
+
spec = importlib.util.spec_from_file_location(module_name, path)
|
| 39 |
+
mod = importlib.util.module_from_spec(spec)
|
| 40 |
+
loader = spec.loader
|
| 41 |
+
assert loader is not None
|
| 42 |
+
loader.exec_module(mod)
|
| 43 |
+
return mod
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def from_pretrained(repo_id: str, map_location: str = "cpu") -> Tuple[torch.nn.Module, object]:
|
| 47 |
+
"""Download model artifacts from HF and return (model, tokenizer).
|
| 48 |
+
|
| 49 |
+
Args:
|
| 50 |
+
repo_id: Hugging Face repo id, e.g. "username/model-repo"
|
| 51 |
+
map_location: device string for torch.load
|
| 52 |
+
|
| 53 |
+
Returns:
|
| 54 |
+
model: Hanuman model instance (on CPU unless moved)
|
| 55 |
+
tokenizer: transformers tokenizer loaded from the repo
|
| 56 |
+
"""
|
| 57 |
+
# Load tokenizer via transformers (works directly with HF repos)
|
| 58 |
+
tokenizer = AutoTokenizer.from_pretrained(repo_id)
|
| 59 |
+
|
| 60 |
+
# Download config
|
| 61 |
+
cfg_path = _download_file(repo_id, "config.json")
|
| 62 |
+
with open(cfg_path, "r", encoding="utf-8") as f:
|
| 63 |
+
cfg = json.load(f)
|
| 64 |
+
|
| 65 |
+
# Download modeling.py and import it dynamically
|
| 66 |
+
modeling_path = _download_file(repo_id, "modeling.py")
|
| 67 |
+
modeling_mod = _load_module_from_path(modeling_path, "hanuman_modeling")
|
| 68 |
+
|
| 69 |
+
if not hasattr(modeling_mod, "Hanuman"):
|
| 70 |
+
raise RuntimeError("Downloaded modeling.py does not define Hanuman class")
|
| 71 |
+
|
| 72 |
+
Hanuman = modeling_mod.Hanuman
|
| 73 |
+
|
| 74 |
+
# Instantiate model using values from config
|
| 75 |
+
model = Hanuman(
|
| 76 |
+
vocab_size=cfg.get("vocab_size", 32000),
|
| 77 |
+
n_positions=cfg.get("n_positions", cfg.get("n_ctx", 4096)),
|
| 78 |
+
n_embd=cfg.get("n_embd", 512),
|
| 79 |
+
n_layer=cfg.get("n_layer", 8),
|
| 80 |
+
n_head=cfg.get("n_head", 8),
|
| 81 |
+
use_think_head=cfg.get("use_think_head", True),
|
| 82 |
+
)
|
| 83 |
+
|
| 84 |
+
# Download weights (prefer safetensors if available)
|
| 85 |
+
# Try safetensors first
|
| 86 |
+
state_path = None
|
| 87 |
+
try:
|
| 88 |
+
state_path = _download_file(repo_id, "pytorch_model.safetensors")
|
| 89 |
+
except Exception:
|
| 90 |
+
try:
|
| 91 |
+
state_path = _download_file(repo_id, "pytorch_model.bin")
|
| 92 |
+
except Exception as e:
|
| 93 |
+
raise RuntimeError(f"Failed to download model weights: {e}")
|
| 94 |
+
|
| 95 |
+
# Load state dict
|
| 96 |
+
# For safetensors, the dyn loader in modeling.from_pretrained uses safetensors; here we'll rely on torch.load
|
| 97 |
+
state = torch.load(state_path, map_location=map_location)
|
| 98 |
+
model.load_state_dict(state)
|
| 99 |
+
|
| 100 |
+
return model, tokenizer
|
hanuman_pkg/quick_start.py
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from hanuman_pkg import from_pretrained
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
def main():
|
| 5 |
+
repo_id = "ZombitX64/GPT4All-Model"
|
| 6 |
+
model, tokenizer = from_pretrained(repo_id)
|
| 7 |
+
|
| 8 |
+
prompt = "สวัสดีครับ ช่วยแนะนำประเทศไทยแบบสั้น ๆ"
|
| 9 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 10 |
+
out = model.generate(inputs["input_ids"], max_new_tokens=50, temperature=1.2, top_k=50, top_p=0.95)
|
| 11 |
+
print(tokenizer.decode(out[0], skip_special_tokens=True))
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
if __name__ == "__main__":
|
| 15 |
+
main()
|