Spaces:
Sleeping
Sleeping
Delete server.py with huggingface_hub
Browse files
server.py
DELETED
|
@@ -1,150 +0,0 @@
|
|
| 1 |
-
import json
|
| 2 |
-
import traceback
|
| 3 |
-
from typing import Optional
|
| 4 |
-
|
| 5 |
-
from fastapi import FastAPI, HTTPException, WebSocket, WebSocketDisconnect
|
| 6 |
-
from pydantic import BaseModel
|
| 7 |
-
|
| 8 |
-
from env.environment import DataValidationEnvironment
|
| 9 |
-
from env.models import DataCleanAction
|
| 10 |
-
from env.tasks import get_task_names
|
| 11 |
-
|
| 12 |
-
app = FastAPI(
|
| 13 |
-
title="Data Validation Pipeline - OpenEnv Environment",
|
| 14 |
-
version="1.0.0",
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
env = DataValidationEnvironment()
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
class ResetRequest(BaseModel):
|
| 21 |
-
task_name: Optional[str] = None
|
| 22 |
-
seed: int = 42
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
class StepRequest(BaseModel):
|
| 26 |
-
action_type: str
|
| 27 |
-
target_field: str = ""
|
| 28 |
-
target_row: int = 0
|
| 29 |
-
new_value: str = ""
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
@app.get("/")
|
| 33 |
-
async def root():
|
| 34 |
-
return {
|
| 35 |
-
"name": "Data Validation Pipeline",
|
| 36 |
-
"description": "An RL environment for training agents to clean and validate structured data",
|
| 37 |
-
"version": "1.0.0",
|
| 38 |
-
"endpoints": {
|
| 39 |
-
"health": "/health",
|
| 40 |
-
"reset": "POST /reset",
|
| 41 |
-
"step": "POST /step",
|
| 42 |
-
"state": "GET /state",
|
| 43 |
-
"tasks": "GET /tasks",
|
| 44 |
-
},
|
| 45 |
-
"tasks": get_task_names(),
|
| 46 |
-
"status": "running",
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
@app.get("/health")
|
| 51 |
-
async def health():
|
| 52 |
-
return {"status": "healthy", "service": "data-validation-env"}
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
@app.post("/reset")
|
| 56 |
-
async def reset(request: ResetRequest = None):
|
| 57 |
-
if request is None:
|
| 58 |
-
request = ResetRequest()
|
| 59 |
-
try:
|
| 60 |
-
obs = env.reset(task_name=request.task_name, seed=request.seed)
|
| 61 |
-
return obs.model_dump()
|
| 62 |
-
except Exception as e:
|
| 63 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
@app.post("/step")
|
| 67 |
-
async def step(request: StepRequest):
|
| 68 |
-
try:
|
| 69 |
-
action = DataCleanAction(
|
| 70 |
-
action_type=request.action_type,
|
| 71 |
-
target_field=request.target_field,
|
| 72 |
-
target_row=request.target_row,
|
| 73 |
-
new_value=request.new_value,
|
| 74 |
-
)
|
| 75 |
-
obs = env.step(action)
|
| 76 |
-
return obs.model_dump()
|
| 77 |
-
except Exception as e:
|
| 78 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
@app.get("/state")
|
| 82 |
-
async def state():
|
| 83 |
-
try:
|
| 84 |
-
s = env.state()
|
| 85 |
-
return s.model_dump()
|
| 86 |
-
except Exception as e:
|
| 87 |
-
raise HTTPException(status_code=400, detail=str(e))
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
@app.get("/tasks")
|
| 91 |
-
async def tasks():
|
| 92 |
-
return {"tasks": get_task_names()}
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
@app.websocket("/ws")
|
| 96 |
-
async def websocket_endpoint(websocket: WebSocket):
|
| 97 |
-
await websocket.accept()
|
| 98 |
-
ws_env = DataValidationEnvironment()
|
| 99 |
-
|
| 100 |
-
try:
|
| 101 |
-
while True:
|
| 102 |
-
data = await websocket.receive_text()
|
| 103 |
-
msg = json.loads(data)
|
| 104 |
-
|
| 105 |
-
method = msg.get("method", "")
|
| 106 |
-
params = msg.get("params", {})
|
| 107 |
-
|
| 108 |
-
try:
|
| 109 |
-
if method == "reset":
|
| 110 |
-
obs = ws_env.reset(
|
| 111 |
-
task_name=params.get("task_name"),
|
| 112 |
-
seed=params.get("seed", 42)
|
| 113 |
-
)
|
| 114 |
-
response = {
|
| 115 |
-
"type": "reset",
|
| 116 |
-
"observation": obs.model_dump(),
|
| 117 |
-
"reward": 0.0,
|
| 118 |
-
"done": False,
|
| 119 |
-
}
|
| 120 |
-
elif method == "step":
|
| 121 |
-
action = DataCleanAction(**params)
|
| 122 |
-
obs = ws_env.step(action)
|
| 123 |
-
response = {
|
| 124 |
-
"type": "step",
|
| 125 |
-
"observation": obs.model_dump(),
|
| 126 |
-
"reward": obs.reward,
|
| 127 |
-
"done": obs.done,
|
| 128 |
-
}
|
| 129 |
-
elif method == "state":
|
| 130 |
-
s = ws_env.state()
|
| 131 |
-
response = {
|
| 132 |
-
"type": "state",
|
| 133 |
-
"state": s.model_dump(),
|
| 134 |
-
}
|
| 135 |
-
else:
|
| 136 |
-
response = {"error": f"Unknown method: {method}"}
|
| 137 |
-
|
| 138 |
-
await websocket.send_text(json.dumps(response))
|
| 139 |
-
except Exception as e:
|
| 140 |
-
await websocket.send_text(json.dumps({
|
| 141 |
-
"error": str(e),
|
| 142 |
-
"traceback": traceback.format_exc()
|
| 143 |
-
}))
|
| 144 |
-
except WebSocketDisconnect:
|
| 145 |
-
pass
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
if __name__ == "__main__":
|
| 149 |
-
import uvicorn
|
| 150 |
-
uvicorn.run(app, host="0.0.0.0", port=8000)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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