Spaces:
Sleeping
Sleeping
Update AgML/mcp_output/mcp_plugin/mcp_service.py
Browse files
AgML/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -1,103 +1,268 @@
|
|
| 1 |
-
import os
|
| 2 |
-
import sys
|
| 3 |
-
|
| 4 |
-
# Add the local source directory to sys.path
|
| 5 |
-
source_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))), "source")
|
| 6 |
-
if source_path not in sys.path:
|
| 7 |
-
sys.path.insert(0, source_path)
|
| 8 |
-
|
| 9 |
from fastmcp import FastMCP
|
| 10 |
|
| 11 |
-
# Import core modules
|
| 12 |
-
from agml.data.loader import DataLoader
|
| 13 |
-
from agml.models.classification import ClassificationModel
|
| 14 |
-
from agml.models.detection import DetectionModel
|
| 15 |
-
from agml.models.segmentation import SegmentationModel
|
| 16 |
-
|
| 17 |
# Create the FastMCP service application
|
| 18 |
mcp = FastMCP("agml_service")
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
"""
|
| 23 |
-
|
| 24 |
|
| 25 |
Parameters:
|
| 26 |
-
- dataset_name
|
| 27 |
|
| 28 |
Returns:
|
| 29 |
-
- dict:
|
| 30 |
"""
|
| 31 |
try:
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 35 |
except Exception as e:
|
| 36 |
return {"success": False, "error": str(e)}
|
| 37 |
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
"""
|
| 41 |
-
|
| 42 |
|
| 43 |
Parameters:
|
| 44 |
-
-
|
| 45 |
-
-
|
| 46 |
|
| 47 |
Returns:
|
| 48 |
-
- dict:
|
| 49 |
"""
|
| 50 |
try:
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
except Exception as e:
|
| 57 |
return {"success": False, "error": str(e)}
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
"""
|
| 62 |
-
|
| 63 |
|
| 64 |
Parameters:
|
| 65 |
-
-
|
| 66 |
-
-
|
| 67 |
|
| 68 |
Returns:
|
| 69 |
-
- dict:
|
| 70 |
"""
|
| 71 |
try:
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
except Exception as e:
|
| 78 |
return {"success": False, "error": str(e)}
|
| 79 |
|
| 80 |
-
|
| 81 |
-
|
|
|
|
| 82 |
"""
|
| 83 |
-
|
| 84 |
|
| 85 |
Parameters:
|
| 86 |
-
-
|
| 87 |
-
-
|
| 88 |
|
| 89 |
Returns:
|
| 90 |
-
- dict:
|
| 91 |
"""
|
| 92 |
try:
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
return {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 98 |
except Exception as e:
|
| 99 |
return {"success": False, "error": str(e)}
|
| 100 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 101 |
def create_app() -> FastMCP:
|
| 102 |
"""
|
| 103 |
Create and return the FastMCP application instance.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
from fastmcp import FastMCP
|
| 2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
# Create the FastMCP service application
|
| 4 |
mcp = FastMCP("agml_service")
|
| 5 |
|
| 6 |
+
|
| 7 |
+
@mcp.tool(name="list_available_datasets", description="List all available AgML public datasets")
|
| 8 |
+
def list_available_datasets() -> dict:
|
| 9 |
+
"""
|
| 10 |
+
List all available public datasets in AgML.
|
| 11 |
+
|
| 12 |
+
Returns:
|
| 13 |
+
- dict: A dictionary with success status and list of available datasets.
|
| 14 |
+
"""
|
| 15 |
+
try:
|
| 16 |
+
from agml.data.public import public_data_sources
|
| 17 |
+
datasets = list(public_data_sources().keys())
|
| 18 |
+
return {
|
| 19 |
+
"success": True,
|
| 20 |
+
"datasets": datasets,
|
| 21 |
+
"count": len(datasets)
|
| 22 |
+
}
|
| 23 |
+
except Exception as e:
|
| 24 |
+
return {"success": False, "error": str(e)}
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
@mcp.tool(name="get_dataset_info", description="Get information about a specific dataset")
|
| 28 |
+
def get_dataset_info(dataset_name: str) -> dict:
|
| 29 |
+
"""
|
| 30 |
+
Get detailed information about a specific AgML dataset.
|
| 31 |
+
|
| 32 |
+
Parameters:
|
| 33 |
+
- dataset_name: Name of the dataset (e.g., 'bean_disease_uganda')
|
| 34 |
+
|
| 35 |
+
Returns:
|
| 36 |
+
- dict: Dataset information including task, location, classes, etc.
|
| 37 |
+
"""
|
| 38 |
+
try:
|
| 39 |
+
from agml.data.public import public_data_sources
|
| 40 |
+
sources = public_data_sources()
|
| 41 |
+
|
| 42 |
+
if dataset_name not in sources:
|
| 43 |
+
return {
|
| 44 |
+
"success": False,
|
| 45 |
+
"error": f"Dataset '{dataset_name}' not found. Use list_available_datasets to see available options."
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
info = sources[dataset_name]
|
| 49 |
+
return {
|
| 50 |
+
"success": True,
|
| 51 |
+
"dataset_name": dataset_name,
|
| 52 |
+
"info": info
|
| 53 |
+
}
|
| 54 |
+
except Exception as e:
|
| 55 |
+
return {"success": False, "error": str(e)}
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
@mcp.tool(name="load_dataset", description="Load an AgML dataset")
|
| 59 |
+
def load_dataset(dataset_name: str, batch_size: int = 8) -> dict:
|
| 60 |
+
"""
|
| 61 |
+
Load an AgML dataset.
|
| 62 |
+
|
| 63 |
+
Parameters:
|
| 64 |
+
- dataset_name: Name of the dataset to load
|
| 65 |
+
- batch_size: Batch size for data loading (default: 8)
|
| 66 |
+
|
| 67 |
+
Returns:
|
| 68 |
+
- dict: Information about the loaded dataset
|
| 69 |
+
"""
|
| 70 |
+
try:
|
| 71 |
+
from agml.data import AgMLDataLoader
|
| 72 |
+
|
| 73 |
+
loader = AgMLDataLoader(dataset_name)
|
| 74 |
+
|
| 75 |
+
return {
|
| 76 |
+
"success": True,
|
| 77 |
+
"dataset_name": dataset_name,
|
| 78 |
+
"task_type": str(loader.info['task_type']),
|
| 79 |
+
"num_images": loader.num_images,
|
| 80 |
+
"num_classes": getattr(loader, 'num_classes', None),
|
| 81 |
+
"classes": getattr(loader, 'classes', None)
|
| 82 |
+
}
|
| 83 |
+
except Exception as e:
|
| 84 |
+
return {"success": False, "error": str(e)}
|
| 85 |
+
|
| 86 |
+
|
| 87 |
+
@mcp.tool(name="download_dataset", description="Download a public AgML dataset")
|
| 88 |
+
def download_dataset(dataset_name: str) -> dict:
|
| 89 |
"""
|
| 90 |
+
Download a public dataset from AgML.
|
| 91 |
|
| 92 |
Parameters:
|
| 93 |
+
- dataset_name: Name of the dataset to download
|
| 94 |
|
| 95 |
Returns:
|
| 96 |
+
- dict: Download status information
|
| 97 |
"""
|
| 98 |
try:
|
| 99 |
+
from agml.data.public import download_public_dataset
|
| 100 |
+
|
| 101 |
+
download_public_dataset(dataset_name)
|
| 102 |
+
|
| 103 |
+
return {
|
| 104 |
+
"success": True,
|
| 105 |
+
"message": f"Dataset '{dataset_name}' downloaded successfully"
|
| 106 |
+
}
|
| 107 |
except Exception as e:
|
| 108 |
return {"success": False, "error": str(e)}
|
| 109 |
|
| 110 |
+
|
| 111 |
+
@mcp.tool(name="list_model_benchmarks", description="List available model benchmarks")
|
| 112 |
+
def list_model_benchmarks() -> dict:
|
| 113 |
+
"""
|
| 114 |
+
List all available model benchmarks in AgML.
|
| 115 |
+
|
| 116 |
+
Returns:
|
| 117 |
+
- dict: Available model benchmarks by task type
|
| 118 |
+
"""
|
| 119 |
+
try:
|
| 120 |
+
from agml.models.benchmarks import list_model_benchmarks
|
| 121 |
+
|
| 122 |
+
benchmarks = list_model_benchmarks()
|
| 123 |
+
|
| 124 |
+
return {
|
| 125 |
+
"success": True,
|
| 126 |
+
"benchmarks": benchmarks
|
| 127 |
+
}
|
| 128 |
+
except Exception as e:
|
| 129 |
+
return {"success": False, "error": str(e)}
|
| 130 |
+
|
| 131 |
+
|
| 132 |
+
@mcp.tool(name="get_model_benchmark", description="Get benchmark results for a specific model")
|
| 133 |
+
def get_model_benchmark(model_name: str, dataset_name: str) -> dict:
|
| 134 |
"""
|
| 135 |
+
Get benchmark results for a specific model on a dataset.
|
| 136 |
|
| 137 |
Parameters:
|
| 138 |
+
- model_name: Name of the model
|
| 139 |
+
- dataset_name: Name of the dataset
|
| 140 |
|
| 141 |
Returns:
|
| 142 |
+
- dict: Benchmark results
|
| 143 |
"""
|
| 144 |
try:
|
| 145 |
+
from agml.models.benchmarks import get_model_benchmark
|
| 146 |
+
|
| 147 |
+
result = get_model_benchmark(model_name, dataset_name)
|
| 148 |
+
|
| 149 |
+
return {
|
| 150 |
+
"success": True,
|
| 151 |
+
"model": model_name,
|
| 152 |
+
"dataset": dataset_name,
|
| 153 |
+
"benchmark": result
|
| 154 |
+
}
|
| 155 |
except Exception as e:
|
| 156 |
return {"success": False, "error": str(e)}
|
| 157 |
|
| 158 |
+
|
| 159 |
+
@mcp.tool(name="create_classification_model", description="Create an AgML classification model")
|
| 160 |
+
def create_classification_model(model_name: str, num_classes: int) -> dict:
|
| 161 |
"""
|
| 162 |
+
Create an AgML classification model.
|
| 163 |
|
| 164 |
Parameters:
|
| 165 |
+
- model_name: Name of the model architecture (e.g., 'resnet18', 'efficientnet_b0')
|
| 166 |
+
- num_classes: Number of classes for classification
|
| 167 |
|
| 168 |
Returns:
|
| 169 |
+
- dict: Model creation status and information
|
| 170 |
"""
|
| 171 |
try:
|
| 172 |
+
from agml.models import ClassificationModel
|
| 173 |
+
|
| 174 |
+
model = ClassificationModel(model_name=model_name, num_classes=num_classes)
|
| 175 |
+
|
| 176 |
+
return {
|
| 177 |
+
"success": True,
|
| 178 |
+
"model_name": model_name,
|
| 179 |
+
"num_classes": num_classes,
|
| 180 |
+
"message": "Model created successfully"
|
| 181 |
+
}
|
| 182 |
except Exception as e:
|
| 183 |
return {"success": False, "error": str(e)}
|
| 184 |
|
| 185 |
+
|
| 186 |
+
@mcp.tool(name="create_detection_model", description="Create an AgML object detection model")
|
| 187 |
+
def create_detection_model(model_name: str, num_classes: int) -> dict:
|
| 188 |
"""
|
| 189 |
+
Create an AgML object detection model.
|
| 190 |
|
| 191 |
Parameters:
|
| 192 |
+
- model_name: Name of the model architecture (e.g., 'fasterrcnn_resnet50_fpn')
|
| 193 |
+
- num_classes: Number of classes for detection
|
| 194 |
|
| 195 |
Returns:
|
| 196 |
+
- dict: Model creation status and information
|
| 197 |
"""
|
| 198 |
try:
|
| 199 |
+
from agml.models import DetectionModel
|
| 200 |
+
|
| 201 |
+
model = DetectionModel(model_name=model_name, num_classes=num_classes)
|
| 202 |
+
|
| 203 |
+
return {
|
| 204 |
+
"success": True,
|
| 205 |
+
"model_name": model_name,
|
| 206 |
+
"num_classes": num_classes,
|
| 207 |
+
"message": "Model created successfully"
|
| 208 |
+
}
|
| 209 |
except Exception as e:
|
| 210 |
return {"success": False, "error": str(e)}
|
| 211 |
|
| 212 |
+
|
| 213 |
+
@mcp.tool(name="create_segmentation_model", description="Create an AgML segmentation model")
|
| 214 |
+
def create_segmentation_model(model_name: str, num_classes: int) -> dict:
|
| 215 |
+
"""
|
| 216 |
+
Create an AgML segmentation model.
|
| 217 |
+
|
| 218 |
+
Parameters:
|
| 219 |
+
- model_name: Name of the model architecture (e.g., 'deeplabv3_resnet50')
|
| 220 |
+
- num_classes: Number of classes for segmentation
|
| 221 |
+
|
| 222 |
+
Returns:
|
| 223 |
+
- dict: Model creation status and information
|
| 224 |
+
"""
|
| 225 |
+
try:
|
| 226 |
+
from agml.models import SegmentationModel
|
| 227 |
+
|
| 228 |
+
model = SegmentationModel(model_name=model_name, num_classes=num_classes)
|
| 229 |
+
|
| 230 |
+
return {
|
| 231 |
+
"success": True,
|
| 232 |
+
"model_name": model_name,
|
| 233 |
+
"num_classes": num_classes,
|
| 234 |
+
"message": "Model created successfully"
|
| 235 |
+
}
|
| 236 |
+
except Exception as e:
|
| 237 |
+
return {"success": False, "error": str(e)}
|
| 238 |
+
|
| 239 |
+
|
| 240 |
+
@mcp.tool(name="export_dataset_to_yolo", description="Export AgML dataset to YOLO format")
|
| 241 |
+
def export_dataset_to_yolo(dataset_name: str, output_dir: str) -> dict:
|
| 242 |
+
"""
|
| 243 |
+
Export an AgML dataset to YOLO format.
|
| 244 |
+
|
| 245 |
+
Parameters:
|
| 246 |
+
- dataset_name: Name of the dataset to export
|
| 247 |
+
- output_dir: Directory where to save the YOLO formatted data
|
| 248 |
+
|
| 249 |
+
Returns:
|
| 250 |
+
- dict: Export status
|
| 251 |
+
"""
|
| 252 |
+
try:
|
| 253 |
+
from agml.data import AgMLDataLoader
|
| 254 |
+
|
| 255 |
+
loader = AgMLDataLoader(dataset_name)
|
| 256 |
+
loader.export_to_yolo(output_dir)
|
| 257 |
+
|
| 258 |
+
return {
|
| 259 |
+
"success": True,
|
| 260 |
+
"message": f"Dataset exported to YOLO format at {output_dir}"
|
| 261 |
+
}
|
| 262 |
+
except Exception as e:
|
| 263 |
+
return {"success": False, "error": str(e)}
|
| 264 |
+
|
| 265 |
+
|
| 266 |
def create_app() -> FastMCP:
|
| 267 |
"""
|
| 268 |
Create and return the FastMCP application instance.
|