Taimwe commited on
Commit
884b7d7
·
verified ·
1 Parent(s): a01c1d1

JSONL fallback for datasets without Json feature type

Browse files
Files changed (1) hide show
  1. train_securecoder.py +67 -0
train_securecoder.py CHANGED
@@ -399,6 +399,66 @@ def _messages_from_any(row: dict, kind: str) -> tuple[list[dict], list[dict]]:
399
  # --------------------------------------------------------------------------
400
  # Loading, rendering, dataset construction
401
  # --------------------------------------------------------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
402
  def load_source(src: Source, token: str | None, progress: bool = False) -> list[dict]:
403
  """Pull up to ``limit`` rows from one Hub dataset, streaming so we never
404
  download more than we need.
@@ -448,6 +508,13 @@ def load_source(src: Source, token: str | None, progress: bool = False) -> list[
448
  except Exception as exc: # noqa: BLE001 - try the next candidate
449
  last_exc = exc
450
 
 
 
 
 
 
 
 
451
  raise last_exc if last_exc else RuntimeError(f"could not load {src.repo}")
452
 
453
 
 
399
  # --------------------------------------------------------------------------
400
  # Loading, rendering, dataset construction
401
  # --------------------------------------------------------------------------
402
+ def _read_jsonl(path: str, limit: int) -> list[dict]:
403
+ """Parse a JSON-lines file, stopping at ``limit`` good rows."""
404
+ rows: list[dict] = []
405
+ with open(path, "r", encoding="utf-8") as fh:
406
+ for line in fh:
407
+ line = line.strip()
408
+ if not line:
409
+ continue
410
+ try:
411
+ parsed = json.loads(line)
412
+ except json.JSONDecodeError:
413
+ continue
414
+ if isinstance(parsed, dict):
415
+ rows.append(parsed)
416
+ if len(rows) >= limit:
417
+ break
418
+ return rows
419
+
420
+
421
+ def _jsonl_fallback(src: Source, token: str | None, limit: int) -> list[dict]:
422
+ """Read ``data/*.jsonl`` directly when ``load_dataset`` cannot.
423
+
424
+ Some repos ship only JSON-lines and no parquet. ``datasets`` then infers a
425
+ schema from the JSON, any nested object becomes a ``Json`` feature, and
426
+ ``datasets`` <4.4 - which unsloth pins via ``datasets>=3.4.1,<4.4.0`` -
427
+ has no such feature type, so ``load_dataset`` raises before reading a row.
428
+ Pulling the file and parsing it ourselves skips schema inference entirely.
429
+ """
430
+ from huggingface_hub import hf_hub_download, list_repo_files
431
+
432
+ try:
433
+ files = list_repo_files(src.repo, repo_type="dataset", token=token)
434
+ except Exception as exc: # noqa: BLE001 - caller reports the original error
435
+ log.debug(" %s: list_repo_files failed (%s)", source_name(src), exc)
436
+ return []
437
+
438
+ names = [f for f in files if f.endswith(".jsonl")]
439
+ preferred = [
440
+ f"data/{src.split}.jsonl",
441
+ f"{src.split}.jsonl",
442
+ "data/train.jsonl",
443
+ "train.jsonl",
444
+ ]
445
+ ordered = [f for f in preferred if f in names]
446
+ ordered += [f for f in sorted(names) if f not in ordered]
447
+
448
+ for name in ordered:
449
+ try:
450
+ path = hf_hub_download(src.repo, name, repo_type="dataset", token=token)
451
+ rows = _read_jsonl(path, limit)
452
+ except Exception as exc: # noqa: BLE001 - try the next candidate
453
+ log.debug(" %s: %s failed (%s)", source_name(src), name, exc)
454
+ continue
455
+ if rows:
456
+ log.info(" %s: jsonl fallback read %d rows from %s",
457
+ source_name(src), len(rows), name)
458
+ return rows
459
+ return []
460
+
461
+
462
  def load_source(src: Source, token: str | None, progress: bool = False) -> list[dict]:
463
  """Pull up to ``limit`` rows from one Hub dataset, streaming so we never
464
  download more than we need.
 
508
  except Exception as exc: # noqa: BLE001 - try the next candidate
509
  last_exc = exc
510
 
511
+ # No config/split worked. If the repo is JSON-lines, `datasets` may simply
512
+ # be unable to express its schema (see `_jsonl_fallback`), so read the file
513
+ # directly instead of giving up on the source.
514
+ rows = _jsonl_fallback(src, token, src.limit)
515
+ if rows:
516
+ return rows
517
+
518
  raise last_exc if last_exc else RuntimeError(f"could not load {src.repo}")
519
 
520