Views
No views yet
AutoModel. Instead, use the huggingface_hub library to download the files and run the code locally.pip install torch transformers tokenizers huggingface_hub1
2import torch
3from tokenizers import Tokenizer
4from huggingface_hub import snapshot_download
5import os
6import sys
7
8# --- 1. DOWNLOAD FILES ---
9# This downloads model.py, got_tokenizer_v1.json, and best_model_params.pt to a local cache
10repo_id = "aman0419/got-slm-4.5m"
11local_dir = "got_slm_model"
12
13print(f"Downloading model from {repo_id}...")
14model_path = snapshot_download(repo_id=repo_id, local_dir=local_dir)
15sys.path.append(model_path) # Add download folder to path so we can import model.py
16
17# --- 2. IMPORT CUSTOM MODEL ---
18from model import SLM, get_default_config
19
20# --- 3. LOAD MODEL ---
21device = 'cuda' if torch.cuda.is_available() else 'cpu'
22print(f"Using device: {device}")
23
24config = get_default_config()
25model = SLM(config)
26
27# Load weights (map_location handles CPU/GPU automatically)
28weights_path = os.path.join(model_path, "best_model_params.pt")
29model.load_state_dict(torch.load(weights_path, map_location=device))
30model.to(device)
31model.eval()
32
33# --- 4. LOAD TOKENIZER ---
34tokenizer_path = os.path.join(model_path, "got_tokenizer_v1.json")
35tokenizer = Tokenizer.from_file(tokenizer_path)
36
37# --- 5. GENERATE TEXT ---
38input_text = "Tyrion Lannister poured a cup of wine"
39print(f"\nGenerating text for prompt: '{input_text}'\n" + "-"*50)
40
41# Encode
42encoded = tokenizer.encode(input_text)
43idx = torch.tensor(encoded.ids, dtype=torch.long, device=device).unsqueeze(0)
44
45# Generate
46with torch.no_grad():
47 generated_ids = model.generate(idx, max_new_tokens=60, temperature=0.8)
48
49# Decode
50output_text = tokenizer.decode(generated_ids[0].tolist())
51print(output_text)
52print("-" * 50)