Download ml/infer.py from paralaif/tiny-scene-three: direct link, hf CLI and curl.
- Browser
- Download file 1.21 kB
-
https://huggingface.co/paralaif/tiny-scene-three/resolve/main/ml/infer.py
- Command line
-
hf download hf://paralaif/tiny-scene-three/ml/infer.py
-
curl -L -o infer.py https://huggingface.co/paralaif/tiny-scene-three/resolve/main/ml/infer.py
1.21 kB
| """Run the trained model from the command line, or dump reference outputs for the JavaScript parity test. | |
| python infer.py "a red fox jumping over a car in the desert" | |
| python infer.py --parity ../tests/parity.json | |
| """ | |
| import json | |
| import random | |
| import sys | |
| from pathlib import Path | |
| import torch | |
| import gen | |
| import tok | |
| from model import TinyGPT | |
| HERE = Path(__file__).resolve().parent | |
| def load(dev="cpu"): | |
| cfg = json.loads((HERE.parent / "web" / "model" / "model.json").read_text())["config"] | |
| m = TinyGPT(cfg["vocab"], cfg["ctx"], cfg["d"], cfg["layers"], cfg["heads"]) | |
| m.load_state_dict(torch.load(HERE / "model.pt", map_location=dev)) | |
| return m.to(dev).eval() | |
| def run(model, text): | |
| return [tok.VOCAB[i] for i in model.generate(tok.encode_prompt(text), tok.EOS)] | |
| if __name__ == "__main__": | |
| model = load() | |
| if sys.argv[1] == "--parity": | |
| r = random.Random(424242) | |
| prompts = [gen.sample(r)[0] for _ in range(300)] | |
| Path(sys.argv[2]).write_text(json.dumps([[p, run(model, p)] for p in prompts], ensure_ascii=False)) | |
| print("wrote", len(prompts)) | |
| else: | |
| for p in sys.argv[1:]: | |
| print(p, "\n ->", " ".join(run(model, p))) | |