Upload predictor.py with huggingface_hub
Browse files- predictor.py +81 -0
predictor.py
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import joblib
|
| 3 |
+
import json
|
| 4 |
+
import numpy as np
|
| 5 |
+
import pandas as pd
|
| 6 |
+
from catboost import CatBoostClassifier
|
| 7 |
+
|
| 8 |
+
class RelationshipPredictor:
|
| 9 |
+
"""
|
| 10 |
+
Relationship Longevity Predictor
|
| 11 |
+
|
| 12 |
+
Predicts compatibility between two individuals based on their
|
| 13 |
+
personal profiles, values, and interests.
|
| 14 |
+
|
| 15 |
+
Returns:
|
| 16 |
+
- compatibility_score (0-1): Predicted probability of successful match
|
| 17 |
+
- prediction: "High Compatibility" / "Moderate Compatibility" / "Low Compatibility"
|
| 18 |
+
- key_factors: Top factors driving the prediction
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
def __init__(self, model_dir="./"):
|
| 22 |
+
self.xgb = joblib.load(f"{model_dir}/xgboost_model.joblib")
|
| 23 |
+
self.lgb = joblib.load(f"{model_dir}/lightgbm_model.joblib")
|
| 24 |
+
self.cat = CatBoostClassifier()
|
| 25 |
+
self.cat.load_model(f"{model_dir}/catboost_model.cbm")
|
| 26 |
+
self.feature_cols = joblib.load(f"{model_dir}/feature_columns.joblib")
|
| 27 |
+
|
| 28 |
+
with open(f"{model_dir}/ensemble_config.json") as f:
|
| 29 |
+
self.config = json.load(f)
|
| 30 |
+
|
| 31 |
+
def predict(self, person_a: dict, person_b: dict) -> dict:
|
| 32 |
+
"""
|
| 33 |
+
Predict relationship compatibility between two people.
|
| 34 |
+
|
| 35 |
+
Args:
|
| 36 |
+
person_a: Dict with keys like age, race, interests, personality scores
|
| 37 |
+
person_b: Dict with same structure
|
| 38 |
+
|
| 39 |
+
Returns:
|
| 40 |
+
Dict with compatibility_score, prediction label, and key factors
|
| 41 |
+
"""
|
| 42 |
+
# Build feature vector from the two profiles
|
| 43 |
+
features = self._build_features(person_a, person_b)
|
| 44 |
+
|
| 45 |
+
# Ensemble prediction
|
| 46 |
+
xgb_prob = self.xgb.predict_proba(features)[:, 1][0]
|
| 47 |
+
lgb_prob = self.lgb.predict_proba(features)[:, 1][0]
|
| 48 |
+
cat_prob = self.cat.predict_proba(features)[:, 1][0]
|
| 49 |
+
|
| 50 |
+
w = self.config['weights']
|
| 51 |
+
score = w['xgboost'] * xgb_prob + w['lightgbm'] * lgb_prob + w['catboost'] * cat_prob
|
| 52 |
+
|
| 53 |
+
if score >= 0.7:
|
| 54 |
+
label = "High Compatibility"
|
| 55 |
+
elif score >= 0.4:
|
| 56 |
+
label = "Moderate Compatibility"
|
| 57 |
+
else:
|
| 58 |
+
label = "Low Compatibility"
|
| 59 |
+
|
| 60 |
+
return {
|
| 61 |
+
'compatibility_score': round(float(score), 4),
|
| 62 |
+
'prediction': label,
|
| 63 |
+
'individual_models': {
|
| 64 |
+
'xgboost': round(float(xgb_prob), 4),
|
| 65 |
+
'lightgbm': round(float(lgb_prob), 4),
|
| 66 |
+
'catboost': round(float(cat_prob), 4),
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
def _build_features(self, a, b):
|
| 71 |
+
"""Build engineered feature vector from two person profiles."""
|
| 72 |
+
# This would map raw profile inputs to the trained feature space
|
| 73 |
+
# Implementation depends on the input format
|
| 74 |
+
raise NotImplementedError(
|
| 75 |
+
"Implement feature mapping based on your input format. "
|
| 76 |
+
"See feature_columns.joblib for required features."
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
# Usage example:
|
| 80 |
+
# predictor = RelationshipPredictor("./model_output")
|
| 81 |
+
# result = predictor.predict(person_a_profile, person_b_profile)
|