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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Agent Comparison β FSDS Cleaning Environment
|
| 3 |
+
=============================================
|
| 4 |
+
Benchmarks four agents on the held-out evaluation set and prints a
|
| 5 |
+
side-by-side comparison table.
|
| 6 |
+
|
| 7 |
+
Agents evaluated
|
| 8 |
+
----------------
|
| 9 |
+
1. RandomAgent β lower bound (uniform random actions)
|
| 10 |
+
2. HeuristicAgent β upper bound (scripted oracle policy)
|
| 11 |
+
3. SFT model β supervised fine-tuned checkpoint warm-start
|
| 12 |
+
4. GRPO model β RL-trained checkpoint (SFT β GRPO)
|
| 13 |
+
|
| 14 |
+
Run in Colab after both training files have completed:
|
| 15 |
+
- training_sft.py β ./data-cleaning-sft-final
|
| 16 |
+
- training_colab.py β ./data-cleaning-grpo-final
|
| 17 |
+
"""
|
| 18 |
+
|
| 19 |
+
# ββ Cell 1 βΈ Install (skip if already installed) ββββββββββββββββββββββ
|
| 20 |
+
# %%
|
| 21 |
+
# !pip install -q "openenv-core[core]>=0.2.1"
|
| 22 |
+
# !pip install -q "git+https://huggingface.co/spaces/israaaML/fsds_cleaning_env"
|
| 23 |
+
# !pip uninstall -y vllm
|
| 24 |
+
# !pip install -q unsloth
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
# ββ Cell 2 βΈ Imports & config βββββββββββββββββββββββββββββββββββββββββ
|
| 28 |
+
# %%
|
| 29 |
+
import json
|
| 30 |
+
from pathlib import Path
|
| 31 |
+
|
| 32 |
+
ENV_URL = "https://israaaML-fsds-cleaning-env.hf.space"
|
| 33 |
+
SFT_MODEL_PATH = "./data-cleaning-sft-final"
|
| 34 |
+
GRPO_MODEL_PATH = "./data-cleaning-grpo-final"
|
| 35 |
+
EPISODES_PER_TASK = 3 # increase for more reliable estimates (slower)
|
| 36 |
+
OUTPUT_FILE = "./results_comparison.json"
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ββ Cell 3 βΈ Connect to environment & sanity check ββββββββββββββββββββ
|
| 40 |
+
# %%
|
| 41 |
+
from fsds_cleaning_env import FSDSCleaningEnv
|
| 42 |
+
from fsds_cleaning_env.evaluation_tasks import EVAL_TASKS
|
| 43 |
+
from fsds_cleaning_env.evaluate_agent import run_evaluation
|
| 44 |
+
from fsds_cleaning_env.agents import RandomAgent, HeuristicAgent, LLMAgent
|
| 45 |
+
from fsds_cleaning_env.metrics import aggregate_metrics, compute_episode_metrics
|
| 46 |
+
|
| 47 |
+
with FSDSCleaningEnv(base_url=ENV_URL).sync() as env:
|
| 48 |
+
env.reset(task_id="ecommerce_mobile")
|
| 49 |
+
brief = env.call_tool("get_task_brief")
|
| 50 |
+
print(f"Connected to env. Task: {brief.get('title')}")
|
| 51 |
+
tasks_list = env.call_tool("list_tasks")
|
| 52 |
+
print(f"Available tasks: {[t['task_id'] for t in tasks_list.get('tasks', [])]}")
|
| 53 |
+
print(f"\nEval tasks ({len(EVAL_TASKS)} scenarios):")
|
| 54 |
+
for t in EVAL_TASKS:
|
| 55 |
+
print(f" {t.name} (task_id={t.task_id}, seed_index={t.eval_index})")
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
# ββ Cell 4 βΈ Run all agents βββββββββββββββββββββββββββββββββββββββββββ
|
| 59 |
+
# %%
|
| 60 |
+
results = {}
|
| 61 |
+
|
| 62 |
+
# ββ 4a. Random (lower bound) ββββββββββββββββββββββββββββββββββββββββββ
|
| 63 |
+
print("\n[1/4] Evaluating RandomAgent β¦")
|
| 64 |
+
results["random"] = run_evaluation(
|
| 65 |
+
RandomAgent(),
|
| 66 |
+
base_url=ENV_URL,
|
| 67 |
+
max_episodes_per_task=EPISODES_PER_TASK,
|
| 68 |
+
)
|
| 69 |
+
agg = results["random"]["aggregate"]
|
| 70 |
+
print(f" success={agg['success_rate']:.0%} return={agg['avg_return']:.4f} steps={agg['avg_steps']:.1f}")
|
| 71 |
+
|
| 72 |
+
# ββ 4b. Heuristic (upper bound) βββββββββββββββββββββββββββββββββββββββ
|
| 73 |
+
print("\n[2/4] Evaluating HeuristicAgent β¦")
|
| 74 |
+
results["heuristic"] = run_evaluation(
|
| 75 |
+
HeuristicAgent(),
|
| 76 |
+
base_url=ENV_URL,
|
| 77 |
+
max_episodes_per_task=EPISODES_PER_TASK,
|
| 78 |
+
)
|
| 79 |
+
agg = results["heuristic"]["aggregate"]
|
| 80 |
+
print(f" success={agg['success_rate']:.0%} return={agg['avg_return']:.4f} steps={agg['avg_steps']:.1f}")
|
| 81 |
+
|
| 82 |
+
# ββ 4c. SFT model βββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 83 |
+
print(f"\n[3/4] Evaluating SFT model ({SFT_MODEL_PATH}) β¦")
|
| 84 |
+
results["sft"] = run_evaluation(
|
| 85 |
+
LLMAgent(model_path=SFT_MODEL_PATH, temperature=0.0),
|
| 86 |
+
base_url=ENV_URL,
|
| 87 |
+
max_episodes_per_task=EPISODES_PER_TASK,
|
| 88 |
+
)
|
| 89 |
+
agg = results["sft"]["aggregate"]
|
| 90 |
+
print(f" success={agg['success_rate']:.0%} return={agg['avg_return']:.4f} steps={agg['avg_steps']:.1f}")
|
| 91 |
+
|
| 92 |
+
# ββ 4d. GRPO model ββββββββββββββββββββββββββββββββββββββββββββββββββββ
|
| 93 |
+
print(f"\n[4/4] Evaluating GRPO model ({GRPO_MODEL_PATH}) β¦")
|
| 94 |
+
results["grpo"] = run_evaluation(
|
| 95 |
+
LLMAgent(model_path=GRPO_MODEL_PATH, temperature=0.0),
|
| 96 |
+
base_url=ENV_URL,
|
| 97 |
+
max_episodes_per_task=EPISODES_PER_TASK,
|
| 98 |
+
)
|
| 99 |
+
agg = results["grpo"]["aggregate"]
|
| 100 |
+
print(f" success={agg['success_rate']:.0%} return={agg['avg_return']:.4f} steps={agg['avg_steps']:.1f}")
|
| 101 |
+
|
| 102 |
+
|
| 103 |
+
# ββ Cell 5 βΈ Comparison table βββββββββββββββββββββββββββββββββββββββββ
|
| 104 |
+
# %%
|
| 105 |
+
AGENTS = [
|
| 106 |
+
("Random", "random"),
|
| 107 |
+
("Heuristic", "heuristic"),
|
| 108 |
+
("SFT", "sft"),
|
| 109 |
+
("GRPO", "grpo"),
|
| 110 |
+
]
|
| 111 |
+
|
| 112 |
+
COL_W = 12
|
| 113 |
+
|
| 114 |
+
def _col(v, w=COL_W):
|
| 115 |
+
return str(v).center(w)
|
| 116 |
+
|
| 117 |
+
header = (
|
| 118 |
+
f"{'Agent':<14}"
|
| 119 |
+
+ _col("Success %")
|
| 120 |
+
+ _col("Avg Return")
|
| 121 |
+
+ _col("Avg Steps")
|
| 122 |
+
+ _col("Avg Invalid")
|
| 123 |
+
+ _col("Episodes")
|
| 124 |
+
)
|
| 125 |
+
sep = "-" * len(header)
|
| 126 |
+
|
| 127 |
+
print("\n" + sep)
|
| 128 |
+
print(" FSDS Cleaning Agent Benchmark")
|
| 129 |
+
print(sep)
|
| 130 |
+
print(header)
|
| 131 |
+
print(sep)
|
| 132 |
+
|
| 133 |
+
for label, key in AGENTS:
|
| 134 |
+
if key not in results:
|
| 135 |
+
continue
|
| 136 |
+
agg = results[key]["aggregate"]
|
| 137 |
+
print(
|
| 138 |
+
f"{label:<14}"
|
| 139 |
+
+ _col(f"{agg['success_rate']:.1%}")
|
| 140 |
+
+ _col(f"{agg['avg_return']:.4f}")
|
| 141 |
+
+ _col(f"{agg['avg_steps']:.1f}")
|
| 142 |
+
+ _col(f"{agg['avg_invalid_actions']:.2f}")
|
| 143 |
+
+ _col(agg["episodes"])
|
| 144 |
+
)
|
| 145 |
+
|
| 146 |
+
print(sep)
|
| 147 |
+
|
| 148 |
+
# Improvement of GRPO over SFT
|
| 149 |
+
if "sft" in results and "grpo" in results:
|
| 150 |
+
sft_sr = results["sft"]["aggregate"]["success_rate"]
|
| 151 |
+
grpo_sr = results["grpo"]["aggregate"]["success_rate"]
|
| 152 |
+
sft_ret = results["sft"]["aggregate"]["avg_return"]
|
| 153 |
+
grpo_ret = results["grpo"]["aggregate"]["avg_return"]
|
| 154 |
+
print(f"\nGRPO vs SFT β success rate delta : {grpo_sr - sft_sr:+.1%}")
|
| 155 |
+
print(f"GRPO vs SFT β avg return delta : {grpo_ret - sft_ret:+.4f}")
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
# ββ Cell 6 βΈ Per-task breakdown βββββββββββββββββββββββββββββββββββββββ
|
| 159 |
+
# %%
|
| 160 |
+
# Group per-episode results by task_id for a fine-grained breakdown.
|
| 161 |
+
from collections import defaultdict
|
| 162 |
+
|
| 163 |
+
print("\n=== Per-task success rates ===")
|
| 164 |
+
task_ids = sorted({ep["task_id"] for ep in results["heuristic"]["episodes"]})
|
| 165 |
+
|
| 166 |
+
col_labels = [label for label, _ in AGENTS if _ in results]
|
| 167 |
+
keys = [key for _, key in AGENTS if key in results]
|
| 168 |
+
|
| 169 |
+
# Header
|
| 170 |
+
print(f"\n{'Task':<30}" + "".join(f"{lbl:>12}" for lbl in col_labels))
|
| 171 |
+
print("-" * (30 + 12 * len(col_labels)))
|
| 172 |
+
|
| 173 |
+
for tid in task_ids:
|
| 174 |
+
row = f"{tid:<30}"
|
| 175 |
+
for key in keys:
|
| 176 |
+
eps = [e for e in results[key]["episodes"] if e["task_id"] == tid]
|
| 177 |
+
if not eps:
|
| 178 |
+
row += f"{'N/A':>12}"
|
| 179 |
+
else:
|
| 180 |
+
sr = sum(1 for e in eps if e.get("success", False)) / len(eps)
|
| 181 |
+
row += f"{sr:>11.0%} "
|
| 182 |
+
print(row)
|
| 183 |
+
|
| 184 |
+
|
| 185 |
+
# ββ Cell 7 βΈ Save results βββββββββββββββββββββββββββββββββββββββββββββ
|
| 186 |
+
# %%
|
| 187 |
+
Path(OUTPUT_FILE).write_text(json.dumps(results, indent=2))
|
| 188 |
+
print(f"\nFull results saved to {OUTPUT_FILE}")
|