Spaces:
Sleeping
Sleeping
changes in app.py
#13
by gensis88 - opened
- server/app.py +195 -181
server/app.py
CHANGED
|
@@ -1,181 +1,195 @@
|
|
| 1 |
-
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
-
# All rights reserved.
|
| 3 |
-
#
|
| 4 |
-
# This source code is licensed under the BSD-style license found in the
|
| 5 |
-
# LICENSE file in the root directory of this source tree.
|
| 6 |
-
|
| 7 |
-
"""
|
| 8 |
-
FastAPI application for the Wildfire Environment.
|
| 9 |
-
"""
|
| 10 |
-
import os
|
| 11 |
-
from typing import List
|
| 12 |
-
from fastapi import Request
|
| 13 |
-
from fastapi.staticfiles import StaticFiles
|
| 14 |
-
from fastapi.responses import FileResponse
|
| 15 |
-
|
| 16 |
-
try:
|
| 17 |
-
from openenv.core.env_server.http_server import create_app
|
| 18 |
-
except Exception as e: # pragma: no cover
|
| 19 |
-
raise ImportError(
|
| 20 |
-
"openenv is required
|
| 21 |
-
) from e
|
| 22 |
-
|
| 23 |
-
try:
|
| 24 |
-
from ..models import
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
from
|
| 31 |
-
from
|
| 32 |
-
from
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
if
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
@app.
|
| 75 |
-
def
|
| 76 |
-
active_env = get_active_env()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
return {"observation": obs_dict, "
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
@app.get("/
|
| 118 |
-
def
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
"
|
| 124 |
-
"
|
| 125 |
-
"
|
| 126 |
-
}
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Copyright (c) Meta Platforms, Inc. and affiliates.
|
| 2 |
+
# All rights reserved.
|
| 3 |
+
#
|
| 4 |
+
# This source code is licensed under the BSD-style license found in the
|
| 5 |
+
# LICENSE file in the root directory of this source tree.
|
| 6 |
+
|
| 7 |
+
"""
|
| 8 |
+
FastAPI application for the Wildfire Environment.
|
| 9 |
+
"""
|
| 10 |
+
import os
|
| 11 |
+
from typing import List
|
| 12 |
+
from fastapi import Request
|
| 13 |
+
from fastapi.staticfiles import StaticFiles
|
| 14 |
+
from fastapi.responses import FileResponse
|
| 15 |
+
|
| 16 |
+
try:
|
| 17 |
+
from openenv.core.env_server.http_server import create_app
|
| 18 |
+
except Exception as e: # pragma: no cover
|
| 19 |
+
raise ImportError(
|
| 20 |
+
"openenv is required. Install dependencies with '\n uv sync\n'"
|
| 21 |
+
) from e
|
| 22 |
+
|
| 23 |
+
try:
|
| 24 |
+
from ..models import (
|
| 25 |
+
WildfireAction, WildfireObservation,
|
| 26 |
+
TaskResponse, GraderResponse, BaselineResponse,
|
| 27 |
+
ResetRequest, StepResponse, ResetResponse, StateResponse,
|
| 28 |
+
)
|
| 29 |
+
# FIX: correct filename and class name
|
| 30 |
+
from .wildfireEnvironment_environment import WildfireEnv
|
| 31 |
+
from .tasks import TASKS, TaskGrader
|
| 32 |
+
from .agent import BaselineAgent
|
| 33 |
+
except (ModuleNotFoundError, ImportError):
|
| 34 |
+
from models import (
|
| 35 |
+
WildfireAction, WildfireObservation,
|
| 36 |
+
TaskResponse, GraderResponse, BaselineResponse,
|
| 37 |
+
ResetRequest, StepResponse, ResetResponse, StateResponse,
|
| 38 |
+
)
|
| 39 |
+
# FIX: correct filename and class name
|
| 40 |
+
from server.wildfireEnvironment_environment import WildfireEnv
|
| 41 |
+
from server.tasks import TASKS, TaskGrader
|
| 42 |
+
from server.agent import BaselineAgent
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Create the app with web interface and README integration
|
| 46 |
+
app = create_app(
|
| 47 |
+
WildfireEnv,
|
| 48 |
+
WildfireAction,
|
| 49 |
+
WildfireObservation,
|
| 50 |
+
env_name="wildfireEnvironment",
|
| 51 |
+
max_concurrent_envs=1,
|
| 52 |
+
)
|
| 53 |
+
|
| 54 |
+
# -----------------------------------------------------------------------------
|
| 55 |
+
# Remove auto-generated /step, /reset, /state so we can define bare endpoints
|
| 56 |
+
# -----------------------------------------------------------------------------
|
| 57 |
+
app.router.routes = [
|
| 58 |
+
r for r in app.router.routes
|
| 59 |
+
if getattr(r, "path", None) not in ["/reset", "/step", "/state"]
|
| 60 |
+
]
|
| 61 |
+
|
| 62 |
+
GLOBAL_ENV = WildfireEnv()
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def get_active_env():
|
| 66 |
+
"""Return the currently active OpenEnv session instance."""
|
| 67 |
+
if hasattr(app.state, "env_manager"):
|
| 68 |
+
envs = getattr(app.state.env_manager, "_envs", {})
|
| 69 |
+
if envs:
|
| 70 |
+
return list(envs.values())[-1]
|
| 71 |
+
return GLOBAL_ENV
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
@app.api_route("/reset", methods=["GET", "POST"], response_model=ResetResponse)
|
| 75 |
+
def reset_env(body: ResetRequest = None):
|
| 76 |
+
active_env = get_active_env()
|
| 77 |
+
task = (body.task if body else None) or 1
|
| 78 |
+
obs = active_env.reset(task=task)
|
| 79 |
+
obs_dict = obs.model_dump() if hasattr(obs, "model_dump") else obs.dict()
|
| 80 |
+
return {"observation": obs_dict, "info": {"task": task}}
|
| 81 |
+
|
| 82 |
+
|
| 83 |
+
@app.post("/step", response_model=StepResponse)
|
| 84 |
+
def step_env(action: WildfireAction):
|
| 85 |
+
active_env = get_active_env()
|
| 86 |
+
obs = active_env.step(action)
|
| 87 |
+
obs_dict = obs.model_dump() if hasattr(obs, "model_dump") else obs.dict()
|
| 88 |
+
info = obs_dict.get("metadata", {})
|
| 89 |
+
return {"observation": obs_dict, "reward": obs.reward, "done": obs.done, "info": info}
|
| 90 |
+
|
| 91 |
+
|
| 92 |
+
@app.get("/state", response_model=StateResponse)
|
| 93 |
+
def get_state():
|
| 94 |
+
active_env = get_active_env()
|
| 95 |
+
return {"state": active_env.get_internal_state()}
|
| 96 |
+
|
| 97 |
+
|
| 98 |
+
# -----------------------------------------------------------------------------
|
| 99 |
+
# Additional routes
|
| 100 |
+
# -----------------------------------------------------------------------------
|
| 101 |
+
|
| 102 |
+
STATIC_DIR = os.path.join(
|
| 103 |
+
os.path.dirname(os.path.dirname(os.path.abspath(__file__))), "static"
|
| 104 |
+
)
|
| 105 |
+
if os.path.isdir(STATIC_DIR):
|
| 106 |
+
app.mount("/static", StaticFiles(directory=STATIC_DIR), name="static")
|
| 107 |
+
|
| 108 |
+
|
| 109 |
+
@app.get("/", include_in_schema=False)
|
| 110 |
+
def serve_frontend():
|
| 111 |
+
index = os.path.join(STATIC_DIR, "index.html")
|
| 112 |
+
if os.path.exists(index):
|
| 113 |
+
return FileResponse(index)
|
| 114 |
+
return {"message": "Wildfire ICS OpenEnv API is running. Visit /docs for the API."}
|
| 115 |
+
|
| 116 |
+
|
| 117 |
+
@app.get("/tasks", response_model=List[TaskResponse])
|
| 118 |
+
def list_tasks():
|
| 119 |
+
return [
|
| 120 |
+
{
|
| 121 |
+
"id": t.id,
|
| 122 |
+
"name": t.name,
|
| 123 |
+
"difficulty": t.difficulty,
|
| 124 |
+
"description": t.description,
|
| 125 |
+
"grader": t.grader,
|
| 126 |
+
}
|
| 127 |
+
for t in TASKS
|
| 128 |
+
]
|
| 129 |
+
|
| 130 |
+
|
| 131 |
+
@app.get("/grader", response_model=GraderResponse)
|
| 132 |
+
def get_grader_scores():
|
| 133 |
+
active_env = get_active_env()
|
| 134 |
+
grader = TaskGrader(active_env)
|
| 135 |
+
return {
|
| 136 |
+
"task_scores": {
|
| 137 |
+
"task_1": grader.grade_task_1(),
|
| 138 |
+
"task_2": grader.grade_task_2(),
|
| 139 |
+
"task_3": grader.grade_task_3(),
|
| 140 |
+
}
|
| 141 |
+
}
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
@app.get("/baseline", response_model=BaselineResponse)
|
| 145 |
+
def run_baseline():
|
| 146 |
+
"""Run the rule-based baseline agent on a fresh env instance."""
|
| 147 |
+
fresh_env = WildfireEnv()
|
| 148 |
+
|
| 149 |
+
current_task = 1
|
| 150 |
+
try:
|
| 151 |
+
active_env = get_active_env()
|
| 152 |
+
if hasattr(active_env, "task"):
|
| 153 |
+
current_task = active_env.task
|
| 154 |
+
except Exception:
|
| 155 |
+
pass
|
| 156 |
+
|
| 157 |
+
fresh_env.reset(task=current_task)
|
| 158 |
+
agent = BaselineAgent(fresh_env)
|
| 159 |
+
|
| 160 |
+
total_reward = 0.0
|
| 161 |
+
steps = 0
|
| 162 |
+
|
| 163 |
+
while not fresh_env.done:
|
| 164 |
+
obs_obj = fresh_env._get_obs()
|
| 165 |
+
obs_dict = obs_obj.model_dump() if hasattr(obs_obj, "model_dump") else obs_obj.dict()
|
| 166 |
+
|
| 167 |
+
# Use multi-action dispatch so baseline commands all burning divisions
|
| 168 |
+
actions = agent.select_all_actions(obs_dict)
|
| 169 |
+
for action_dict in actions:
|
| 170 |
+
action_obj = WildfireAction(**action_dict)
|
| 171 |
+
obs_res = fresh_env.step(action_obj)
|
| 172 |
+
total_reward += obs_res.reward
|
| 173 |
+
if fresh_env.done:
|
| 174 |
+
break
|
| 175 |
+
steps += 1
|
| 176 |
+
|
| 177 |
+
grader = TaskGrader(fresh_env)
|
| 178 |
+
return {
|
| 179 |
+
"task_scores": {
|
| 180 |
+
"task_1": grader.grade_task_1(),
|
| 181 |
+
"task_2": grader.grade_task_2(),
|
| 182 |
+
"task_3": grader.grade_task_3(),
|
| 183 |
+
},
|
| 184 |
+
"total_reward": round(total_reward, 4),
|
| 185 |
+
"steps_taken": steps,
|
| 186 |
+
}
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def main(host: str = "0.0.0.0", port: int = 8000):
|
| 190 |
+
import uvicorn
|
| 191 |
+
uvicorn.run(app, host=host, port=port)
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
if __name__ == "__main__":
|
| 195 |
+
main()
|