Spaces:
Sleeping
Sleeping
File size: 10,810 Bytes
734c6cd 2d793d9 734c6cd 2d793d9 734c6cd 2d793d9 734c6cd 601f21d 443c45a 734c6cd 443c45a 734c6cd 11c2f9b 2d793d9 734c6cd 2d793d9 734c6cd f1499c3 734c6cd 2d793d9 601f21d 2d793d9 734c6cd 2d793d9 734c6cd 601f21d 734c6cd 2d793d9 601f21d 734c6cd 601f21d 734c6cd 601f21d 734c6cd 601f21d 2d793d9 601f21d 2d793d9 601f21d 734c6cd 601f21d 734c6cd 601f21d 734c6cd 2d793d9 734c6cd 601f21d 734c6cd 2d793d9 734c6cd 2d793d9 601f21d 734c6cd 601f21d 2d793d9 601f21d 734c6cd 601f21d 734c6cd d547cf7 601f21d 734c6cd 3d8d49f | 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 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 | """
Inference Script - Data Cleaning OpenEnv
=========================================
Runs a baseline LLM agent against all tasks
and produces output in the required format for hackathon submission.
Required environment variables:
API_BASE_URL - LLM API endpoint (default: https://api.openai.com/v1)
MODEL_NAME - model identifier (default: gpt-4o-mini)
HF_TOKEN - Hugging Face API key (REQUIRED)
"""
import os
import sys
import json
import time
import re
from typing import List, Dict, Any, Optional
from dotenv import load_dotenv
from openai import OpenAI
load_dotenv()
# βββββββββββββββββββββββββββββββββββββββββ
# CONFIG
# βββββββββββββββββββββββββββββββββββββββββ
API_BASE_URL = os.getenv("API_BASE_URL", "https://api.openai.com/v1")
MODEL_NAME = os.getenv("MODEL_NAME", "gpt-4o-mini")
HF_TOKEN = os.getenv("HF_TOKEN")
if HF_TOKEN is None:
raise ValueError("HF_TOKEN environment variable is required")
MAX_STEPS = 20
TEMPERATURE = 0.1
MAX_TOKENS = 400
VALID_TASKS = [
"easy_dedup_rename",
"medium_missing_dtype",
"hard_full_pipeline",
]
ENV_NAME = "data-cleaning-openenv"
SUCCESS_SCORE_THRESHOLD = 0.3
SYSTEM_PROMPT = """
You are an expert data cleaning agent.
You will receive information about a dirty dataset and must clean it
step by step using the available operations.
Available operations:
- remove_duplicates: {"operation": "remove_duplicates", "parameters": {}}
- fill_missing_mean: {"operation": "fill_missing_mean", "parameters": {}}
- fill_missing_mode: {"operation": "fill_missing_mode", "parameters": {}}
- fill_missing_median: {"operation": "fill_missing_median", "parameters": {}}
- fix_dtype: {"operation": "fix_dtype", "parameters": {"dtype": "auto"}}
- remove_outliers: {"operation": "remove_outliers", "parameters": {"method": "iqr"}}
- rename_columns: {"operation": "rename_columns", "parameters": {}}
- validate_schema: {"operation": "validate_schema", "parameters": {}}
- finish: {"operation": "finish", "parameters": {}}
Rules:
1. Always respond with ONLY a valid JSON object
2. No explanations, no markdown, no extra text
3. Just the JSON action object
4. Call finish when you think the dataset is clean
Example response:
{"operation": "remove_duplicates", "parameters": {}}
"""
# βββββββββββββββββββββββββββββββββββββββββ
# STDOUT LOGGING β matches official sample format exactly
# βββββββββββββββββββββββββββββββββββββββββ
def _clamp(v: float) -> float:
"""Clamp to strictly (0, 1) β grader rejects exactly 0.0 and 1.0."""
return max(0.01, min(0.99, float(v)))
def log_start(task: str, env: str, model: str) -> None:
print(f"[START] task={task} env={env} model={model}", flush=True)
def log_step(step: int, action: str, reward: float, done: bool, error: Optional[str]) -> None:
# reward to 2 decimal places per spec
error_val = error if error else "null"
done_val = str(done).lower()
print(
f"[STEP] step={step} action={action} reward={_clamp(reward):.2f} done={done_val} error={error_val}",
flush=True,
)
def log_end(success: bool, steps: int, score: float, rewards: List[float]) -> None:
# [END] format from official sample:
# [END] success=... steps=... score=<score> rewards=<r1,r2,...>
rewards_str = ",".join(f"{_clamp(r):.2f}" for r in rewards)
print(
f"[END] success={str(success).lower()} steps={steps} score={_clamp(score):.2f} rewards={rewards_str}",
flush=True,
)
# βββββββββββββββββββββββββββββββββββββββββ
# HELPERS
# βββββββββββββββββββββββββββββββββββββββββ
def build_user_prompt(observation: Dict[str, Any]) -> str:
return f"""
Task: {observation.get("task_description", "")}
Step: {observation.get("step", 0)}
Last message: {observation.get("message", "")}
Current dataset info:
- Shape: {observation.get("shape", [])}
- Columns: {observation.get("columns", [])}
- Duplicate rows: {observation.get("duplicate_count", 0)}
- Missing values: {observation.get("missing_values", {})}
- Data types: {observation.get("dtypes", {})}
Sample rows (first 3):
{json.dumps(observation.get("sample_rows", []), indent=2)}
Available operations: {observation.get("available_operations", [])}
What is your next action? Respond with JSON only.
"""
def parse_action(response_text: str) -> Dict[str, Any]:
text = response_text.strip()
if "```json" in text:
text = text.split("```json")[1].split("```")[0].strip()
elif "```" in text:
text = text.split("```")[1].split("```")[0].strip()
try:
action = json.loads(text)
if "operation" not in action:
return {"operation": "finish", "parameters": {}}
if "parameters" not in action:
action["parameters"] = {}
return action
except json.JSONDecodeError:
match = re.search(r"\{.*\}", text, re.DOTALL)
if match:
try:
return json.loads(match.group())
except Exception:
pass
return {"operation": "finish", "parameters": {}}
def format_action_str(operation: str, parameters: Dict) -> str:
if not parameters:
return operation
return f"{operation}({json.dumps(parameters)})"
# βββββββββββββββββββββββββββββββββββββββββ
# MAIN TASK RUNNER
# βββββββββββββββββββββββββββββββββββββββββ
def run_task(client: OpenAI, task_id: str) -> Dict[str, Any]:
from environment import DataCleaningEnv
from models import Action
env = DataCleaningEnv(task_id=task_id)
log_start(task=task_id, env=ENV_NAME, model=MODEL_NAME)
result = env.reset()
obs = result.observation.model_dump()
done = result.done
step = 0
rewards: List[float] = []
actions_taken: List[str] = []
score = 0.01
success = False
last_error: Optional[str] = None
try:
while not done and step < MAX_STEPS:
step += 1
last_error = None
# ββ Call LLM ββββββββββββββββββββββββββββββββββββββββββββ
try:
completion = client.chat.completions.create(
model=MODEL_NAME,
messages=[
{"role": "system", "content": SYSTEM_PROMPT},
{"role": "user", "content": build_user_prompt(obs)},
],
temperature=TEMPERATURE,
max_tokens=MAX_TOKENS,
stream=False,
)
response_text = completion.choices[0].message.content or ""
except Exception as e:
last_error = str(e)
response_text = '{"operation": "finish", "parameters": {}}'
# ββ Parse & step env βββββββββββββββββββββββββββββββββββββ
action_dict = parse_action(response_text)
action = Action(
operation=action_dict.get("operation", "finish"),
parameters=action_dict.get("parameters", {}),
)
actions_taken.append(action.operation)
try:
result = env.step(action)
obs = result.observation.model_dump()
done = result.done
reward = float(result.reward.total)
msg = obs.get("message", "")
if "failed" in msg.lower() or "error" in msg.lower():
last_error = msg
except Exception as e:
last_error = str(e)
reward = 0.01
done = True
rewards.append(reward)
# ββ [STEP] line ββββββββββββββββββββββββββββββββββββββββββ
log_step(
step=step,
action=format_action_str(action.operation, action.parameters),
reward=reward,
done=done,
error=last_error,
)
if done:
break
time.sleep(0.5)
# score = average reward across all steps, clamped strictly to (0,1)
score = _clamp(sum(rewards) / max(len(rewards), 1))
success = score >= SUCCESS_SCORE_THRESHOLD
finally:
# [END] always emitted β even on exception
log_end(success=success, steps=step, score=score, rewards=rewards)
return {
"task_id": task_id,
"final_score": score,
"steps": step,
"rewards": [_clamp(r) for r in rewards],
"actions": actions_taken,
"success": success,
}
# βββββββββββββββββββββββββββββββββββββββββ
# ENTRY POINT
# βββββββββββββββββββββββββββββββββββββββββ
def main():
if not HF_TOKEN:
print("ERROR: HF_TOKEN not set", file=sys.stderr)
sys.exit(1)
client = OpenAI(base_url=API_BASE_URL, api_key=HF_TOKEN)
all_results = []
for task_id in VALID_TASKS:
try:
result = run_task(client, task_id)
all_results.append(result)
except Exception as e:
print(f"[ERROR] Task {task_id} failed: {e}", file=sys.stderr)
log_end(success=False, steps=0, score=0.01, rewards=[0.01])
all_results.append({
"task_id": task_id,
"final_score": 0.01,
"success": False,
"error": str(e),
})
avg = sum(r.get("final_score", 0.01) for r in all_results) / len(all_results)
output = {
"model": MODEL_NAME,
"tasks": all_results,
"average_score": _clamp(avg),
}
with open("baseline_results.json", "w") as f:
json.dump(output, f, indent=2)
if __name__ == "__main__":
main() |