Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(data): accept leading UTF-8 BOMs in text datasets (#1954)
Nurkhan Esenbekf0888fedd651
diff --git a/changelog.d/0.76.0/1954.fixed.md b/changelog.d/0.76.0/1954.fixed.mdnew file mode 100644index 00000000..467adb6a--- /dev/null+++ b/changelog.d/0.76.0/1954.fixed.md@@ -0,0 +1 @@+- Accept a leading UTF-8 BOM in local JSON, CSV and TXT datasets, matching JSONL, so Excel/Windows exports parse and detect their format correctly while embedded U+FEFF characters are preserved. Thanks to @VincentTLe for the report and proposed fix. (#1922 by @kok-o in #1954)diff --git a/docs/data.md b/docs/data.mdindex aabeeb03..001f7690 100644--- a/docs/data.md+++ b/docs/data.md@@ -687,6 +687,10 @@ same object check before displaying a pair. Rejected row contents are not printeSoup supports these formats (auto-detected). Files can be JSONL, JSON, CSV, Parquet, or TXT.+Local JSONL, JSON, CSV and TXT files accept UTF-8 with or without a leading BOM,+including Excel's CSV UTF-8 exports. The signature is removed before parsing;+U+FEFF characters inside field values or document text are preserved.+**Auto-detection.** It reads the first 100 rows, not just the first. Each row resolvesto the richest format its keys fit. If every row agrees, that format is used. A filethat mixes `chatml` and `tool-calling` rows loads as `tool-calling` (the plain rowsdiff --git a/src/soup_cli/data/loader.py b/src/soup_cli/data/loader.pyindex c0597ab2..f189b983 100644--- a/src/soup_cli/data/loader.py+++ b/src/soup_cli/data/loader.py@@ -138,7 +138,7 @@ def _load_jsonl(def _load_json(path: Path, *, issues: list[str] | None = None, skip_non_object: bool = True,) -> list[dict]:- with open(path, encoding="utf-8") as f:+ with open(path, encoding="utf-8-sig") as f:raw = json.load(f)if isinstance(raw, list):if not skip_non_object:@@ -159,7 +159,7 @@ def _load_json(def _load_csv(path: Path) -> list[dict]:import csv- with open(path, encoding="utf-8") as f:+ with open(path, encoding="utf-8-sig") as f:reader = csv.DictReader(f)return list(reader)@@ -200,7 +200,7 @@ def _load_txt(path: Path) -> list[dict]:f"[yellow]Warning: large text file ({file_size / 1024 / 1024:.0f} MB). "f"Consider splitting into smaller files or using JSONL format.[/]")- with open(path, encoding="utf-8") as f:+ with open(path, encoding="utf-8-sig") as f:content = f.read()# Split by double newline (paragraph/document separator) or treat each line as a docdiff --git a/tests/test_loader.py b/tests/test_loader.pyindex 8b8750a2..e1651393 100644--- a/tests/test_loader.py+++ b/tests/test_loader.py@@ -46,6 +46,50 @@ def test_load_csv(tmp_path: Path):assert data[1]["output"] == "ML is..."[email protected]("suffix", ["json", "jsonl", "csv", "txt"])[email protected]("bom", [False, True], ids=["plain", "bom"])+def test_utf8_bom_datasets_match_plain_utf8(tmp_path: Path, suffix: str, bom: bool) -> None:+ """A BOM is an encoding signature, not a field name or document character."""+ rows = [{"instruction": "Сұрақ", "input": "", "output": "Жауап"},+ {"instruction": "Question", "input": "", "output": "Answer"}]+ bodies = {+ "json": json.dumps(rows, ensure_ascii=False),+ "jsonl": "\n".join(json.dumps(row, ensure_ascii=False) for row in rows) + "\n",+ "csv": "instruction,input,output\r\nСұрақ,,Жауап\r\nQuestion,,Answer\r\n",+ "txt": "Сұрақ\n\nQuestion\n",+ }+ expected = [{"text": "Сұрақ"}, {"text": "Question"}] if suffix == "txt" else rows+ path = tmp_path / f"data.{suffix}"+ path.write_bytes((b"\xef\xbb\xbf" if bom else b"") + bodies[suffix].encode("utf-8"))+ assert load_raw_data(path) == expected+++def test_excel_utf8_bom_csv_is_detected_as_alpaca(tmp_path: Path) -> None:+ from soup_cli.data.formats import detect_format, format_to_messages++ path = tmp_path / "excel.csv"+ path.write_bytes(b"\xef\xbb\xbf" + (+ 'instruction,input,output\r\n"Question, with comma",context,Answer\r\n'+ ).encode("utf-8"))+ rows = load_raw_data(path)+ assert detect_format(rows) == "alpaca"+ assert format_to_messages(rows[0], "alpaca") == {"messages": [+ {"role": "user", "content": "Question, with comma\ncontext"},+ {"role": "assistant", "content": "Answer"},+ ]}++[email protected]("suffix, body", [+ ("json", '[{"text":"inner\ufeffmarker"}]'),+ ("csv", "text\r\ninner\ufeffmarker\r\n"),+ ("txt", "inner\ufeffmarker\n"),+])+def test_bom_codec_preserves_embedded_character(tmp_path: Path, suffix: str, body: str) -> None:+ path = tmp_path / f"data.{suffix}"+ path.write_bytes(b"\xef\xbb\xbf" + body.encode("utf-8"))+ assert load_raw_data(path) == [{"text": "inner\ufeffmarker"}]++def test_load_nonexistent_file(tmp_path: Path):"""Loading nonexistent file should raise FileNotFoundError."""with pytest.raises(FileNotFoundError):