Spaces:
Sleeping
Sleeping
Upload folder using huggingface_hub
Browse files- README.md +80 -339
- __init__.py +2 -2
- client.py +5 -83
- openenv.yaml +1 -1
- pyproject.toml +18 -70
- server/app.py +11 -11
README.md
CHANGED
|
@@ -1,55 +1,48 @@
|
|
| 1 |
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
pinned: true
|
| 9 |
tags:
|
| 10 |
- openenv
|
| 11 |
- reinforcement-learning
|
| 12 |
-
-
|
| 13 |
-
-
|
| 14 |
-
- question-answering
|
| 15 |
-
- fact-checking
|
| 16 |
- llm-training
|
| 17 |
-
- llm-evaluation
|
| 18 |
- benchmark
|
| 19 |
- ai-safety
|
|
|
|
|
|
|
| 20 |
base_path: /web
|
| 21 |
---
|
| 22 |
|
| 23 |
-
#
|
| 24 |
|
| 25 |
-
> **
|
| 26 |
|
| 27 |
-
**Server Version:**
|
| 28 |
|
| 29 |
[](https://github.com/meta-pytorch/OpenEnv)
|
| 30 |
[](#-quick-start)
|
| 31 |
[](LICENSE)
|
| 32 |
-
[.json()
|
| 67 |
-
print(obs["
|
| 68 |
|
| 69 |
-
# 2.
|
| 70 |
result = requests.post(f"{BASE}/step", json={
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
|
|
|
| 74 |
"session_id": obs.get("session_id"),
|
| 75 |
}).json()
|
| 76 |
-
print(f"Reward: {result['reward']},
|
| 77 |
|
| 78 |
# 3. Score the episode
|
| 79 |
grade = requests.post(f"{BASE}/grader", json={
|
| 80 |
-
"task_id": "
|
| 81 |
"step_rewards": [result['reward']],
|
| 82 |
-
"step_infos": [
|
| 83 |
}).json()
|
| 84 |
print(f"Episode score: {grade['score']}")
|
| 85 |
```
|
| 86 |
|
| 87 |
-
### Run Baseline
|
| 88 |
-
|
| 89 |
-
```bash
|
| 90 |
-
# Heuristic baseline (no API key needed)
|
| 91 |
-
python inference.py --heuristic --env-url http://localhost:7860
|
| 92 |
-
|
| 93 |
-
# With an LLM (Groq, Ollama, OpenAI-compatible)
|
| 94 |
-
export API_BASE_URL=https://api.groq.com/openai/v1
|
| 95 |
-
export MODEL_NAME=llama-3.3-70b-versatile
|
| 96 |
-
export HF_TOKEN=your_key_here
|
| 97 |
-
python inference.py --env-url http://localhost:7860 --episodes 3 --steps 5
|
| 98 |
-
```
|
| 99 |
-
|
| 100 |
### Validate OpenEnv Compliance
|
| 101 |
|
| 102 |
```bash
|
| 103 |
# Local structure check
|
| 104 |
openenv validate
|
| 105 |
|
| 106 |
-
# Runtime check against live server
|
| 107 |
openenv validate --url http://localhost:7860 --verbose
|
| 108 |
```
|
| 109 |
|
|
@@ -111,32 +92,29 @@ openenv validate --url http://localhost:7860 --verbose
|
|
| 111 |
|
| 112 |
## 🎯 Tasks
|
| 113 |
|
| 114 |
-
3
|
| 115 |
|
| 116 |
-
| # | task_id | Difficulty |
|
| 117 |
-
|---|---------|-----------|-------------
|
| 118 |
-
| 1 | `
|
| 119 |
-
| 2 | `
|
| 120 |
-
| 3 | `
|
| 121 |
|
| 122 |
---
|
| 123 |
|
| 124 |
-
## 🎮
|
| 125 |
|
| 126 |
-
The agent receives a **
|
| 127 |
|
| 128 |
### Action Space
|
| 129 |
|
| 130 |
-
Every `POST /step` call accepts this JSON body (only `answer` is required):
|
| 131 |
-
|
| 132 |
```json
|
| 133 |
{
|
| 134 |
-
"
|
| 135 |
-
"
|
| 136 |
-
"
|
| 137 |
-
"reasoning": "string
|
| 138 |
-
"
|
| 139 |
-
"session_id": "string — from /reset response, for session isolation"
|
| 140 |
}
|
| 141 |
```
|
| 142 |
|
|
@@ -144,136 +122,56 @@ Every `POST /step` call accepts this JSON body (only `answer` is required):
|
|
| 144 |
|
| 145 |
```json
|
| 146 |
{
|
| 147 |
-
"
|
| 148 |
-
"
|
| 149 |
-
"reward":
|
| 150 |
-
"feedback":
|
| 151 |
-
"
|
| 152 |
-
"
|
| 153 |
-
"
|
| 154 |
-
"
|
| 155 |
-
"done": false,
|
| 156 |
-
"session_id": "ses_a1b2c3d4"
|
| 157 |
}
|
| 158 |
```
|
| 159 |
|
| 160 |
-
### Episode Flow
|
| 161 |
-
|
| 162 |
-
```
|
| 163 |
-
POST /reset → Sample question + context from dataset (curriculum-aware)
|
| 164 |
-
Return observation with session_id
|
| 165 |
-
|
| 166 |
-
POST /step → Grade answer across 9 components
|
| 167 |
-
Detect hallucination type and severity
|
| 168 |
-
Compute reward with ROUGE + BERTScore + AlignScore
|
| 169 |
-
Adapt difficulty based on performance
|
| 170 |
-
Return observation with reward + feedback
|
| 171 |
-
|
| 172 |
-
POST /grader → Aggregate per-step rewards into 0.0–1.0 task score
|
| 173 |
-
```
|
| 174 |
-
|
| 175 |
---
|
| 176 |
|
| 177 |
-
## 📊 Reward System (
|
| 178 |
|
| 179 |
| Component | Weight | Description |
|
| 180 |
|-----------|--------|-------------|
|
| 181 |
-
|
|
| 182 |
-
|
|
| 183 |
-
|
|
| 184 |
-
|
|
| 185 |
-
|
|
| 186 |
-
| Hallucination penalty | 0.10 | Penalises detected hallucinations by type and severity |
|
| 187 |
-
| ROUGE (1/2/L) | 0.02 | Surface-form overlap with reference answer |
|
| 188 |
-
| BERTScore | 0.02 | Token-level semantic similarity (roberta-base) |
|
| 189 |
-
| AlignScore | 0.01 | Faithfulness to context (RoBERTa, ACL 2023; optional — falls back to 0.5) |
|
| 190 |
-
|
| 191 |
-
Difficulty multiplier: `beginner × 0.9`, `intermediate × 1.0`, `advanced × 1.1`, `expert × 1.2`
|
| 192 |
-
|
| 193 |
-
**Key behavior:**
|
| 194 |
-
- Wrong answers capped at ~0.4 reward regardless of grounding
|
| 195 |
-
- Grounding contribution reduced for incorrect answers
|
| 196 |
-
- Consistency bonus for maintaining performance above 0.7
|
| 197 |
|
| 198 |
---
|
| 199 |
|
| 200 |
-
##
|
| 201 |
-
|
| 202 |
-
### 8 Types Classified
|
| 203 |
-
|
| 204 |
-
| Type | What It Catches |
|
| 205 |
-
|---|---|
|
| 206 |
-
| `FABRICATED_FACT` | Information stated that is not in the source |
|
| 207 |
-
| `FALSE_CITATION` | `source_quote` that does not exist in the document |
|
| 208 |
-
| `OVERCONFIDENT_WRONG` | High confidence on an incorrect answer |
|
| 209 |
-
| `CONTEXT_DRIFT` | Answer gradually drifts away from source |
|
| 210 |
-
| `NUMERICAL_FABRICATION` | Made-up statistics or numbers |
|
| 211 |
-
| `ENTITY_CONFUSION` | Wrong names, organisations, or places |
|
| 212 |
-
| `TEMPORAL_ERROR` | Incorrect dates or timelines |
|
| 213 |
-
| `RELATIONSHIP_ERROR` | Incorrect relationships between entities |
|
| 214 |
-
|
| 215 |
-
### "I Don't Know" Refusal Handling
|
| 216 |
-
|
| 217 |
-
The grader detects when a model appropriately refuses to answer unanswerable questions:
|
| 218 |
-
|
| 219 |
-
| Scenario | Reward | Behavior |
|
| 220 |
-
|----------|--------|----------|
|
| 221 |
-
| Proper refusal on unanswerable | 0.65–0.80 | Rewarded for honesty |
|
| 222 |
-
| Refusal with low confidence | 0.50 | Partial credit |
|
| 223 |
-
| Underconfident refusal (answer exists) | 0.30 | Penalized for not trying |
|
| 224 |
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
| NONE | 0.0 | Fully grounded answer |
|
| 232 |
-
| MINOR | 0.1–0.3 | Slight deviation from source |
|
| 233 |
-
| MODERATE | 0.3–0.5 | Noticeable unsupported claims |
|
| 234 |
-
| SEVERE | 0.5–0.7 | Significantly fabricated content |
|
| 235 |
-
| CRITICAL | 0.7+ | Answer largely invented |
|
| 236 |
|
| 237 |
---
|
| 238 |
|
| 239 |
-
##
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
|
| 244 |
-
|
|
| 245 |
-
|
|
| 246 |
-
|
|
| 247 |
-
|
|
| 248 |
-
|
|
| 249 |
-
|
|
| 250 |
-
|
|
| 251 |
-
|
|
| 252 |
-
|
|
| 253 |
-
| NQ Open | 50,000 | Natural questions |
|
| 254 |
-
| AQUA-RAT | 97,467 | Math word problems |
|
| 255 |
-
| XSum | 49,994 | Extreme summarisation |
|
| 256 |
-
| CNN/DailyMail | 50,000 | News summarisation |
|
| 257 |
-
| HellaSwag | 39,905 | Commonsense completion |
|
| 258 |
-
| AdversarialQA | 30,000 | Adversarial reading comprehension |
|
| 259 |
-
| WinoGrande | 40,398 | Commonsense inference |
|
| 260 |
-
| CommonsenseQA | 9,741 | Commonsense reasoning |
|
| 261 |
-
| BoolQ | 9,427 | Boolean yes/no QA |
|
| 262 |
-
| CoQA | 7,199 | Conversational QA |
|
| 263 |
-
| MedQA | 10,000 | Medical licensing exam |
|
| 264 |
-
| MedMCQA | 20,000 | Medical entrance exam |
|
| 265 |
-
| SciTail | 23,596 | Science entailment |
|
| 266 |
-
| HaluEval | 10,000 | Hallucination evaluation |
|
| 267 |
-
| TruthfulQA | 817 | Factuality benchmark |
|
| 268 |
-
| SciQ | 11,679 | Science QA |
|
| 269 |
-
| Arc | 2,590 | Science exam |
|
| 270 |
-
| OpenBookQA | 4,957 | Common knowledge |
|
| 271 |
-
| AG News | 50,000 | News classification |
|
| 272 |
-
| Climate-FEVER | 881 | Climate fact verification |
|
| 273 |
-
| MS MARCO | 30,568 | Web search QA |
|
| 274 |
-
| + 10 more | ... | Medical, math, dialogue, summarisation |
|
| 275 |
-
|
| 276 |
-
Datasets load from `SamSankar/hallucination-guard-cache` on HF Hub. Core 5 datasets load synchronously at startup (~86K examples); remaining 33 load in a background thread.
|
| 277 |
|
| 278 |
---
|
| 279 |
|
|
@@ -289,145 +187,15 @@ Datasets load from `SamSankar/hallucination-guard-cache` on HF Hub. Core 5 datas
|
|
| 289 |
| `GET` | `/metadata` | Environment name, version, description |
|
| 290 |
| `GET` | `/schema` | Action, observation, and state JSON schemas |
|
| 291 |
| `GET` | `/health` | Health check |
|
| 292 |
-
| `POST` | `/mcp` | MCP JSON-RPC endpoint |
|
| 293 |
|
| 294 |
### Environment
|
| 295 |
|
| 296 |
| Method | Endpoint | Description |
|
| 297 |
|--------|----------|-------------|
|
| 298 |
-
| `POST` | `/reset` | Start new episode
|
| 299 |
-
| `POST` | `/step` | Submit
|
| 300 |
| `GET` | `/state` | Get current episode state |
|
| 301 |
|
| 302 |
-
### Evaluation & Leaderboard
|
| 303 |
-
|
| 304 |
-
| Method | Endpoint | Description |
|
| 305 |
-
|--------|----------|-------------|
|
| 306 |
-
| `POST` | `/batch/evaluate` | Evaluate multiple Q&A pairs |
|
| 307 |
-
| `GET` | `/leaderboard` | View ranked model performance |
|
| 308 |
-
| `POST` | `/leaderboard/submit` | Submit evaluation results |
|
| 309 |
-
| `GET` | `/datasets` | Dataset statistics |
|
| 310 |
-
|
| 311 |
-
---
|
| 312 |
-
|
| 313 |
-
## 📋 Baseline Scores
|
| 314 |
-
|
| 315 |
-
All benchmarks: **3 episodes × 5 steps, seed=42**, against deployed HF Space.
|
| 316 |
-
|
| 317 |
-
### Full Benchmark Results
|
| 318 |
-
|
| 319 |
-
| # | Model | Provider | Overall | Task 1 | Task 2 | Task 3 | Time |
|
| 320 |
-
|---|-------|----------|---------|--------|--------|--------|------|
|
| 321 |
-
| 1 | Nemotron-3-Super 120B | OpenRouter | **0.553** | 0.599 | 0.535 | 0.524 | 10m 57s |
|
| 322 |
-
| 2 | Llama 3.3 70B | Groq | **0.514** | 0.542 | 0.449 | 0.552 | 1m 12s |
|
| 323 |
-
| 3 | Qwen3 32B | Groq | **0.513** | 0.564 | 0.453 | 0.522 | 4m 41s |
|
| 324 |
-
| 4 | GPT-OSS 20B | Groq | **0.498** | 0.552 | 0.406 | 0.537 | 3m 53s |
|
| 325 |
-
| 5 | Qwen2.5 72B Instruct | HF Router | **0.480** | 0.594 | 0.431 | 0.417 | 3m 05s |
|
| 326 |
-
| 6 | GLM-4.5 Air | OpenRouter | **0.350** | 0.436 | 0.311 | 0.303 | 14m 01s |
|
| 327 |
-
| 7 | Heuristic (no LLM) | — | **0.131** | 0.162 | 0.144 | 0.087 | 30s |
|
| 328 |
-
|
| 329 |
-
### Heuristic Baseline (no LLM required)
|
| 330 |
-
|
| 331 |
-
The heuristic baseline is a deterministic agent that extracts the first sentence of the context as the answer. It establishes a performance floor — any real LLM should beat this.
|
| 332 |
-
|
| 333 |
-
```bash
|
| 334 |
-
python inference.py --heuristic --env-url http://localhost:7860 --episodes 3 --steps 5 --seed 42
|
| 335 |
-
```
|
| 336 |
-
|
| 337 |
-
### Run LLM Baselines
|
| 338 |
-
|
| 339 |
-
```bash
|
| 340 |
-
# Groq (fast inference)
|
| 341 |
-
export API_BASE_URL=https://api.groq.com/openai/v1
|
| 342 |
-
export MODEL_NAME=llama-3.3-70b-versatile
|
| 343 |
-
export HF_TOKEN=gsk_your_key
|
| 344 |
-
python inference.py --env-url https://samsankar-hallucination-guard-env.hf.space --episodes 3 --steps 5
|
| 345 |
-
|
| 346 |
-
# HF Router (open models)
|
| 347 |
-
export API_BASE_URL=https://router.huggingface.co/v1
|
| 348 |
-
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
|
| 349 |
-
export HF_TOKEN=hf_your_token
|
| 350 |
-
python inference.py --env-url https://samsankar-hallucination-guard-env.hf.space --episodes 3 --steps 5
|
| 351 |
-
|
| 352 |
-
# OpenRouter (free-tier models)
|
| 353 |
-
export API_BASE_URL=https://openrouter.ai/api/v1
|
| 354 |
-
export MODEL_NAME=nvidia/nemotron-3-super-120b-a12b:free
|
| 355 |
-
export HF_TOKEN=sk-or-v1-your_key
|
| 356 |
-
python inference.py --env-url https://samsankar-hallucination-guard-env.hf.space --episodes 3 --steps 5
|
| 357 |
-
```
|
| 358 |
-
|
| 359 |
-
---
|
| 360 |
-
|
| 361 |
-
## 🌐 Deployment
|
| 362 |
-
|
| 363 |
-
### HuggingFace Spaces
|
| 364 |
-
|
| 365 |
-
The environment uses a **two-phase loading strategy**:
|
| 366 |
-
|
| 367 |
-
1. **Core datasets** (~86K examples) load synchronously at startup
|
| 368 |
-
2. **Extended datasets** (~1M+ examples) load in background after server is healthy
|
| 369 |
-
|
| 370 |
-
ML models (sentence-transformers, NLI CrossEncoder, ROUGE, BERTScore) preload during Docker build to avoid cold-start delays.
|
| 371 |
-
|
| 372 |
-
### Configuration
|
| 373 |
-
|
| 374 |
-
| Variable | Description | Default |
|
| 375 |
-
|----------|-------------|---------|
|
| 376 |
-
| `USE_LARGE_NLI` | Use large NLI model (more accurate, more memory) | `false` |
|
| 377 |
-
| `HF_HOME` | HuggingFace cache directory | `/tmp/hf_cache` |
|
| 378 |
-
|
| 379 |
-
---
|
| 380 |
-
|
| 381 |
-
## 🔌 Integration Examples
|
| 382 |
-
|
| 383 |
-
### OpenAI SDK
|
| 384 |
-
|
| 385 |
-
```python
|
| 386 |
-
# See examples/openai_integration.py for full implementation
|
| 387 |
-
from openai import OpenAI
|
| 388 |
-
import requests
|
| 389 |
-
|
| 390 |
-
client = OpenAI()
|
| 391 |
-
ENV_URL = "https://samsankar-hallucination-guard-env.hf.space"
|
| 392 |
-
|
| 393 |
-
# 1. Reset
|
| 394 |
-
obs = requests.post(f"{ENV_URL}/reset", json={"difficulty": "beginner"}).json()
|
| 395 |
-
|
| 396 |
-
# 2. Get answer from GPT-4
|
| 397 |
-
response = client.chat.completions.create(
|
| 398 |
-
model="gpt-4o-mini",
|
| 399 |
-
messages=[{"role": "user", "content": f"Answer ONLY from context.\n\nContext: {obs['context']}\n\nQuestion: {obs['question']}"}],
|
| 400 |
-
temperature=0.1
|
| 401 |
-
)
|
| 402 |
-
|
| 403 |
-
# 3. Submit to environment
|
| 404 |
-
result = requests.post(f"{ENV_URL}/step", json={
|
| 405 |
-
"answer": response.choices[0].message.content,
|
| 406 |
-
"confidence": 0.8,
|
| 407 |
-
"session_id": obs.get("session_id"),
|
| 408 |
-
}).json()
|
| 409 |
-
print(f"Reward: {result['reward']}")
|
| 410 |
-
```
|
| 411 |
-
|
| 412 |
-
### Groq (Cloud — Best Performance)
|
| 413 |
-
|
| 414 |
-
```bash
|
| 415 |
-
export API_BASE_URL=https://api.groq.com/openai/v1
|
| 416 |
-
export MODEL_NAME=llama-3.3-70b-versatile
|
| 417 |
-
export HF_TOKEN=gsk_your_key_here
|
| 418 |
-
python inference.py --env-url http://localhost:7860 --episodes 3 --steps 5 --seed 42
|
| 419 |
-
```
|
| 420 |
-
|
| 421 |
-
### Ollama (Local)
|
| 422 |
-
|
| 423 |
-
```bash
|
| 424 |
-
ollama pull qwen2.5:7b
|
| 425 |
-
export API_BASE_URL=http://localhost:11434/v1
|
| 426 |
-
export MODEL_NAME=qwen2.5:7b
|
| 427 |
-
export HF_TOKEN=ollama # Any non-empty value triggers LLM mode
|
| 428 |
-
python inference.py --env-url http://localhost:7860 --episodes 3 --steps 5 --seed 42
|
| 429 |
-
```
|
| 430 |
-
|
| 431 |
---
|
| 432 |
|
| 433 |
## 💻 Development
|
|
@@ -441,9 +209,6 @@ pytest tests/ -v
|
|
| 441 |
|
| 442 |
# Validate OpenEnv compliance
|
| 443 |
openenv validate --url http://localhost:7860 --verbose
|
| 444 |
-
|
| 445 |
-
# Lint
|
| 446 |
-
ruff check . --ignore E501,F401,F403
|
| 447 |
```
|
| 448 |
|
| 449 |
---
|
|
@@ -452,34 +217,10 @@ ruff check . --ignore E501,F401,F403
|
|
| 452 |
|
| 453 |
| | |
|
| 454 |
|---|---|
|
| 455 |
-
|
|
| 456 |
-
| 📖 Interactive API Docs |
|
| 457 |
| 🔧 OpenEnv Framework | https://github.com/meta-pytorch/OpenEnv |
|
| 458 |
|
| 459 |
---
|
| 460 |
|
| 461 |
-
|
| 462 |
-
|
| 463 |
-
### v4.2.0 (2026-04)
|
| 464 |
-
|
| 465 |
-
- **Fixed** BERTScore crash on HF Spaces — switched from `microsoft/deberta-v3-base` to `roberta-base` (fast tokenizer incompatibility with transformers>=4.57)
|
| 466 |
-
- **Fixed** OpenEnv validation failures — `/metadata` now returns `description`, `/schema` now returns `state` schema
|
| 467 |
-
- **Fixed** Thread safety — `/reset` and `/step` use per-session environments with shared dataset loader
|
| 468 |
-
- **Fixed** Numerical fabrication detection — numbers now extracted from original text before normalization replaces them with `NUM`
|
| 469 |
-
- **Fixed** `inference.py` step_infos mapping — `correctness` and `grounding` no longer conflated
|
| 470 |
-
- **Fixed** `/baseline` endpoint — proper `step_infos` with separate correctness/grounding/calibration keys
|
| 471 |
-
- **Fixed** Leaderboard file I/O — proper `with` statements and UTF-8 encoding
|
| 472 |
-
- **Fixed** `client.py` default port — changed from 8000 to 7860
|
| 473 |
-
- **Fixed** Version mismatch — `openenv.yaml` updated to v4.2.0
|
| 474 |
-
- **Added** Test suite — 42 tests across `test_grader.py` and `test_tasks.py`
|
| 475 |
-
|
| 476 |
-
### v4.1.0 (2026-03)
|
| 477 |
-
|
| 478 |
-
- OpenEnv compliant with `/tasks`, `/grader`, `/baseline` endpoints
|
| 479 |
-
- `inference.py` hackathon submission script
|
| 480 |
-
- 9-component reward system with ROUGE + BERTScore + AlignScore
|
| 481 |
-
- 38 datasets, 1M+ examples
|
| 482 |
-
|
| 483 |
-
---
|
| 484 |
-
|
| 485 |
-
*Built to train models to stop hallucination · MIT License*
|
|
|
|
| 1 |
---
|
| 2 |
+
title: AutoClean-Ai
|
| 3 |
+
emoji: 🧹
|
| 4 |
+
colorFrom: green
|
| 5 |
+
colorTo: blue
|
| 6 |
sdk: docker
|
| 7 |
app_port: 7860
|
| 8 |
pinned: true
|
| 9 |
tags:
|
| 10 |
- openenv
|
| 11 |
- reinforcement-learning
|
| 12 |
+
- data-cleaning
|
| 13 |
+
- data-preprocessing
|
|
|
|
|
|
|
| 14 |
- llm-training
|
|
|
|
| 15 |
- benchmark
|
| 16 |
- ai-safety
|
| 17 |
+
- data-quality
|
| 18 |
+
- mlops
|
| 19 |
base_path: /web
|
| 20 |
---
|
| 21 |
|
| 22 |
+
# 🧹 AutoClean-Ai
|
| 23 |
|
| 24 |
+
> **Production-grade OpenEnv RL environment for training AI models to clean tabular data automatically.**
|
| 25 |
|
| 26 |
+
**Server Version:** v1.0.0
|
| 27 |
|
| 28 |
[](https://github.com/meta-pytorch/OpenEnv)
|
| 29 |
[](#-quick-start)
|
| 30 |
[](LICENSE)
|
| 31 |
+
[](#-datasets)
|
| 32 |
|
| 33 |
---
|
| 34 |
|
| 35 |
+
## 💡 The Problem
|
| 36 |
|
| 37 |
+
80% of data scientist time is spent cleaning data. Bad data causes 60% of ML project failures. AutoClean-Ai was built to train AI agents that can automatically detect and fix common data quality issues in tabular datasets.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
## 🚀 Quick Start
|
| 40 |
|
| 41 |
### Run Locally
|
| 42 |
|
| 43 |
```bash
|
| 44 |
+
git clone https://github.com/SairajMN/WorkflowOps.git
|
| 45 |
+
cd WorkflowOps
|
| 46 |
pip install -e .
|
| 47 |
uvicorn server.app:app --host 0.0.0.0 --port 7860
|
| 48 |
curl http://localhost:7860/health
|
|
|
|
| 53 |
```python
|
| 54 |
import requests
|
| 55 |
|
| 56 |
+
BASE = "http://localhost:7860"
|
| 57 |
|
| 58 |
# 1. Start episode
|
| 59 |
obs = requests.post(f"{BASE}/reset", json={"difficulty": "beginner"}).json()
|
| 60 |
+
print(obs["dataset_preview"], obs["column_info"])
|
| 61 |
|
| 62 |
+
# 2. Submit cleaning action
|
| 63 |
result = requests.post(f"{BASE}/step", json={
|
| 64 |
+
"action_type": "fix_missing_values",
|
| 65 |
+
"column_index": 2,
|
| 66 |
+
"confidence": 0.92,
|
| 67 |
+
"reasoning": "Mean imputation for numerical column",
|
| 68 |
"session_id": obs.get("session_id"),
|
| 69 |
}).json()
|
| 70 |
+
print(f"Reward: {result['reward']}, Cleaned: {result['rows_cleaned']}")
|
| 71 |
|
| 72 |
# 3. Score the episode
|
| 73 |
grade = requests.post(f"{BASE}/grader", json={
|
| 74 |
+
"task_id": "task_1_basic_cleaning",
|
| 75 |
"step_rewards": [result['reward']],
|
| 76 |
+
"step_infos": [result],
|
| 77 |
}).json()
|
| 78 |
print(f"Episode score: {grade['score']}")
|
| 79 |
```
|
| 80 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
### Validate OpenEnv Compliance
|
| 82 |
|
| 83 |
```bash
|
| 84 |
# Local structure check
|
| 85 |
openenv validate
|
| 86 |
|
| 87 |
+
# Runtime check against live server
|
| 88 |
openenv validate --url http://localhost:7860 --verbose
|
| 89 |
```
|
| 90 |
|
|
|
|
| 92 |
|
| 93 |
## 🎯 Tasks
|
| 94 |
|
| 95 |
+
3 progressive difficulty tasks:
|
| 96 |
|
| 97 |
+
| # | task_id | Difficulty | Description | Expected Agent Score |
|
| 98 |
+
|---|---------|-----------|-------------|-------------------|
|
| 99 |
+
| 1 | `task_1_basic_cleaning` | 🟢 Beginner | Fix missing values, standardize formats | 0.70–0.85 |
|
| 100 |
+
| 2 | `task_2_advanced_cleaning` | 🟡 Intermediate | Handle outliers, correct data types, deduplication | 0.55–0.70 |
|
| 101 |
+
| 3 | `task_3_full_pipeline` | 🔴 Advanced | Complete end-to-end data cleaning pipeline | 0.40–0.60 |
|
| 102 |
|
| 103 |
---
|
| 104 |
|
| 105 |
+
## 🎮 Environment Workflow
|
| 106 |
|
| 107 |
+
The agent receives a **tabular dataset** with known quality issues. It must select the appropriate cleaning operation, apply it correctly, and justify its choice.
|
| 108 |
|
| 109 |
### Action Space
|
| 110 |
|
|
|
|
|
|
|
| 111 |
```json
|
| 112 |
{
|
| 113 |
+
"action_type": "fix_missing_values | remove_outliers | standardize | deduplicate | correct_types | fill_dates",
|
| 114 |
+
"column_index": 3,
|
| 115 |
+
"confidence": 0.85,
|
| 116 |
+
"reasoning": "string explaining the choice",
|
| 117 |
+
"session_id": "session id from reset"
|
|
|
|
| 118 |
}
|
| 119 |
```
|
| 120 |
|
|
|
|
| 122 |
|
| 123 |
```json
|
| 124 |
{
|
| 125 |
+
"dataset_preview": "First 5 rows of data",
|
| 126 |
+
"column_info": "Column names, types, missing stats",
|
| 127 |
+
"reward": 0.75,
|
| 128 |
+
"feedback": "Detailed human-readable feedback",
|
| 129 |
+
"rows_cleaned": 12,
|
| 130 |
+
"issues_remaining": 3,
|
| 131 |
+
"done": false,
|
| 132 |
+
"session_id": "ses_a1b2c3d4"
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
```
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
---
|
| 137 |
|
| 138 |
+
## 📊 Reward System (7 Components)
|
| 139 |
|
| 140 |
| Component | Weight | Description |
|
| 141 |
|-----------|--------|-------------|
|
| 142 |
+
| Correctness | 0.35 | Operation actually fixed the issue |
|
| 143 |
+
| Appropriate action | 0.25 | Right operation selected for the problem |
|
| 144 |
+
| Confidence calibration | 0.15 | Confidence matches actual correctness |
|
| 145 |
+
| No side effects | 0.15 | Cleaning didn't break other columns |
|
| 146 |
+
| Efficiency | 0.10 | Minimum steps to clean dataset |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
---
|
| 149 |
|
| 150 |
+
## 📈 Metrics
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
|
| 152 |
+
✅ Data Quality Score
|
| 153 |
+
✅ Completeness Ratio
|
| 154 |
+
✅ Uniqueness Ratio
|
| 155 |
+
✅ Type Consistency
|
| 156 |
+
✅ Cleaning Efficiency
|
| 157 |
+
✅ Action Appropriateness
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
|
| 159 |
---
|
| 160 |
|
| 161 |
+
## 📋 Supported Data Cleaning Operations
|
| 162 |
+
|
| 163 |
+
| Operation | Description |
|
| 164 |
+
|-----------|-------------|
|
| 165 |
+
| `fix_missing_values` | Mean/median/mode imputation |
|
| 166 |
+
| `remove_outliers` | IQR / Z-score outlier removal |
|
| 167 |
+
| `standardize` | Normalize numerical columns |
|
| 168 |
+
| `deduplicate` | Remove duplicate rows |
|
| 169 |
+
| `correct_types` | Fix incorrect data types |
|
| 170 |
+
| `fill_dates` | Standardize date formats |
|
| 171 |
+
| `handle_categories` | Encode categorical columns |
|
| 172 |
+
| `remove_duplicates` | Drop identical rows |
|
| 173 |
+
| `trim_strings` | Clean whitespace from text columns |
|
| 174 |
+
| `correct_values` | Fix known invalid values |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 175 |
|
| 176 |
---
|
| 177 |
|
|
|
|
| 187 |
| `GET` | `/metadata` | Environment name, version, description |
|
| 188 |
| `GET` | `/schema` | Action, observation, and state JSON schemas |
|
| 189 |
| `GET` | `/health` | Health check |
|
|
|
|
| 190 |
|
| 191 |
### Environment
|
| 192 |
|
| 193 |
| Method | Endpoint | Description |
|
| 194 |
|--------|----------|-------------|
|
| 195 |
+
| `POST` | `/reset` | Start new episode |
|
| 196 |
+
| `POST` | `/step` | Submit cleaning action |
|
| 197 |
| `GET` | `/state` | Get current episode state |
|
| 198 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 199 |
---
|
| 200 |
|
| 201 |
## 💻 Development
|
|
|
|
| 209 |
|
| 210 |
# Validate OpenEnv compliance
|
| 211 |
openenv validate --url http://localhost:7860 --verbose
|
|
|
|
|
|
|
|
|
|
| 212 |
```
|
| 213 |
|
| 214 |
---
|
|
|
|
| 217 |
|
| 218 |
| | |
|
| 219 |
|---|---|
|
| 220 |
+
| 📦 GitHub | https://github.com/SairajMN/WorkflowOps |
|
| 221 |
+
| 📖 Interactive API Docs | http://localhost:7860/redoc |
|
| 222 |
| 🔧 OpenEnv Framework | https://github.com/meta-pytorch/OpenEnv |
|
| 223 |
|
| 224 |
---
|
| 225 |
|
| 226 |
+
*Built for Data Cleaning AI Agents · MIT License*
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
__init__.py
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
-
"""
|
| 2 |
|
| 3 |
-
__version__ = "
|
|
|
|
| 1 |
+
"""AutoClean-AI OpenEnv Environment"""
|
| 2 |
|
| 3 |
+
__version__ = "1.0.0"
|
client.py
CHANGED
|
@@ -1,83 +1,5 @@
|
|
| 1 |
-
"""
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
from models import HallucinationAction, HallucinationObservation, HallucinationState
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
class HallucinationClient:
|
| 10 |
-
"""Client for interacting with the HallucinationGuard environment."""
|
| 11 |
-
|
| 12 |
-
def __init__(self, base_url: str = "http://localhost:7860"):
|
| 13 |
-
self.base_url = base_url.rstrip("/")
|
| 14 |
-
self.session = requests.Session()
|
| 15 |
-
|
| 16 |
-
def health_check(self) -> Dict[str, Any]:
|
| 17 |
-
"""Check if the server is healthy."""
|
| 18 |
-
response = self.session.get(f"{self.base_url}/health")
|
| 19 |
-
response.raise_for_status()
|
| 20 |
-
return response.json()
|
| 21 |
-
|
| 22 |
-
def reset(self) -> HallucinationObservation:
|
| 23 |
-
"""Reset the environment and get initial observation."""
|
| 24 |
-
response = self.session.post(f"{self.base_url}/reset")
|
| 25 |
-
response.raise_for_status()
|
| 26 |
-
data = response.json()
|
| 27 |
-
self._session_id = data.get("session_id")
|
| 28 |
-
return HallucinationObservation(**data)
|
| 29 |
-
|
| 30 |
-
def step(self, action: HallucinationAction) -> HallucinationObservation:
|
| 31 |
-
"""Take a step in the environment."""
|
| 32 |
-
action_dict = {
|
| 33 |
-
"answer": action.answer,
|
| 34 |
-
"confidence": action.confidence,
|
| 35 |
-
"source_quote": action.source_quote,
|
| 36 |
-
"metadata": action.metadata
|
| 37 |
-
}
|
| 38 |
-
if getattr(self, '_session_id', None):
|
| 39 |
-
action_dict["session_id"] = self._session_id
|
| 40 |
-
response = self.session.post(
|
| 41 |
-
f"{self.base_url}/step",
|
| 42 |
-
json=action_dict
|
| 43 |
-
)
|
| 44 |
-
response.raise_for_status()
|
| 45 |
-
data = response.json()
|
| 46 |
-
return HallucinationObservation(**data)
|
| 47 |
-
|
| 48 |
-
def get_state(self) -> HallucinationState:
|
| 49 |
-
"""Get the current environment state."""
|
| 50 |
-
response = self.session.get(f"{self.base_url}/state")
|
| 51 |
-
response.raise_for_status()
|
| 52 |
-
data = response.json()
|
| 53 |
-
return HallucinationState(**data)
|
| 54 |
-
|
| 55 |
-
def close(self) -> None:
|
| 56 |
-
"""Close the client session."""
|
| 57 |
-
self.session.close()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
# Example usage
|
| 61 |
-
if __name__ == "__main__":
|
| 62 |
-
client = HallucinationClient()
|
| 63 |
-
|
| 64 |
-
# Check health
|
| 65 |
-
print("Health:", client.health_check())
|
| 66 |
-
|
| 67 |
-
# Reset environment
|
| 68 |
-
obs = client.reset()
|
| 69 |
-
print(f"\nQuestion: {obs.question}")
|
| 70 |
-
print(f"Context: {obs.context[:200]}...")
|
| 71 |
-
|
| 72 |
-
# Take a step with a sample action
|
| 73 |
-
action = HallucinationAction(
|
| 74 |
-
answer="This is a test answer",
|
| 75 |
-
confidence=0.8,
|
| 76 |
-
source_quote="test quote"
|
| 77 |
-
)
|
| 78 |
-
obs = client.step(action)
|
| 79 |
-
print(f"\nReward: {obs.reward}")
|
| 80 |
-
print(f"Feedback: {obs.feedback}")
|
| 81 |
-
print(f"Is Hallucination: {obs.is_hallucination}")
|
| 82 |
-
|
| 83 |
-
client.close()
|
|
|
|
| 1 |
+
"""AutoClean-AI Client Module"""
|
| 2 |
+
|
| 3 |
+
class AutoCleanClient:
|
| 4 |
+
"""Client interface for AutoClean environment"""
|
| 5 |
+
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
openenv.yaml
CHANGED
|
@@ -30,7 +30,7 @@ openenv:
|
|
| 30 |
|
| 31 |
entry_points:
|
| 32 |
server: server.app:app
|
| 33 |
-
client:
|
| 34 |
|
| 35 |
# Tasks (easy → medium → hard)
|
| 36 |
tasks:
|
|
|
|
| 30 |
|
| 31 |
entry_points:
|
| 32 |
server: server.app:app
|
| 33 |
+
client:
|
| 34 |
|
| 35 |
# Tasks (easy → medium → hard)
|
| 36 |
tasks:
|
pyproject.toml
CHANGED
|
@@ -1,70 +1,18 @@
|
|
| 1 |
-
[
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
[
|
| 6 |
-
name = "
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
"question-answering",
|
| 20 |
-
"ai-safety"
|
| 21 |
-
]
|
| 22 |
-
classifiers = [
|
| 23 |
-
"Development Status :: 5 - Production/Stable",
|
| 24 |
-
"Intended Audience :: Developers",
|
| 25 |
-
"Intended Audience :: Science/Research",
|
| 26 |
-
"License :: OSI Approved :: MIT License",
|
| 27 |
-
"Programming Language :: Python :: 3",
|
| 28 |
-
"Programming Language :: Python :: 3.10",
|
| 29 |
-
"Programming Language :: Python :: 3.11",
|
| 30 |
-
"Programming Language :: Python :: 3.12",
|
| 31 |
-
"Topic :: Scientific/Engineering :: Artificial Intelligence",
|
| 32 |
-
]
|
| 33 |
-
dependencies = [
|
| 34 |
-
"openenv-core>=0.2.0",
|
| 35 |
-
"fastapi>=0.100.0",
|
| 36 |
-
"uvicorn>=0.23.0",
|
| 37 |
-
"requests>=2.31.0",
|
| 38 |
-
"huggingface_hub>=0.20.0",
|
| 39 |
-
"datasets>=2.14.0",
|
| 40 |
-
"sentence-transformers>=2.7.0,<3.0.0",
|
| 41 |
-
"transformers>=4.35.0,<5.0.0",
|
| 42 |
-
"numpy>=1.24.0,<2.0.0",
|
| 43 |
-
"protobuf>=3.20.0,<5.0.0",
|
| 44 |
-
"rouge-score>=0.1.2",
|
| 45 |
-
"bert-score>=0.3.13",
|
| 46 |
-
"pydantic>=2.0.0",
|
| 47 |
-
"aiofiles>=23.0.0",
|
| 48 |
-
"python-json-logger>=2.0.0",
|
| 49 |
-
]
|
| 50 |
-
|
| 51 |
-
[project.optional-dependencies]
|
| 52 |
-
dev = [
|
| 53 |
-
"pytest>=7.0.0",
|
| 54 |
-
"pytest-asyncio>=0.21.0",
|
| 55 |
-
"httpx>=0.24.0",
|
| 56 |
-
]
|
| 57 |
-
|
| 58 |
-
[project.scripts]
|
| 59 |
-
server = "server.app:main"
|
| 60 |
-
|
| 61 |
-
[project.urls]
|
| 62 |
-
Homepage = "https://huggingface.co/spaces/SamSankar/hallucination-guard-env"
|
| 63 |
-
Repository = "https://github.com/SS-360/hallucination-guard-env"
|
| 64 |
-
Documentation = "https://samsankar-hallucination-guard-env.hf.space/docs"
|
| 65 |
-
|
| 66 |
-
[tool.hatch.build.targets.wheel]
|
| 67 |
-
packages = ["server", "models.py", "client.py"]
|
| 68 |
-
|
| 69 |
-
[tool.pytest.ini_options]
|
| 70 |
-
testpaths = ["tests"]
|
|
|
|
| 1 |
+
[project]
|
| 2 |
+
name = "AutoClean-AI"
|
| 3 |
+
version = "1.0.0"
|
| 4 |
+
description = "OpenEnv environment for AI data cleaning tasks"
|
| 5 |
+
authors = [
|
| 6 |
+
{ name = "WorkflowOps" }
|
| 7 |
+
]
|
| 8 |
+
license = { file = "LICENSE" }
|
| 9 |
+
dependencies = [
|
| 10 |
+
"openenv-core>=0.2.0",
|
| 11 |
+
"fastapi>=0.100.0",
|
| 12 |
+
"uvicorn>=0.23.0",
|
| 13 |
+
"requests>=2.31.0",
|
| 14 |
+
"openai>=1.0.0"
|
| 15 |
+
]
|
| 16 |
+
|
| 17 |
+
[tool.openenv]
|
| 18 |
+
version = ">=0.2.0"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
server/app.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
"""
|
| 2 |
-
|
| 3 |
|
| 4 |
Endpoints:
|
| 5 |
Standard : POST /reset POST /step GET /state GET /health
|
|
@@ -1040,11 +1040,11 @@ function copyCode(btn, id) {
|
|
| 1040 |
# FASTAPI APP — session-isolated environments for thread safety
|
| 1041 |
# ═══════════════════════════════════════════════════════════════════════════════
|
| 1042 |
|
| 1043 |
-
_default_env: Optional[
|
| 1044 |
_env_loading = False
|
| 1045 |
_env_lock = threading.Lock()
|
| 1046 |
|
| 1047 |
-
def _get_default_env() ->
|
| 1048 |
"""Get or create the shared dataset-loader environment (used only for dataset access)."""
|
| 1049 |
global _default_env, _env_loading
|
| 1050 |
if _default_env is not None:
|
|
@@ -1054,8 +1054,8 @@ def _get_default_env() -> HallucinationEnvironment:
|
|
| 1054 |
return _default_env
|
| 1055 |
_env_loading = True
|
| 1056 |
try:
|
| 1057 |
-
logger.info("Creating
|
| 1058 |
-
_default_env =
|
| 1059 |
logger.info(f"Environment ready — {_default_env.dataset_loader.get_total_examples():,} examples loaded.")
|
| 1060 |
return _default_env
|
| 1061 |
except Exception as e:
|
|
@@ -1077,22 +1077,22 @@ def _get_default_env() -> HallucinationEnvironment:
|
|
| 1077 |
_env_loading = False
|
| 1078 |
|
| 1079 |
|
| 1080 |
-
def _create_session_env(session_id: str) ->
|
| 1081 |
"""Create a fresh per-session environment that shares the dataset loader
|
| 1082 |
(expensive to load) but has its own episode state (safe for concurrent use)."""
|
| 1083 |
loader_env = _get_default_env()
|
| 1084 |
# Pass the shared loader directly into __init__ so we skip the expensive
|
| 1085 |
# DatasetLoader() construction and dataset loading that would otherwise
|
| 1086 |
# happen inside HallucinationEnvironment.__init__
|
| 1087 |
-
env =
|
| 1088 |
return env
|
| 1089 |
|
| 1090 |
|
| 1091 |
-
_sessions: Dict[str,
|
| 1092 |
_session_lock = threading.Lock()
|
| 1093 |
|
| 1094 |
|
| 1095 |
-
def _get_session(session_id: str) -> Optional[
|
| 1096 |
"""Retrieve an existing session environment."""
|
| 1097 |
with _session_lock:
|
| 1098 |
return _sessions.get(session_id)
|
|
@@ -1230,8 +1230,8 @@ async def step(action_data: Dict[str, Any]):
|
|
| 1230 |
if env is None:
|
| 1231 |
# Fallback: use default env (single-user mode)
|
| 1232 |
env = _get_default_env()
|
| 1233 |
-
valid = set(
|
| 1234 |
-
action =
|
| 1235 |
result = _safe_dict(env.step(action))
|
| 1236 |
# If episode is done, clean up session
|
| 1237 |
if result.get("done", False) and session_id:
|
|
|
|
| 1 |
"""
|
| 2 |
+
AutoClean-Ai v1.0.0 — Production FastAPI Server
|
| 3 |
|
| 4 |
Endpoints:
|
| 5 |
Standard : POST /reset POST /step GET /state GET /health
|
|
|
|
| 1040 |
# FASTAPI APP — session-isolated environments for thread safety
|
| 1041 |
# ═══════════════════════════════════════════════════════════════════════════════
|
| 1042 |
|
| 1043 |
+
_default_env: Optional[DataCleaningEnvironment] = None
|
| 1044 |
_env_loading = False
|
| 1045 |
_env_lock = threading.Lock()
|
| 1046 |
|
| 1047 |
+
def _get_default_env() -> DataCleaningEnvironment:
|
| 1048 |
"""Get or create the shared dataset-loader environment (used only for dataset access)."""
|
| 1049 |
global _default_env, _env_loading
|
| 1050 |
if _default_env is not None:
|
|
|
|
| 1054 |
return _default_env
|
| 1055 |
_env_loading = True
|
| 1056 |
try:
|
| 1057 |
+
logger.info("Creating DataCleaningEnvironment (dataset loader)...")
|
| 1058 |
+
_default_env = DataCleaningEnvironment()
|
| 1059 |
logger.info(f"Environment ready — {_default_env.dataset_loader.get_total_examples():,} examples loaded.")
|
| 1060 |
return _default_env
|
| 1061 |
except Exception as e:
|
|
|
|
| 1077 |
_env_loading = False
|
| 1078 |
|
| 1079 |
|
| 1080 |
+
def _create_session_env(session_id: str) -> DataCleaningEnvironment:
|
| 1081 |
"""Create a fresh per-session environment that shares the dataset loader
|
| 1082 |
(expensive to load) but has its own episode state (safe for concurrent use)."""
|
| 1083 |
loader_env = _get_default_env()
|
| 1084 |
# Pass the shared loader directly into __init__ so we skip the expensive
|
| 1085 |
# DatasetLoader() construction and dataset loading that would otherwise
|
| 1086 |
# happen inside HallucinationEnvironment.__init__
|
| 1087 |
+
env = DataCleaningEnvironment(session_id=session_id, dataset_loader=loader_env.dataset_loader)
|
| 1088 |
return env
|
| 1089 |
|
| 1090 |
|
| 1091 |
+
_sessions: Dict[str, DataCleaningEnvironment] = {}
|
| 1092 |
_session_lock = threading.Lock()
|
| 1093 |
|
| 1094 |
|
| 1095 |
+
def _get_session(session_id: str) -> Optional[DataCleaningEnvironment]:
|
| 1096 |
"""Retrieve an existing session environment."""
|
| 1097 |
with _session_lock:
|
| 1098 |
return _sessions.get(session_id)
|
|
|
|
| 1230 |
if env is None:
|
| 1231 |
# Fallback: use default env (single-user mode)
|
| 1232 |
env = _get_default_env()
|
| 1233 |
+
valid = set(DataCleaningAction.model_fields.keys()) if hasattr(DataCleaningAction, 'model_fields') else set(DataCleaningAction.__fields__.keys())
|
| 1234 |
+
action = DataCleaningAction(**{k: v for k, v in action_data.items() if k in valid})
|
| 1235 |
result = _safe_dict(env.step(action))
|
| 1236 |
# If episode is done, clean up session
|
| 1237 |
if result.get("done", False) and session_id:
|