SyamSashank commited on
Commit
7a7e9a6
·
verified ·
1 Parent(s): b7137df

Update server/app.py

Browse files
Files changed (1) hide show
  1. server/app.py +17 -67
server/app.py CHANGED
@@ -1,17 +1,13 @@
 
1
  import uuid
2
  import os
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
  from typing import Dict, Optional
6
-
7
- # Import your environment logic
8
- # Ensure these files exist in a folder named 'environment' with an __init__.py file
9
  from environment.env import CodeReviewEnv
10
  from environment.models import Action, Observation, Reward
11
 
12
  app = FastAPI(title="CodeReviewEnv")
13
-
14
- # In-memory store for environments categorized by session IDs
15
  sessions: Dict[str, CodeReviewEnv] = {}
16
 
17
  class ResetRequest(BaseModel):
@@ -21,76 +17,30 @@ class StepRequest(BaseModel):
21
  session_id: str
22
  action: Action
23
 
24
- # --- IMPORTANT FOR HUGGING FACE ---
25
- # This root route tells Hugging Face your app is alive.
26
- # Without this, the Space may stay on "Starting..."
27
  @app.get("/")
28
  async def root():
29
- return {
30
- "status": "online",
31
- "environment": "CodeReviewEnv",
32
- "tag": "openenv",
33
- "docs": "/docs"
34
- }
35
-
36
- @app.get("/health")
37
- async def health():
38
- return {"status": "ok"}
39
 
40
  @app.post("/reset")
41
  async def reset_endpoint(req: Optional[ResetRequest] = None):
42
- try:
43
- if req is None:
44
- req = ResetRequest()
45
- # Create a new session with a unique ID
46
- session_id = str(uuid.uuid4())
47
- env = CodeReviewEnv(req.task_id)
48
- obs = env.reset()
49
- sessions[session_id] = env
50
-
51
- # Return observation alongside the session ID
52
- return {
53
- "session_id": session_id,
54
- "observation": obs.dict()
55
- }
56
- except Exception as e:
57
- raise HTTPException(status_code=400, detail=f"Reset failed: {str(e)}")
58
 
59
  @app.post("/step")
60
  async def step_endpoint(req: StepRequest):
61
- # Check if session exists
62
- if req.session_id not in sessions:
63
- raise HTTPException(status_code=400, detail="Invalid session ID or expired environment.")
64
-
65
- # Corrected Indentation: This must be outside the 'if' block
66
  env = sessions[req.session_id]
67
-
68
- try:
69
- obs, reward, done, info = env.step(req.action)
70
-
71
- # Optional: Remove session if 'done' to save memory
72
- if done:
73
- del sessions[req.session_id]
74
-
75
- return {
76
- "observation": obs.dict(),
77
- "reward": reward.dict(),
78
- "done": done,
79
- "info": info
80
- }
81
- except Exception as e:
82
- raise HTTPException(status_code=400, detail=f"Step execution failed: {str(e)}")
83
 
84
- @app.get("/state")
85
- async def state_endpoint(session_id: str):
86
- if session_id not in sessions:
87
- raise HTTPException(status_code=400, detail="Invalid session ID.")
88
- return sessions[session_id].state()
89
-
90
- # Note: In Hugging Face Spaces with Docker,
91
- # you do NOT need 'if __name__ == "__main__": uvicorn.run(...)'
92
- # because it is handled by your Dockerfile CMD.
93
-
94
- def start():
95
  import uvicorn
96
- uvicorn.run("server.app:app", host="0.0.0.0", port=7860)
 
 
 
 
 
1
+
2
  import uuid
3
  import os
4
  from fastapi import FastAPI, HTTPException
5
  from pydantic import BaseModel
6
  from typing import Dict, Optional
 
 
 
7
  from environment.env import CodeReviewEnv
8
  from environment.models import Action, Observation, Reward
9
 
10
  app = FastAPI(title="CodeReviewEnv")
 
 
11
  sessions: Dict[str, CodeReviewEnv] = {}
12
 
13
  class ResetRequest(BaseModel):
 
17
  session_id: str
18
  action: Action
19
 
 
 
 
20
  @app.get("/")
21
  async def root():
22
+ return {"status": "online", "environment": "CodeReviewEnv", "tag": "openenv"}
 
 
 
 
 
 
 
 
 
23
 
24
  @app.post("/reset")
25
  async def reset_endpoint(req: Optional[ResetRequest] = None):
26
+ if req is None: req = ResetRequest()
27
+ session_id = str(uuid.uuid4())
28
+ env = CodeReviewEnv(req.task_id)
29
+ obs = env.reset()
30
+ sessions[session_id] = env
31
+ return {"session_id": session_id, "observation": obs.dict()}
 
 
 
 
 
 
 
 
 
 
32
 
33
  @app.post("/step")
34
  async def step_endpoint(req: StepRequest):
35
+ if req.session_id not in sessions: raise HTTPException(status_code=400, detail="Invalid session ID")
 
 
 
 
36
  env = sessions[req.session_id]
37
+ obs, reward, done, info = env.step(req.action)
38
+ return {"observation": obs.dict(), "reward": reward.dict(), "done": done, "info": info}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
39
 
40
+ def main():
 
 
 
 
 
 
 
 
 
 
41
  import uvicorn
42
+ # 8000 is standard for the OpenEnv multi-mode validator
43
+ uvicorn.run("server.app:app", host="0.0.0.0", port=8000)
44
+
45
+ if __name__ == "__main__":
46
+ main()