""" Step 4: Test your trained multilingual model """ import torch from transformers import GPT2LMHeadModel import sentencepiece as spm import os from pathlib import Path class MultilingualModel: def __init__(self, model_path="./checkpoints_tiny/final"): print("="*60) print("LOADING MULTILINGUAL MODEL") print("="*60) # Check if model exists if not os.path.exists(model_path): print(f"❌ Model not found at: {model_path}") print("Available checkpoints:") checkpoints = list(Path("./checkpoints_tiny").glob("checkpoint-*")) checkpoints += list(Path("./checkpoints_tiny").glob("step*")) checkpoints += list(Path("./checkpoints_tiny").glob("final")) for cp in checkpoints: if cp.is_dir(): print(f" - {cp}") if checkpoints: model_path = str(checkpoints[-1]) # Use most recent print(f"Using: {model_path}") else: raise FileNotFoundError("No checkpoints found!") # Load tokenizer tokenizer_path = os.path.join(model_path, "tokenizer", "spiece.model") if not os.path.exists(tokenizer_path): tokenizer_path = "./final_corpus/multilingual_spm.model" print(f"Loading tokenizer from: {tokenizer_path}") self.tokenizer = spm.SentencePieceProcessor() self.tokenizer.load(tokenizer_path) # Load model print(f"Loading model from: {model_path}") self.model = GPT2LMHeadModel.from_pretrained(model_path) # Setup device self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu") self.model.to(self.device) self.model.eval() print(f"✅ Model loaded on: {self.device}") print(f" Parameters: {sum(p.numel() for p in self.model.parameters())/1e6:.1f}M") print("="*60) def generate(self, prompt, max_length=100, temperature=0.7, top_k=50, top_p=0.95): """Generate text from prompt""" # Add language tag if missing if not any(prompt.startswith(tag) for tag in ['[EN]', '[HI]', '[PA]']): # Try to detect language if any(char in prompt for char in 'अआइईउऊएऐओऔकखगघचछजझटठडढणतथदधनपफबभमयरलवशषसह'): prompt = f"[HI] {prompt}" elif any(char in prompt for char in 'ਅਆਇਈਉਊਏਐਓਔਕਖਗਘਚਛਜਝਟਠਡਢਣਤਥਦਧਨਪਫਬਭਮਯਰਲਵਸ਼ਸਹ'): prompt = f"[PA] {prompt}" else: prompt = f"[EN] {prompt}" # Encode input_ids = self.tokenizer.encode(prompt) input_tensor = torch.tensor([input_ids], device=self.device) # Generate with torch.no_grad(): output = self.model.generate( input_ids=input_tensor, max_length=max_length, temperature=temperature, do_sample=True, top_k=top_k, top_p=top_p, pad_token_id=self.tokenizer.pad_id() if self.tokenizer.pad_id() > 0 else 0, eos_token_id=self.tokenizer.eos_id() if self.tokenizer.eos_id() > 0 else 2, repetition_penalty=1.1, ) # Decode generated = self.tokenizer.decode(output[0].tolist()) # Clean up (remove prompt if it's repeated) if generated.startswith(prompt): result = generated[len(prompt):].strip() else: result = generated return result def batch_generate(self, prompts, **kwargs): """Generate for multiple prompts""" results = [] for prompt in prompts: result = self.generate(prompt, **kwargs) results.append(result) return results def calculate_perplexity(self, text): """Calculate perplexity of given text""" input_ids = self.tokenizer.encode(text) if len(input_ids) < 2: return float('inf') input_tensor = torch.tensor([input_ids], device=self.device) with torch.no_grad(): outputs = self.model(input_ids=input_tensor, labels=input_tensor) loss = outputs.loss perplexity = torch.exp(loss).item() return perplexity def interactive_mode(self): """Interactive chat with model""" print("\n" + "="*60) print("INTERACTIVE MODE") print("="*60) print("Enter prompts in any language (add [EN], [HI], [PA] tags)") print("Commands: /temp X, /len X, /quit, /help") print("="*60) temperature = 0.7 max_length = 100 while True: try: user_input = input("\nYou: ").strip() if not user_input: continue # Handle commands if user_input.startswith('/'): if user_input == '/quit': break elif user_input == '/help': print("Commands:") print(" /temp X - Set temperature (0.1 to 2.0)") print(" /len X - Set max length (20 to 500)") print(" /quit - Exit") print(" /help - Show this help") continue elif user_input.startswith('/temp'): try: temp = float(user_input.split()[1]) if 0.1 <= temp <= 2.0: temperature = temp print(f"Temperature set to {temperature}") else: print("Temperature must be between 0.1 and 2.0") except: print("Usage: /temp 0.7") continue elif user_input.startswith('/len'): try: length = int(user_input.split()[1]) if 20 <= length <= 500: max_length = length print(f"Max length set to {max_length}") else: print("Length must be between 20 and 500") except: print("Usage: /len 100") continue # Generate response print("Model: ", end="", flush=True) response = self.generate(user_input, max_length=max_length, temperature=temperature) print(response) except KeyboardInterrupt: print("\n\nExiting...") break except Exception as e: print(f"Error: {e}") def run_tests(): """Run comprehensive tests""" print("\n" + "="*60) print("COMPREHENSIVE MODEL TESTS") print("="*60) # Load model model = MultilingualModel() # Test prompts by language test_suites = { "English": [ "[EN] The weather today is", "[EN] I want to learn", "[EN] Artificial intelligence", "[EN] The capital of India is", "[EN] Once upon a time", ], "Hindi": [ "[HI] आज का मौसम", "[HI] मैं सीखना चाहता हूं", "[HI] कृत्रिम बुद्धिमत्ता", "[HI] भारत की राजधानी है", "[HI] एक बार की बात है", ], "Punjabi": [ "[PA] ਅੱਜ ਦਾ ਮੌਸਮ", "[PA] ਮੈਂ ਸਿੱਖਣਾ ਚਾਹੁੰਦਾ ਹਾਂ", "[PA] ਕ੍ਰਿਤਰਿਮ ਬੁੱਧੀ", "[PA] ਭਾਰਤ ਦੀ ਰਾਜਧਾਨੀ ਹੈ", "[PA] ਇੱਕ ਵਾਰ ਦੀ ਗੱਲ ਹੈ", ], "Language Switching": [ "[EN] Hello [HI] नमस्ते", "[HI] यह अच्छा है [EN] this is good", "[PA] ਸਤਿ ਸ੍ਰੀ ਅਕਾਲ [EN] Hello everyone", ], "Code Mixing": [ "Hello दुनिया", # No tag, should auto-detect "मेरा name है", # Hindi + English "Today मौसम is good", # English + Hindi ] } for suite_name, prompts in test_suites.items(): print(f"\n{'='*40}") print(f"{suite_name.upper()} TESTS") print('='*40) for i, prompt in enumerate(prompts): print(f"\nTest {i+1}:") print(f"Prompt: {prompt}") # Generate response = model.generate(prompt, max_length=50, temperature=0.7) print(f"Response: {response}") # Calculate perplexity try: perplexity = model.calculate_perplexity(response) print(f"Perplexity: {perplexity:.2f}") except: pass print("-" * 40) def benchmark_model(): """Benchmark model performance""" print("\n" + "="*60) print("MODEL BENCHMARK") print("="*60) model = MultilingualModel() import time # Test generation speed test_prompt = "[EN] The quick brown fox" times = [] for _ in range(10): start = time.time() model.generate(test_prompt, max_length=50) end = time.time() times.append(end - start) avg_time = sum(times) / len(times) print(f"Average generation time (50 tokens): {avg_time:.3f}s") print(f"Tokens per second: {50/avg_time:.1f}") # Memory usage if torch.cuda.is_available(): memory_allocated = torch.cuda.memory_allocated() / 1e9 memory_reserved = torch.cuda.memory_reserved() / 1e9 print(f"GPU Memory allocated: {memory_allocated:.2f} GB") print(f"GPU Memory reserved: {memory_reserved:.2f} GB") def create_web_interface(): """Simple web interface for the model""" html_code = """