TanujInsane commited on
Commit ·
25a7357
1
Parent(s): 2e051b6
Fix 500 error on /reset and manually implement FastAPI endpoints for grader compatibility
Browse files- pyproject.toml +7 -1
- requirements.txt +8 -1
- server/app.py +101 -7
pyproject.toml
CHANGED
|
@@ -30,11 +30,17 @@ dependencies = [
|
|
| 30 |
"pandas",
|
| 31 |
"pydantic>=2.0",
|
| 32 |
"huggingface_hub<1.0.0",
|
| 33 |
-
"openenv-core>=0.2.3",
|
| 34 |
"fastapi",
|
| 35 |
"uvicorn[standard]",
|
| 36 |
"websockets>=15.0.1",
|
| 37 |
"httpx>=0.28.1",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
]
|
| 39 |
|
| 40 |
[project.urls]
|
|
|
|
| 30 |
"pandas",
|
| 31 |
"pydantic>=2.0",
|
| 32 |
"huggingface_hub<1.0.0",
|
|
|
|
| 33 |
"fastapi",
|
| 34 |
"uvicorn[standard]",
|
| 35 |
"websockets>=15.0.1",
|
| 36 |
"httpx>=0.28.1",
|
| 37 |
+
"gradio",
|
| 38 |
+
"flask",
|
| 39 |
+
"datasets",
|
| 40 |
+
"transformers",
|
| 41 |
+
"torch",
|
| 42 |
+
"openenv-core>=0.2.3",
|
| 43 |
+
"pydantic>=2.0",
|
| 44 |
]
|
| 45 |
|
| 46 |
[project.urls]
|
requirements.txt
CHANGED
|
@@ -7,5 +7,12 @@ transformers
|
|
| 7 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 8 |
huggingface_hub>=0.12.0,<0.16.0
|
| 9 |
requests
|
| 10 |
-
|
| 11 |
gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
torch --index-url https://download.pytorch.org/whl/cpu
|
| 8 |
huggingface_hub>=0.12.0,<0.16.0
|
| 9 |
requests
|
|
|
|
| 10 |
gradio
|
| 11 |
+
flask
|
| 12 |
+
pydantic
|
| 13 |
+
datasets
|
| 14 |
+
openenv-core>=0.3.0
|
| 15 |
+
pandas
|
| 16 |
+
openai
|
| 17 |
+
websockets>=15.0.1
|
| 18 |
+
httpx>=0.28.1
|
server/app.py
CHANGED
|
@@ -1,21 +1,115 @@
|
|
| 1 |
import os
|
| 2 |
import sys
|
|
|
|
| 3 |
|
| 4 |
# Allow importing from root directory
|
| 5 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 6 |
|
| 7 |
-
from
|
| 8 |
-
from
|
|
|
|
| 9 |
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
def main():
|
| 14 |
import uvicorn
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
port = int(os.environ.get("PORT", "7860"))
|
| 16 |
-
# Pre-train models before starting
|
| 17 |
-
from app import ensure_models
|
| 18 |
-
ensure_models()
|
| 19 |
uvicorn.run(app, host="0.0.0.0", port=port, log_level="info")
|
| 20 |
|
| 21 |
if __name__ == "__main__":
|
|
|
|
| 1 |
import os
|
| 2 |
import sys
|
| 3 |
+
from typing import Optional
|
| 4 |
|
| 5 |
# Allow importing from root directory
|
| 6 |
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
| 7 |
|
| 8 |
+
from fastapi import FastAPI, HTTPException
|
| 9 |
+
from pydantic import BaseModel
|
| 10 |
+
from server.document_classification_environment import DocumentClassificationEnvironment
|
| 11 |
|
| 12 |
+
app = FastAPI(title="OpenEnv Document Classification API")
|
| 13 |
+
|
| 14 |
+
# Store the active environment instance
|
| 15 |
+
# In a real OpenEnv session, this might be handled per-session,
|
| 16 |
+
# but for a simple hackathon grader, a singleton is often used.
|
| 17 |
+
env_container = {"instance": None}
|
| 18 |
+
|
| 19 |
+
def get_env(difficulty: str = "easy", seed: int = 42):
|
| 20 |
+
if env_container["instance"] is None:
|
| 21 |
+
env_container["instance"] = DocumentClassificationEnvironment(difficulty=difficulty, seed=seed)
|
| 22 |
+
return env_container["instance"]
|
| 23 |
+
|
| 24 |
+
class ResetRequest(BaseModel):
|
| 25 |
+
difficulty: str = "easy"
|
| 26 |
+
seed: Optional[int] = 42
|
| 27 |
+
|
| 28 |
+
class StepRequest(BaseModel):
|
| 29 |
+
action: int
|
| 30 |
+
|
| 31 |
+
@app.get("/")
|
| 32 |
+
async def root():
|
| 33 |
+
return {"status": "online", "environment": "DocumentClassification-v1"}
|
| 34 |
+
|
| 35 |
+
@app.post("/reset")
|
| 36 |
+
async def reset(req: Optional[ResetRequest] = None):
|
| 37 |
+
try:
|
| 38 |
+
difficulty = req.difficulty if req else "easy"
|
| 39 |
+
seed = req.seed if req else 42
|
| 40 |
+
|
| 41 |
+
# Fresh instance for every reset
|
| 42 |
+
env_container["instance"] = DocumentClassificationEnvironment(difficulty=difficulty, seed=seed)
|
| 43 |
+
env = env_container["instance"]
|
| 44 |
+
|
| 45 |
+
# reset() returns DocObservation which has all fields
|
| 46 |
+
obs = env.reset(seed=seed)
|
| 47 |
+
|
| 48 |
+
# Return exactly what the user/grader expects: {"observation": {...}}
|
| 49 |
+
# We exclude reward/done/metadata from the inner observation if they are handled at top level
|
| 50 |
+
obs_dict = obs.model_dump()
|
| 51 |
+
|
| 52 |
+
return {
|
| 53 |
+
"observation": obs_dict,
|
| 54 |
+
"reward": 0.0,
|
| 55 |
+
"done": False,
|
| 56 |
+
"metadata": {}
|
| 57 |
+
}
|
| 58 |
+
except Exception as e:
|
| 59 |
+
import traceback
|
| 60 |
+
print(f"[ERROR] Reset failed: {e}")
|
| 61 |
+
traceback.print_exc()
|
| 62 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 63 |
+
|
| 64 |
+
@app.post("/step")
|
| 65 |
+
async def step(req: StepRequest):
|
| 66 |
+
try:
|
| 67 |
+
env = env_container.get("instance")
|
| 68 |
+
if env is None:
|
| 69 |
+
# Fallback for graders that don't call /reset first or expect a default
|
| 70 |
+
env = get_env()
|
| 71 |
+
|
| 72 |
+
from models import DocAction
|
| 73 |
+
action = DocAction(action_id=req.action)
|
| 74 |
+
obs = env.step(action)
|
| 75 |
+
|
| 76 |
+
obs_dict = obs.model_dump()
|
| 77 |
+
reward = obs_dict.pop("reward", 0.0)
|
| 78 |
+
done = obs_dict.pop("done", False)
|
| 79 |
+
metadata = obs_dict.pop("metadata", {})
|
| 80 |
+
|
| 81 |
+
return {
|
| 82 |
+
"observation": obs_dict,
|
| 83 |
+
"reward": reward,
|
| 84 |
+
"done": done,
|
| 85 |
+
"metadata": metadata
|
| 86 |
+
}
|
| 87 |
+
except Exception as e:
|
| 88 |
+
import traceback
|
| 89 |
+
print(f"[ERROR] Step failed: {e}")
|
| 90 |
+
traceback.print_exc()
|
| 91 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 92 |
+
|
| 93 |
+
@app.get("/state")
|
| 94 |
+
async def state():
|
| 95 |
+
try:
|
| 96 |
+
env = env_container.get("instance")
|
| 97 |
+
if env is None:
|
| 98 |
+
env = get_env()
|
| 99 |
+
return env.state
|
| 100 |
+
except Exception as e:
|
| 101 |
+
raise HTTPException(status_code=500, detail=str(e))
|
| 102 |
|
| 103 |
def main():
|
| 104 |
import uvicorn
|
| 105 |
+
# Pre-train models if needed
|
| 106 |
+
try:
|
| 107 |
+
from startup import ensure_models
|
| 108 |
+
ensure_models()
|
| 109 |
+
except ImportError:
|
| 110 |
+
pass
|
| 111 |
+
|
| 112 |
port = int(os.environ.get("PORT", "7860"))
|
|
|
|
|
|
|
|
|
|
| 113 |
uvicorn.run(app, host="0.0.0.0", port=port, log_level="info")
|
| 114 |
|
| 115 |
if __name__ == "__main__":
|