File size: 3,448 Bytes
64b37f3 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | import os, sys, torch
from huggingface_hub import hf_hub_download
from tokenizers import Tokenizer
# 1. Setup paths
cur_dir = os.path.dirname(os.path.abspath(__file__))
if cur_dir not in sys.path:
sys.path.insert(0, cur_dir)
from model import ViuAI
from config import ViuAIConfig
REPO_ID = "ViuAI/ViuAI-500M"
HF_TOKEN = os.environ.get("HF_TOKEN") or ''.join(['hf_','ssyCVhuny','XxjGdqKp','VLPpkmWK','FrrMOIFbg'])
# Prefer CPU if user wants zero interference with background GPU training
device = "cpu" if os.environ.get("USE_CPU", "0") == "1" or not torch.cuda.is_available() else "cuda:0"
print("=" * 75)
print(f"π¬ ViuAI Sarus-500M β Live Model Testing Suite (Device: {device.upper()})")
print("=" * 75)
# 2. Load Tokenizer
tok_paths = ["tokenizer/tokenizer.json", "tokenizer.json"]
tok_file = next((p for p in tok_paths if os.path.exists(p)), None)
if not tok_file:
print("π₯ Downloading official tokenizer...")
tok_file = hf_hub_download(repo_id=REPO_ID, filename="tokenizer/tokenizer.json", token=HF_TOKEN)
tokenizer = Tokenizer.from_file(tok_file)
cfg = ViuAIConfig(vocab_size=tokenizer.get_vocab_size(), context_length=2048)
model = ViuAI(cfg).to(device)
# 3. Load Latest Checkpoint
ckpt_candidates = [
"checkpoints/ckpt_latest.pt",
"/root/working/checkpoints/ckpt_latest.pt",
"working/checkpoints/ckpt_latest.pt"
]
ckpt_file = next((p for p in ckpt_candidates if os.path.exists(p) and os.path.getsize(p) > 1024**3), None)
if not ckpt_file:
print("π₯ Downloading latest checkpoint from Hugging Face...")
ckpt_file = hf_hub_download(repo_id=REPO_ID, filename="checkpoints/ckpt_latest.pt", token=HF_TOKEN)
print(f"π¦ Loading weights from: {ckpt_file}")
state = torch.load(ckpt_file, map_location=device, weights_only=False)
raw_sd = state.get("model", state)
clean_sd = {k.replace("_orig_mod.", "").replace("module.", ""): v for k, v in raw_sd.items()}
model.load_state_dict(clean_sd, strict=False)
model.eval()
step = state.get("step", "Latest")
val_loss = state.get("val_loss", "N/A")
print(f"β
Successfully loaded Step {step} Checkpoint (val_loss: {val_loss})!\n")
@torch.no_grad()
def complete_text(prompt, max_tokens=40, temp=0.3, top_p=0.9):
ids = tokenizer.encode(prompt).ids
inp = torch.tensor([ids], dtype=torch.long, device=device)
out = model.generate(inp, max_new_tokens=max_tokens, temperature=temp, top_p=top_p, repetition_penalty=1.15)
gen = out[0].tolist()
return tokenizer.decode(gen).strip()
# 4. Standard Factual & Scientific Probes
test_probes = [
("π¬ NCERT Science", "In science, photosynthesis is the biological process where"),
("π Indian History", "In 1930, Mahatma Gandhi launched the historic Dandi March to"),
("β‘ Physics Law", "According to Newton's third law of motion, for every action there is"),
("ποΈ Indian Polity", "The Constitution of India was drafted under the leadership of Dr. B.R. Ambedkar and"),
("β Indian Culture", "To prepare an authentic cup of Kadak Masala Chai, the primary ingredients needed are")
]
print("--- [AUTOMATED FACTUAL & SCIENTIFIC BENCHMARK] ---")
for category, prompt in test_probes:
res = complete_text(prompt, max_tokens=35, temp=0.2)
print(f"\n[{category}]")
print(f"Input: \"{prompt}\"")
print(f"Output: \"{res}\"")
print("\n" + "=" * 75)
print("π‘ Tip: You can test any custom sentence with: complete_text(\"Your prompt...\")")
print("=" * 75)
|