Spaces:
Runtime error
Runtime error
itsZaid05 commited on
Updated app.py
Browse files- server/app.py +5 -21
server/app.py
CHANGED
|
@@ -8,7 +8,6 @@ from server.env import EcoLLMInferenceRoutingEnvironment
|
|
| 8 |
from server.models import RLAction, RLObservation, Strategy, ModelChoice
|
| 9 |
from server.tasks import TASKS
|
| 10 |
|
| 11 |
-
# 1. Create native OpenEnv app
|
| 12 |
app = create_app(
|
| 13 |
EcoLLMInferenceRoutingEnvironment,
|
| 14 |
RLAction,
|
|
@@ -17,7 +16,6 @@ app = create_app(
|
|
| 17 |
max_concurrent_envs=4,
|
| 18 |
)
|
| 19 |
|
| 20 |
-
# 2. Grading Metadata - UPDATED BOUNDS
|
| 21 |
_GRADER_BOUNDS = {
|
| 22 |
"task_1": {"worst": 0.0, "best": 1.0},
|
| 23 |
"task_2": {"worst": 0.5, "best": 2.5},
|
|
@@ -33,7 +31,6 @@ class GraderRequest(BaseModel):
|
|
| 33 |
task_id: str
|
| 34 |
actions: List[GraderAction]
|
| 35 |
|
| 36 |
-
# 3. Add required extra endpoints to the OpenEnv app
|
| 37 |
@app.get("/health")
|
| 38 |
def health():
|
| 39 |
return {"status": "ok"}
|
|
@@ -47,15 +44,9 @@ def list_tasks():
|
|
| 47 |
{"id": "task_3", "name": "Stateful Carbon-Aware Routing", "difficulty": "hard"},
|
| 48 |
]
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
def reset(body: dict = {}):
|
| 52 |
-
task_id = body.get("task_id", "task_1")
|
| 53 |
-
obs = env.reset(task_id=task_id)
|
| 54 |
-
return obs
|
| 55 |
-
|
| 56 |
@app.post("/grader")
|
| 57 |
def grade_episode(request: GraderRequest):
|
| 58 |
-
# Validate task_id
|
| 59 |
if request.task_id not in TASKS:
|
| 60 |
raise HTTPException(status_code=400, detail=f"Invalid task_id: {request.task_id}")
|
| 61 |
|
|
@@ -64,12 +55,12 @@ def grade_episode(request: GraderRequest):
|
|
| 64 |
total_reward = 0.0
|
| 65 |
|
| 66 |
for a in request.actions:
|
| 67 |
-
if obs.done:
|
| 68 |
break
|
| 69 |
try:
|
| 70 |
action = RLAction(
|
| 71 |
-
strategy=Strategy(a.strategy),
|
| 72 |
-
model_choice=ModelChoice(a.model_choice),
|
| 73 |
exit_flag=a.exit_flag
|
| 74 |
)
|
| 75 |
except (ValueError, KeyError) as e:
|
|
@@ -80,18 +71,11 @@ def grade_episode(request: GraderRequest):
|
|
| 80 |
|
| 81 |
bounds = _GRADER_BOUNDS[request.task_id]
|
| 82 |
score = (total_reward - bounds["worst"]) / (bounds["best"] - bounds["worst"])
|
| 83 |
-
|
| 84 |
-
# Normalize to (0.0, 1.0) - NO CLAMPING to 0.01-0.99
|
| 85 |
score = max(0.0, min(1.0, score))
|
| 86 |
|
| 87 |
-
return {
|
| 88 |
-
"task_id": request.task_id,
|
| 89 |
-
"score": score,
|
| 90 |
-
"reward": total_reward
|
| 91 |
-
}
|
| 92 |
|
| 93 |
def main(host: str = "0.0.0.0", port: int = 7860) -> None:
|
| 94 |
-
"""Run the development server."""
|
| 95 |
uvicorn.run(app, host=host, port=port, log_level="info")
|
| 96 |
|
| 97 |
if __name__ == "__main__":
|
|
|
|
| 8 |
from server.models import RLAction, RLObservation, Strategy, ModelChoice
|
| 9 |
from server.tasks import TASKS
|
| 10 |
|
|
|
|
| 11 |
app = create_app(
|
| 12 |
EcoLLMInferenceRoutingEnvironment,
|
| 13 |
RLAction,
|
|
|
|
| 16 |
max_concurrent_envs=4,
|
| 17 |
)
|
| 18 |
|
|
|
|
| 19 |
_GRADER_BOUNDS = {
|
| 20 |
"task_1": {"worst": 0.0, "best": 1.0},
|
| 21 |
"task_2": {"worst": 0.5, "best": 2.5},
|
|
|
|
| 31 |
task_id: str
|
| 32 |
actions: List[GraderAction]
|
| 33 |
|
|
|
|
| 34 |
@app.get("/health")
|
| 35 |
def health():
|
| 36 |
return {"status": "ok"}
|
|
|
|
| 44 |
{"id": "task_3", "name": "Stateful Carbon-Aware Routing", "difficulty": "hard"},
|
| 45 |
]
|
| 46 |
}
|
| 47 |
+
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
@app.post("/grader")
|
| 49 |
def grade_episode(request: GraderRequest):
|
|
|
|
| 50 |
if request.task_id not in TASKS:
|
| 51 |
raise HTTPException(status_code=400, detail=f"Invalid task_id: {request.task_id}")
|
| 52 |
|
|
|
|
| 55 |
total_reward = 0.0
|
| 56 |
|
| 57 |
for a in request.actions:
|
| 58 |
+
if obs.done:
|
| 59 |
break
|
| 60 |
try:
|
| 61 |
action = RLAction(
|
| 62 |
+
strategy=Strategy(a.strategy),
|
| 63 |
+
model_choice=ModelChoice(a.model_choice),
|
| 64 |
exit_flag=a.exit_flag
|
| 65 |
)
|
| 66 |
except (ValueError, KeyError) as e:
|
|
|
|
| 71 |
|
| 72 |
bounds = _GRADER_BOUNDS[request.task_id]
|
| 73 |
score = (total_reward - bounds["worst"]) / (bounds["best"] - bounds["worst"])
|
|
|
|
|
|
|
| 74 |
score = max(0.0, min(1.0, score))
|
| 75 |
|
| 76 |
+
return {"task_id": request.task_id, "score": score, "reward": total_reward}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
|
| 78 |
def main(host: str = "0.0.0.0", port: int = 7860) -> None:
|
|
|
|
| 79 |
uvicorn.run(app, host=host, port=port, log_level="info")
|
| 80 |
|
| 81 |
if __name__ == "__main__":
|