Download aiml/03_training/experiments/migrated_experiments/test_model.py from LevelUp2x/adaptai: direct link, hf CLI and curl.
- Browser
- Download file 3.93 kB
-
https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/main/aiml/03_training/experiments/migrated_experiments/test_model.py
- Command line
-
hf download hf://datasets/LevelUp2x/adaptai/aiml/03_training/experiments/migrated_experiments/test_model.py
-
curl -L -o test_model.py https://huggingface.co/datasets/LevelUp2x/adaptai/resolve/main/aiml/03_training/experiments/migrated_experiments/test_model.py
3.93 kB
| #!/usr/bin/env python3 | |
| """ | |
| Test script to verify the trained model works correctly. | |
| """ | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| import torch | |
| import time | |
| def test_model_loading(): | |
| """Test that the model loads successfully.""" | |
| print("π§ͺ Testing model loading...") | |
| model_path = "/home/x/adaptai/experiments/qwen3-8b-elizabeth-simple/" | |
| # Load tokenizer | |
| print("π₯ Loading tokenizer...") | |
| tokenizer = AutoTokenizer.from_pretrained(model_path, trust_remote_code=True) | |
| print("β Tokenizer loaded successfully!") | |
| # Load model | |
| print("π₯ Loading model...") | |
| start_time = time.time() | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_path, | |
| torch_dtype=torch.bfloat16, | |
| device_map="auto", | |
| trust_remote_code=True | |
| ) | |
| load_time = time.time() - start_time | |
| print(f"β Model loaded successfully in {load_time:.2f} seconds!") | |
| # Check device placement | |
| print(f"π Model device: {model.device}") | |
| print(f"π Model dtype: {model.dtype}") | |
| return model, tokenizer | |
| def test_inference(model, tokenizer): | |
| """Test basic inference.""" | |
| print("\nπ§ͺ Testing inference...") | |
| # Test prompt | |
| prompt = "Hello, how are you today?" | |
| print(f"π Prompt: {prompt}") | |
| # Tokenize | |
| inputs = tokenizer(prompt, return_tensors="pt").to(model.device) | |
| print(f"π’ Input tokens: {inputs.input_ids.shape}") | |
| # Generate | |
| start_time = time.time() | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=50, | |
| do_sample=True, | |
| temperature=0.7, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| gen_time = time.time() - start_time | |
| # Decode | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| print(f"β Generation completed in {gen_time:.2f} seconds!") | |
| print(f"π¬ Response: {response}") | |
| return response | |
| def test_tool_use_capability(model, tokenizer): | |
| """Test tool use capability.""" | |
| print("\nπ§ͺ Testing tool use capability...") | |
| tool_prompt = """Please help me calculate the square root of 144 using the calculator tool. | |
| Available tools: | |
| - calculator: Performs mathematical calculations | |
| Please respond with the tool call format.""" | |
| print(f"π Tool prompt: {tool_prompt[:100]}...") | |
| inputs = tokenizer(tool_prompt, return_tensors="pt").to(model.device) | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=100, | |
| do_sample=True, | |
| temperature=0.3, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| print(f"π¬ Tool use response: {response}") | |
| # Check if response contains tool-like patterns | |
| if "calculator" in response.lower() or "tool" in response.lower(): | |
| print("β Tool use capability detected!") | |
| return True | |
| else: | |
| print("β οΈ Tool use pattern not clearly detected") | |
| return False | |
| if __name__ == "__main__": | |
| print("=" * 60) | |
| print("π€ Qwen3-8B-Elizabeth-Simple Model Test") | |
| print("=" * 60) | |
| try: | |
| model, tokenizer = test_model_loading() | |
| test_inference(model, tokenizer) | |
| tool_use_detected = test_tool_use_capability(model, tokenizer) | |
| print("\n" + "=" * 60) | |
| print("π Model Test Summary:") | |
| print(f" β Model loading: Successful") | |
| print(f" β Basic inference: Working") | |
| print(f" β Tool use capability: {'Detected' if tool_use_detected else 'Needs verification'}") | |
| print(" π Model is ready for deployment!") | |
| print("=" * 60) | |
| except Exception as e: | |
| print(f"\nβ Test failed with error: {e}") | |
| raise |