| import joblib
|
| import os
|
| import pandas as pd
|
| import numpy as np
|
|
|
|
|
| kmeans_model = None
|
| clv_model = None
|
| scaler = None
|
|
|
| def load_models():
|
| """Load all trained ML models"""
|
| global kmeans_model, clv_model, scaler
|
|
|
|
|
| current_dir = os.path.dirname(os.path.abspath(__file__))
|
| project_root = os.path.dirname(current_dir)
|
| models_path = os.path.join(project_root, "models")
|
|
|
| print(f"Looking for models in: {models_path}")
|
|
|
| if not os.path.exists(models_path):
|
| print(f"Models folder not found at: {models_path}")
|
| return False
|
|
|
| try:
|
|
|
| kmeans_path = os.path.join(models_path, "kmeans_model.pkl")
|
| if os.path.exists(kmeans_path):
|
| kmeans_model = joblib.load(kmeans_path)
|
| print("K-Means model loaded")
|
| else:
|
| print(f"File not found: {kmeans_path}")
|
| kmeans_model = None
|
| except Exception as e:
|
| print(f"Could not load K-Means model: {e}")
|
| kmeans_model = None
|
|
|
| try:
|
|
|
| clv_path = os.path.join(models_path, "clv_model.pkl")
|
| if os.path.exists(clv_path):
|
| clv_model = joblib.load(clv_path)
|
| print("CLV model loaded")
|
| else:
|
| print(f"File not found: {clv_path}")
|
| clv_model = None
|
| except Exception as e:
|
| print(f"Could not load CLV model: {e}")
|
| clv_model = None
|
|
|
| try:
|
|
|
| scaler_path = os.path.join(models_path, "scaler.pkl")
|
| if os.path.exists(scaler_path):
|
| scaler = joblib.load(scaler_path)
|
| print("Scaler loaded")
|
| else:
|
| print(f"File not found: {scaler_path}")
|
| scaler = None
|
| except Exception as e:
|
| print(f"Could not load scaler: {e}")
|
| scaler = None
|
|
|
| return kmeans_model is not None or clv_model is not None
|
|
|
| def predict_segment(recency, frequency, monetary):
|
| """Predict customer segment using K-Means model"""
|
| if kmeans_model is None or scaler is None:
|
| return {"error": "Models not loaded"}
|
|
|
|
|
| customer_data = pd.DataFrame({
|
| 'Recency': [recency],
|
| 'Frequency': [frequency],
|
| 'Monetary': [monetary]
|
| })
|
|
|
|
|
| scaled_data = scaler.transform(customer_data)
|
|
|
|
|
| cluster = kmeans_model.predict(scaled_data)[0]
|
|
|
|
|
| segment_map = {
|
| 0: "At-Risk Customers",
|
| 1: "VIP Customers",
|
| 2: "Loyal Regulars",
|
| 3: "New/Occasional"
|
| }
|
|
|
| return {
|
| "cluster": int(cluster),
|
| "segment": segment_map.get(cluster, "Unknown")
|
| }
|
|
|
| def predict_clv(features_dict):
|
| """
|
| Predict Customer Lifetime Value
|
| features_dict should contain all 9 features
|
| """
|
| if clv_model is None:
|
| return {"error": "CLV model not loaded"}
|
|
|
|
|
| feature_columns = [
|
| 'frequency', 'recency', 'avg_quantity', 'avg_unit_price',
|
| 'avg_transaction', 'lifespan_days', 'avg_days_between_purchases',
|
| 'purchases_per_month', 'total_quantity'
|
| ]
|
|
|
|
|
| features = []
|
| for col in feature_columns:
|
| features.append(features_dict.get(col, 0))
|
|
|
|
|
| features_array = np.array(features).reshape(1, -1)
|
|
|
|
|
| prediction = clv_model.predict(features_array)[0]
|
|
|
|
|
| if prediction < 500:
|
| category = "Low Value"
|
| elif prediction < 2000:
|
| category = "Medium Value"
|
| elif prediction < 5000:
|
| category = "High Value"
|
| else:
|
| category = "VIP"
|
|
|
| return {
|
| "predicted_clv": round(prediction, 2),
|
| "value_category": category
|
| } |