Spaces:
Runtime error
Runtime error
Download src/llm_eval/providers/http.py from bhargob11/llm-eval-framework: direct link, hf CLI and curl.
- Browser
- Download file 1.01 kB
-
https://huggingface.co/spaces/bhargob11/llm-eval-framework/resolve/main/src/llm_eval/providers/http.py
- Command line
-
hf download hf://spaces/bhargob11/llm-eval-framework/src/llm_eval/providers/http.py
-
curl -L -o http.py https://huggingface.co/spaces/bhargob11/llm-eval-framework/resolve/main/src/llm_eval/providers/http.py
1.01 kB
| from __future__ import annotations | |
| import json | |
| from typing import Any | |
| from urllib import error, request | |
| class ProviderHTTPError(RuntimeError): | |
| def __init__(self, status_code: int, message: str): | |
| super().__init__(f"HTTP {status_code}: {message}") | |
| self.status_code = status_code | |
| self.message = message | |
| def post_json( | |
| *, | |
| url: str, | |
| payload: dict[str, Any], | |
| headers: dict[str, str], | |
| timeout_seconds: int, | |
| ) -> dict[str, Any]: | |
| body = json.dumps(payload).encode("utf-8") | |
| req = request.Request( | |
| url, | |
| data=body, | |
| method="POST", | |
| headers={**headers, "Content-Type": "application/json"}, | |
| ) | |
| try: | |
| with request.urlopen(req, timeout=timeout_seconds) as resp: | |
| raw = resp.read().decode("utf-8", errors="replace") | |
| return json.loads(raw) | |
| except error.HTTPError as exc: | |
| text = exc.read().decode("utf-8", errors="replace") | |
| raise ProviderHTTPError(exc.code, text[:500]) from exc | |