Spaces:
Running
Running
| """Agent 2 — Discovery Agent (haversine distance, no Maps API needed on free tier)""" | |
| import json, math, os | |
| from pathlib import Path | |
| PROVIDERS_FILE = Path(__file__).parent.parent / "data" / "providers.json" | |
| AREA_COORDS = { | |
| "G-13": (33.6844, 73.0479), "G-11": (33.6938, 73.0551), | |
| "G-10": (33.7020, 73.0397), "G-9": (33.6995, 73.0440), | |
| "G-6": (33.7290, 73.0935), "F-10": (33.7077, 73.0354), | |
| "F-7": (33.7192, 73.0587), "E-11": (33.7215, 73.0194), | |
| "I-8": (33.6741, 73.0632), "I-10": (33.6800, 73.0530), | |
| "DEFAULT": (33.6844, 73.0479), | |
| } | |
| def haversine(lat1, lon1, lat2, lon2) -> float: | |
| R = 6371 | |
| dlat = math.radians(lat2 - lat1) | |
| dlon = math.radians(lon2 - lon1) | |
| a = math.sin(dlat/2)**2 + math.cos(math.radians(lat1)) * math.cos(math.radians(lat2)) * math.sin(dlon/2)**2 | |
| return round(R * 2 * math.asin(math.sqrt(a)), 2) | |
| def run(intent: dict) -> dict: | |
| service_type = intent.get("service_type") or "" | |
| location = (intent.get("location") or "DEFAULT").upper().strip() | |
| with open(PROVIDERS_FILE) as f: | |
| all_providers = json.load(f) | |
| user_lat, user_lng = AREA_COORDS.get(location, AREA_COORDS["DEFAULT"]) | |
| matched = [] | |
| for p in all_providers: | |
| cats = [c.lower() for c in p.get("service_categories", [])] | |
| if service_type.lower() in cats: | |
| dist = haversine(user_lat, user_lng, p["location"]["lat"], p["location"]["lng"]) | |
| matched.append({**p, "distance_km": dist}) | |
| matched.sort(key=lambda x: x["distance_km"]) | |
| return {"providers": matched, "user_location": location, "total_found": len(matched)} | |