Spaces:
Sleeping
Sleeping
File size: 8,682 Bytes
e7b5120 376edbc 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 | import json
import logging
import os
from pathlib import Path
import xgboost as xgb
import numpy as np
from decimal import Decimal
logger = logging.getLogger(__name__)
class RefundModelService:
"""Service to predict refund percentage using XGBoost model"""
_instance = None
_model = None
_feature_columns = None
def __new__(cls):
if cls._instance is None:
cls._instance = super().__new__(cls)
return cls._instance
def __init__(self):
if self._model is None:
self._load_model()
def _load_model(self):
"""Load the XGBoost model and feature columns"""
try:
# Get the model directory (adjust path based on your Django setup)
base_dir = Path(__file__).resolve().parent.parent # backend/
model_path = base_dir / 'customer_score_models' / 'xgb_model.ubj'
features_path = base_dir / 'customer_score_models' / 'feature_columns.json'
# Load model
self._model = xgb.Booster()
self._model.load_model(str(model_path))
logger.info(f"XGBoost model loaded from {model_path}")
# Load feature columns
with open(features_path, 'r') as f:
self._feature_columns = json.load(f)
logger.info(f"Feature columns loaded: {len(self._feature_columns)} features")
except Exception as e:
logger.error(f"Error loading refund model: {str(e)}", exc_info=True)
raise
def _prepare_features(self, order_detail):
"""
Prepare feature vector from OrderDetail instance
Expected features from feature_columns.json:
- user_age
- address_pincode
- base_price
- order_quantity
- product_price
- discount_applied
- days_to_return
- is_exchanged
- risk_score (calculated)
- discount_ratio (calculated)
- price_inverse (calculated)
- days_inverse (calculated)
- product_name_* (one-hot encoded)
- payment_method_* (one-hot encoded)
"""
try:
order = order_detail.order
user = order.user
address = order.shipping_address
product = order_detail.product
# Initialize feature dictionary with all features set to 0
features = {col: 0 for col in self._feature_columns}
# Basic numerical features
features['user_age'] = user.user_age if user.user_age else 30 # default age
features['address_pincode'] = int(address.pincode) if address.pincode.isdigit() else 0
features['base_price'] = float(product.base_price)
features['order_quantity'] = int(order_detail.order_quantity)
features['product_price'] = float(order_detail.product_price)
features['discount_applied'] = float(order_detail.discount_applied)
features['days_to_return'] = int(order_detail.days_to_return)
features['is_exchanged'] = 1 if order_detail.is_exchanged else 0
# Calculated features
# Risk score (simple heuristic based on return behavior)
features['risk_score'] = self._calculate_risk_score(order_detail)
# Discount ratio
if features['product_price'] > 0:
features['discount_ratio'] = features['discount_applied'] / features['product_price']
else:
features['discount_ratio'] = 0
# Price inverse (1/price for scaling)
if features['product_price'] > 0:
features['price_inverse'] = 1.0 / features['product_price']
else:
features['price_inverse'] = 0
# Days inverse
if features['days_to_return'] > 0:
features['days_inverse'] = 1.0 / features['days_to_return']
else:
features['days_inverse'] = 0
# One-hot encode product_name
product_name_normalized = product.product_name.strip()
product_feature_name = f"product_name_{product_name_normalized}"
if product_feature_name in features:
features[product_feature_name] = 1
# One-hot encode payment_method
payment_method_feature = f"payment_method_{order.payment_method}"
if payment_method_feature in features:
features[payment_method_feature] = 1
# Convert to array in the correct order
feature_array = np.array([features[col] for col in self._feature_columns], dtype=np.float32)
return feature_array
except Exception as e:
logger.error(f"Error preparing features: {str(e)}", exc_info=True)
raise
def _calculate_risk_score(self, order_detail):
"""
Calculate a risk score for the order
This is a simple heuristic - adjust based on your business logic
"""
risk = 0.0
# Higher discount = higher risk
discount_pct = float(order_detail.discount_applied)
if discount_pct > 20:
risk += 0.3
elif discount_pct > 10:
risk += 0.15
# Quick return = higher risk
if order_detail.days_to_return < 7:
risk += 0.3
elif order_detail.days_to_return < 14:
risk += 0.15
# High price items = higher risk
if float(order_detail.product_price) > 1000:
risk += 0.2
return min(risk, 1.0) # Cap at 1.0
def predict_refund_percentage(self, order_detail):
"""
Predict refund percentage for an order detail
Args:
order_detail: OrderDetail instance
Returns:
float: Refund percentage (0-100)
"""
try:
# Prepare features
feature_array = self._prepare_features(order_detail)
# Create DMatrix for prediction
dmatrix = xgb.DMatrix(feature_array.reshape(1, -1))
# Predict
prediction = self._model.predict(dmatrix, validate_features=False)[0]
# Ensure prediction is in valid range (0-100)
refund_percentage = max(0.0, min(100.0, float(prediction)))
logger.info(f"Predicted refund percentage for OrderDetail {order_detail.id}: {refund_percentage:.2f}%")
return refund_percentage
except Exception as e:
logger.error(f"Error predicting refund percentage: {str(e)}", exc_info=True)
# Return a default safe percentage on error
return 80.0 # Default to 80% refund on error
def calculate_refund_amount(self, order_detail):
"""
Calculate actual refund amount based on predicted percentage
Args:
order_detail: OrderDetail instance
Returns:
Decimal: Refund amount
"""
try:
refund_percentage = self.predict_refund_percentage(order_detail)
# Calculate base amount (price after discount)
total_price = order_detail.product_price * order_detail.order_quantity
discount_amount = (order_detail.discount_applied / Decimal(100)) * total_price
final_price = total_price - discount_amount
# Apply refund percentage
refund_amount = final_price * Decimal((100 - refund_percentage))
# Round to 2 decimal places
refund_amount = refund_amount.quantize(Decimal('0.01'))
logger.info(
f"OrderDetail {order_detail.id}: "
f"Total={final_price}, Refund%={refund_percentage:.2f}, "
f"RefundAmount={refund_amount}"
)
return refund_amount, refund_percentage
except Exception as e:
logger.error(f"Error calculating refund amount: {str(e)}", exc_info=True)
# Return full price on error as a safe default
total = order_detail.product_price * order_detail.order_quantity
return total, 100.0
# Singleton instance getter
def get_refund_model_service():
"""Get or create the singleton RefundModelService instance"""
return RefundModelService()
|