85 lines
2.9 KiB
Python
85 lines
2.9 KiB
Python
from __future__ import annotations
|
|
import json
|
|
import requests
|
|
from django.http import JsonResponse, HttpRequest
|
|
from django.views.decorators.csrf import csrf_exempt
|
|
from django.views.decorators.http import require_http_methods
|
|
|
|
def _post_json(url: str, payload: dict, timeout: float = 60.0) -> dict:
|
|
r = requests.post(url, json=payload, timeout=timeout)
|
|
r.raise_for_status()
|
|
return r.json()
|
|
|
|
def _base(request: HttpRequest) -> str:
|
|
return request.build_absolute_uri("/")[:-1]
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["GET", "POST"])
|
|
def format_sami(request: HttpRequest):
|
|
"""
|
|
Body: {"payload": {...}} where payload matches SAMI RunRequest.
|
|
Calls /api/agents/execute and returns {"text": "...", ...original...}
|
|
"""
|
|
try:
|
|
body = json.loads(request.body.decode("utf-8") or "{}")
|
|
except Exception:
|
|
body = {}
|
|
payload = body.get("payload") or {}
|
|
|
|
data = _post_json(f"{_base(request)}/api/agents/execute",
|
|
{"agent": "sami", "payload": payload})
|
|
|
|
# Build text
|
|
try:
|
|
beta = data.get("beta"); r2 = data.get("r2")
|
|
resid = data.get("residuals") or []
|
|
top = sorted(resid, key=lambda x: x.get("rank", 1e9))[:3]
|
|
lines = []
|
|
if beta is not None and r2 is not None:
|
|
lines.append(f"SAMI run: β={beta:.3f}, R²={r2:.3f}")
|
|
for c in top:
|
|
lines.append(f"{c.get('rank')}. {c.get('city')}: {c.get('sami', 0):+0.2f}")
|
|
if data.get("share_url"):
|
|
lines.append("")
|
|
lines.append(data["share_url"])
|
|
text = "\n".join(lines) if lines else "SAMI results ready."
|
|
data["text"] = text
|
|
except Exception:
|
|
pass
|
|
|
|
return JsonResponse(data, safe=False)
|
|
|
|
@csrf_exempt
|
|
@require_http_methods(["GET", "POST"])
|
|
def format_sites(request: HttpRequest):
|
|
"""
|
|
Body: {"payload": {...}} where payload matches SiteSearchRequest.
|
|
Calls /api/agents/execute and returns {"text": "...", ...original...}
|
|
"""
|
|
try:
|
|
body = json.loads(request.body.decode("utf-8") or "{}")
|
|
except Exception:
|
|
body = {}
|
|
payload = body.get("payload") or {}
|
|
|
|
data = _post_json(f"{_base(request)}/api/agents/execute",
|
|
{"agent": "sites", "payload": payload})
|
|
|
|
# Build text
|
|
try:
|
|
city = data.get("city"); business = data.get("business")
|
|
cands = data.get("candidates") or []
|
|
top = cands[:3]
|
|
lines = [f"Top sites for {business} in {city}:"]
|
|
for i, c in enumerate(top, 1):
|
|
lines.append(f"{i}. score={c.get('score',0):.2f} @ ({c.get('lat',0):.5f},{c.get('lon',0):.5f})")
|
|
# include useful links if present
|
|
for k in ("share_url", "isochrones_geojson_url", "candidates_geojson_url"):
|
|
if data.get(k):
|
|
lines.append(data[k])
|
|
data["text"] = "\n".join(lines)
|
|
except Exception:
|
|
pass
|
|
|
|
return JsonResponse(data, safe=False)
|