Download server/app.py from 7ViksSai/supply-chain-ghost-protocol: direct link, hf CLI and curl.
- Browser
- Download file 3.68 kB
-
https://huggingface.co/spaces/7ViksSai/supply-chain-ghost-protocol/resolve/main/server/app.py
- Command line
-
hf download hf://spaces/7ViksSai/supply-chain-ghost-protocol/server/app.py
-
curl -L -o app.py https://huggingface.co/spaces/7ViksSai/supply-chain-ghost-protocol/resolve/main/server/app.py
3.68 kB
| """ | |
| OpenEnv FastAPI entrypoint — uses ``create_app`` (reference env pattern). | |
| """ | |
| from __future__ import annotations | |
| import json | |
| from pathlib import Path | |
| from typing import Any, Dict | |
| from fastapi import FastAPI, HTTPException | |
| from fastapi.responses import FileResponse, RedirectResponse | |
| from fastapi.staticfiles import StaticFiles | |
| from openenv.core.env_server.http_server import create_app | |
| from models import Action, Observation | |
| from server.supply_chain_environment import SupplyChainOpenEnvironment | |
| from tasks import ALL_TASKS | |
| _ROOT = Path(__file__).resolve().parent.parent | |
| _STATIC_DIR = _ROOT / "static" | |
| _ROLLOUTS_DIR = _ROOT / "rollouts" | |
| app: FastAPI = create_app( | |
| SupplyChainOpenEnvironment, | |
| Action, | |
| Observation, | |
| env_name="supply-chain-ghost-protocol", | |
| max_concurrent_envs=16, | |
| ) | |
| def root() -> FileResponse | RedirectResponse: | |
| index_path = _STATIC_DIR / "index.html" | |
| if not index_path.exists(): | |
| return RedirectResponse(url="/docs") | |
| return FileResponse(str(index_path), media_type="text/html") | |
| def viewer() -> FileResponse: | |
| html_path = _STATIC_DIR / "viewer.html" | |
| if not html_path.exists(): | |
| raise HTTPException(status_code=404, detail="viewer.html not found") | |
| return FileResponse(str(html_path), media_type="text/html") | |
| def list_tasks() -> Dict[str, Any]: | |
| return { | |
| "tasks": [ | |
| { | |
| "task_id": t.task_id, | |
| "name": t.name, | |
| "difficulty": t.difficulty, | |
| "episode_length": t.episode_length, | |
| "description": t.description, | |
| } | |
| for t in ALL_TASKS | |
| ] | |
| } | |
| def list_rollouts() -> Dict[str, Any]: | |
| if not _ROLLOUTS_DIR.exists(): | |
| return {"rollouts": []} | |
| results = [] | |
| for f in sorted(_ROLLOUTS_DIR.glob("*.json")): | |
| entry: Dict[str, Any] = {"filename": f.name} | |
| try: | |
| data = json.loads(f.read_text(encoding="utf-8")) | |
| entry.update( | |
| { | |
| "task_name": data.get("task_name", ""), | |
| "difficulty": data.get("difficulty", ""), | |
| "policy_name": data.get("policy_name", ""), | |
| "final_score": data.get("final_score"), | |
| "episode_length": data.get("episode_length"), | |
| } | |
| ) | |
| except (json.JSONDecodeError, OSError): | |
| pass | |
| results.append(entry) | |
| return {"rollouts": results} | |
| def get_rollout(filename: str) -> FileResponse: | |
| if ".." in filename or "/" in filename or "\\" in filename: | |
| raise HTTPException(status_code=400, detail="Invalid filename") | |
| filepath = _ROLLOUTS_DIR / filename | |
| if not filepath.exists() or filepath.suffix != ".json": | |
| raise HTTPException(status_code=404, detail="Rollout not found") | |
| resolved = filepath.resolve() | |
| if not str(resolved).startswith(str(_ROLLOUTS_DIR.resolve())): | |
| raise HTTPException(status_code=400, detail="Invalid path") | |
| return FileResponse(str(resolved), media_type="application/json") | |
| if _STATIC_DIR.exists(): | |
| app.mount("/static", StaticFiles(directory=str(_STATIC_DIR)), name="static") | |
| def main() -> None: | |
| import uvicorn | |
| uvicorn.run( | |
| "server.app:app", | |
| host="0.0.0.0", | |
| port=7860, | |
| log_level="info", | |
| ) | |
| if __name__ == "__main__": | |
| main() | |