Coding-With-Bashir commited on
Commit
6becb96
·
verified ·
1 Parent(s): b3e98b4

Upload .\scripts\test_pipeline.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. .//scripts//test_pipeline.py +141 -0
.//scripts//test_pipeline.py ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Small end-to-end test for BwengeAi pipeline."""
2
+
3
+ import json
4
+ import logging
5
+ import sys
6
+ import shutil
7
+ from pathlib import Path
8
+
9
+ import yaml
10
+
11
+ sys.path.insert(0, str(Path(__file__).parent.parent / "src"))
12
+
13
+ from data_collection.huggingface_collector import HuggingfaceCollector
14
+ from data_collection.data_processor import DataProcessor
15
+
16
+ logging.basicConfig(
17
+ level=logging.INFO,
18
+ format="%(asctime)s - %(name)s - %(levelname)s - %(message)s",
19
+ )
20
+ logger = logging.getLogger(__name__)
21
+
22
+
23
+ PROJECT_ROOT = Path(__file__).resolve().parent.parent
24
+
25
+ def load_config(config_path: str = None) -> dict:
26
+ if config_path is None:
27
+ config_path = str(PROJECT_ROOT / "configs/test_small.yaml")
28
+ with open(config_path, "r", encoding="utf-8") as f:
29
+ return yaml.safe_load(f)
30
+
31
+
32
+ def main():
33
+ config = load_config()
34
+ data_config = config.get("data", {})
35
+
36
+ raw_dir = str(PROJECT_ROOT / data_config.get("raw_dir", "data/raw_test"))
37
+ processed_dir = str(PROJECT_ROOT / data_config.get("processed_dir", "data/processed_test"))
38
+
39
+ Path(raw_dir).mkdir(parents=True, exist_ok=True)
40
+ Path(processed_dir).mkdir(parents=True, exist_ok=True)
41
+
42
+ logger.info("=" * 60)
43
+ logger.info("STEP 1: Collect 4 small HuggingFace datasets")
44
+ logger.info("=" * 60)
45
+
46
+ hf_collector = HuggingfaceCollector(
47
+ output_dir=f"{raw_dir}/huggingface",
48
+ config=data_config,
49
+ )
50
+ hf_results = hf_collector.collect_all()
51
+
52
+ successful = sum(1 for r in hf_results if r.get("status") == "success")
53
+ failed = sum(1 for r in hf_results if r.get("status") == "failed")
54
+ total_rows = sum(r.get("rows", 0) for r in hf_results if r.get("status") == "success")
55
+
56
+ logger.info(f"Collection: {successful} succeeded, {failed} failed, {total_rows} total rows")
57
+
58
+ logger.info("=" * 60)
59
+ logger.info("STEP 2: Process and clean data")
60
+ logger.info("=" * 60)
61
+
62
+ processor = DataProcessor(
63
+ raw_dir=raw_dir,
64
+ processed_dir=processed_dir,
65
+ )
66
+
67
+ import shutil as sh
68
+ seen_names = set()
69
+ for jsonl_file in Path(raw_dir).rglob("*.jsonl"):
70
+ if jsonl_file.parent == Path(raw_dir):
71
+ continue
72
+ parent_dir = jsonl_file.parent.name
73
+ prefixed_name = f"{parent_dir}_{jsonl_file.name}"
74
+ dest = Path(raw_dir) / prefixed_name
75
+ if prefixed_name not in seen_names:
76
+ sh.copy2(jsonl_file, dest)
77
+ seen_names.add(prefixed_name)
78
+
79
+ processing_summary = processor.process_all()
80
+
81
+ logger.info("=" * 60)
82
+ logger.info("TEST RESULTS")
83
+ logger.info("=" * 60)
84
+
85
+ raw_path = Path(raw_dir)
86
+ processed_path = Path(processed_dir)
87
+
88
+ raw_files = list(raw_path.rglob("*.jsonl"))
89
+ raw_total_rows = 0
90
+ for f in raw_files:
91
+ with open(f, encoding="utf-8") as fh:
92
+ raw_total_rows += sum(1 for _ in fh)
93
+
94
+ logger.info(f"Raw files: {len(raw_files)}")
95
+ logger.info(f"Raw total rows: {raw_total_rows:,}")
96
+
97
+ training_path = processed_path / "training_data.jsonl"
98
+ instruction_path = processed_path / "instruction_data.jsonl"
99
+ chat_path = processed_path / "chat_data.jsonl"
100
+ summary_path = processed_path / "processing_summary.json"
101
+
102
+ for p in [training_path, instruction_path, chat_path, summary_path]:
103
+ exists = p.exists()
104
+ size = p.stat().st_size if exists else 0
105
+ logger.info(f" {p.name}: {'EXISTS' if exists else 'MISSING'} ({size:,} bytes)")
106
+
107
+ if summary_path.exists():
108
+ with open(summary_path, encoding="utf-8") as f:
109
+ summary = json.load(f)
110
+ logger.info(f"\nProcessing Summary:")
111
+ logger.info(f" Total loaded: {summary.get('total_loaded', 0):,}")
112
+ logger.info(f" After lang filter: {summary.get('total_after_lang_filter', 0):,}")
113
+ logger.info(f" After dedup: {summary.get('total_after_dedup', 0):,}")
114
+ logger.info(f" Total tokens: {summary.get('total_tokens', 0):,}")
115
+ logger.info(f" Training samples: {summary.get('total_training_samples', 0):,}")
116
+ logger.info(f" Instruction samples: {summary.get('total_instruction', 0):,}")
117
+ logger.info(f" Chat samples: {summary.get('total_chat', 0):,}")
118
+
119
+ if summary.get('per_file_stats'):
120
+ logger.info(f"\nPer-file stats:")
121
+ for fs in summary['per_file_stats']:
122
+ logger.info(f" {fs['file']}: loaded={fs['loaded']}, kept={fs['kept']}, tokens={fs['tokens']}")
123
+
124
+ all_ok = (
125
+ training_path.exists()
126
+ and summary_path.exists()
127
+ and raw_total_rows > 0
128
+ )
129
+
130
+ logger.info("\n" + "=" * 60)
131
+ if all_ok:
132
+ logger.info("TEST PASSED - Pipeline works end-to-end!")
133
+ else:
134
+ logger.info("TEST FAILED - Check errors above")
135
+ logger.info("=" * 60)
136
+
137
+ return 0 if all_ok else 1
138
+
139
+
140
+ if __name__ == "__main__":
141
+ sys.exit(main())