File size: 15,278 Bytes
5c97387 97353a3 5015673 2f3537a 97353a3 d6558bb 5c97387 2f3537a 5c97387 a738515 2f3537a 5c97387 97353a3 2f3537a 0020ded 2f3537a 2fcce3e a9a8eca 2f3537a a9a8eca 2f3537a 2fcce3e 2f3537a cca5bb1 a9a8eca 2f3537a 2fcce3e 2f3537a 2fcce3e 2f3537a 2fcce3e 2f3537a cca5bb1 2f3537a 2fcce3e 97353a3 2f3537a 2fcce3e a738515 2fcce3e 5015673 2f3537a 175e3dd 2f3537a 5015673 2fcce3e 2f3537a 0020ded 2f3537a 2fcce3e 2f3537a 0020ded 2fcce3e 2f3537a 2fcce3e 2f3537a 2fcce3e 2f3537a 2fcce3e 2f3537a 2fcce3e 4425935 97353a3 5c97387 2fcce3e cca5bb1 a76c50f 5c97387 a76c50f 4425935 175e3dd 0020ded ff3a113 97353a3 cca5bb1 2fcce3e cca5bb1 a76c50f 2f3537a a76c50f 2fcce3e a76c50f 2f3537a 2fcce3e 2f3537a 2fcce3e 5c97387 a76c50f cca5bb1 4425935 5c97387 2f3537a a738515 2f3537a 0020ded 2f3537a a738515 0020ded a738515 2f3537a cca5bb1 0020ded 175e3dd 0020ded 175e3dd cca5bb1 175e3dd 0020ded cca5bb1 a738515 2f3537a 175e3dd cca5bb1 a738515 2f3537a cca5bb1 2f3537a a738515 2f3537a a738515 2f3537a a738515 2f3537a a738515 175e3dd a738515 2f3537a a738515 2f3537a a738515 4425935 2f3537a 175e3dd 917097f 6baf7e5 917097f 6baf7e5 917097f 2f3537a a738515 2f3537a cca5bb1 2f3537a a738515 2f3537a 917097f 2f3537a ff3a113 d6558bb 2f3537a 0020ded 2f3537a d6558bb 6baf7e5 2f3537a 6baf7e5 2f3537a 6baf7e5 2f3537a 175e3dd e1c75d3 2f3537a 6baf7e5 2fcce3e 2f3537a 6baf7e5 2f3537a 6baf7e5 a738515 6baf7e5 2f3537a 6baf7e5 cca5bb1 2f3537a cca5bb1 6baf7e5 2f3537a 175e3dd 0020ded 2f3537a cca5bb1 2f3537a 0020ded 2f3537a 6baf7e5 2f3537a | 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 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 | import json
import logging
import datasets
import math
import re
from datasets import load_dataset, get_dataset_config_names, get_dataset_infos
from huggingface_hub import HfApi, DatasetCard, DatasetCardData
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
class DatasetCommandCenter:
def __init__(self, token=None):
self.token = token
self.api = HfApi(token=token)
# ==========================================
# 1. METADATA & SCHEMA INSPECTION
# ==========================================
def get_dataset_metadata(self, dataset_id):
"""
Fetches Configs and Splits.
"""
configs = ['default']
splits = ['train', 'test', 'validation']
license_name = "unknown"
try:
# 1. Fetch Configs
try:
found_configs = get_dataset_config_names(dataset_id, token=self.token)
if found_configs:
configs = found_configs
except Exception:
pass
# 2. Fetch Metadata (Splits & License)
try:
selected = configs[0]
infos = get_dataset_infos(dataset_id, token=self.token)
info = None
if selected in infos:
info = infos[selected]
elif 'default' in infos:
info = infos['default']
elif infos:
info = list(infos.values())[0]
if info:
splits = list(info.splits.keys())
license_name = info.license or "unknown"
except Exception:
pass
return {
"status": "success",
"configs": configs,
"splits": splits,
"license_detected": license_name
}
except Exception as e:
return {"status": "error", "message": str(e)}
def get_splits_for_config(self, dataset_id, config_name):
try:
infos = get_dataset_infos(dataset_id, config_name=config_name, token=self.token)
if config_name in infos:
splits = list(infos[config_name].splits.keys())
elif len(infos) > 0:
splits = list(infos.values())[0].splits.keys()
else:
splits = ['train', 'test']
return {"status": "success", "splits": splits}
except:
return {"status": "success", "splits": ['train', 'test', 'validation']}
def _sanitize_for_json(self, obj):
"""
Recursively cleans data for JSON serialization.
"""
if isinstance(obj, float):
if math.isnan(obj) or math.isinf(obj):
return None
return obj
elif isinstance(obj, dict):
return {k: self._sanitize_for_json(v) for k, v in obj.items()}
elif isinstance(obj, list):
return [self._sanitize_for_json(v) for v in obj]
elif isinstance(obj, (str, int, bool, type(None))):
return obj
else:
return str(obj)
def _flatten_object(self, obj, parent_key='', sep='.'):
"""
Recursively finds keys for the UI dropdowns.
"""
items = {}
# Transparently parse JSON strings
if isinstance(obj, str):
s = obj.strip()
if (s.startswith('{') and s.endswith('}')) or (s.startswith('[') and s.endswith(']')):
try:
obj = json.loads(s)
except:
pass
if isinstance(obj, dict):
for k, v in obj.items():
new_key = f"{parent_key}{sep}{k}" if parent_key else k
items.update(self._flatten_object(v, new_key, sep=sep))
elif isinstance(obj, list):
new_key = f"{parent_key}" if parent_key else "list_content"
items[new_key] = "List"
else:
items[parent_key] = type(obj).__name__
return items
def inspect_dataset(self, dataset_id, config, split):
try:
conf = config if config != 'default' else None
ds_stream = load_dataset(dataset_id, name=conf, split=split, streaming=True, token=self.token)
sample_rows = []
available_paths = set()
schema_map = {}
for i, row in enumerate(ds_stream):
if i >= 10: break
# CRITICAL FIX: Force Materialization
row = dict(row)
# Clean row for UI
clean_row = self._sanitize_for_json(row)
sample_rows.append(clean_row)
# Schema Discovery
flattened = self._flatten_object(row)
available_paths.update(flattened.keys())
# List Mode Detection
for k, v in row.items():
if k not in schema_map:
schema_map[k] = {"type": "Object"}
val = v
if isinstance(val, str):
try: val = json.loads(val)
except: pass
if isinstance(val, list):
schema_map[k]["type"] = "List"
sorted_paths = sorted(list(available_paths))
schema_tree = {}
for path in sorted_paths:
root = path.split('.')[0]
if root not in schema_tree:
schema_tree[root] = []
schema_tree[root].append(path)
return {
"status": "success",
"samples": sample_rows,
"schema_tree": schema_tree,
"schema": schema_map,
"dataset_id": dataset_id
}
except Exception as e:
return {"status": "error", "message": str(e)}
# ==========================================
# 2. CORE EXTRACTION LOGIC
# ==========================================
def _get_value_by_path(self, obj, path):
"""
Retrieves a value from the row.
"""
if not path: return obj
# 1. Try Direct Access (Fixes "Simple Path" for columns with dots in name)
try:
if isinstance(obj, dict) and path in obj:
return obj[path]
except: pass
# 2. Try Dot Notation
keys = path.split('.')
current = obj
for i, key in enumerate(keys):
try:
# Use get() if possible, or key access
if isinstance(current, dict):
current = current.get(key)
else:
return None
except:
return None
if current is None: return None
# Lazy Parsing: Only parse string if we need to go deeper
is_last_key = (i == len(keys) - 1)
if not is_last_key and isinstance(current, str):
s = current.strip()
if (s.startswith('{') and s.endswith('}')) or (s.startswith('[') and s.endswith(']')):
try:
current = json.loads(s)
except:
return None
return current
def _extract_from_list_logic(self, row, source_col, filter_key, filter_val, target_path):
"""
FROM source_col FIND ITEM WHERE filter_key == filter_val EXTRACT target_path
"""
data = row.get(source_col)
if isinstance(data, str):
try:
data = json.loads(data)
except:
return None
if not isinstance(data, list):
return None
matched_item = None
for item in data:
# String comparison for safety
if str(item.get(filter_key, '')) == str(filter_val):
matched_item = item
break
if matched_item:
return self._get_value_by_path(matched_item, target_path)
return None
def _apply_projection(self, row, recipe):
new_row = {}
# Eval Context (requires explicit dict)
eval_context = row.copy()
eval_context['row'] = row
eval_context['json'] = json
eval_context['re'] = re
for col_def in recipe['columns']:
t_type = col_def.get('type', 'simple')
target_col = col_def['name']
try:
if t_type == 'simple':
new_row[target_col] = self._get_value_by_path(row, col_def['source'])
elif t_type == 'list_search':
new_row[target_col] = self._extract_from_list_logic(
row,
col_def['source'],
col_def['filter_key'],
col_def['filter_val'],
col_def['target_key']
)
elif t_type == 'python':
val = eval(col_def['expression'], {}, eval_context)
new_row[target_col] = val
except Exception as e:
raise ValueError(f"Column '{target_col}' failed: {str(e)}")
return new_row
# ==========================================
# 3. DOCUMENTATION (MODEL CARD)
# ==========================================
def _generate_card(self, source_id, target_id, recipe, license_name):
card_data = DatasetCardData(
language="en",
license=license_name,
tags=["dataset-command-center", "etl", "generated-dataset"],
base_model=source_id,
)
content = f"""
# {target_id.split('/')[-1]}
This dataset is a transformation of [{source_id}](https://huggingface.co/datasets/{source_id}).
It was generated using the **Hugging Face Dataset Command Center**.
## Transformation Recipe
The following operations were applied to the source data:
| Target Column | Operation Type | Source / Logic |
|---------------|----------------|----------------|
"""
for col in recipe['columns']:
c_type = col.get('type', 'simple')
c_name = col['name']
c_src = col.get('source', '-')
logic = "-"
if c_type == 'simple':
logic = f"Mapped from `{c_src}`"
elif c_type == 'list_search':
logic = f"Get `{col['target_key']}` where `{col['filter_key']} == {col['filter_val']}`"
elif c_type == 'python':
logic = f"Python: `{col.get('expression')}`"
content += f"| **{c_name}** | {c_type} | {logic} |\n"
if recipe.get('filter_rule'):
content += f"\n### Row Filtering\n**Filter Applied:** `{recipe['filter_rule']}`\n"
content += f"\n## Original License\nThis dataset inherits the license: `{license_name}` from the source."
card = DatasetCard.from_template(card_data, content=content)
return card
# ==========================================
# 4. EXECUTION
# ==========================================
def process_and_push(self, source_id, config, split, target_id, recipe, max_rows=None, new_license=None):
logger.info(f"Job started: {source_id} -> {target_id}")
conf = config if config != 'default' else None
def gen():
ds_stream = load_dataset(source_id, name=conf, split=split, streaming=True, token=self.token)
count = 0
for i, row in enumerate(ds_stream):
if max_rows and count >= int(max_rows):
break
# CRITICAL FIX: Force Materialization
row = dict(row)
# 1. Filter
if recipe.get('filter_rule'):
try:
ctx = row.copy()
ctx['row'] = row
ctx['json'] = json
ctx['re'] = re
if not eval(recipe['filter_rule'], {}, ctx):
continue
except Exception as e:
raise ValueError(f"Filter crashed on row {i}: {e}")
# 2. Projection
try:
yield self._apply_projection(row, recipe)
count += 1
except ValueError as ve:
raise ve
except Exception as e:
raise ValueError(f"Unexpected crash on row {i}: {e}")
try:
# 1. Process & Push
new_dataset = datasets.Dataset.from_generator(gen)
new_dataset.push_to_hub(target_id, token=self.token)
# 2. Card
try:
card = self._generate_card(source_id, target_id, recipe, new_license or "unknown")
card.push_to_hub(target_id, token=self.token)
except Exception as e:
logger.error(f"Failed to push Dataset Card: {e}")
return {"status": "success", "rows_processed": len(new_dataset)}
except Exception as e:
logger.error(f"Job Failed: {e}")
return {"status": "failed", "error": str(e)}
# ==========================================
# 5. PREVIEW
# ==========================================
def preview_transform(self, dataset_id, config, split, recipe):
conf = config if config != 'default' else None
try:
ds_stream = load_dataset(dataset_id, name=conf, split=split, streaming=True, token=self.token)
processed = []
for i, row in enumerate(ds_stream):
if len(processed) >= 5: break
# CRITICAL FIX: Force Materialization
row = dict(row)
# Check Filter
passed = True
if recipe.get('filter_rule'):
try:
ctx = row.copy()
ctx['row'] = row
ctx['json'] = json
ctx['re'] = re
if not eval(recipe['filter_rule'], {}, ctx):
passed = False
except:
passed = False
if passed:
try:
new_row = self._apply_projection(row, recipe)
# Sanitize to prevent JSON crashes
clean_new_row = self._sanitize_for_json(new_row)
processed.append(clean_new_row)
except Exception as e:
processed.append({"_preview_error": f"Error: {str(e)}"})
return processed
except Exception as e:
raise e |