Spaces:
Paused
Paused
Download server.py from oma5/fab-yield-agent: direct link, hf CLI and curl.
- Browser
- Download file 3.7 kB
-
https://huggingface.co/spaces/oma5/fab-yield-agent/resolve/main/server.py
- Command line
-
hf download hf://spaces/oma5/fab-yield-agent/server.py
-
curl -L -o server.py https://huggingface.co/spaces/oma5/fab-yield-agent/resolve/main/server.py
3.7 kB
| """ | |
| server.py β FastAPI server for the Fab Yield RL Environment. | |
| Mirrors hf-orchestrator/server.py pattern: | |
| - FastAPI app called 'app' (Uvicorn target: server:app) | |
| - /reset POST endpoint β returns observation dict | |
| - /step POST endpoint β returns obs + rewards + done + info | |
| - / GET health check (HF Spaces ping) | |
| - Port 7860 (HF Spaces requirement) | |
| """ | |
| from fastapi import FastAPI, HTTPException | |
| from pydantic import BaseModel | |
| from typing import Optional, Dict, Any | |
| from environment.env import FabYieldEnv | |
| # βββ App βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| app = FastAPI( | |
| title="Semiconductor Fab & AI Hardware Optimization Environment", | |
| description="Multi-Agent RL Environment for OpenEnv Hackathon", | |
| version="1.1.0", | |
| ) | |
| # Global env instance | |
| env = FabYieldEnv(difficulty=1) | |
| # βββ Request / Response Models ββββββββββββββββββββββββββββββββββββββββββββββββ | |
| class ResetRequest(BaseModel): | |
| seed: Optional[int] = None | |
| difficulty: int = 1 | |
| class StepRequest(BaseModel): | |
| think: str = "" # NEW: Test-Time Compute | |
| params: Dict[str, float] | |
| primary_bottleneck: str = "" | |
| reasoning: str = "" | |
| submit: bool = False | |
| class StepResponse(BaseModel): | |
| observation: Dict[str, Any] | |
| rewards: Dict[str, float] | |
| done: bool | |
| info: Dict[str, Any] | |
| # βββ Endpoints ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ | |
| def health_check(): | |
| try: | |
| state = env.state() | |
| step = state.get("step") | |
| except RuntimeError: | |
| step = None | |
| return {"status": "online", "architecture": "OpenEnv", "current_step": step} | |
| def reset_environment(request: ResetRequest = ResetRequest()): | |
| # env.reset() now returns a standard dict | |
| obs_dict = env.reset(seed=request.seed, difficulty=request.difficulty) | |
| return obs_dict | |
| def step_environment(request: StepRequest) -> StepResponse: | |
| try: | |
| # Convert Pydantic request to dict for OpenEnv step() | |
| action_dict = request.model_dump() | |
| obs_dict, rewards, done, info = env.step(action_dict) | |
| return StepResponse( | |
| observation=obs_dict, | |
| rewards=rewards, | |
| done=done, | |
| info=info | |
| ) | |
| except RuntimeError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| def get_state(): | |
| """OpenEnv standard state reader.""" | |
| try: | |
| return env.state() | |
| except RuntimeError as e: | |
| raise HTTPException(status_code=400, detail=str(e)) | |
| def get_action_space(): | |
| return env.action_space | |
| def get_observation_space(): | |
| return env.observation_space | |
| import subprocess | |
| import os | |
| def startup_event(): | |
| print("π Booting Server and triggering Training Run...") | |
| # 1. Set the exact environment variables | |
| train_env = os.environ.copy() | |
| train_env["EPOCHS"] = "150" # Change to "150" when ready for the real burn | |
| train_env["GROUP_SIZE"] = "12" # G=8 for the A10G math stability | |
| train_env["SPACE_URL"] = "http://localhost:7860" | |
| # 2. Launch train.py as an independent background process | |
| subprocess.Popen(["python", "train.py"], env=train_env) |