import joblib import xgboost as xgb import shap def processing(processed_dict): # model_path = r"C:\Users\abinisha\citrus\centurion-dataform-mcard\flowx\model_1\artifacts\xgboost_model_v2.joblib" model_path = "./xgboost_model.joblib" model = joblib.load(model_path) explainer = shap.TreeExplainer(model) feature_names = model.feature_names # build and score X = [processed_dict[f] for f in feature_names] dmatrix = xgb.DMatrix([X], feature_names=feature_names) pred = float(model.predict(dmatrix)[0]) # get SHAP values as 1×n_features shap_matrix = explainer.shap_values(dmatrix) shap_vals = shap_matrix[0] if getattr(shap_matrix, "ndim", 1) > 1 else shap_matrix # rank & sort features by descending SHAP impact ranked = sorted(zip(feature_names, shap_vals), key=lambda fv: fv[1], reverse=True) # build a *list* of pre-sorted reason-codes shape_reasoncode = [ { "feature": feat, "value": processed_dict[feat], "shap_rank": i+1 } for i, (feat, _) in enumerate(ranked) ] return { "prediction": pred, "shape_reasoncode": shape_reasoncode }