Download backend/app/services/remote_model_client.py from interactAi-inc/Pit-Lane-Project: direct link, hf CLI and curl.
- Browser
- Download file 2.58 kB
-
https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/app/services/remote_model_client.py
- Command line
-
hf download hf://interactAi-inc/Pit-Lane-Project/backend/app/services/remote_model_client.py
-
curl -L -o remote_model_client.py https://huggingface.co/interactAi-inc/Pit-Lane-Project/resolve/main/backend/app/services/remote_model_client.py
2.58 kB
| import math | |
| from typing import Any, Mapping | |
| import httpx | |
| from app.config import Settings | |
| class RemoteModelError(RuntimeError): | |
| pass | |
| class RemoteModelClient: | |
| """Hosted-model transport; adjust only this file when its API is known.""" | |
| def __init__(self, settings: Settings) -> None: | |
| self._url = settings.remote_model_url | |
| self._token = settings.remote_model_api_token | |
| self._timeout = settings.remote_model_timeout_seconds | |
| def configured(self) -> bool: | |
| return bool(self._url) | |
| async def predict(self, rows: list[Mapping[str, Any]]) -> list[float]: | |
| if not self._url: | |
| raise RemoteModelError("REMOTE_MODEL_URL is not configured") | |
| headers = {"Accept": "application/json"} | |
| if self._token: | |
| headers["Authorization"] = f"Bearer {self._token}" | |
| payload = {"rows": [self._json_safe(dict(row)) for row in rows]} | |
| try: | |
| async with httpx.AsyncClient(timeout=self._timeout) as client: | |
| response = await client.post(self._url, json=payload, headers=headers) | |
| response.raise_for_status() | |
| body = response.json() | |
| except (httpx.HTTPError, ValueError) as exc: | |
| raise RemoteModelError(f"Hosted model request failed: {exc}") from exc | |
| probabilities = body.get("probabilities") if isinstance(body, dict) else None | |
| if not isinstance(probabilities, list) or len(probabilities) != len(rows): | |
| raise RemoteModelError( | |
| "Hosted model must return {'probabilities': [..]} in input order" | |
| ) | |
| try: | |
| result = [float(value) for value in probabilities] | |
| except (TypeError, ValueError) as exc: | |
| raise RemoteModelError("Hosted model returned non-numeric probabilities") from exc | |
| if any(not math.isfinite(value) or not 0.0 <= value <= 1.0 for value in result): | |
| raise RemoteModelError("Hosted probabilities must be finite and between 0 and 1") | |
| return result | |
| def _json_safe(row: dict[str, Any]) -> dict[str, Any]: | |
| safe: dict[str, Any] = {} | |
| for key, value in row.items(): | |
| if value is None: | |
| safe[key] = None | |
| continue | |
| try: | |
| value = value.item() | |
| except AttributeError: | |
| pass | |
| safe[key] = value if not isinstance(value, float) or math.isfinite(value) else None | |
| return safe |