CoolFace
Apppublic

DarkyCodez/MetaxScaler

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
app.py70 linesDownload Raw Back to server
1from fastapi import FastAPI
2from pydantic import BaseModel
3from typing import Dict, Any
4import uvicorn
5
6from server.core_sim import GreenhouseEnv
7
8# Pydantic Models
9class ActionRequest(BaseModel):
10    action: int
11
12
13
14app = FastAPI(title="Agricultural Environment")
15
16
17env = GreenhouseEnv()
18
19
20
21@app.get("/")
22def home():
23    
24    return {"message": "Agricultural Environment is Live"}
25
26
27
28@app.get("/state")
29def get_state():
30    return env.get_state()
31
32
33
34@app.post("/reset")
35def reset():
36    env.reset()
37    return env.get_state()
38
39
40
41@app.post("/step")
42def step(action_request: ActionRequest) -> Dict[str, Any]:
43    reward = env.step(action_request.action)
44    state = env.get_state()
45    done = (state["crop_health"] <= 0.0) or (action_request.action == 3)
46    
47    observation = {
48        "soil_moisture": state["soil_moisture"],
49        "nitrogen_level": state["nitrogen_level"],
50        "crop_health": state["crop_health"]
51    }
52    
53    info = {
54        "turn_count": state["turn_count"],
55        "action_taken": action_request.action
56    }
57    
58    return {
59        "observation": observation,
60        "reward": reward,
61        "done": done,
62        "info": info
63    }
64
65def main():
66    import uvicorn
67    uvicorn.run("server.app:app", host="0.0.0.0", port=7860)
68
69if __name__ == "__main__":
70    main()