SyamSashank commited on
Commit
3e0331f
·
verified ·
1 Parent(s): 09b5e1f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +34 -5
app.py CHANGED
@@ -1,13 +1,18 @@
1
  import uuid
 
2
  from fastapi import FastAPI, HTTPException
3
  from pydantic import BaseModel
 
 
 
 
4
  from environment.env import CodeReviewEnv
5
  from environment.models import Action, Observation, Reward
6
 
7
  app = FastAPI(title="CodeReviewEnv")
8
 
9
- # In-memory store for environments categorized by session ID
10
- sessions = {}
11
 
12
  class ResetRequest(BaseModel):
13
  task_id: str
@@ -16,6 +21,18 @@ class StepRequest(BaseModel):
16
  session_id: str
17
  action: Action
18
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  @app.get("/health")
20
  async def health():
21
  return {"status": "ok"}
@@ -28,23 +45,31 @@ async def reset_endpoint(req: ResetRequest):
28
  env = CodeReviewEnv(req.task_id)
29
  obs = env.reset()
30
  sessions[session_id] = env
31
-
32
  # Return observation alongside the session ID
33
  return {
34
  "session_id": session_id,
35
  "observation": obs.dict()
36
  }
37
  except Exception as e:
38
- raise HTTPException(status_code=400, detail=str(e))
39
 
40
  @app.post("/step")
41
  async def step_endpoint(req: StepRequest):
 
42
  if req.session_id not in sessions:
43
  raise HTTPException(status_code=400, detail="Invalid session ID or expired environment.")
44
 
 
45
  env = sessions[req.session_id]
 
46
  try:
47
  obs, reward, done, info = env.step(req.action)
 
 
 
 
 
48
  return {
49
  "observation": obs.dict(),
50
  "reward": reward.dict(),
@@ -52,10 +77,14 @@ async def step_endpoint(req: StepRequest):
52
  "info": info
53
  }
54
  except Exception as e:
55
- raise HTTPException(status_code=400, detail=str(e))
56
 
57
  @app.get("/state")
58
  async def state_endpoint(session_id: str):
59
  if session_id not in sessions:
60
  raise HTTPException(status_code=400, detail="Invalid session ID.")
61
  return sessions[session_id].state()
 
 
 
 
 
1
  import uuid
2
+ import os
3
  from fastapi import FastAPI, HTTPException
4
  from pydantic import BaseModel
5
+ from typing import Dict
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):
18
  task_id: str
 
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"}
 
45
  env = CodeReviewEnv(req.task_id)
46
  obs = env.reset()
47
  sessions[session_id] = env
48
+
49
  # Return observation alongside the session ID
50
  return {
51
  "session_id": session_id,
52
  "observation": obs.dict()
53
  }
54
  except Exception as e:
55
+ raise HTTPException(status_code=400, detail=f"Reset failed: {str(e)}")
56
 
57
  @app.post("/step")
58
  async def step_endpoint(req: StepRequest):
59
+ # Check if session exists
60
  if req.session_id not in sessions:
61
  raise HTTPException(status_code=400, detail="Invalid session ID or expired environment.")
62
 
63
+ # Corrected Indentation: This must be outside the 'if' block
64
  env = sessions[req.session_id]
65
+
66
  try:
67
  obs, reward, done, info = env.step(req.action)
68
+
69
+ # Optional: Remove session if 'done' to save memory
70
+ if done:
71
+ del sessions[req.session_id]
72
+
73
  return {
74
  "observation": obs.dict(),
75
  "reward": reward.dict(),
 
77
  "info": info
78
  }
79
  except Exception as e:
80
+ raise HTTPException(status_code=400, detail=f"Step execution failed: {str(e)}")
81
 
82
  @app.get("/state")
83
  async def state_endpoint(session_id: str):
84
  if session_id not in sessions:
85
  raise HTTPException(status_code=400, detail="Invalid session ID.")
86
  return sessions[session_id].state()
87
+
88
+ # Note: In Hugging Face Spaces with Docker,
89
+ # you do NOT need 'if __name__ == "__main__": uvicorn.run(...)'
90
+ # because it is handled by your Dockerfile CMD.