Deploy tools/idfm_places_search.py via Meta-MCP
Browse files- tools/idfm_places_search.py +66 -0
tools/idfm_places_search.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import json
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# --- User Defined Logic ---
|
| 7 |
+
import requests
|
| 8 |
+
from typing import List, Dict, Any, Optional
|
| 9 |
+
import json
|
| 10 |
+
|
| 11 |
+
def idfm_places_search(
|
| 12 |
+
api_token: str,
|
| 13 |
+
q: str,
|
| 14 |
+
type_filter: Optional[str] = None,
|
| 15 |
+
count: int = 10
|
| 16 |
+
) -> str:
|
| 17 |
+
"""
|
| 18 |
+
Search for places (addresses, POIs, stations) in Île-de-France using PRIM Navitia API.
|
| 19 |
+
|
| 20 |
+
Args:
|
| 21 |
+
api_token (str): Your PRIM API token used as username for Basic Auth.
|
| 22 |
+
q (str): Search query (address, place name, station).
|
| 23 |
+
type_filter (Optional[str]): Optional type to filter results.
|
| 24 |
+
Allowed values: 'stop_area', 'address', 'poi', 'administrative_region'.
|
| 25 |
+
count (int): Number of results to return (default 10, max 50).
|
| 26 |
+
|
| 27 |
+
Returns:
|
| 28 |
+
str: JSON string containing formatted search results with name, type, coordinates (lat/lon), and id.
|
| 29 |
+
"""
|
| 30 |
+
url = "https://prim.iledefrance-mobilites.fr/marketplace/v2/navitia/coverage/fr-idf/places"
|
| 31 |
+
params: Dict[str, Any] = {"q": q, "count": min(count, 50)}
|
| 32 |
+
|
| 33 |
+
if type_filter:
|
| 34 |
+
params["type[]"] = type_filter
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
response = requests.get(url, params=params, auth=(api_token, ""), timeout=10)
|
| 38 |
+
response.raise_for_status()
|
| 39 |
+
data = response.json()
|
| 40 |
+
|
| 41 |
+
results = []
|
| 42 |
+
for place in data.get("places", []):
|
| 43 |
+
coord = place.get("coord", {})
|
| 44 |
+
results.append({
|
| 45 |
+
"name": place.get("name", ""),
|
| 46 |
+
"type": place.get("embedded_type", ""),
|
| 47 |
+
"lat": float(coord.get("lat", 0)),
|
| 48 |
+
"lon": float(coord.get("lon", 0)),
|
| 49 |
+
"id": place.get("id", "")
|
| 50 |
+
})
|
| 51 |
+
|
| 52 |
+
return json.dumps({"status": "success", "count": len(results), "places": results}, ensure_ascii=False, indent=2)
|
| 53 |
+
|
| 54 |
+
except requests.exceptions.RequestException as e:
|
| 55 |
+
return json.dumps({"status": "error", "message": str(e)}, ensure_ascii=False, indent=2)
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# --- Interface Factory ---
|
| 59 |
+
def create_interface():
|
| 60 |
+
return gr.Interface(
|
| 61 |
+
fn=idfm_places_search,
|
| 62 |
+
inputs=[gr.Textbox(label=k) for k in ['api_token', 'q', 'type_filter', 'count']],
|
| 63 |
+
outputs=gr.JSON(label="JSON string with search results containing place names, types, coordinates (lat/lon), and IDs"),
|
| 64 |
+
title="idfm_places_search",
|
| 65 |
+
description="Auto-generated tool: idfm_places_search"
|
| 66 |
+
)
|