1pip install torch tiktoken huggingface_hub
2python nanogpt_slm_pretrained_inference.py
1# Import loads the model automatically (one-time download from HuggingFace)
2## nanogpt_slm_pretrained_inference.py
3
4from nanogpt_slm_pretrained_inference import ask, generate_text
5
6# Text completion
7print(ask("Once upon a time there was"))
8print()
9
10# Control generation
11print(ask(
12 "The meaning of life is",
13 temperature=1.0, # higher = more creative
14 top_k=100, # wider sampling pool
15 max_tokens=150 # longer output
16))
17print()
18
19# generate_text is an alias for ask
20print(generate_text("She opened the door and saw", max_tokens=200))
21print()
1from huggingface_hub import hf_hub_download
2import torch
3
4model_path = hf_hub_download(
5 repo_id="nishantup/nanogpt-slm-124m",
6 filename="nanogpt_slm_best.pth"
7)
8
9# Full architecture in nanogpt_slm_pretrained_inference.py
10from nanogpt_slm_pretrained_inference import GPT, GPTKV, GPTConfig
11
12config = GPTConfig()
13model = GPTKV(config) # KV-cache enabled variant
14model.load_state_dict(torch.load(model_path, map_location="cpu"))
15model.eval()
1ask(prompt, max_tokens=200, temperature=0.8, top_k=40)
2generate_text(prompt, max_tokens=200, temperature=0.8, top_k=40) # alias