Spaces:
Sleeping

File size: 3,501 Bytes
1be0df2
 
 
 
 
 
2b2d7e5
1be0df2
 
 
 
 
 
 
 
 
2b2d7e5
 
 
 
 
1be0df2
 
 
 
 
 
 
2b2d7e5
 
1be0df2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e3700b9
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from __future__ import annotations

import json
from typing import Any, Dict

from fastapi import FastAPI, WebSocket, WebSocketDisconnect
from fastapi.responses import JSONResponse, RedirectResponse

from server.environment import DataCleaningEnvironment

app = FastAPI(
    title="Data Cleaning Environment",
    description="OpenEnv-compliant environment for training AI agents on real-world data cleaning tasks.",
    version="1.0.0",
)

@app.get("/")
def read_root():
    """Redirect the main page to the API documentation."""
    return RedirectResponse(url="/docs")

# One environment instance per WebSocket session
# ---------------------------------------------------------------------------

@app.get("/health")
async def health():
    return {"status": "ok"}

# ... (keep the rest of your file exactly the same below this)


@app.get("/info")
async def info():
    return {
        "name": "data-cleaning-env",
        "tasks": ["easy", "medium", "hard"],
        "actions": [
            "fill_nulls", "drop_duplicates", "fix_type",
            "normalise_column", "drop_column", "fix_date_format",
            "clip_outliers", "rename_column", "submit",
        ],
        "spec_version": "1.0",
    }


@app.post("/reset")
async def reset(body: Dict[str, Any] = {}):
    env = DataCleaningEnvironment()
    task_id = body.get("task_id", "easy")
    obs = env.reset(task_id=task_id)
    # Store env in app state keyed by a simple counter (demo — use session mgmt for prod)
    app.state.env = env
    return JSONResponse(content={"observation": obs, "state": env.state})


@app.post("/step")
async def step(action: Dict[str, Any]):
    env = getattr(app.state, "env", None)
    if env is None:
        return JSONResponse(status_code=400, content={"error": "Call /reset first"})
    result = env.step(action)
    return JSONResponse(content={"observation": result, "state": env.state})


@app.get("/state")
async def state():
    env = getattr(app.state, "env", None)
    if env is None:
        return JSONResponse(status_code=400, content={"error": "Call /reset first"})
    return JSONResponse(content=env.state)


# WebSocket endpoint — primary interface (low latency, persistent session)
# ---------------------------------------------------------------------------

@app.websocket("/ws")
async def websocket_endpoint(websocket: WebSocket):
    await websocket.accept()
    env = DataCleaningEnvironment()

    try:
        while True:
            raw = await websocket.receive_text()
            msg = json.loads(raw)
            method  = msg.get("method")
            payload = msg.get("payload", {})

            if method == "reset":
                task_id = payload.get("task_id", "easy")
                obs = env.reset(task_id=task_id)
                await websocket.send_text(json.dumps({"observation": obs, "state": env.state}))

            elif method == "step":
                result = env.step(payload)
                await websocket.send_text(json.dumps({"observation": result, "state": env.state}))

            elif method == "state":
                await websocket.send_text(json.dumps({"state": env.state}))

            else:
                await websocket.send_text(json.dumps({"error": f"Unknown method '{method}'"}))

    except WebSocketDisconnect:
        pass
def main():
    import uvicorn
    # The judge uses this to start your app
    uvicorn.run("server.app:app", host="0.0.0.0", port=8000, reload=False)

if __name__ == "__main__":
    main()