Spaces:
Sleeping
Sleeping
Update osmnx/mcp_output/mcp_plugin/mcp_service.py
Browse files
osmnx/mcp_output/mcp_plugin/mcp_service.py
CHANGED
|
@@ -1,273 +1,45 @@
|
|
| 1 |
from fastmcp import FastMCP
|
| 2 |
|
| 3 |
# Create the FastMCP service application
|
| 4 |
-
mcp = FastMCP("
|
| 5 |
|
| 6 |
-
|
| 7 |
-
|
| 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 |
-
|
| 217 |
|
| 218 |
Parameters:
|
| 219 |
-
-
|
| 220 |
-
-
|
| 221 |
|
| 222 |
Returns:
|
| 223 |
-
- dict:
|
| 224 |
"""
|
| 225 |
try:
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
model = SegmentationModel(model_name=model_name, num_classes=num_classes)
|
| 229 |
-
|
| 230 |
return {
|
| 231 |
"success": True,
|
| 232 |
-
"
|
| 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 |
-
|
| 241 |
-
def export_dataset_to_yolo(dataset_name: str, output_dir: str) -> dict:
|
| 242 |
"""
|
| 243 |
-
|
| 244 |
|
| 245 |
Parameters:
|
| 246 |
-
-
|
| 247 |
-
- output_dir: Directory where to save the YOLO formatted data
|
| 248 |
|
| 249 |
Returns:
|
| 250 |
-
- dict:
|
| 251 |
"""
|
| 252 |
try:
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 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.
|
| 269 |
-
|
| 270 |
-
Returns:
|
| 271 |
-
- FastMCP: The FastMCP application instance.
|
| 272 |
-
"""
|
| 273 |
-
return mcp
|
|
|
|
| 1 |
from fastmcp import FastMCP
|
| 2 |
|
| 3 |
# Create the FastMCP service application
|
| 4 |
+
mcp = FastMCP("osmnx_service")
|
| 5 |
|
| 6 |
+
@mcp.tool(name="get_graph_from_place", description="Retrieve a graph from a place name using OSMnx")
|
| 7 |
+
def get_graph_from_place(place_name: str, network_type: str = "drive") -> dict:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
"""
|
| 9 |
+
Retrieve a graph representation of a place using OSMnx.
|
| 10 |
|
| 11 |
Parameters:
|
| 12 |
+
- place_name: Name of the place to retrieve the graph for
|
| 13 |
+
- network_type: Type of network to retrieve (e.g., 'drive', 'walk', 'bike')
|
| 14 |
|
| 15 |
Returns:
|
| 16 |
+
- dict: Information about the retrieved graph
|
| 17 |
"""
|
| 18 |
try:
|
| 19 |
+
import osmnx as ox
|
| 20 |
+
graph = ox.graph_from_place(place_name, network_type=network_type)
|
|
|
|
|
|
|
| 21 |
return {
|
| 22 |
"success": True,
|
| 23 |
+
"graph_info": str(graph)
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
except Exception as e:
|
| 26 |
return {"success": False, "error": str(e)}
|
| 27 |
|
| 28 |
+
@mcp.tool(name="plot_graph", description="Plot a graph using OSMnx")
|
| 29 |
+
def plot_graph(graph_data: dict) -> dict:
|
|
|
|
| 30 |
"""
|
| 31 |
+
Plot a graph using OSMnx.
|
| 32 |
|
| 33 |
Parameters:
|
| 34 |
+
- graph_data: Serialized graph data to plot
|
|
|
|
| 35 |
|
| 36 |
Returns:
|
| 37 |
+
- dict: Status of the plotting operation
|
| 38 |
"""
|
| 39 |
try:
|
| 40 |
+
import osmnx as ox
|
| 41 |
+
graph = ox.io.load_graphml(graph_data)
|
| 42 |
+
ox.plot_graph(graph)
|
| 43 |
+
return {"success": True}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
except Exception as e:
|
| 45 |
+
return {"success": False, "error": str(e)}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|