goatfahad commited on
Commit
f0f0b5f
·
verified ·
1 Parent(s): bd463df

Upload pipeline\01_ingest.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. pipeline//01_ingest.py +63 -0
pipeline//01_ingest.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Flow: ingest_corpus
2
+ Purpose: Download and cache all text corpora to data/raw/
3
+ """
4
+ from __future__ import annotations
5
+
6
+ import json
7
+ from pathlib import Path
8
+
9
+ from prefect import flow, task
10
+
11
+ DATA_DIR = Path(__file__).resolve().parent.parent / "data"
12
+
13
+
14
+ @task(name="load_all_corpus")
15
+ def _load_all_corpus() -> list[dict]:
16
+ from human_condition.corpus.builder import CorpusBuilder
17
+
18
+ builder = CorpusBuilder()
19
+ docs = builder.build()
20
+ return [
21
+ {
22
+ "source": d.source,
23
+ "title": d.title,
24
+ "text": d.text,
25
+ "metadata": d.metadata,
26
+ }
27
+ for d in docs
28
+ ]
29
+
30
+
31
+ @task(name="save_corpus_jsonl")
32
+ def _save_corpus(docs: list[dict]) -> str:
33
+ out = DATA_DIR / "raw" / "corpus.jsonl"
34
+ out.parent.mkdir(parents=True, exist_ok=True)
35
+ with open(out, "w", encoding="utf-8") as f:
36
+ for doc in docs:
37
+ f.write(json.dumps(doc, ensure_ascii=False) + "\n")
38
+ print(f"Saved {len(docs)} documents to {out}")
39
+ return str(out)
40
+
41
+
42
+ @task(name="validate_corpus")
43
+ def _validate_corpus(path: str) -> int:
44
+ with open(path, encoding="utf-8") as f:
45
+ lines = f.readlines()
46
+ count = len(lines)
47
+ if count < 1:
48
+ raise ValueError(f"Corpus has no records ({path})")
49
+ print(f"Corpus validated: {count} documents")
50
+ return count
51
+
52
+
53
+ @flow(name="ingest_corpus")
54
+ def ingest_corpus() -> int:
55
+ """Download and cache all source text corpora."""
56
+ docs = _load_all_corpus()
57
+ path = _save_corpus(docs)
58
+ count = _validate_corpus(path)
59
+ return count
60
+
61
+
62
+ if __name__ == "__main__":
63
+ ingest_corpus()