Upload code/test_reasoning_search.py with huggingface_hub
Browse files12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182import osimport torchfrom transformers import PreTrainedTokenizerFastfrom huggingface_hub import hf_hub_download
# 1. Download necessary filesHF_TOKEN = os.environ.get("HF_TOKEN")REPO_ID = "ViuAI/ViuAI-500M"
print("Downloading checkpoint...")ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="sft_checkpoints/sft_v3/sft_ckpt_latest.pt", token=HF_TOKEN, force_download=True)config_path = hf_hub_download(repo_id=REPO_ID, filename="code/config.py", token=HF_TOKEN)model_path = hf_hub_download(repo_id=REPO_ID, filename="code/model.py", token=HF_TOKEN)tokenizer_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer/tokenizer.json", token=HF_TOKEN)
import syssys.path.append(os.path.dirname(config_path))from config import ViuAIConfigfrom model import ViuAI
# 2. Load Checkpointprint("Loading checkpoint into memory...")checkpoint = torch.load(ckpt_path, map_location="cpu", weights_only=False) # load full to get stepstep = checkpoint.get("step", "Unknown")train_loss = checkpoint.get("train_loss", "Unknown")val_loss = checkpoint.get("val_loss", "Unknown")print(f"Current Training Step: {step}")print(f"Latest Train Loss: {train_loss}")print(f"Latest Val Loss: {val_loss}")
# 3. Load Model & Tokenizerdevice = "cuda" if torch.cuda.is_available() else "cpu"tokenizer = PreTrainedTokenizerFast(tokenizer_file=tokenizer_path)
# Initialize modelconfig = ViuAIConfig(vocab_size=64009)model = ViuAI(config)model.load_state_dict(checkpoint["model"], strict=False)model.to(device)model.eval()
# 4. Generate Functiondef generate_raw(prompt_text): full_prompt = f"<|user|>\n{prompt_text}<|endofturn|>\n<|assistant|>\n" tokens = tokenizer.encode(full_prompt, return_tensors="pt").to(device) # Stop tokens: <|endofturn|> and </search> eot_id = tokenizer.convert_tokens_to_ids("<|endofturn|>") end_search_id = tokenizer.convert_tokens_to_ids("</search>") stop_ids = (eot_id, end_search_id) with torch.no_grad(): out = model.generate( tokens, max_new_tokens=400, temperature=0.6, top_k=40, top_p=0.9, repetition_penalty=1.15, eos_token_id=stop_ids ) full_output = tokenizer.decode(out[0]) # Extract only what assistant said assistant_reply = full_output.split("<|assistant|>\n")[-1] return assistant_reply
# 5. Promptsprompts = [ "15 multiply by 23 kitna hota hai? Step by step batao.", "Agar ek train 60 km/h ki speed se 3 ghante chalti hai, toh kitni distance tay karegi?", "Aaj Delhi ka mausam kaisa hai?", "2026 mein sabse latest smartphone launch kaunsa hua hai?", "Capital of France kya hai?"]
print("\n--- RESULTS ---\n")for i, p in enumerate(prompts): print(f"PROMPT {i+1}: {p}") raw_out = generate_raw(p) print("RAW OUTPUT:") print(raw_out) print("-" * 50)code/test_reasoning_search.pyAdded+82