Upload folder using huggingface_hub
Browse files- ccfd_1.0_decision-tree.pkl +3 -0
- ccfd_1.0_random-forest.pkl +3 -0
- ccfd_1.0_xg-boost.pkl +3 -0
- handler.py +106 -0
- requirements.txt +4 -0
ccfd_1.0_decision-tree.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:3fbdba46e3c71e148e877b8d61b5f66afad69087e521cdf8b8b694affdeb3374
|
| 3 |
+
size 155243
|
ccfd_1.0_random-forest.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:d30529b90df0f17fa7396347c4230061093ab45c200307b0ce65fb3f5288e12b
|
| 3 |
+
size 43463794
|
ccfd_1.0_xg-boost.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e53f13355a23ff71608e94bbd3af142a30b8535b76fe78e96433c9202e5debd4
|
| 3 |
+
size 5746222
|
handler.py
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import joblib # Use joblib to load files saved with joblib.dump
|
| 3 |
+
import pandas as pd
|
| 4 |
+
from typing import Dict, Any, List
|
| 5 |
+
|
| 6 |
+
# Define the models and the version based on the training script output
|
| 7 |
+
class EndpointHandler:
|
| 8 |
+
VERSION = "1.0"
|
| 9 |
+
|
| 10 |
+
# Maps user-friendly aliases to the EXACT filenames from your training script
|
| 11 |
+
MODEL_MAP = {
|
| 12 |
+
"decision_tree": "classifier_FULL_Decision_Tree.pkl",
|
| 13 |
+
"random_forest": "classifier_FULL_Random_Forest.pkl",
|
| 14 |
+
"xgboost": "classifier_FULL_XGBoost.pkl",
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
# List of all features the model expects (is_fraud is excluded as it's the target)
|
| 18 |
+
EXPECTED_FEATURES = [
|
| 19 |
+
"cc_num", "merchant", "category", "amt", "gender", "state", "zip",
|
| 20 |
+
"lat", "long", "city_pop", "job", "unix_time", "merch_lat",
|
| 21 |
+
"merch_long", "age", "trans_hour", "trans_day", "trans_month",
|
| 22 |
+
"trans_weekday", "distance"
|
| 23 |
+
]
|
| 24 |
+
|
| 25 |
+
def __init__(self, path="."):
|
| 26 |
+
"""Loads all three Pipeline objects using joblib."""
|
| 27 |
+
self.models = {}
|
| 28 |
+
print(f"Server starting up for version: {self.VERSION}")
|
| 29 |
+
|
| 30 |
+
for alias, filename in self.MODEL_MAP.items():
|
| 31 |
+
model_path = os.path.join(path, filename)
|
| 32 |
+
try:
|
| 33 |
+
# Use joblib.load for files saved with joblib.dump
|
| 34 |
+
self.models[alias] = joblib.load(model_path)
|
| 35 |
+
print(f"✅ Pipeline loaded for {alias}")
|
| 36 |
+
except Exception as e:
|
| 37 |
+
# Note: Errors here are often due to package version mismatch
|
| 38 |
+
print(f"❌ Error loading {filename}. Check scikit-learn/xgboost versions: {e}")
|
| 39 |
+
|
| 40 |
+
def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
|
| 41 |
+
"""Handles the API request, selects the model, and performs inference."""
|
| 42 |
+
|
| 43 |
+
inputs = data.get("inputs", {})
|
| 44 |
+
target_model_alias = inputs.get("model_name")
|
| 45 |
+
requested_version = inputs.get("model_version")
|
| 46 |
+
features_list = inputs.get("features")
|
| 47 |
+
|
| 48 |
+
base_response = {"server_version": self.VERSION}
|
| 49 |
+
|
| 50 |
+
# 1. Validation Checks
|
| 51 |
+
if requested_version != self.VERSION:
|
| 52 |
+
return {
|
| 53 |
+
**base_response,
|
| 54 |
+
"error": f"Requested version '{requested_version}' does not match server version '{self.VERSION}'."
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
if not target_model_alias or target_model_alias not in self.models:
|
| 58 |
+
return {
|
| 59 |
+
**base_response,
|
| 60 |
+
"error": f"Model '{target_model_alias}' not found. Available: {list(self.MODEL_MAP.keys())}",
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
if not features_list:
|
| 64 |
+
return {
|
| 65 |
+
**base_response,
|
| 66 |
+
"error": "No transaction features provided in the 'features' list."
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
# 2. Prepare Data
|
| 70 |
+
model_pipeline = self.models[target_model_alias]
|
| 71 |
+
|
| 72 |
+
try:
|
| 73 |
+
# Convert list of dicts to DataFrame and ensure column order matches training data
|
| 74 |
+
df_features = pd.DataFrame(features_list)
|
| 75 |
+
|
| 76 |
+
# CRITICAL: Reindex to ensure the columns are in the exact order the pipeline expects
|
| 77 |
+
if set(df_features.columns) != set(self.EXPECTED_FEATURES):
|
| 78 |
+
return {
|
| 79 |
+
**base_response,
|
| 80 |
+
"error": "Input features do not match expected features. Check column names."
|
| 81 |
+
}
|
| 82 |
+
df_features = df_features[self.EXPECTED_FEATURES]
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
return {
|
| 86 |
+
**base_response,
|
| 87 |
+
"error": f"Data preparation failed. Ensure JSON fields match all expected features: {str(e)}"
|
| 88 |
+
}
|
| 89 |
+
|
| 90 |
+
# 3. Predict Probability
|
| 91 |
+
try:
|
| 92 |
+
# predict_proba runs the ColumnTransformer (preprocessing) and then the classifier
|
| 93 |
+
# We take the probability of the positive class (Fraud=1), which is column [:, 1]
|
| 94 |
+
probabilities = model_pipeline.predict_proba(df_features)[:, 1]
|
| 95 |
+
|
| 96 |
+
return {
|
| 97 |
+
**base_response,
|
| 98 |
+
"model_used": target_model_alias,
|
| 99 |
+
"model_version": requested_version,
|
| 100 |
+
"prediction_probabilities": probabilities.tolist(),
|
| 101 |
+
}
|
| 102 |
+
except Exception as e:
|
| 103 |
+
return {
|
| 104 |
+
**base_response,
|
| 105 |
+
"error": f"Prediction execution failed. This may indicate a data type mismatch: {str(e)}",
|
| 106 |
+
}
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
pandas
|
| 2 |
+
scikit-learn
|
| 3 |
+
xgboost
|
| 4 |
+
joblib
|