Spaces:
Running
Running
Download test_phi3_model.py from Vishwas896/Vish-AI: direct link, hf CLI and curl.
- Browser
- Download file 8.42 kB
-
https://huggingface.co/spaces/Vishwas896/Vish-AI/resolve/main/test_phi3_model.py
- Command line
-
hf download hf://spaces/Vishwas896/Vish-AI/test_phi3_model.py
-
curl -L -o test_phi3_model.py https://huggingface.co/spaces/Vishwas896/Vish-AI/resolve/main/test_phi3_model.py
8.42 kB
| """ | |
| Test script to verify Phi-3 model can be loaded and used | |
| Run this before deploying to ensure everything works | |
| """ | |
| import sys | |
| import time | |
| def test_imports(): | |
| """Test that all required packages can be imported""" | |
| print("=" * 60) | |
| print("π Testing imports...") | |
| print("=" * 60) | |
| try: | |
| import torch | |
| print(f"β PyTorch: {torch.__version__}") | |
| except ImportError as e: | |
| print(f"β PyTorch import failed: {e}") | |
| return False | |
| try: | |
| import transformers | |
| print(f"β Transformers: {transformers.__version__}") | |
| except ImportError as e: | |
| print(f"β Transformers import failed: {e}") | |
| return False | |
| try: | |
| import gradio | |
| print(f"β Gradio: {gradio.__version__}") | |
| except ImportError as e: | |
| print(f"β Gradio import failed: {e}") | |
| return False | |
| try: | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| print("β AutoModelForCausalLM and AutoTokenizer imported") | |
| except ImportError as e: | |
| print(f"β Failed to import model classes: {e}") | |
| return False | |
| print("\nβ All imports successful!\n") | |
| return True | |
| def test_model_loading(): | |
| """Test loading the Phi-3 model (this will download ~7GB on first run)""" | |
| print("=" * 60) | |
| print("π Testing Phi-3 model loading...") | |
| print("=" * 60) | |
| print("β οΈ Note: First run will download ~7GB model files") | |
| print(" This may take several minutes depending on internet speed\n") | |
| try: | |
| import torch | |
| from transformers import AutoModelForCausalLM, AutoTokenizer | |
| model_name = "microsoft/Phi-3-mini-4k-instruct" | |
| print(f"π₯ Loading tokenizer from {model_name}...") | |
| start_time = time.time() | |
| tokenizer = AutoTokenizer.from_pretrained( | |
| model_name, | |
| trust_remote_code=True | |
| ) | |
| tokenizer_time = time.time() - start_time | |
| print(f"β Tokenizer loaded in {tokenizer_time:.2f}s") | |
| print(f"\nπ₯ Loading model from {model_name}...") | |
| print(" Using CPU (for testing)...") | |
| start_time = time.time() | |
| model = AutoModelForCausalLM.from_pretrained( | |
| model_name, | |
| device_map="cpu", | |
| torch_dtype=torch.float32, | |
| trust_remote_code=True, | |
| low_cpu_mem_usage=True | |
| ) | |
| model_time = time.time() - start_time | |
| print(f"β Model loaded in {model_time:.2f}s") | |
| # Get model info | |
| param_count = sum(p.numel() for p in model.parameters()) | |
| print(f"\nπ Model Information:") | |
| print(f" Parameters: {param_count:,}") | |
| print(f" Size: ~{param_count * 4 / 1024 / 1024 / 1024:.2f}GB (FP32)") | |
| return True, model, tokenizer | |
| except Exception as e: | |
| print(f"\nβ Model loading failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False, None, None | |
| def test_inference(model, tokenizer): | |
| """Test model inference with a simple example""" | |
| print("\n" + "=" * 60) | |
| print("π Testing model inference...") | |
| print("=" * 60) | |
| try: | |
| import torch | |
| # Test prompt | |
| test_prompt = "What is artificial intelligence?" | |
| print(f"\nπ Test prompt: '{test_prompt}'") | |
| # Format prompt | |
| messages = [{"role": "user", "content": test_prompt}] | |
| formatted_prompt = tokenizer.apply_chat_template( | |
| messages, | |
| tokenize=False, | |
| add_generation_prompt=True | |
| ) | |
| # Tokenize | |
| inputs = tokenizer(formatted_prompt, return_tensors="pt") | |
| # Generate | |
| print("\nβ³ Generating response (this may take 10-30 seconds on CPU)...") | |
| start_time = time.time() | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=50, | |
| temperature=0.7, | |
| do_sample=True, | |
| top_p=0.9, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| inference_time = time.time() - start_time | |
| # Decode | |
| full_response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| # Extract response | |
| if "<|assistant|>" in full_response: | |
| response = full_response.split("<|assistant|>")[-1].strip() | |
| else: | |
| response = full_response[len(formatted_prompt):].strip() | |
| print(f"β Response generated in {inference_time:.2f}s") | |
| print(f"\nπ€ Model response:\n{response}\n") | |
| return True | |
| except Exception as e: | |
| print(f"\nβ Inference failed: {e}") | |
| import traceback | |
| traceback.print_exc() | |
| return False | |
| def test_all_tasks(model, tokenizer): | |
| """Test all three tasks: chat, summarization, sentiment""" | |
| print("\n" + "=" * 60) | |
| print("π Testing all Vish AI tasks...") | |
| print("=" * 60) | |
| import torch | |
| tasks = [ | |
| { | |
| "name": "Chat", | |
| "prompt": "Hello! How can you help me?", | |
| "max_tokens": 50 | |
| }, | |
| { | |
| "name": "Summarization", | |
| "prompt": "Summarize the following text concisely: Artificial Intelligence is transforming industries by automating tasks and improving decision-making. Machine learning enables computers to learn from data without explicit programming. This technology is used in healthcare, finance, and transportation.", | |
| "max_tokens": 60 | |
| }, | |
| { | |
| "name": "Sentiment", | |
| "prompt": "Analyze the sentiment of this text. Respond with POSITIVE, NEGATIVE, or NEUTRAL: I love this product! It's amazing!", | |
| "max_tokens": 5 | |
| } | |
| ] | |
| all_passed = True | |
| for task in tasks: | |
| print(f"\nπ Testing {task['name']}...") | |
| print(f" Prompt: {task['prompt'][:60]}...") | |
| try: | |
| messages = [{"role": "user", "content": task['prompt']}] | |
| formatted = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True) | |
| inputs = tokenizer(formatted, return_tensors="pt") | |
| with torch.no_grad(): | |
| outputs = model.generate( | |
| **inputs, | |
| max_new_tokens=task['max_tokens'], | |
| temperature=0.7, | |
| do_sample=True, | |
| pad_token_id=tokenizer.eos_token_id | |
| ) | |
| response = tokenizer.decode(outputs[0], skip_special_tokens=True) | |
| if "<|assistant|>" in response: | |
| response = response.split("<|assistant|>")[-1].strip() | |
| print(f" β {task['name']}: Success") | |
| print(f" Response: {response[:100]}...") | |
| except Exception as e: | |
| print(f" β {task['name']}: Failed - {e}") | |
| all_passed = False | |
| return all_passed | |
| def main(): | |
| print("\n" + "=" * 60) | |
| print("π§ͺ Vish AI - Phi-3 Model Test Suite") | |
| print("=" * 60) | |
| # Test 1: Imports | |
| if not test_imports(): | |
| print("\nβ Import test failed. Please install required packages:") | |
| print(" pip install -r requirements.txt") | |
| sys.exit(1) | |
| # Test 2: Model loading | |
| success, model, tokenizer = test_model_loading() | |
| if not success: | |
| print("\nβ Model loading failed. Check error messages above.") | |
| sys.exit(1) | |
| # Test 3: Basic inference | |
| if not test_inference(model, tokenizer): | |
| print("\nβ Inference test failed.") | |
| sys.exit(1) | |
| # Test 4: All tasks | |
| if not test_all_tasks(model, tokenizer): | |
| print("\nβ οΈ Some task tests failed, but model is functional.") | |
| # Final summary | |
| print("\n" + "=" * 60) | |
| print("β All tests passed!") | |
| print("=" * 60) | |
| print("\nπ Your Vish AI setup is ready!") | |
| print("\nNext steps:") | |
| print("1. Run the main application: python app.py") | |
| print("2. Access at: http://localhost:7860") | |
| print("3. (Optional) Fine-tune the model: python fine_tune_phi3.py") | |
| print("4. Deploy to Hugging Face Spaces for production") | |
| print("\n" + "=" * 60) | |
| if __name__ == "__main__": | |
| main() | |