Spaces:
Sleeping
Sleeping
File size: 9,334 Bytes
d2d30e9 40fcf49 d2d30e9 40fcf49 fa53e30 40fcf49 d2d30e9 d4930ce d2d30e9 40fcf49 d2d30e9 40fcf49 d2d30e9 40fcf49 d2d30e9 40fcf49 d2d30e9 40fcf49 d2d30e9 40fcf49 d2d30e9 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 40fcf49 5ededc8 d2d30e9 69e5273 d2d30e9 fa53e30 40fcf49 fa53e30 40fcf49 fa53e30 d2d30e9 fa53e30 40fcf49 fa53e30 d2d30e9 fa53e30 d2d30e9 fa53e30 d2d30e9 d4930ce 40fcf49 d4930ce 40fcf49 d4930ce 40fcf49 | 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 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | """
FastAPI application exposing the OpenEnv-compatible HTTP API.
Endpoints:
GET /health Health check
GET /metadata Environment info
GET /schema Action / observation / state schemas
POST /reset Start new episode
POST /step Execute cleaning action (with 30s timeout)
GET /state Episode metadata
POST /state Episode metadata (backward compat)
GET /profile Rich data quality profile of current DataFrame
GET /report Full episode cleaning summary (health certificate)
GET /export Download current cleaned DataFrame as CSV
"""
import asyncio
import os
from typing import Any, Dict, Optional
from fastapi import Body, FastAPI, HTTPException
from fastapi.responses import PlainTextResponse, HTMLResponse
from pydantic import BaseModel
import uvicorn
from models import DataCleaningAction, DataCleaningObservation, DataCleaningState, EpisodeReport
from server.environment import DataCleaningEnvironment
app = FastAPI(
title="Data Cleaning OpenEnv",
description=(
"A real-world data cleaning environment for AI agent training and evaluation. "
"An agent interacts with dirty pandas DataFrames through a standard reset/step/state API, "
"learning to fix missing values, duplicates, format inconsistencies, outliers, and dtype errors. "
"Grounded in CleanAgent (2024), AutoDCWorkflow (EMNLP 2025), and Meta-scale data quality principles."
),
version="0.2.0",
)
# Single shared environment instance
env = DataCleaningEnvironment()
STEP_TIMEOUT_SECONDS = 30
class ResetRequest(BaseModel):
task_id: Optional[int] = None
class StepResponse(BaseModel):
observation: DataCleaningObservation
reward: float
done: bool
info: dict = {}
# ------------------------------------------------------------------
# Core OpenEnv routes
# ------------------------------------------------------------------
@app.get("/", response_class=HTMLResponse, include_in_schema=False)
def ui():
"""DataMedic — live agent monitoring dashboard."""
ui_path = os.path.join(os.path.dirname(__file__), "ui.html")
with open(ui_path, "r") as f:
return HTMLResponse(content=f.read())
@app.get("/health")
def health():
return {"status": "healthy"}
@app.get("/metadata")
def metadata():
return {
"name": "data-cleaning-env",
"description": (
"A real-world data cleaning RL environment. The agent diagnoses dirty datasets, "
"plans a treatment, executes cleaning operations step-by-step, and produces a "
"health certificate — grounded in AutoDCWorkflow, CleanAgent, and HoloClean research."
),
"version": "0.2.0",
"tags": ["openenv", "data-cleaning", "rl", "real-world", "agentic"],
"tasks": [
{"id": "task1", "name": "Fill Missing Values", "difficulty": "easy"},
{"id": "task2", "name": "Fix Formats and Remove Duplicates", "difficulty": "medium"},
{"id": "task3", "name": "Full Cleaning Pipeline", "difficulty": "hard"},
{"id": "task4", "name": "Multi-Source Schema Alignment + Merge", "difficulty": "expert"},
],
"observation_extras": ["dq_metrics", "tried_operations", "plan"],
"papers": [
"Bendinelli et al. 2025 — LLM Agents for Cleaning Tabular ML Datasets (arXiv:2503.06664)",
"CleanAgent — Qi & Wang 2024 (arXiv:2403.08291)",
"AutoDCWorkflow — EMNLP 2025 Findings",
"HoloClean — Rekatsinas et al. 2017",
],
}
@app.get("/schema")
def schema():
return {
"action": {
"type": "object",
"properties": {
"operation": {
"type": "string",
"enum": [
"fill_missing", "drop_duplicates", "fix_format",
"replace_value", "drop_outliers", "fix_dtype",
"align_schema", "merge_sources",
],
},
"column": {"type": "string", "nullable": True},
"params": {"type": "object", "nullable": True},
},
"required": ["operation"],
},
"observation": {
"type": "object",
"properties": {
"done": {"type": "boolean"},
"reward": {"type": "number"},
"data_preview": {"type": "string"},
"data_shape": {"type": "array", "items": {"type": "integer"}},
"missing_counts": {"type": "object"},
"duplicate_count": {"type": "integer"},
"dtype_issues": {"type": "object"},
"task_description": {"type": "string"},
"message": {"type": "string"},
"step_count": {"type": "integer"},
"current_score": {"type": "number"},
"dq_metrics": {"type": "object", "description": "Completeness/uniqueness/validity %"},
"tried_operations": {"type": "array", "description": "Operations already applied"},
"plan": {"type": "array", "description": "Agent recommended next actions"},
},
},
"state": {
"type": "object",
"properties": {
"episode_id": {"type": "string"},
"task_id": {"type": "integer"},
"step_count": {"type": "integer"},
"max_steps": {"type": "integer"},
"total_errors": {"type": "integer"},
"errors_remaining": {"type": "integer"},
},
},
}
@app.post("/reset", response_model=StepResponse)
def reset(req: ResetRequest = ResetRequest()):
try:
obs = env.reset(task_id=req.task_id)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
return StepResponse(observation=obs, reward=obs.reward, done=False)
@app.post("/step", response_model=StepResponse)
async def step(body: Dict[str, Any] = Body(...)):
"""
Accept both openenv-core wrapped format:
{"action": {"operation": "...", ...}, "timeout_s": 15}
and direct format:
{"operation": "...", "column": "...", "params": {...}}
Times out after 30 seconds to prevent hanging during evaluation.
"""
action_data = body.get("action", body)
try:
action = DataCleaningAction(**action_data)
loop = asyncio.get_event_loop()
obs = await asyncio.wait_for(
loop.run_in_executor(None, env.step, action),
timeout=STEP_TIMEOUT_SECONDS,
)
except asyncio.TimeoutError:
raise HTTPException(status_code=504, detail=f"Step timed out after {STEP_TIMEOUT_SECONDS}s")
except (TypeError, KeyError, Exception) as e:
raise HTTPException(status_code=400, detail=str(e))
return StepResponse(observation=obs, reward=obs.reward, done=obs.done)
@app.get("/state", response_model=DataCleaningState)
def state_get():
return env.state()
@app.post("/state", response_model=DataCleaningState)
def state_post():
return env.state()
# ------------------------------------------------------------------
# Phase 2: Intelligence endpoints
# ------------------------------------------------------------------
@app.get("/profile")
def profile():
"""
Rich data quality profile of the current DataFrame.
Returns per-column statistics (null %, unique %, min/max/mean for numerics,
top values for categoricals) plus dataset-level DQ metrics:
completeness %, uniqueness %, validity %.
Inspired by standard Data Quality dimensions (ISO 8000) and
Meta's data schematization approach.
"""
try:
return env.get_profile()
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/report", response_model=EpisodeReport)
def report():
"""
Full episode cleaning summary — the 'health certificate'.
Returns: initial vs final score, score improvement, step efficiency,
ordered list of operations applied, issues fixed by category,
and final DQ metrics. Call after the episode completes for best results.
"""
try:
return env.get_report()
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
@app.get("/export")
def export():
"""
Download the current (cleaned) DataFrame as a CSV file.
Returns the live state of the DataFrame — call after the agent
finishes cleaning to get the cleaned output.
"""
try:
csv_data = env.get_export()
return PlainTextResponse(
content=csv_data,
media_type="text/csv",
headers={"Content-Disposition": "attachment; filename=cleaned_data.csv"},
)
except Exception as e:
raise HTTPException(status_code=400, detail=str(e))
# ------------------------------------------------------------------
# Entry point
# ------------------------------------------------------------------
def main():
uvicorn.run("server.app:app", host="0.0.0.0", port=8000, workers=1)
if __name__ == "__main__":
main() |