Spaces:
Sleeping
Sleeping
Update causalml/mcp_output/mcp_plugin/mcp_service.py
Browse files
causalml/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -11,15 +11,16 @@ import numpy as np
|
|
| 11 |
import pandas as pd
|
| 12 |
from typing import Optional
|
| 13 |
|
| 14 |
-
# Import causalml components
|
| 15 |
from causalml.inference.meta import (
|
| 16 |
BaseSLearner, BaseTLearner, BaseXLearner, BaseRLearner,
|
| 17 |
BaseDRLearner, TMLELearner
|
| 18 |
)
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
|
|
|
| 23 |
from causalml.propensity import (
|
| 24 |
LogisticRegressionPropensityModel,
|
| 25 |
GradientBoostedPropensityModel
|
|
@@ -34,7 +35,6 @@ mcp = FastMCP("causalml_service")
|
|
| 34 |
|
| 35 |
# Session storage for models
|
| 36 |
_meta_learners = {}
|
| 37 |
-
_tree_models = {}
|
| 38 |
_propensity_models = {}
|
| 39 |
_matches = {}
|
| 40 |
|
|
@@ -188,88 +188,6 @@ def estimate_ate(learner_id: str, X: list, treatment: list, y: list) -> dict:
|
|
| 188 |
except Exception as e:
|
| 189 |
return {"success": False, "error": str(e)}
|
| 190 |
|
| 191 |
-
# ==================== Uplift Tree Tools ====================
|
| 192 |
-
|
| 193 |
-
@mcp.tool()
|
| 194 |
-
def create_uplift_tree(model_id: str, max_depth: int = 3, min_samples_leaf: int = 100) -> dict:
|
| 195 |
-
"""Create an Uplift Tree classifier."""
|
| 196 |
-
try:
|
| 197 |
-
model = UpliftTreeClassifier(
|
| 198 |
-
max_depth=max_depth,
|
| 199 |
-
min_samples_leaf=min_samples_leaf
|
| 200 |
-
)
|
| 201 |
-
_tree_models[model_id] = model
|
| 202 |
-
|
| 203 |
-
return {
|
| 204 |
-
"success": True,
|
| 205 |
-
"model_id": model_id,
|
| 206 |
-
"type": "UpliftTree"
|
| 207 |
-
}
|
| 208 |
-
except Exception as e:
|
| 209 |
-
return {"success": False, "error": str(e)}
|
| 210 |
-
|
| 211 |
-
@mcp.tool()
|
| 212 |
-
def create_causal_tree(model_id: str, max_depth: int = 3, min_samples_leaf: int = 100) -> dict:
|
| 213 |
-
"""Create a Causal Tree regressor."""
|
| 214 |
-
try:
|
| 215 |
-
model = CausalTreeRegressor(
|
| 216 |
-
max_depth=max_depth,
|
| 217 |
-
min_samples_leaf=min_samples_leaf
|
| 218 |
-
)
|
| 219 |
-
_tree_models[model_id] = model
|
| 220 |
-
|
| 221 |
-
return {
|
| 222 |
-
"success": True,
|
| 223 |
-
"model_id": model_id,
|
| 224 |
-
"type": "CausalTree"
|
| 225 |
-
}
|
| 226 |
-
except Exception as e:
|
| 227 |
-
return {"success": False, "error": str(e)}
|
| 228 |
-
|
| 229 |
-
@mcp.tool()
|
| 230 |
-
def fit_tree_model(model_id: str, X: list, treatment: list, y: list) -> dict:
|
| 231 |
-
"""Fit an uplift/causal tree model."""
|
| 232 |
-
try:
|
| 233 |
-
if model_id not in _tree_models:
|
| 234 |
-
return {"success": False, "error": f"Model {model_id} not found"}
|
| 235 |
-
|
| 236 |
-
model = _tree_models[model_id]
|
| 237 |
-
X_arr = np.array(X)
|
| 238 |
-
treatment_arr = np.array(treatment)
|
| 239 |
-
y_arr = np.array(y)
|
| 240 |
-
|
| 241 |
-
model.fit(X_arr, treatment_arr, y_arr)
|
| 242 |
-
|
| 243 |
-
return {
|
| 244 |
-
"success": True,
|
| 245 |
-
"model_id": model_id,
|
| 246 |
-
"n_samples": len(y),
|
| 247 |
-
"fitted": True
|
| 248 |
-
}
|
| 249 |
-
except Exception as e:
|
| 250 |
-
return {"success": False, "error": str(e)}
|
| 251 |
-
|
| 252 |
-
@mcp.tool()
|
| 253 |
-
def predict_uplift(model_id: str, X: list) -> dict:
|
| 254 |
-
"""Predict uplift scores."""
|
| 255 |
-
try:
|
| 256 |
-
if model_id not in _tree_models:
|
| 257 |
-
return {"success": False, "error": f"Model {model_id} not found"}
|
| 258 |
-
|
| 259 |
-
model = _tree_models[model_id]
|
| 260 |
-
X_arr = np.array(X)
|
| 261 |
-
|
| 262 |
-
uplift = model.predict(X_arr)
|
| 263 |
-
|
| 264 |
-
return {
|
| 265 |
-
"success": True,
|
| 266 |
-
"model_id": model_id,
|
| 267 |
-
"uplift_scores": uplift.tolist(),
|
| 268 |
-
"n_samples": len(uplift)
|
| 269 |
-
}
|
| 270 |
-
except Exception as e:
|
| 271 |
-
return {"success": False, "error": str(e)}
|
| 272 |
-
|
| 273 |
# ==================== Propensity Score Tools ====================
|
| 274 |
|
| 275 |
@mcp.tool()
|
|
@@ -465,22 +383,6 @@ def list_meta_learners() -> dict:
|
|
| 465 |
"count": len(learners)
|
| 466 |
}
|
| 467 |
|
| 468 |
-
@mcp.tool()
|
| 469 |
-
def list_tree_models() -> dict:
|
| 470 |
-
"""List all created tree models."""
|
| 471 |
-
models = []
|
| 472 |
-
for model_id, model in _tree_models.items():
|
| 473 |
-
models.append({
|
| 474 |
-
"id": model_id,
|
| 475 |
-
"type": model.__class__.__name__
|
| 476 |
-
})
|
| 477 |
-
|
| 478 |
-
return {
|
| 479 |
-
"success": True,
|
| 480 |
-
"models": models,
|
| 481 |
-
"count": len(models)
|
| 482 |
-
}
|
| 483 |
-
|
| 484 |
@mcp.tool()
|
| 485 |
def get_causalml_info() -> dict:
|
| 486 |
"""Get information about causalml library."""
|
|
@@ -494,10 +396,7 @@ def get_causalml_info() -> dict:
|
|
| 494 |
"BaseSLearner", "BaseTLearner", "BaseXLearner",
|
| 495 |
"BaseRLearner", "BaseDRLearner", "TMLELearner"
|
| 496 |
],
|
| 497 |
-
"
|
| 498 |
-
"UpliftTreeClassifier", "UpliftRandomForestClassifier",
|
| 499 |
-
"CausalTreeRegressor", "CausalRandomForestRegressor"
|
| 500 |
-
]
|
| 501 |
}
|
| 502 |
except Exception as e:
|
| 503 |
return {"success": False, "error": str(e)}
|
|
|
|
| 11 |
import pandas as pd
|
| 12 |
from typing import Optional
|
| 13 |
|
| 14 |
+
# Import causalml components (avoiding tree modules due to sklearn compatibility issues)
|
| 15 |
from causalml.inference.meta import (
|
| 16 |
BaseSLearner, BaseTLearner, BaseXLearner, BaseRLearner,
|
| 17 |
BaseDRLearner, TMLELearner
|
| 18 |
)
|
| 19 |
+
# Note: Tree models temporarily disabled due to sklearn.utils.validation compatibility
|
| 20 |
+
# from causalml.inference.tree import (
|
| 21 |
+
# UpliftTreeClassifier, UpliftRandomForestClassifier,
|
| 22 |
+
# CausalTreeRegressor, CausalRandomForestRegressor
|
| 23 |
+
# )
|
| 24 |
from causalml.propensity import (
|
| 25 |
LogisticRegressionPropensityModel,
|
| 26 |
GradientBoostedPropensityModel
|
|
|
|
| 35 |
|
| 36 |
# Session storage for models
|
| 37 |
_meta_learners = {}
|
|
|
|
| 38 |
_propensity_models = {}
|
| 39 |
_matches = {}
|
| 40 |
|
|
|
|
| 188 |
except Exception as e:
|
| 189 |
return {"success": False, "error": str(e)}
|
| 190 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
# ==================== Propensity Score Tools ====================
|
| 192 |
|
| 193 |
@mcp.tool()
|
|
|
|
| 383 |
"count": len(learners)
|
| 384 |
}
|
| 385 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 386 |
@mcp.tool()
|
| 387 |
def get_causalml_info() -> dict:
|
| 388 |
"""Get information about causalml library."""
|
|
|
|
| 396 |
"BaseSLearner", "BaseTLearner", "BaseXLearner",
|
| 397 |
"BaseRLearner", "BaseDRLearner", "TMLELearner"
|
| 398 |
],
|
| 399 |
+
"note": "Tree models (UpliftTree, CausalTree) temporarily disabled due to sklearn compatibility"
|
|
|
|
|
|
|
|
|
|
| 400 |
}
|
| 401 |
except Exception as e:
|
| 402 |
return {"success": False, "error": str(e)}
|