CoolFace
Apppublic

Nihar-776/Hackathon-MetaPytorch

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes
App README

Record Repair — OpenEnv Environment

An OpenEnv-compliant reinforcement learning environment where an LLM agent receives a corrupted JSON employee record and must return a fully corrected version in each step.

Environment Description

The environment generates synthetic employee records using the faker Python library. Each record has exactly 7 fields: name, email, phone, dob, salary, department, and join_date. On each episode reset, one or more fields are corrupted using a configurable set of corruption strategies. The agent receives the corrupted record and must return a corrected version. The environment scores the correction and returns a reward in [0.0, 1.0].

This is a real-world data quality task — corrupted records are a daily reality in ETL pipelines, CRM imports, and database migrations. The environment simulates that problem at the record level.

Action Space

The agent submits a POST body to /step with this JSON schema:

json
{
  "corrected_record": {
    "name":       "Alice Johnson",
    "email":      "alice.johnson@example.com",
    "phone":      "555-123-4567",
    "dob":        "1990-06-15",
    "salary":     87500.0,
    "department": "Engineering",
    "join_date":  "2018-03-01"
  },
  "confidence": 0.9,
  "task_id":    "task1_single_typo",
  "session_id": "<uuid returned by /reset>"
}
FieldTypeDescription
corrected_recordobjectFull 7-field record with agent's corrections applied
confidencefloat 0–1Agent's self-reported confidence (logged, not scored)
task_idstringMust match the task_id from the current episode
session_idstringMust match the session_id returned by /reset

Observation Space

Both /reset and /step return this structure:

json
{
  "task_id":               "task1_single_typo",
  "session_id":            "550e8400-e29b-41d4-a716-446655440000",
  "step":                  1,
  "corrupted_record": {
    "name":       "Alce Johnson",
    "email":      "alice.johnson@example.com",
    "phone":      "555-123-4567",
    "dob":        "1990-06-15",
    "salary":     87500.0,
    "department": "Engineering",
    "join_date":  "2018-03-01"
  },
  "corruption_types_hint": ["typo"],
  "num_corrupted_fields":  1,
  "fields_still_wrong":    0,
  "score_so_far":          0.94,
  "max_steps":             5,
  "done":                  true,
  "message":               "Success! Score: 0.9400"
}
FieldTypeDescription
corrupted_recordobjectThe record with corruptions applied — what the agent must fix
corruption_types_hintlist[str]Which corruption types are present — NOT which fields
num_corrupted_fieldsintTotal fields corrupted in this episode
fields_still_wrongintFields still wrong after the last step (0 after reset)
score_so_farfloatCumulative reward across all steps
doneboolTrue when episode is over

Reward Function

reward = 0.6 × exact_accuracy
       + 0.3 × fuzzy_accuracy
       + 0.1 × no_hallucination
  • exact_accuracy: fraction of corrupted fields the agent fixed exactly (after normalisation: strip, lowercase, remove commas/hyphens/currency suffixes)
  • fuzzy_accuracy: average SequenceMatcher ratio across corrupted fields — gives partial credit for near-correct answers like almost-fixed typos
  • no_hallucination: fraction of clean (non-corrupted) fields the agent left unchanged — penalises overcorrection

All three components and the final reward are in [0.0, 1.0].

Corruption Types

TypeDescriptionExample
typoCharacter-level errors in string fields"Engineering""Enginreeing"
nullField set to None or """alice@example.com"None
formatStructured format mangled"2020-01-15""15/01/2020"
swapTwo field values exchangedname contains email address
numericSalary wrong scale/unit87500.0"87,500.00 USD"

Tasks

Task IDDifficultyCorruptionsMax StepsSuccess Threshold
task1_single_typoEasy1 (typo only)50.80
task2_multi_corruptMedium3 (null + format + numeric)100.70
task3_full_adversarialHard5 (all types)150.60

Setup and Running Locally

Prerequisites: Python 3.11+, Docker

Without Docker

bash
git clone https://huggingface.co/spaces/Nihar-776/Hackathon-MetaPytorch
cd Hackathon-MetaPytorch
pip install -r requirements.txt
uvicorn app:app --host 0.0.0.0 --port 7860

With Docker

bash
docker build -t record-repair-env .
docker run -p 7860:7860 record-repair-env

Run the inference script

In a second terminal after the server is running:

bash
export ENV_BASE_URL=http://localhost:7860
export API_BASE_URL=https://router.huggingface.co/v1
export MODEL_NAME=Qwen/Qwen2.5-72B-Instruct
export HF_TOKEN=hf_your_token_here

python inference.py

Quick API test (no LLM needed)

bash
# Health check
curl http://localhost:7860/

# Start an episode
curl -X POST http://localhost:7860/reset \
  -H "Content-Type: application/json" \
  -d '{"task_id": "task1_single_typo"}'

# Submit a correction (replace session_id with value from reset response)
curl -X POST http://localhost:7860/step \
  -H "Content-Type: application/json" \
  -d '{
    "corrected_record": {"name":"Alice Johnson","email":"alice@example.com","phone":"555-123-4567","dob":"1990-06-15","salary":87500.0,"department":"Engineering","join_date":"2018-03-01"},
    "confidence": 1.0,
    "task_id": "task1_single_typo",
    "session_id": "<your_session_id>"
  }'

File Structure

.
├── app.py              ← FastAPI server (/reset, /step, /state)
├── models.py           ← Pydantic typed models (Action, Observation, State)
├── data_generator.py   ← Faker-based employee record generator
├── corruptor.py        ← 5 corruption strategies with severity control
├── graders.py          ← Reward function (exact + fuzzy + anti-hallucination)
├── inference.py        ← LLM agent baseline using OpenAI client
├── tasks.yaml          ← Task definitions (easy / medium / hard)
├── openenv.yaml        ← OpenEnv specification manifest
├── Dockerfile          ← Container build
├── requirements.txt    ← Pinned Python dependencies
└── README.md           ← This file