Spaces:
Sleeping
Sleeping
Update auto-sklearn/mcp_output/mcp_plugin/mcp_service.py
Browse files
auto-sklearn/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -1,260 +1,451 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastmcp import FastMCP
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
# Create the FastMCP service application
|
| 4 |
mcp = FastMCP("auto_sklearn_service")
|
| 5 |
|
| 6 |
-
#
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
def example_tool() -> dict:
|
| 11 |
"""
|
| 12 |
-
|
| 13 |
|
| 14 |
Returns:
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
"""
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
-
|
| 23 |
|
| 24 |
Parameters:
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
Returns:
|
| 28 |
-
|
| 29 |
"""
|
| 30 |
try:
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
return {
|
| 34 |
"success": True,
|
| 35 |
-
"
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
| 39 |
}
|
| 40 |
except Exception as e:
|
| 41 |
-
return {"success": False, "error": str(e)}
|
| 42 |
|
| 43 |
|
| 44 |
-
@mcp.tool(name="
|
| 45 |
-
def
|
| 46 |
"""
|
| 47 |
-
|
| 48 |
|
| 49 |
Parameters:
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
Returns:
|
| 55 |
-
|
| 56 |
"""
|
| 57 |
try:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
)
|
|
|
|
|
|
|
|
|
|
| 64 |
return {
|
| 65 |
"success": True,
|
| 66 |
-
"
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
| 70 |
}
|
| 71 |
except Exception as e:
|
| 72 |
-
return {"success": False, "error": str(e)}
|
| 73 |
|
| 74 |
|
| 75 |
-
@mcp.tool(name="
|
| 76 |
-
def
|
| 77 |
"""
|
| 78 |
-
Fit
|
| 79 |
|
| 80 |
Parameters:
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
- dataset_name: Name of the dataset.
|
| 85 |
|
| 86 |
Returns:
|
| 87 |
-
|
| 88 |
"""
|
| 89 |
try:
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
return {
|
| 92 |
"success": True,
|
| 93 |
-
"
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
}
|
| 96 |
except Exception as e:
|
| 97 |
-
return {"success": False, "error": str(e)}
|
| 98 |
|
| 99 |
|
| 100 |
-
@mcp.tool(name="
|
| 101 |
-
def
|
| 102 |
"""
|
| 103 |
-
Make predictions using
|
| 104 |
|
| 105 |
Parameters:
|
| 106 |
-
|
| 107 |
-
|
| 108 |
|
| 109 |
Returns:
|
| 110 |
-
|
| 111 |
"""
|
| 112 |
try:
|
| 113 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
return {
|
| 115 |
"success": True,
|
| 116 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 117 |
}
|
| 118 |
except Exception as e:
|
| 119 |
-
return {"success": False, "error": str(e)}
|
| 120 |
|
| 121 |
|
| 122 |
-
@mcp.tool(name="
|
| 123 |
-
def
|
| 124 |
"""
|
| 125 |
-
|
| 126 |
|
| 127 |
Parameters:
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
- time_limit: Total time limit for the AutoML process.
|
| 131 |
-
- per_run_time_limit: Time limit for each model training.
|
| 132 |
-
- memory_limit: Memory limit for each model training (default: 3072MB).
|
| 133 |
|
| 134 |
Returns:
|
| 135 |
-
|
| 136 |
"""
|
| 137 |
try:
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
|
|
|
| 145 |
return {
|
| 146 |
"success": True,
|
| 147 |
-
"
|
| 148 |
-
|
| 149 |
-
|
|
|
|
|
|
|
| 150 |
}
|
| 151 |
except Exception as e:
|
| 152 |
-
return {"success": False, "error": str(e)}
|
| 153 |
|
| 154 |
|
| 155 |
-
@mcp.tool(name="
|
| 156 |
-
def
|
| 157 |
"""
|
| 158 |
-
|
| 159 |
|
| 160 |
Parameters:
|
| 161 |
-
|
| 162 |
-
- X_test: Test features.
|
| 163 |
-
- y_test: Test labels.
|
| 164 |
|
| 165 |
Returns:
|
| 166 |
-
|
| 167 |
"""
|
| 168 |
try:
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 172 |
return {
|
| 173 |
"success": True,
|
| 174 |
-
"
|
| 175 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 176 |
}
|
| 177 |
except Exception as e:
|
| 178 |
-
return {"success": False, "error": str(e)}
|
| 179 |
|
| 180 |
|
| 181 |
-
@mcp.tool(name="
|
| 182 |
-
def
|
| 183 |
"""
|
| 184 |
-
Get
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
Returns:
|
| 187 |
-
|
| 188 |
"""
|
| 189 |
try:
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
classifiers = classification_components.ClassifierChoice.get_components()
|
| 194 |
-
preprocessors = preprocessing_components.PreprocessorChoice.get_components()
|
| 195 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 196 |
return {
|
| 197 |
"success": True,
|
| 198 |
-
"
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
}
|
| 201 |
except Exception as e:
|
| 202 |
-
return {"success": False, "error": str(e)}
|
| 203 |
|
| 204 |
|
| 205 |
-
@mcp.tool(name="
|
| 206 |
-
def
|
| 207 |
"""
|
| 208 |
-
|
| 209 |
|
| 210 |
Parameters:
|
| 211 |
-
|
| 212 |
-
- time_limit: Total time limit for the meta-learning process.
|
| 213 |
-
- per_run_time_limit: Time limit for each meta-learning iteration.
|
| 214 |
|
| 215 |
Returns:
|
| 216 |
-
|
| 217 |
"""
|
| 218 |
try:
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
return {
|
| 225 |
"success": True,
|
| 226 |
-
"message": "
|
| 227 |
-
"
|
| 228 |
}
|
| 229 |
except Exception as e:
|
| 230 |
-
return {"success": False, "error": str(e)}
|
| 231 |
|
| 232 |
|
| 233 |
-
@mcp.tool(name="
|
| 234 |
-
def
|
| 235 |
"""
|
| 236 |
-
|
| 237 |
|
| 238 |
Parameters:
|
| 239 |
-
|
| 240 |
|
| 241 |
Returns:
|
| 242 |
-
|
| 243 |
"""
|
| 244 |
try:
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
return {
|
| 247 |
"success": True,
|
| 248 |
-
"
|
|
|
|
| 249 |
}
|
| 250 |
except Exception as e:
|
| 251 |
-
return {"success": False, "error": str(e)}
|
|
|
|
| 252 |
|
| 253 |
def create_app() -> FastMCP:
|
| 254 |
"""
|
| 255 |
Create and return the FastMCP application instance.
|
| 256 |
|
| 257 |
Returns:
|
| 258 |
-
|
| 259 |
"""
|
| 260 |
-
return mcp
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import sys
|
| 3 |
+
from typing import Dict, Any, List, Optional
|
| 4 |
+
import json
|
| 5 |
+
|
| 6 |
from fastmcp import FastMCP
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
# Import core modules from installed auto-sklearn package
|
| 10 |
+
from autosklearn.estimators import AutoSklearnClassifier, AutoSklearnRegressor
|
| 11 |
|
| 12 |
# Create the FastMCP service application
|
| 13 |
mcp = FastMCP("auto_sklearn_service")
|
| 14 |
|
| 15 |
+
# Store models by ID
|
| 16 |
+
_classifiers: Dict[str, AutoSklearnClassifier] = {}
|
| 17 |
+
_regressors: Dict[str, AutoSklearnRegressor] = {}
|
| 18 |
+
|
| 19 |
|
| 20 |
+
@mcp.tool(name="get_library_info")
|
| 21 |
+
def get_library_info() -> dict:
|
|
|
|
| 22 |
"""
|
| 23 |
+
Get information about the auto-sklearn library.
|
| 24 |
|
| 25 |
Returns:
|
| 26 |
+
dict: Version and configuration information.
|
| 27 |
+
"""
|
| 28 |
+
try:
|
| 29 |
+
from autosklearn import __version__
|
| 30 |
+
|
| 31 |
+
return {
|
| 32 |
+
"success": True,
|
| 33 |
+
"result": {
|
| 34 |
+
"library": "auto-sklearn",
|
| 35 |
+
"version": __version__,
|
| 36 |
+
"estimators": ["AutoSklearnClassifier", "AutoSklearnRegressor"],
|
| 37 |
+
"features": [
|
| 38 |
+
"Automated Machine Learning",
|
| 39 |
+
"Ensemble Learning",
|
| 40 |
+
"Meta-learning",
|
| 41 |
+
"Hyperparameter Optimization (SMAC)",
|
| 42 |
+
],
|
| 43 |
+
},
|
| 44 |
+
"error": None,
|
| 45 |
+
}
|
| 46 |
+
except Exception as e:
|
| 47 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
@mcp.tool(name="create_classifier")
|
| 51 |
+
def create_classifier(
|
| 52 |
+
classifier_id: str,
|
| 53 |
+
time_left_for_this_task: int = 3600,
|
| 54 |
+
per_run_time_limit: Optional[int] = None,
|
| 55 |
+
ensemble_size: int = 50,
|
| 56 |
+
ensemble_nbest: int = 50,
|
| 57 |
+
seed: int = 1,
|
| 58 |
+
memory_limit: int = 3072,
|
| 59 |
+
n_jobs: Optional[int] = None,
|
| 60 |
+
) -> dict:
|
| 61 |
"""
|
| 62 |
+
Create a new AutoSklearnClassifier.
|
| 63 |
+
|
| 64 |
+
Parameters:
|
| 65 |
+
classifier_id (str): Unique identifier for the classifier.
|
| 66 |
+
time_left_for_this_task (int): Total time budget in seconds.
|
| 67 |
+
per_run_time_limit (Optional[int]): Time limit per model evaluation.
|
| 68 |
+
ensemble_size (int): Number of models in the final ensemble.
|
| 69 |
+
ensemble_nbest (int): Consider only the best n models for ensemble.
|
| 70 |
+
seed (int): Random seed.
|
| 71 |
+
memory_limit (int): Memory limit in MB.
|
| 72 |
+
n_jobs (Optional[int]): Number of parallel jobs.
|
| 73 |
|
| 74 |
+
Returns:
|
| 75 |
+
dict: Success status and classifier information.
|
| 76 |
+
"""
|
| 77 |
+
try:
|
| 78 |
+
if classifier_id in _classifiers:
|
| 79 |
+
return {"success": False, "result": None, "error": f"Classifier '{classifier_id}' already exists"}
|
| 80 |
+
|
| 81 |
+
clf = AutoSklearnClassifier(
|
| 82 |
+
time_left_for_this_task=time_left_for_this_task,
|
| 83 |
+
per_run_time_limit=per_run_time_limit,
|
| 84 |
+
ensemble_size=ensemble_size,
|
| 85 |
+
ensemble_nbest=ensemble_nbest,
|
| 86 |
+
seed=seed,
|
| 87 |
+
memory_limit=memory_limit,
|
| 88 |
+
n_jobs=n_jobs,
|
| 89 |
+
)
|
| 90 |
+
|
| 91 |
+
_classifiers[classifier_id] = clf
|
| 92 |
+
|
| 93 |
+
return {
|
| 94 |
+
"success": True,
|
| 95 |
+
"result": {
|
| 96 |
+
"classifier_id": classifier_id,
|
| 97 |
+
"time_budget": time_left_for_this_task,
|
| 98 |
+
"ensemble_size": ensemble_size,
|
| 99 |
+
"message": "Classifier created successfully",
|
| 100 |
+
},
|
| 101 |
+
"error": None,
|
| 102 |
+
}
|
| 103 |
+
except Exception as e:
|
| 104 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 105 |
+
|
| 106 |
+
|
| 107 |
+
@mcp.tool(name="create_regressor")
|
| 108 |
+
def create_regressor(
|
| 109 |
+
regressor_id: str,
|
| 110 |
+
time_left_for_this_task: int = 3600,
|
| 111 |
+
per_run_time_limit: Optional[int] = None,
|
| 112 |
+
ensemble_size: int = 50,
|
| 113 |
+
ensemble_nbest: int = 50,
|
| 114 |
+
seed: int = 1,
|
| 115 |
+
memory_limit: int = 3072,
|
| 116 |
+
n_jobs: Optional[int] = None,
|
| 117 |
+
) -> dict:
|
| 118 |
"""
|
| 119 |
+
Create a new AutoSklearnRegressor.
|
| 120 |
|
| 121 |
Parameters:
|
| 122 |
+
regressor_id (str): Unique identifier for the regressor.
|
| 123 |
+
time_left_for_this_task (int): Total time budget in seconds.
|
| 124 |
+
per_run_time_limit (Optional[int]): Time limit per model evaluation.
|
| 125 |
+
ensemble_size (int): Number of models in the final ensemble.
|
| 126 |
+
ensemble_nbest (int): Consider only the best n models for ensemble.
|
| 127 |
+
seed (int): Random seed.
|
| 128 |
+
memory_limit (int): Memory limit in MB.
|
| 129 |
+
n_jobs (Optional[int]): Number of parallel jobs.
|
| 130 |
|
| 131 |
Returns:
|
| 132 |
+
dict: Success status and regressor information.
|
| 133 |
"""
|
| 134 |
try:
|
| 135 |
+
if regressor_id in _regressors:
|
| 136 |
+
return {"success": False, "result": None, "error": f"Regressor '{regressor_id}' already exists"}
|
| 137 |
+
|
| 138 |
+
reg = AutoSklearnRegressor(
|
| 139 |
+
time_left_for_this_task=time_left_for_this_task,
|
| 140 |
+
per_run_time_limit=per_run_time_limit,
|
| 141 |
+
ensemble_size=ensemble_size,
|
| 142 |
+
ensemble_nbest=ensemble_nbest,
|
| 143 |
+
seed=seed,
|
| 144 |
+
memory_limit=memory_limit,
|
| 145 |
+
n_jobs=n_jobs,
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
_regressors[regressor_id] = reg
|
| 149 |
+
|
| 150 |
return {
|
| 151 |
"success": True,
|
| 152 |
+
"result": {
|
| 153 |
+
"regressor_id": regressor_id,
|
| 154 |
+
"time_budget": time_left_for_this_task,
|
| 155 |
+
"ensemble_size": ensemble_size,
|
| 156 |
+
"message": "Regressor created successfully",
|
| 157 |
+
},
|
| 158 |
+
"error": None,
|
| 159 |
}
|
| 160 |
except Exception as e:
|
| 161 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 162 |
|
| 163 |
|
| 164 |
+
@mcp.tool(name="fit_classifier")
|
| 165 |
+
def fit_classifier(classifier_id: str, X_train: List[List[float]], y_train: List) -> dict:
|
| 166 |
"""
|
| 167 |
+
Fit a classifier with training data.
|
| 168 |
|
| 169 |
Parameters:
|
| 170 |
+
classifier_id (str): ID of the classifier to fit.
|
| 171 |
+
X_train (List[List[float]]): Training features.
|
| 172 |
+
y_train (List): Training labels.
|
| 173 |
|
| 174 |
Returns:
|
| 175 |
+
dict: Success status and fitting information.
|
| 176 |
"""
|
| 177 |
try:
|
| 178 |
+
if classifier_id not in _classifiers:
|
| 179 |
+
return {"success": False, "result": None, "error": f"Classifier '{classifier_id}' not found"}
|
| 180 |
+
|
| 181 |
+
clf = _classifiers[classifier_id]
|
| 182 |
+
X_train_np = np.array(X_train)
|
| 183 |
+
y_train_np = np.array(y_train)
|
| 184 |
+
|
| 185 |
+
clf.fit(X_train_np, y_train_np)
|
| 186 |
+
|
| 187 |
return {
|
| 188 |
"success": True,
|
| 189 |
+
"result": {
|
| 190 |
+
"classifier_id": classifier_id,
|
| 191 |
+
"num_samples": len(X_train),
|
| 192 |
+
"num_features": len(X_train[0]) if X_train else 0,
|
| 193 |
+
"message": "Classifier fitted successfully",
|
| 194 |
+
},
|
| 195 |
+
"error": None,
|
| 196 |
}
|
| 197 |
except Exception as e:
|
| 198 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 199 |
|
| 200 |
|
| 201 |
+
@mcp.tool(name="fit_regressor")
|
| 202 |
+
def fit_regressor(regressor_id: str, X_train: List[List[float]], y_train: List[float]) -> dict:
|
| 203 |
"""
|
| 204 |
+
Fit a regressor with training data.
|
| 205 |
|
| 206 |
Parameters:
|
| 207 |
+
regressor_id (str): ID of the regressor to fit.
|
| 208 |
+
X_train (List[List[float]]): Training features.
|
| 209 |
+
y_train (List[float]): Training targets.
|
|
|
|
| 210 |
|
| 211 |
Returns:
|
| 212 |
+
dict: Success status and fitting information.
|
| 213 |
"""
|
| 214 |
try:
|
| 215 |
+
if regressor_id not in _regressors:
|
| 216 |
+
return {"success": False, "result": None, "error": f"Regressor '{regressor_id}' not found"}
|
| 217 |
+
|
| 218 |
+
reg = _regressors[regressor_id]
|
| 219 |
+
X_train_np = np.array(X_train)
|
| 220 |
+
y_train_np = np.array(y_train)
|
| 221 |
+
|
| 222 |
+
reg.fit(X_train_np, y_train_np)
|
| 223 |
+
|
| 224 |
return {
|
| 225 |
"success": True,
|
| 226 |
+
"result": {
|
| 227 |
+
"regressor_id": regressor_id,
|
| 228 |
+
"num_samples": len(X_train),
|
| 229 |
+
"num_features": len(X_train[0]) if X_train else 0,
|
| 230 |
+
"message": "Regressor fitted successfully",
|
| 231 |
+
},
|
| 232 |
+
"error": None,
|
| 233 |
}
|
| 234 |
except Exception as e:
|
| 235 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 236 |
|
| 237 |
|
| 238 |
+
@mcp.tool(name="predict_classifier")
|
| 239 |
+
def predict_classifier(classifier_id: str, X_test: List[List[float]]) -> dict:
|
| 240 |
"""
|
| 241 |
+
Make predictions using a fitted classifier.
|
| 242 |
|
| 243 |
Parameters:
|
| 244 |
+
classifier_id (str): ID of the classifier.
|
| 245 |
+
X_test (List[List[float]]): Test features.
|
| 246 |
|
| 247 |
Returns:
|
| 248 |
+
dict: Predictions.
|
| 249 |
"""
|
| 250 |
try:
|
| 251 |
+
if classifier_id not in _classifiers:
|
| 252 |
+
return {"success": False, "result": None, "error": f"Classifier '{classifier_id}' not found"}
|
| 253 |
+
|
| 254 |
+
clf = _classifiers[classifier_id]
|
| 255 |
+
X_test_np = np.array(X_test)
|
| 256 |
+
|
| 257 |
+
predictions = clf.predict(X_test_np)
|
| 258 |
+
|
| 259 |
return {
|
| 260 |
"success": True,
|
| 261 |
+
"result": {
|
| 262 |
+
"predictions": predictions.tolist(),
|
| 263 |
+
"num_predictions": len(predictions),
|
| 264 |
+
},
|
| 265 |
+
"error": None,
|
| 266 |
}
|
| 267 |
except Exception as e:
|
| 268 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 269 |
|
| 270 |
|
| 271 |
+
@mcp.tool(name="predict_regressor")
|
| 272 |
+
def predict_regressor(regressor_id: str, X_test: List[List[float]]) -> dict:
|
| 273 |
"""
|
| 274 |
+
Make predictions using a fitted regressor.
|
| 275 |
|
| 276 |
Parameters:
|
| 277 |
+
regressor_id (str): ID of the regressor.
|
| 278 |
+
X_test (List[List[float]]): Test features.
|
|
|
|
|
|
|
|
|
|
| 279 |
|
| 280 |
Returns:
|
| 281 |
+
dict: Predictions.
|
| 282 |
"""
|
| 283 |
try:
|
| 284 |
+
if regressor_id not in _regressors:
|
| 285 |
+
return {"success": False, "result": None, "error": f"Regressor '{regressor_id}' not found"}
|
| 286 |
+
|
| 287 |
+
reg = _regressors[regressor_id]
|
| 288 |
+
X_test_np = np.array(X_test)
|
| 289 |
+
|
| 290 |
+
predictions = reg.predict(X_test_np)
|
| 291 |
+
|
| 292 |
return {
|
| 293 |
"success": True,
|
| 294 |
+
"result": {
|
| 295 |
+
"predictions": predictions.tolist(),
|
| 296 |
+
"num_predictions": len(predictions),
|
| 297 |
+
},
|
| 298 |
+
"error": None,
|
| 299 |
}
|
| 300 |
except Exception as e:
|
| 301 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 302 |
|
| 303 |
|
| 304 |
+
@mcp.tool(name="get_classifier_model_performance")
|
| 305 |
+
def get_classifier_model_performance(classifier_id: str) -> dict:
|
| 306 |
"""
|
| 307 |
+
Get performance statistics of explored models.
|
| 308 |
|
| 309 |
Parameters:
|
| 310 |
+
classifier_id (str): ID of the classifier.
|
|
|
|
|
|
|
| 311 |
|
| 312 |
Returns:
|
| 313 |
+
dict: Performance statistics.
|
| 314 |
"""
|
| 315 |
try:
|
| 316 |
+
if classifier_id not in _classifiers:
|
| 317 |
+
return {"success": False, "result": None, "error": f"Classifier '{classifier_id}' not found"}
|
| 318 |
+
|
| 319 |
+
clf = _classifiers[classifier_id]
|
| 320 |
+
|
| 321 |
+
# Get leaderboard information
|
| 322 |
+
leaderboard = clf.leaderboard()
|
| 323 |
+
|
| 324 |
return {
|
| 325 |
"success": True,
|
| 326 |
+
"result": {
|
| 327 |
+
"classifier_id": classifier_id,
|
| 328 |
+
"num_models": len(leaderboard) if leaderboard is not None else 0,
|
| 329 |
+
"leaderboard_summary": leaderboard.head(10).to_dict() if leaderboard is not None else {},
|
| 330 |
+
},
|
| 331 |
+
"error": None,
|
| 332 |
}
|
| 333 |
except Exception as e:
|
| 334 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 335 |
|
| 336 |
|
| 337 |
+
@mcp.tool(name="get_regressor_model_performance")
|
| 338 |
+
def get_regressor_model_performance(regressor_id: str) -> dict:
|
| 339 |
"""
|
| 340 |
+
Get performance statistics of explored models.
|
| 341 |
+
|
| 342 |
+
Parameters:
|
| 343 |
+
regressor_id (str): ID of the regressor.
|
| 344 |
|
| 345 |
Returns:
|
| 346 |
+
dict: Performance statistics.
|
| 347 |
"""
|
| 348 |
try:
|
| 349 |
+
if regressor_id not in _regressors:
|
| 350 |
+
return {"success": False, "result": None, "error": f"Regressor '{regressor_id}' not found"}
|
| 351 |
+
|
| 352 |
+
reg = _regressors[regressor_id]
|
| 353 |
+
|
| 354 |
+
# Get leaderboard information
|
| 355 |
+
leaderboard = reg.leaderboard()
|
| 356 |
+
|
| 357 |
+
return {
|
| 358 |
+
"success": True,
|
| 359 |
+
"result": {
|
| 360 |
+
"regressor_id": regressor_id,
|
| 361 |
+
"num_models": len(leaderboard) if leaderboard is not None else 0,
|
| 362 |
+
"leaderboard_summary": leaderboard.head(10).to_dict() if leaderboard is not None else {},
|
| 363 |
+
},
|
| 364 |
+
"error": None,
|
| 365 |
+
}
|
| 366 |
+
except Exception as e:
|
| 367 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 368 |
|
|
|
|
|
|
|
| 369 |
|
| 370 |
+
@mcp.tool(name="list_models")
|
| 371 |
+
def list_models() -> dict:
|
| 372 |
+
"""
|
| 373 |
+
List all stored classifiers and regressors.
|
| 374 |
+
|
| 375 |
+
Returns:
|
| 376 |
+
dict: List of model IDs.
|
| 377 |
+
"""
|
| 378 |
+
try:
|
| 379 |
return {
|
| 380 |
"success": True,
|
| 381 |
+
"result": {
|
| 382 |
+
"classifiers": list(_classifiers.keys()),
|
| 383 |
+
"regressors": list(_regressors.keys()),
|
| 384 |
+
"total": len(_classifiers) + len(_regressors),
|
| 385 |
+
},
|
| 386 |
+
"error": None,
|
| 387 |
}
|
| 388 |
except Exception as e:
|
| 389 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 390 |
|
| 391 |
|
| 392 |
+
@mcp.tool(name="delete_classifier")
|
| 393 |
+
def delete_classifier(classifier_id: str) -> dict:
|
| 394 |
"""
|
| 395 |
+
Delete a stored classifier.
|
| 396 |
|
| 397 |
Parameters:
|
| 398 |
+
classifier_id (str): ID of the classifier to delete.
|
|
|
|
|
|
|
| 399 |
|
| 400 |
Returns:
|
| 401 |
+
dict: Confirmation of deletion.
|
| 402 |
"""
|
| 403 |
try:
|
| 404 |
+
if classifier_id not in _classifiers:
|
| 405 |
+
return {"success": False, "result": None, "error": f"Classifier '{classifier_id}' not found"}
|
| 406 |
+
|
| 407 |
+
del _classifiers[classifier_id]
|
| 408 |
+
|
| 409 |
return {
|
| 410 |
"success": True,
|
| 411 |
+
"result": {"message": f"Classifier '{classifier_id}' deleted"},
|
| 412 |
+
"error": None,
|
| 413 |
}
|
| 414 |
except Exception as e:
|
| 415 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 416 |
|
| 417 |
|
| 418 |
+
@mcp.tool(name="delete_regressor")
|
| 419 |
+
def delete_regressor(regressor_id: str) -> dict:
|
| 420 |
"""
|
| 421 |
+
Delete a stored regressor.
|
| 422 |
|
| 423 |
Parameters:
|
| 424 |
+
regressor_id (str): ID of the regressor to delete.
|
| 425 |
|
| 426 |
Returns:
|
| 427 |
+
dict: Confirmation of deletion.
|
| 428 |
"""
|
| 429 |
try:
|
| 430 |
+
if regressor_id not in _regressors:
|
| 431 |
+
return {"success": False, "result": None, "error": f"Regressor '{regressor_id}' not found"}
|
| 432 |
+
|
| 433 |
+
del _regressors[regressor_id]
|
| 434 |
+
|
| 435 |
return {
|
| 436 |
"success": True,
|
| 437 |
+
"result": {"message": f"Regressor '{regressor_id}' deleted"},
|
| 438 |
+
"error": None,
|
| 439 |
}
|
| 440 |
except Exception as e:
|
| 441 |
+
return {"success": False, "result": None, "error": str(e)}
|
| 442 |
+
|
| 443 |
|
| 444 |
def create_app() -> FastMCP:
|
| 445 |
"""
|
| 446 |
Create and return the FastMCP application instance.
|
| 447 |
|
| 448 |
Returns:
|
| 449 |
+
FastMCP: the FastMCP application instance
|
| 450 |
"""
|
| 451 |
+
return mcp
|