Spaces:
Sleeping
Sleeping
File size: 12,158 Bytes
8345e43 5205ffb 8345e43 5205ffb 8345e43 5205ffb 8345e43 | 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 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 | """Gradio web dashboard for manual testing of the DataClean-Env environment.
Provides interactive controls for task selection, action execution,
dataset inspection, quality issue review, and reward tracking.
"""
from __future__ import annotations
from typing import Any, Dict, List, Optional, Tuple
import gradio as gr
import pandas as pd
from dataclean_env.models import DataCleanAction
from dataclean_env.server.environment import DataCleanEnvironment
from dataclean_env.server.tasks import list_tasks
# ---------------------------------------------------------------------------
# Constants
# ---------------------------------------------------------------------------
ACTION_TYPES: list[str] = [
"fix_value",
"delete_row",
"fill_missing",
"standardize_format",
"merge_duplicates",
"flag_anomaly",
"split_column",
"rename_column",
"cast_type",
"mark_complete",
]
TASK_CHOICES: list[str] = [t["task_id"] for t in list_tasks()]
CUSTOM_CSS = """
@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&family=Fira+Sans:wght@400;500;600;700&display=swap');
:root {
--primary: #2563EB;
--cta: #F97316;
--bg: #F8FAFC;
--text: #1E293B;
}
body, .gradio-container {
font-family: 'Fira Sans', sans-serif !important;
background: var(--bg) !important;
color: var(--text) !important;
}
.dark body, .dark .gradio-container {
background: #0F172A !important;
color: #E2E8F0 !important;
}
code, .mono, .dataframe td, .dataframe th {
font-family: 'Fira Code', monospace !important;
}
.stat-card {
background: white;
border: 1px solid #E2E8F0;
border-radius: 8px;
padding: 12px 16px;
text-align: center;
}
.dark .stat-card {
background: #1E293B;
border-color: #334155;
}
.stat-card .label {
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 0.05em;
color: #64748B;
}
.stat-card .value {
font-size: 1.5rem;
font-weight: 700;
color: var(--primary);
font-family: 'Fira Code', monospace;
}
button.primary {
background: var(--primary) !important;
}
button.secondary, button.stop {
background: var(--cta) !important;
}
.reward-display {
font-family: 'Fira Code', monospace;
font-size: 1.25rem;
font-weight: 700;
padding: 8px 16px;
border-radius: 6px;
text-align: center;
}
"""
# ---------------------------------------------------------------------------
# Environment wrapper (single shared instance)
# ---------------------------------------------------------------------------
_env = DataCleanEnvironment()
_last_obs: Optional[Any] = None
_action_history: list[dict[str, str]] = []
def _obs_to_dataframe(obs: Any) -> pd.DataFrame:
"""Convert observation rows into a pandas DataFrame."""
if not obs.rows:
return pd.DataFrame()
return pd.DataFrame(obs.rows, columns=obs.columns)
def _issue_table(obs: Any) -> pd.DataFrame:
"""Build a DataFrame of quality issues grouped by type."""
if not obs.issue_groups:
return pd.DataFrame(columns=["Type", "Count", "Example"])
rows = []
for group in obs.issue_groups:
example = group.examples[0].description if group.examples else ""
rows.append({
"Type": group.issue_type,
"Count": group.count,
"Example": example,
})
return pd.DataFrame(rows)
def _history_table() -> pd.DataFrame:
"""Return last 10 actions as a DataFrame."""
if not _action_history:
return pd.DataFrame(columns=["#", "Action", "Status", "Message"])
recent = _action_history[-10:]
return pd.DataFrame(recent)
def _stat_html(label: str, value: Any) -> str:
return (
f'<div class="stat-card">'
f'<div class="label">{label}</div>'
f'<div class="value">{value}</div>'
f'</div>'
)
def _format_reward(reward: Any) -> str:
if reward is None:
return "---"
return f"{float(reward):.4f}"
# ---------------------------------------------------------------------------
# Callbacks
# ---------------------------------------------------------------------------
def reset_env(
task_id: str, seed: int
) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, str, str, str, str, str]:
"""Reset the environment with the selected task and seed."""
global _last_obs, _action_history
_action_history = []
obs = _env.reset(seed=int(seed), task_id=task_id)
_last_obs = obs
data_df = _obs_to_dataframe(obs)
issues_df = _issue_table(obs)
history_df = _history_table()
rows_html = _stat_html("Rows", obs.data_summary.row_count)
nulls_html = _stat_html("Nulls", obs.data_summary.null_count)
issues_html = _stat_html("Issues", obs.data_summary.issue_count)
score_html = _stat_html("Score", _format_reward(obs.reward))
reward_text = f"Reward: {_format_reward(obs.reward)} | Step: {obs.step_number}/{obs.max_steps}"
return data_df, issues_df, history_df, rows_html, nulls_html, issues_html, score_html, reward_text
def execute_action(
action_type: str,
row_id: str,
column: str,
value: str,
extra_json: str,
) -> Tuple[pd.DataFrame, pd.DataFrame, pd.DataFrame, str, str, str, str, str]:
"""Execute an action on the environment and return updated state."""
global _last_obs
if _last_obs is None:
raise gr.Error("Reset the environment first.")
if _last_obs.done:
raise gr.Error("Episode is done. Reset to start a new one.")
params: Dict[str, Any] = {}
if row_id.strip():
params["row_id"] = int(row_id.strip())
if column.strip():
params["column"] = column.strip()
if value.strip():
# Map the generic "value" form field to the correct param name
if action_type == "fix_value":
params["new_value"] = value.strip()
else:
params["value"] = value.strip()
if extra_json.strip():
import json
try:
extra = json.loads(extra_json.strip())
if isinstance(extra, dict):
# Normalize merge_duplicates aliases
if action_type == "merge_duplicates":
if "row_id_1" in extra and "row_id1" not in extra:
extra["row_id1"] = extra.pop("row_id_1")
if "row_id_2" in extra and "row_id2" not in extra:
extra["row_id2"] = extra.pop("row_id_2")
params.update(extra)
except json.JSONDecodeError:
raise gr.Error("Extra params must be valid JSON object.")
action = DataCleanAction(action_type=action_type, params=params)
obs = _env.step(action)
_last_obs = obs
status = obs.last_action_result.status if obs.last_action_result else "unknown"
message = obs.last_action_result.message if obs.last_action_result else ""
_action_history.append({
"#": str(len(_action_history) + 1),
"Action": action_type,
"Status": status,
"Message": message[:80],
})
data_df = _obs_to_dataframe(obs)
issues_df = _issue_table(obs)
history_df = _history_table()
rows_html = _stat_html("Rows", obs.data_summary.row_count)
nulls_html = _stat_html("Nulls", obs.data_summary.null_count)
issues_html = _stat_html("Issues", obs.data_summary.issue_count)
score_html = _stat_html("Score", _format_reward(obs.reward))
reward_text = f"Reward: {_format_reward(obs.reward)} | Step: {obs.step_number}/{obs.max_steps}"
return data_df, issues_df, history_df, rows_html, nulls_html, issues_html, score_html, reward_text
# ---------------------------------------------------------------------------
# Layout
# ---------------------------------------------------------------------------
def build_ui() -> gr.Blocks:
"""Construct and return the Gradio Blocks application."""
with gr.Blocks(
title="DataClean-Env Dashboard",
css=CUSTOM_CSS,
theme=gr.themes.Soft(
primary_hue="blue",
secondary_hue="orange",
font=["Fira Sans", "sans-serif"],
font_mono=["Fira Code", "monospace"],
),
) as app:
gr.Markdown("## DataClean-Env / Manual Testing Dashboard")
with gr.Row():
# ---- LEFT PANEL (30%) ----
with gr.Column(scale=3, min_width=280):
gr.Markdown("### Task Configuration")
task_dd = gr.Dropdown(
choices=TASK_CHOICES,
value=TASK_CHOICES[0] if TASK_CHOICES else "easy_contacts",
label="Task",
)
seed_input = gr.Number(value=42, label="Seed", precision=0)
reset_btn = gr.Button("Reset Environment", variant="primary")
gr.Markdown("### Data Summary")
with gr.Row():
rows_stat = gr.HTML(_stat_html("Rows", "---"))
nulls_stat = gr.HTML(_stat_html("Nulls", "---"))
with gr.Row():
issues_stat = gr.HTML(_stat_html("Issues", "---"))
score_stat = gr.HTML(_stat_html("Score", "---"))
gr.Markdown("### Execute Action")
action_dd = gr.Dropdown(
choices=ACTION_TYPES,
value=ACTION_TYPES[0],
label="Action Type",
)
row_id_input = gr.Textbox(label="row_id", placeholder="e.g. 3")
column_input = gr.Textbox(label="column", placeholder="e.g. email")
value_input = gr.Textbox(label="value / new_value", placeholder="e.g. john@example.com")
extra_input = gr.Textbox(
label="Extra params (JSON)",
placeholder='{"format_type": "date:YYYY-MM-DD"} or {"row_id1": 0, "row_id2": 3, "strategy": "merge_prefer_nonnull"}',
)
exec_btn = gr.Button("Execute", variant="secondary")
# ---- RIGHT PANEL (70%) ----
with gr.Column(scale=7):
reward_display = gr.Markdown(
value="Reward: --- | Step: 0/0",
elem_classes=["reward-display"],
)
gr.Markdown("### Dataset")
data_table = gr.Dataframe(
interactive=False,
wrap=True,
row_count=30,
)
with gr.Row():
with gr.Column(scale=1):
gr.Markdown("### Quality Issues")
issues_table = gr.Dataframe(
interactive=False,
wrap=True,
row_count=15,
)
with gr.Column(scale=1):
gr.Markdown("### Action History")
history_table = gr.Dataframe(
interactive=False,
wrap=True,
row_count=10,
)
# ---- Wiring ----
all_outputs = [
data_table,
issues_table,
history_table,
rows_stat,
nulls_stat,
issues_stat,
score_stat,
reward_display,
]
reset_btn.click(
fn=reset_env,
inputs=[task_dd, seed_input],
outputs=all_outputs,
)
exec_btn.click(
fn=execute_action,
inputs=[action_dd, row_id_input, column_input, value_input, extra_input],
outputs=all_outputs,
)
return app
# ---------------------------------------------------------------------------
# Entrypoint
# ---------------------------------------------------------------------------
def main() -> None:
"""Launch the dashboard."""
app = build_ui()
app.launch(
server_name="0.0.0.0",
server_port=7860,
share=False,
show_error=True,
)
if __name__ == "__main__":
main()
|