File size: 11,457 Bytes
2e8991c | 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | # ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# FULL PIPELINE: Load Nia checkpoint + run 100-prompt eval
# ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
!pip install -q transformers huggingface_hub tokenizers
import torch
import torch.nn.functional as F
from huggingface_hub import hf_hub_download
from transformers import AutoConfig, AutoModel
from tokenizers import Tokenizer
import csv
REPO = "Mathematicaljuice/nia"
# ββ 1. Load model ββ
config = AutoConfig.from_pretrained(REPO, trust_remote_code=True)
model = AutoModel.from_config(config, trust_remote_code=True)
ckpt_path = hf_hub_download(repo_id=REPO, filename="pytorch_model.bin")
ckpt = torch.load(ckpt_path, map_location="cpu")
state_dict = ckpt["model_state_dict"] if "model_state_dict" in ckpt else ckpt
state_dict = {k.replace("module.", "").replace("_orig_mod.", ""): v
for k, v in state_dict.items()}
missing, unexpected = model.load_state_dict(state_dict, strict=False)
print("Missing:", missing, "| Unexpected:", unexpected)
device = "cuda" if torch.cuda.is_available() else "cpu"
model.to(device).eval()
print("Model class:", type(model).__name__, "| has generate():", hasattr(model, "generate"))
# ββ 2. Tokenizer ββ
tok_path = hf_hub_download(repo_id=REPO, filename="tokenizer.json")
tok = Tokenizer.from_file(tok_path)
IM_END = tok.token_to_id('<|im_end|>')
# ββ 3. Greedy chat function (deterministic, avoids sampling noise) ββ
@torch.inference_mode()
def chat_factual(instruction, max_new_tokens=80, rep_penalty=1.3):
prompt = f'<|im_start|>user\n{instruction}\n<|im_end|>\n<|im_start|>assistant\n'
ids = tok.encode(prompt).ids
idx = torch.tensor([ids], dtype=torch.long, device=device)
if hasattr(model, "generate"):
out = model.generate(idx, max_new_tokens=max_new_tokens,
temperature=0.01, top_k=1, top_p=1.0,
repetition_penalty=rep_penalty, eos_id=IM_END)
reply_ids = out[0].tolist()[len(ids):]
else:
cur = idx
for _ in range(max_new_tokens):
logits = model(cur)
logits = logits[0] if isinstance(logits, tuple) else logits
logits = logits[:, -1, :].float()
for tid in set(cur[0].tolist()):
logits[0, tid] /= rep_penalty
next_tok = torch.argmax(logits, dim=-1, keepdim=True)
cur = torch.cat([cur, next_tok], dim=1)
if next_tok.item() == IM_END:
break
reply_ids = cur[0].tolist()[len(ids):]
return tok.decode(reply_ids, skip_special_tokens=True).strip()
# ββ 4. 100 test prompts ββ
test_prompts = [
# Identity (15)
"What is your name?", "Who are you?", "Who made you?", "Who created you?",
"Are you human?", "Are you a real person?", "What kind of AI are you?",
"Tell me about yourself.", "What should I call you?", "Is your name Claude?",
"Is your name GPT?", "Which company built you?", "Introduce yourself briefly.",
"Do you have a name?", "What company are you from?",
# General knowledge (25)
"What is the capital of France?", "What is the capital of Malawi?",
"What is the capital of Japan?", "Who wrote Romeo and Juliet?",
"What is the largest planet in the solar system?", "How many continents are there?",
"What is the boiling point of water in Celsius?", "Who was the first president of the USA?",
"What language is spoken in Brazil?", "What is the chemical symbol for gold?",
"How many days are in a leap year?", "What is the tallest mountain in the world?",
"Who painted the Mona Lisa?", "What is the longest river in the world?",
"What year did World War II end?", "What is the currency of Japan?",
"How many bones are in the human body?", "What is the smallest country in the world?",
"Who invented the telephone?", "What is the speed of light?",
"What is the capital of Kenya?", "Who developed the theory of relativity?",
"What is the largest ocean on Earth?", "What is the freezing point of water in Fahrenheit?",
"How many strings does a standard guitar have?",
# Plain instructions (20)
"Give me three tips for staying focused while studying.",
"Write a short motivational quote.", "Explain photosynthesis in one paragraph.",
"List five healthy breakfast foods.", "Give me a recipe idea for dinner tonight.",
"Suggest a name for a pet dog.", "Write a two-sentence bedtime story.",
"Explain how a rainbow forms.", "Give me tips for a good night's sleep.",
"Summarize what a computer virus is.", "Suggest three books for a beginner reader.",
"Explain the water cycle simply.", "Give advice for a first job interview.",
"Write a short poem about the ocean.", "Explain what gravity is.",
"List three benefits of exercise.", "Suggest a fun weekend activity.",
"Explain what an algorithm is.", "Give me a tip for saving money.",
"Write a one-sentence joke.",
# Grounded QA β answerable (15)
"Context:\nThe Eiffel Tower was completed in 1889 and designed by engineer Gustave Eiffel.\n\nQuestion: Who designed the Eiffel Tower?\nAnswer using only the information in the context above.",
"Context:\nMount Kilimanjaro is located in Tanzania and is the highest mountain in Africa.\n\nQuestion: Which country is Mount Kilimanjaro in?\nAnswer using only the information in the context above.",
"Context:\nThe Great Wall of China was built over many centuries to protect Chinese states from invasions.\n\nQuestion: Why was the Great Wall of China built?\nAnswer using only the information in the context above.",
"Context:\nMalawi is a landlocked country in southeastern Africa, bordered by Zambia, Tanzania, and Mozambique.\n\nQuestion: What type of country is Malawi?\nAnswer using only the information in the context above.",
"Context:\nThe Amazon River flows through South America and is one of the longest rivers in the world.\n\nQuestion: Which continent does the Amazon River flow through?\nAnswer using only the information in the context above.",
"Context:\nWilliam Shakespeare was born in Stratford-upon-Avon in 1564.\n\nQuestion: Where was William Shakespeare born?\nAnswer using only the information in the context above.",
"Context:\nThe Sahara Desert is the largest hot desert in the world, located in North Africa.\n\nQuestion: Where is the Sahara Desert located?\nAnswer using only the information in the context above.",
"Context:\nThe first modern Olympic Games were held in Athens, Greece, in 1896.\n\nQuestion: Where were the first modern Olympic Games held?\nAnswer using only the information in the context above.",
"Context:\nPenicillin was discovered by Alexander Fleming in 1928.\n\nQuestion: Who discovered penicillin?\nAnswer using only the information in the context above.",
"Context:\nThe Great Barrier Reef is located off the coast of Queensland, Australia.\n\nQuestion: Which country is the Great Barrier Reef near?\nAnswer using only the information in the context above.",
"Context:\nThe Statue of Liberty was a gift from France to the United States in 1886.\n\nQuestion: Which country gave the Statue of Liberty to the United States?\nAnswer using only the information in the context above.",
"Context:\nLake Malawi is one of the African Great Lakes and borders Malawi, Mozambique, and Tanzania.\n\nQuestion: What is Lake Malawi?\nAnswer using only the information in the context above.",
"Context:\nThe printing press was invented by Johannes Gutenberg in the 15th century.\n\nQuestion: Who invented the printing press?\nAnswer using only the information in the context above.",
"Context:\nThe Nile River flows through eleven countries in northeastern Africa.\n\nQuestion: How many countries does the Nile flow through?\nAnswer using only the information in the context above.",
"Context:\nThe Taj Mahal was built by Mughal emperor Shah Jahan in memory of his wife.\n\nQuestion: Who built the Taj Mahal?\nAnswer using only the information in the context above.",
# Grounded QA β unanswerable, should decline rather than guess (10)
"Context:\nThe Eiffel Tower was completed in 1889 as the entrance arch for the World's Fair.\n\nQuestion: How tall is the Eiffel Tower?\nAnswer using only the information in the context above.",
"Context:\nMalawi is a landlocked country in southeastern Africa.\n\nQuestion: What is the population of Malawi?\nAnswer using only the information in the context above.",
"Context:\nWilliam Shakespeare was born in Stratford-upon-Avon.\n\nQuestion: How many plays did Shakespeare write?\nAnswer using only the information in the context above.",
"Context:\nPenicillin was discovered by Alexander Fleming in 1928.\n\nQuestion: What disease does penicillin treat?\nAnswer using only the information in the context above.",
"Context:\nThe first modern Olympic Games were held in Athens in 1896.\n\nQuestion: How many athletes competed in the first Olympics?\nAnswer using only the information in the context above.",
"Context:\nThe Statue of Liberty was a gift from France in 1886.\n\nQuestion: How tall is the Statue of Liberty?\nAnswer using only the information in the context above.",
"Context:\nThe Great Wall of China was built over many centuries.\n\nQuestion: How long is the Great Wall of China?\nAnswer using only the information in the context above.",
"Context:\nThe printing press was invented by Johannes Gutenberg.\n\nQuestion: What city was Gutenberg born in?\nAnswer using only the information in the context above.",
"Context:\nLake Malawi borders Malawi, Mozambique, and Tanzania.\n\nQuestion: How deep is Lake Malawi?\nAnswer using only the information in the context above.",
"Context:\nThe Amazon River is one of the longest rivers in the world.\n\nQuestion: What animals live in the Amazon River?\nAnswer using only the information in the context above.",
# Boundary check β identity shouldn't leak into unrelated answers (15)
"Explain how a rainbow forms.", "What is the capital of Italy?",
"Give me a recipe for pancakes.", "Explain what electricity is.",
"List three planets in our solar system.", "What is 12 times 8?",
"Explain how airplanes fly.", "Give tips for learning a new language.",
"What is the tallest animal on Earth?", "Explain what recycling is.",
"Suggest a good exercise routine for beginners.", "What is the capital of Germany?",
"Explain how vaccines work.", "List three uses of solar energy.",
"What is the largest desert in the world?",
]
print(f"\nTotal prompts: {len(test_prompts)}\n")
# ββ 5. Run eval + save ββ
results = []
for i, p in enumerate(test_prompts):
reply = chat_factual(p)
results.append({"idx": i, "prompt": p, "response": reply})
print(f"[{i+1}/{len(test_prompts)}] {p[:60]}...")
print(f" -> {reply[:100]}\n")
with open("nia_eval_results.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=["idx", "prompt", "response"])
writer.writeheader()
writer.writerows(results)
print("Saved to nia_eval_results.csv") |