SyamSashank commited on
Commit
6bbdeab
·
verified ·
1 Parent(s): e461952

Update server/app.py

Browse files
Files changed (1) hide show
  1. server/app.py +96 -0
server/app.py CHANGED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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):
18
+ task_id: str = "easy"
19
+
20
+ 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)