Spaces:
Sleeping
Sleeping
Commit ·
7258656
1
Parent(s): 0760ac9
Added mandatory API endpoints /tasks /grader /baseline
Browse files- my_env/server/app.py +137 -33
my_env/server/app.py
CHANGED
|
@@ -7,25 +7,17 @@
|
|
| 7 |
"""
|
| 8 |
FastAPI application for the Smart Farm Irrigation Environment.
|
| 9 |
|
| 10 |
-
This module creates an HTTP server that exposes the FarmEnvironment
|
| 11 |
-
over HTTP and WebSocket endpoints, compatible with EnvClient.
|
| 12 |
-
|
| 13 |
Endpoints:
|
| 14 |
-
- POST /reset:
|
| 15 |
-
- POST /step:
|
| 16 |
-
- GET /state:
|
| 17 |
-
- GET /schema:
|
| 18 |
-
- WS /ws:
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Production:
|
| 25 |
-
uvicorn server.app:app --host 0.0.0.0 --port 8000 --workers 4
|
| 26 |
-
|
| 27 |
-
# Or run directly:
|
| 28 |
-
python -m server.app
|
| 29 |
"""
|
| 30 |
|
| 31 |
try:
|
|
@@ -42,35 +34,147 @@ except ModuleNotFoundError:
|
|
| 42 |
from models import FarmAction, FarmObservation
|
| 43 |
from server.my_env_environment import FarmEnvironment
|
| 44 |
|
|
|
|
|
|
|
|
|
|
| 45 |
# Create the app with web interface and README integration
|
| 46 |
app = create_app(
|
| 47 |
-
|
| 48 |
FarmAction,
|
| 49 |
FarmObservation,
|
| 50 |
env_name="smart-farm-irrigation",
|
| 51 |
-
max_concurrent_envs=1,
|
| 52 |
)
|
| 53 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
"""
|
| 57 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
Args:
|
| 65 |
-
host: Host address to bind to (default: "0.0.0.0")
|
| 66 |
-
port: Port number to listen on (default: 8000)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
"""
|
| 72 |
import uvicorn
|
| 73 |
-
|
| 74 |
uvicorn.run(app, host=host, port=port)
|
| 75 |
|
| 76 |
|
|
|
|
| 7 |
"""
|
| 8 |
FastAPI application for the Smart Farm Irrigation Environment.
|
| 9 |
|
|
|
|
|
|
|
|
|
|
| 10 |
Endpoints:
|
| 11 |
+
- POST /reset: Reset the environment
|
| 12 |
+
- POST /step: Execute an action
|
| 13 |
+
- GET /state: Get current environment state
|
| 14 |
+
- GET /schema: Get action/observation schemas
|
| 15 |
+
- WS /ws: WebSocket endpoint for persistent sessions
|
| 16 |
+
|
| 17 |
+
# Mandatory Hackathon Endpoints:
|
| 18 |
+
- GET /tasks: Returns list of all tasks + action schema
|
| 19 |
+
- POST /grader: Returns score after episode ends
|
| 20 |
+
- GET /baseline: Runs baseline agent, returns all 3 scores
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
"""
|
| 22 |
|
| 23 |
try:
|
|
|
|
| 34 |
from models import FarmAction, FarmObservation
|
| 35 |
from server.my_env_environment import FarmEnvironment
|
| 36 |
|
| 37 |
+
from fastapi import Request
|
| 38 |
+
from fastapi.responses import JSONResponse
|
| 39 |
+
|
| 40 |
# Create the app with web interface and README integration
|
| 41 |
app = create_app(
|
| 42 |
+
FarmEnvironment,
|
| 43 |
FarmAction,
|
| 44 |
FarmObservation,
|
| 45 |
env_name="smart-farm-irrigation",
|
| 46 |
+
max_concurrent_envs=1,
|
| 47 |
)
|
| 48 |
|
| 49 |
+
# MANDATORY ENDPOINT 1 — /tasks
|
| 50 |
+
# Returns list of tasks and action schema
|
| 51 |
+
@app.get("/tasks")
|
| 52 |
+
async def get_tasks():
|
| 53 |
+
"""
|
| 54 |
+
Returns all 3 tasks and the action schema.
|
| 55 |
+
Mandatory endpoint for hackathon submission.
|
| 56 |
+
"""
|
| 57 |
+
try:
|
| 58 |
+
from tasks import get_all_tasks
|
| 59 |
+
except ImportError:
|
| 60 |
+
from my_env.tasks import get_all_tasks
|
| 61 |
|
| 62 |
+
return JSONResponse(content=get_all_tasks())
|
| 63 |
+
|
| 64 |
+
# MANDATORY ENDPOINT 2 — /grader
|
| 65 |
+
# Returns score after episode ends
|
| 66 |
+
@app.post("/grader")
|
| 67 |
+
async def grade_episode(request: Request):
|
| 68 |
"""
|
| 69 |
+
Grades a completed episode and returns score.
|
| 70 |
+
|
| 71 |
+
Expected request body:
|
| 72 |
+
{
|
| 73 |
+
"task_id": 1,
|
| 74 |
+
"episode_history": [
|
| 75 |
+
{
|
| 76 |
+
"day": 1,
|
| 77 |
+
"soil_moisture": 55.0,
|
| 78 |
+
"crop_health": "good",
|
| 79 |
+
"water_action": 2,
|
| 80 |
+
"rainfall": 0.0
|
| 81 |
+
},
|
| 82 |
+
...
|
| 83 |
+
]
|
| 84 |
+
}
|
| 85 |
+
|
| 86 |
+
Returns score between 0.0 and 1.0
|
| 87 |
+
"""
|
| 88 |
+
try:
|
| 89 |
+
from graders import grade_task1, grade_task2, grade_task3
|
| 90 |
+
except ImportError:
|
| 91 |
+
from my_env.graders import grade_task1, grade_task2, grade_task3
|
| 92 |
+
|
| 93 |
+
try:
|
| 94 |
+
body = await request.json()
|
| 95 |
+
task_id = body.get("task_id", 1)
|
| 96 |
+
episode_history = body.get("episode_history", [])
|
| 97 |
+
|
| 98 |
+
# Select correct grader
|
| 99 |
+
graders = {
|
| 100 |
+
1: grade_task1,
|
| 101 |
+
2: grade_task2,
|
| 102 |
+
3: grade_task3,
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
if task_id not in graders:
|
| 106 |
+
return JSONResponse(
|
| 107 |
+
content={"error": f"Invalid task_id: {task_id}. Must be 1, 2, or 3"},
|
| 108 |
+
status_code=400
|
| 109 |
+
)
|
| 110 |
+
|
| 111 |
+
score = graders[task_id](episode_history)
|
| 112 |
+
|
| 113 |
+
return JSONResponse(content={
|
| 114 |
+
"task_id": task_id,
|
| 115 |
+
"score": score,
|
| 116 |
+
"min_score": 0.0,
|
| 117 |
+
"max_score": 1.0,
|
| 118 |
+
})
|
| 119 |
+
|
| 120 |
+
except Exception as e:
|
| 121 |
+
return JSONResponse(
|
| 122 |
+
content={"error": str(e)},
|
| 123 |
+
status_code=500
|
| 124 |
+
)
|
| 125 |
+
|
| 126 |
+
# MANDATORY ENDPOINT 3 — /baseline
|
| 127 |
+
# Runs baseline agent, returns all scores
|
| 128 |
+
@app.get("/baseline")
|
| 129 |
+
async def run_baseline(city: str = "Indore"):
|
| 130 |
+
"""
|
| 131 |
+
Runs the baseline rule-based agent through all 3 tasks
|
| 132 |
+
and returns reproducible scores.
|
| 133 |
|
| 134 |
+
Query params:
|
| 135 |
+
city: City name for weather data (default: Indore)
|
| 136 |
+
|
| 137 |
+
Returns scores for all 3 tasks + average score
|
| 138 |
+
"""
|
| 139 |
+
try:
|
| 140 |
+
from baseline import main as run_baseline_agent
|
| 141 |
+
except ImportError:
|
| 142 |
+
from my_env.baseline import main as run_baseline_agent
|
| 143 |
+
|
| 144 |
+
try:
|
| 145 |
+
results = run_baseline_agent(city=city)
|
| 146 |
+
|
| 147 |
+
# Format results for response
|
| 148 |
+
scores = {}
|
| 149 |
+
for result in results:
|
| 150 |
+
scores[f"task_{result['task_id']}"] = {
|
| 151 |
+
"name": result["task_name"],
|
| 152 |
+
"score": result["score"],
|
| 153 |
+
"steps_completed": result["steps_completed"],
|
| 154 |
+
}
|
| 155 |
+
|
| 156 |
+
avg = sum(r["score"] for r in results) / len(results)
|
| 157 |
+
|
| 158 |
+
return JSONResponse(content={
|
| 159 |
+
"city": city,
|
| 160 |
+
"agent": "Simple Rule-Based Agent",
|
| 161 |
+
"scores": scores,
|
| 162 |
+
"average_score": round(avg, 2),
|
| 163 |
+
"status": "success"
|
| 164 |
+
})
|
| 165 |
+
|
| 166 |
+
except Exception as e:
|
| 167 |
+
return JSONResponse(
|
| 168 |
+
content={"error": str(e)},
|
| 169 |
+
status_code=500
|
| 170 |
+
)
|
| 171 |
|
|
|
|
|
|
|
|
|
|
| 172 |
|
| 173 |
+
def main(host: str = "0.0.0.0", port: int = 8000):
|
| 174 |
+
"""
|
| 175 |
+
Entry point for direct execution via uv run or python -m.
|
| 176 |
"""
|
| 177 |
import uvicorn
|
|
|
|
| 178 |
uvicorn.run(app, host=host, port=port)
|
| 179 |
|
| 180 |
|