Spaces:
Sleeping
Sleeping
Download utils/validation.py from JoyZhan/BackendPayactiv: direct link, hf CLI and curl.
- Browser
- Download file 1.19 kB
-
https://huggingface.co/spaces/JoyZhan/BackendPayactiv/resolve/main/utils/validation.py
- Command line
-
hf download hf://spaces/JoyZhan/BackendPayactiv/utils/validation.py
-
curl -L -o validation.py https://huggingface.co/spaces/JoyZhan/BackendPayactiv/resolve/main/utils/validation.py
1.19 kB
| """Validation helpers for incoming request data.""" | |
| from typing import List | |
| from fastapi import HTTPException | |
| def validate_history(history: List[float], min_length: int = 2) -> None: | |
| """Raise HTTP 422 if the time-series history is unusable. | |
| Checks performed: | |
| * Minimum length constraint | |
| * No NaN or infinite values | |
| * At least some non-zero variance (all-identical series cannot be | |
| meaningfully extrapolated by a trend model, but we allow it and | |
| simply emit a warning via the response) | |
| """ | |
| if len(history) < min_length: | |
| raise HTTPException( | |
| status_code=422, | |
| detail=( | |
| f"history must contain at least {min_length} data points, " | |
| f"got {len(history)}" | |
| ), | |
| ) | |
| for i, v in enumerate(history): | |
| if v != v: # NaN check (works without math.isnan) | |
| raise HTTPException( | |
| status_code=422, | |
| detail=f"history[{i}] is NaN", | |
| ) | |
| if v == float("inf") or v == float("-inf"): | |
| raise HTTPException( | |
| status_code=422, | |
| detail=f"history[{i}] is infinite", | |
| ) | |