ViuAI commited on
Commit
3f611eb
·
verified ·
1 Parent(s): ede2a37

Upload evaluate.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. evaluate.py +95 -0
evaluate.py ADDED
@@ -0,0 +1,95 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # ==============================================================================
2
+ # 🚀 ViuTranslate — Evaluation & Benchmark Suite
3
+ # ==============================================================================
4
+
5
+ import os
6
+ import sys
7
+ import json
8
+ import time
9
+ import torch
10
+ from tokenizers import Tokenizer
11
+ from huggingface_hub import hf_hub_download
12
+
13
+ if hasattr(sys.stdout, "reconfigure"):
14
+ sys.stdout.reconfigure(encoding="utf-8", errors="replace")
15
+
16
+ cur_dir = os.path.dirname(os.path.abspath(__file__)) if "__file__" in locals() else os.getcwd()
17
+ if cur_dir not in sys.path:
18
+ sys.path.insert(0, cur_dir)
19
+
20
+ from model import ViuAI
21
+ from config import ViuAIConfig
22
+
23
+ REPO_ID = "ViuAI/ViuTranslate"
24
+ DATA_REPO_ID = "ViuAI/ViuTranslate-Data"
25
+ EOT_ID = 64002
26
+
27
+ def main():
28
+ print("=" * 80)
29
+ print("📊 ViuTranslate Benchmark Evaluation (Gold Human Test Set)")
30
+ print("=" * 80)
31
+
32
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
33
+ print(f"Device: {device.type.upper()}")
34
+
35
+ # Load Model
36
+ ckpt_path = "viutranslate_final.pt"
37
+ if not os.path.exists(ckpt_path):
38
+ ckpt_path = hf_hub_download(repo_id=REPO_ID, filename="viutranslate_final.pt")
39
+
40
+ tok_path = "tokenizer.json"
41
+ if not os.path.exists(tok_path):
42
+ tok_path = hf_hub_download(repo_id=REPO_ID, filename="tokenizer.json")
43
+
44
+ tokenizer = Tokenizer.from_file(tok_path)
45
+ cfg = ViuAIConfig(vocab_size=64003, context_length=2048)
46
+ model = ViuAI(cfg).to(device)
47
+
48
+ state = torch.load(ckpt_path, map_location=device, weights_only=False)
49
+ weights = state.get("model_state_dict", state)
50
+ model.load_state_dict(weights, strict=False)
51
+ model.eval()
52
+ print("✅ Model loaded.")
53
+
54
+ # Download Validation Set
55
+ val_json_path = "raw/viu_translate_val.json"
56
+ if not os.path.exists(val_json_path):
57
+ try:
58
+ val_json_path = hf_hub_download(repo_id=DATA_REPO_ID, filename="raw/viu_translate_val.json", repo_type="dataset")
59
+ except Exception:
60
+ val_json_path = None
61
+
62
+ if val_json_path and os.path.exists(val_json_path):
63
+ with open(val_json_path, 'r', encoding='utf-8') as f:
64
+ val_data = json.load(f)
65
+ print(f"Loaded {len(val_data):,} gold validation pairs.")
66
+ else:
67
+ print("Val JSON not found. Running curated benchmark cases.")
68
+ val_data = []
69
+
70
+ # Curated Benchmark Tests
71
+ benchmark_cases = [
72
+ ("The sun rises in the east and sets in the west.", "सूरज पूर्व में उगता है और पश्चिम में डूबता है।"),
73
+ ("Consistency and discipline are the keys to long term success.", "निरंतरता और अनुशासन दीर्घकालिक सफलता की कुंजी हैं।"),
74
+ ("Artificial intelligence is transforming industries across the globe.", "कृत्रिम बुद्धिमत्ता दुनिया भर के उद्योगों को बदल रही है।"),
75
+ ("Where is the nearest railway station?", "निकटतम रेलवे स्टेशन कहाँ है?"),
76
+ ("Regular exercise is essential for maintaining physical and mental health.", "शारीरिक और मानसिक स्वास्थ्य बनाए रखने के लिए नियमित व्यायाम आवश्यक है।")
77
+ ]
78
+
79
+ print("\n🔍 Running Curated Test Cases:")
80
+ for en, hi_ref in benchmark_cases:
81
+ prompt = f"<|user|>\n{en}<|endofturn|>\n<|assistant|>\n"
82
+ inp = torch.tensor([tokenizer.encode(prompt).ids], device=device)
83
+ with torch.no_grad():
84
+ out = model.generate(inp, max_new_tokens=80, temperature=0.2, eos_token_id=EOT_ID)
85
+ gen = tokenizer.decode(out[0][inp.shape[1]:].tolist()).replace("<|endofturn|>", "").strip()
86
+ print(f"\n• EN: {en}")
87
+ print(f" REF: {hi_ref}")
88
+ print(f" GEN: {gen}")
89
+
90
+ print("\n" + "=" * 80)
91
+ print("✅ Evaluation Completed!")
92
+ print("=" * 80)
93
+
94
+ if __name__ == "__main__":
95
+ main()