Spaces:
Sleeping
Sleeping
File size: 4,108 Bytes
e7b5120 | 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 | """
Test script for the refund model service
Run this to verify the model loads and can make predictions
"""
import sys
from pathlib import Path
# Add the parent directory to path to import the service
sys.path.insert(0, str(Path(__file__).parent))
try:
import xgboost as xgb
import numpy as np
import json
print("β Dependencies imported successfully")
except ImportError as e:
print(f"β Missing dependency: {e}")
print("Run: pip install xgboost numpy")
sys.exit(1)
# Test 1: Load model files
print("\n=== Test 1: Loading Model Files ===")
try:
model_path = Path(__file__).parent / '../../customer_score_models' / 'xgb_model.ubj'
features_path = Path(__file__).parent / '../../customer_score_models' / 'feature_columns.json'
assert model_path.exists(), f"Model file not found: {model_path}"
assert features_path.exists(), f"Features file not found: {features_path}"
print(f"β Model file found: {model_path}")
print(f"β Features file found: {features_path}")
# Load model
model = xgb.Booster()
model.load_model(str(model_path))
print("β XGBoost model loaded successfully")
# Load features
with open(features_path, 'r') as f:
feature_columns = json.load(f)
print(f"β Feature columns loaded: {len(feature_columns)} features")
except Exception as e:
print(f"β Error loading model: {e}")
sys.exit(1)
# Test 2: Make a test prediction
print("\n=== Test 2: Test Prediction ===")
try:
# Create a dummy feature vector (all zeros except a few)
dummy_features = np.zeros(len(feature_columns), dtype=np.float32)
# Set some realistic values
feature_dict = {col: 0 for col in feature_columns}
feature_dict['user_age'] = 35
feature_dict['address_pincode'] = 400001
feature_dict['base_price'] = 500.0
feature_dict['order_quantity'] = 1
feature_dict['product_price'] = 500.0
feature_dict['discount_applied'] = 10.0
feature_dict['days_to_return'] = 5
feature_dict['is_exchanged'] = 0
feature_dict['risk_score'] = 0.3
feature_dict['discount_ratio'] = 0.02
feature_dict['price_inverse'] = 0.002
feature_dict['days_inverse'] = 0.2
# Set one product type (example)
if 'product_name_Electronics Product' in feature_dict:
feature_dict['product_name_Electronics Product'] = 1
# Set one payment method
if 'payment_method_Credit Card' in feature_dict:
feature_dict['payment_method_Credit Card'] = 1
# Convert to array
feature_array = np.array([feature_dict[col] for col in feature_columns], dtype=np.float32)
# Make prediction
dmatrix = xgb.DMatrix(feature_array.reshape(1, -1))
prediction = model.predict(dmatrix, validate_features=False)[0]
print(f"β Test prediction successful: {prediction:.2f}%")
if 0 <= prediction <= 100:
print("β Prediction is in valid range (0-100)")
else:
print(f"β Warning: Prediction outside expected range: {prediction}")
except Exception as e:
print(f"β Error making prediction: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
# Test 3: Check feature columns structure
print("\n=== Test 3: Feature Columns Analysis ===")
try:
# Count feature types
product_features = [f for f in feature_columns if f.startswith('product_name_')]
payment_features = [f for f in feature_columns if f.startswith('payment_method_')]
numeric_features = [f for f in feature_columns if not (f.startswith('product_name_') or f.startswith('payment_method_'))]
print(f"β Numeric features: {len(numeric_features)}")
print(f" Examples: {numeric_features[:5]}")
print(f"β Product name features: {len(product_features)}")
print(f"β Payment method features: {len(payment_features)}")
print(f" Available: {[f.replace('payment_method_', '') for f in payment_features]}")
except Exception as e:
print(f"β Error analyzing features: {e}")
print("\n" + "="*50)
print("β All tests passed! Model is ready to use.")
print("="*50)
|