Spaces:
Running
Running
Download python/borderland_web/static_api.py from FatUnc6969/borderland-rl: direct link, hf CLI and curl.
- Browser
- Download file 3.09 kB
-
https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_web/static_api.py
- Command line
-
hf download hf://spaces/FatUnc6969/borderland-rl/python/borderland_web/static_api.py
-
curl -L -o static_api.py https://huggingface.co/spaces/FatUnc6969/borderland-rl/resolve/main/python/borderland_web/static_api.py
3.09 kB
| from __future__ import annotations | |
| import json | |
| from .server import Application, MAX_BODY, RequestError, SESSION_ID, STATIC, _object | |
| class StaticApi: | |
| def __init__(self, application=None, archive_workload="standard", generation_version=3, notebook_bytes=768, memory_profile="static"): | |
| self.application = application if application is not None else Application(archive_workload=archive_workload, generation_version=generation_version, notebook_bytes=notebook_bytes, memory_profile=memory_profile) | |
| def dispatch(self, method, path, body_json): | |
| try: | |
| status, data = self._dispatch(method, path, body_json) | |
| return json.dumps({'status': status, 'data': data}, ensure_ascii=False, allow_nan=False) | |
| except RequestError as error: | |
| status, message = error.status, error.message | |
| except Exception: | |
| status, message = 500, 'The operation could not finish. Try again or start a new campaign.' | |
| return json.dumps({'status': status, 'data': {'error': message}}, allow_nan=False) | |
| def _dispatch(self, method, path, body_json): | |
| if method == 'GET': | |
| if path == '/api/recordings': | |
| return 200, json.loads((STATIC / 'beam-recordings.json').read_text(encoding='utf-8')) | |
| if isinstance(path, str) and path.startswith('/api/session/'): | |
| session_id = path.removeprefix('/api/session/') | |
| if SESSION_ID.fullmatch(session_id): | |
| session = self.application.session(session_id) | |
| with session.lock: | |
| return 200, {'transition': session.transition} | |
| raise RequestError(404, 'Route not found.') | |
| if method != 'POST' or path not in {'/api/session', '/api/action', '/api/notebook', '/api/demo', '/api/twins'}: | |
| raise RequestError(404, 'Route not found.') | |
| try: | |
| encoded = body_json.encode('utf-8') if isinstance(body_json, str) else b'' | |
| if len(encoded) > MAX_BODY: | |
| raise RequestError(413, 'Request body is too large.') | |
| body = json.loads(encoded, object_pairs_hook=_object, parse_constant=self._reject_constant) | |
| except (ValueError, UnicodeError, RecursionError): | |
| raise RequestError(400, 'Request body must be valid JSON without duplicate keys or non-finite numbers.') | |
| if not isinstance(body, dict): | |
| raise RequestError(400, 'Request body must be a JSON object.') | |
| if path in {'/api/session', '/api/demo', '/api/twins'} and body: | |
| raise RequestError(400, 'This endpoint accepts an empty object only.') | |
| if path == '/api/session': | |
| return 201, self.application.create() | |
| if path in {'/api/action', '/api/notebook'}: | |
| return self.application.command(path.rsplit('/', 1)[1], body) | |
| if path == '/api/demo': | |
| return 200, self.application.demonstration() | |
| return 200, self.application.twins() | |
| def _reject_constant(value): | |
| raise ValueError('Non-finite JSON number.') | |