Spaces:
Sleeping
Sleeping
Fix: Add POST support for /reset and /step endpoints
Browse files- /reset now accepts both GET and POST
- Added /step POST endpoint
- Maintains env_instance state between calls
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
- server/app.py +29 -3
server/app.py
CHANGED
|
@@ -5,6 +5,8 @@ from env import create_env
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
|
|
|
|
|
|
| 8 |
|
| 9 |
@app.get("/")
|
| 10 |
def root():
|
|
@@ -12,10 +14,34 @@ def root():
|
|
| 12 |
|
| 13 |
|
| 14 |
@app.get("/reset")
|
|
|
|
| 15 |
def reset(task: str = "easy"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
except Exception as e:
|
| 21 |
return {"error": str(e)}
|
|
|
|
| 5 |
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
+
env_instance = None
|
| 9 |
+
|
| 10 |
|
| 11 |
@app.get("/")
|
| 12 |
def root():
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
@app.get("/reset")
|
| 17 |
+
@app.post("/reset")
|
| 18 |
def reset(task: str = "easy"):
|
| 19 |
+
global env_instance
|
| 20 |
+
try:
|
| 21 |
+
env_instance = create_env(task)
|
| 22 |
+
obs = env_instance.reset()
|
| 23 |
+
return {
|
| 24 |
+
"status": "ok",
|
| 25 |
+
"task": task,
|
| 26 |
+
"observation": str(obs)
|
| 27 |
+
}
|
| 28 |
+
except Exception as e:
|
| 29 |
+
return {"error": str(e)}
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
@app.post("/step")
|
| 33 |
+
def step(action: str = "analyze_pattern"):
|
| 34 |
+
global env_instance
|
| 35 |
try:
|
| 36 |
+
if env_instance is None:
|
| 37 |
+
env_instance = create_env()
|
| 38 |
+
env_instance.reset()
|
| 39 |
+
obs, reward, done, info = env_instance.step(action)
|
| 40 |
+
return {
|
| 41 |
+
"observation": str(obs),
|
| 42 |
+
"reward": reward,
|
| 43 |
+
"done": done,
|
| 44 |
+
"info": info
|
| 45 |
+
}
|
| 46 |
except Exception as e:
|
| 47 |
return {"error": str(e)}
|