prashasti commited on
Commit
01468da
·
1 Parent(s): 205f6c7

Changes to expose end points

Browse files
Files changed (1) hide show
  1. app.py +98 -39
app.py CHANGED
@@ -1,56 +1,115 @@
1
  """
2
- app.py quick local episode runner using the heuristic baseline agent.
3
 
4
- Usage
5
- python app.py
6
-
7
- This runs a single episode of the Simple task using the rule-based agent
8
- and prints a per-step log. It is intentionally lightweight and dependency-free
9
- (no LLM API key required) so it works out of the box.
10
  """
 
11
  from __future__ import annotations
12
- from typing import Any, Dict, List
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
- from tasks.task_simple import create_env
15
- from agent.baseline import act, reset_history
 
 
16
 
 
 
17
 
18
- def run_episode() -> List[Dict[str, Any]]:
19
- env = create_env()
20
- state = env.reset()
21
- reset_history()
22
 
23
- logs: List[Dict[str, Any]] = []
24
- done = False
25
- step = 0
26
 
27
- print("[app.py] Starting simple-task episode with heuristic baseline agent.")
 
28
 
29
- while not done:
30
- action = act(state)
31
- next_state, reward, done, info = env.step(action)
 
 
 
32
 
33
- logs.append({
34
- "step": step,
35
- "state": state,
36
- "action": action,
37
- "reward": round(reward, 3),
38
- "info": info,
39
- })
40
 
41
- print(
42
- f" step={step} action={action:<15} reward={round(reward, 3):>8.3f} "
43
- f"resolved={str(info.get('resolved', False)).lower()}"
44
- )
45
 
46
- state = next_state
47
- step += 1
 
 
 
 
 
48
 
49
- success = any(e["info"].get("resolved", False) for e in logs)
50
- print(f"\n[app.py] Episode finished — steps={step} success={str(success).lower()}")
51
- return logs
52
 
53
 
54
  if __name__ == "__main__":
55
- logs = run_episode()
56
- print(f"Steps: {len(logs)}")
 
1
  """
2
+ HF Spaces server DebugOps Environment (OpenEnv compatible)
3
 
4
+ Exposes:
5
+ POST /reset
6
+ POST /step
7
+ GET /state
 
 
8
  """
9
+
10
  from __future__ import annotations
11
+ from typing import Dict, Any, Optional
12
+
13
+ import os
14
+ import uvicorn
15
+ from fastapi import FastAPI, HTTPException
16
+ from pydantic import BaseModel
17
+
18
+ # Import your environments
19
+ from tasks.task_simple import create_env as create_simple
20
+ from tasks.task_multi_service import create_env as create_multi
21
+ from tasks.task_critical import create_env as create_critical
22
+
23
+
24
+ app = FastAPI(title="DebugOps AI Environment", version="1.0.0")
25
+
26
+ # Global env instance
27
+ _env = None
28
+
29
+ class ResetRequest(BaseModel):
30
+ task: str = "simple" # simple | multi_service | critical
31
+
32
+
33
+ class StepRequest(BaseModel):
34
+ action: str
35
+
36
+ def create_env(task: str):
37
+ if task == "simple":
38
+ return create_simple()
39
+ elif task == "multi_service":
40
+ return create_multi()
41
+ elif task == "critical":
42
+ return create_critical()
43
+ else:
44
+ raise ValueError(f"Invalid task: {task}")
45
+
46
+
47
+ def get_env():
48
+ global _env
49
+ if _env is None:
50
+ raise HTTPException(status_code=400, detail="Call /reset first")
51
+ return _env
52
+
53
+ @app.get("/")
54
+ def root():
55
+ return {
56
+ "name": "DebugOps AI Environment",
57
+ "description": "Production debugging RL environment (OpenEnv compatible)",
58
+ "endpoints": ["/reset", "/step", "/state", "/health"],
59
+ }
60
+
61
+
62
+ @app.get("/health")
63
+ def health():
64
+ return {"status": "ok"}
65
+
66
+
67
+ @app.post("/reset")
68
+ def reset(request: ResetRequest):
69
+ global _env
70
+ try:
71
+ _env = create_env(request.task)
72
+ obs = _env.reset()
73
 
74
+ return {
75
+ "observation": obs,
76
+ "done": False,
77
+ }
78
 
79
+ except Exception as e:
80
+ raise HTTPException(status_code=500, detail=str(e))
81
 
 
 
 
 
82
 
83
+ @app.post("/step")
84
+ def step(request: StepRequest):
85
+ env = get_env()
86
 
87
+ try:
88
+ obs, reward, done, info = env.step(request.action)
89
 
90
+ return {
91
+ "observation": obs,
92
+ "reward": float(round(reward, 3)),
93
+ "done": done,
94
+ "info": info,
95
+ }
96
 
97
+ except Exception as e:
98
+ raise HTTPException(status_code=400, detail=str(e))
 
 
 
 
 
99
 
 
 
 
 
100
 
101
+ @app.get("/state")
102
+ def state():
103
+ env = get_env()
104
+ try:
105
+ return env.state()
106
+ except Exception as e:
107
+ raise HTTPException(status_code=400, detail=str(e))
108
 
109
+ def main():
110
+ port = int(os.getenv("PORT", 7860))
111
+ uvicorn.run(app, host="0.0.0.0", port=port)
112
 
113
 
114
  if __name__ == "__main__":
115
+ main()