File size: 1,665 Bytes
09ca083
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

import requests
import json
import sys

# We can run this LOCALLY against the "app" logic if we import it, 
# But easier to just unit test `preprocessing.py` and model loading.

from preprocessing import preprocess_input, EXPECTED_COLS
import joblib
import pandas as pd

# Mock Input mimicking the new frontend
mock_input = {
    "policy_annual_premium": 1200,
    "total_claim_amount": 52000,
    "vehicle_age": 12,
    "days_since_bind": 300,
    "months_as_customer": 48,
    "capital-gains": 0,
    "capital-loss": 0,
    "injury_share": 0.1,
    "property_share": 0.1,
    "umbrella_limit": 0,
    "incident_hour_of_the_day": 14,
    
    # Cats
    "incident_severity": "Major Damage",
    "collision_type": "Rear Collision",
    "police_report_available": "YES",
    "authorities_contacted": "Police",
    "number_of_vehicles_involved": 2,
    "bodily_injuries": 1
}

print("Running Preprocessing...")
df = preprocess_input(mock_input)
print("Resulting DataFrame shape:", df.shape)
print("Columns:", list(df.columns))

# Verify specific engineered cols
print(f"Hour Bin: {df['hour_bin_4'].values[0]}")
print(f"Collision Missing Flag: {df['collision_type_missing'].values[0]}")

# Load Model
print("\nLoading Model...")
models = joblib.load("models/best_tree_models_uncalibrated.joblib")
model = list(models['Trees'].values())[0]

print("Predicting...")
try:
    pred = model.predict_proba(df)
    print(f"Success! Prediction: {pred[0]}")
except Exception as e:
    print(f"FAILED: {e}")
    # Inspect difference
    if hasattr(model, 'feature_names_in_'):
        miss = set(model.feature_names_in_) - set(df.columns)
        print(f"Missing cols: {miss}")