File size: 13,468 Bytes
d97776b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
Text Processing Pipeline for MicroGPT Training

Drag-and-drop zip/epub/txt files into inbox/ and run this script
to parse, clean, chunk, and split them into train.txt/val.txt.

Usage:
    python pipeline.py              # Process inbox and rebuild output
    python pipeline.py --rebuild    # Only rebuild train/val from existing parsed chunks
    python pipeline.py --stats      # Show corpus statistics
    python pipeline.py --push       # Rebuild and push to HuggingFace
"""

import argparse
import json
import logging
import random
import sys
from datetime import datetime
from pathlib import Path

import yaml

from cleaner import TextCleaner
from chunker import TextChunker
from parsers.txt_parser import parse_txt
from parsers.epub_parser import parse_epub
from parsers.zip_parser import parse_zip

SCRIPT_DIR = Path(__file__).resolve().parent

PARSERS = {
    ".txt": parse_txt,
    ".epub": parse_epub,
    ".zip": parse_zip,
}


class Pipeline:
    """Main text processing pipeline for MicroGPT training data."""

    def __init__(self, config_path: Path | None = None):
        if config_path is None:
            config_path = SCRIPT_DIR / "config.yaml"

        with open(config_path) as f:
            self.config = yaml.safe_load(f)

        # Resolve paths relative to script directory
        paths = self.config["paths"]
        self.inbox = SCRIPT_DIR / paths["inbox"]
        self.output = SCRIPT_DIR / paths["output"]
        self.archive = SCRIPT_DIR / paths["archive"]
        self.logs = SCRIPT_DIR / paths["logs"]
        self.parsed = SCRIPT_DIR / paths["parsed"]
        self.manifest_path = SCRIPT_DIR / "processed_files.json"

        # Create directories
        for d in [self.inbox, self.output, self.archive, self.logs, self.parsed]:
            d.mkdir(parents=True, exist_ok=True)

        # Initialize components
        self.cleaner = TextCleaner(self.config["cleaning"])
        self.chunker = TextChunker(self.config["chunking"])

        # Setup logging
        self._setup_logging()

        # Load manifest
        self.manifest = self._load_manifest()

    def _setup_logging(self):
        log_file = self.logs / f"pipeline_{datetime.now():%Y%m%d}.log"
        logging.basicConfig(
            level=logging.INFO,
            format="%(asctime)s [%(levelname)s] %(name)s: %(message)s",
            handlers=[
                logging.FileHandler(log_file, encoding="utf-8"),
                logging.StreamHandler(sys.stdout),
            ],
        )
        self.logger = logging.getLogger("pipeline")

    def _load_manifest(self) -> dict:
        if self.manifest_path.exists():
            return json.loads(self.manifest_path.read_text(encoding="utf-8"))
        return {"processed_files": []}

    def _save_manifest(self):
        self.manifest_path.write_text(
            json.dumps(self.manifest, indent=2, ensure_ascii=False),
            encoding="utf-8",
        )

    def process_file(self, filepath: Path) -> list[str]:
        """Process a single file through parse -> clean -> chunk.

        Args:
            filepath: Path to the input file.

        Returns:
            List of text chunks ready for training.
        """
        ext = filepath.suffix.lower()
        parser = PARSERS.get(ext)
        if parser is None:
            self.logger.warning("Unsupported file type: %s (%s)", filepath.name, ext)
            return []

        self.logger.info("Parsing %s ...", filepath.name)
        try:
            raw_text = parser(filepath)
        except Exception as e:
            self.logger.error("Parse error for %s: %s", filepath.name, e)
            return []

        if not raw_text.strip():
            self.logger.warning("No text extracted from %s", filepath.name)
            return []

        self.logger.info("  Raw text: %d chars", len(raw_text))

        # Clean
        cleaned = self.cleaner.clean(raw_text)
        self.logger.info("  Cleaned text: %d chars", len(cleaned))

        if not cleaned:
            self.logger.warning("  No text remaining after cleaning for %s", filepath.name)
            return []

        # Chunk
        chunks = self.chunker.chunk(cleaned)
        self.logger.info("  Chunks: %d (max %d chars each)", len(chunks), self.config["chunking"]["max_chars"])

        return chunks

    def process_inbox(self) -> int:
        """Process all files in the inbox directory.

        Returns:
            Total number of new chunks added.
        """
        files = sorted(
            f for f in self.inbox.iterdir()
            if f.is_file() and f.suffix.lower() in PARSERS and not f.name.startswith(".")
        )

        if not files:
            self.logger.info("No files to process in inbox/")
            return 0

        self.logger.info("Found %d file(s) in inbox/", len(files))
        total_chunks = 0

        for filepath in files:
            chunks = self.process_file(filepath)

            if chunks:
                # Save chunks to parsed/ directory
                slug = filepath.stem.replace(" ", "_").lower()
                parsed_file = self.parsed / f"{slug}.txt"

                # Handle name collisions
                counter = 1
                while parsed_file.exists():
                    parsed_file = self.parsed / f"{slug}_{counter}.txt"
                    counter += 1

                parsed_file.write_text("\n".join(chunks), encoding="utf-8")
                total_chunks += len(chunks)

                self.logger.info("  Saved %d chunks to %s", len(chunks), parsed_file.name)

                # Record in manifest
                self.manifest["processed_files"].append({
                    "source": filepath.name,
                    "parsed_file": parsed_file.name,
                    "chunks": len(chunks),
                    "timestamp": datetime.now().isoformat(),
                })

            # Move to archive
            archive_dest = self.archive / filepath.name
            counter = 1
            while archive_dest.exists():
                archive_dest = self.archive / f"{filepath.stem}_{counter}{filepath.suffix}"
                counter += 1

            filepath.rename(archive_dest)
            self.logger.info("  Archived %s -> %s", filepath.name, archive_dest.name)

        self._save_manifest()
        self.logger.info("Processed %d file(s), %d total new chunks", len(files), total_chunks)
        return total_chunks

    def rebuild_output(self) -> tuple[int, int]:
        """Rebuild train.txt and val.txt from all parsed chunks.

        Returns:
            Tuple of (train_count, val_count).
        """
        # Collect all chunks from parsed/ directory
        all_chunks = []
        parsed_files = sorted(self.parsed.glob("*.txt"))

        for pf in parsed_files:
            lines = [
                line.strip()
                for line in pf.read_text(encoding="utf-8").splitlines()
                if line.strip()
            ]
            all_chunks.extend(lines)
            self.logger.info("  Loaded %d chunks from %s", len(lines), pf.name)

        if not all_chunks:
            self.logger.warning("No chunks found in parsed/ directory")
            return 0, 0

        # Shuffle and split
        split_config = self.config["splitting"]
        rng = random.Random(split_config.get("seed", 42))
        if split_config.get("shuffle", True):
            rng.shuffle(all_chunks)

        train_ratio = split_config.get("train_ratio", 0.9)
        split_idx = int(len(all_chunks) * train_ratio)
        train_chunks = all_chunks[:split_idx]
        val_chunks = all_chunks[split_idx:]

        # Write output files
        train_path = self.output / "train.txt"
        val_path = self.output / "val.txt"
        train_path.write_text("\n".join(train_chunks), encoding="utf-8")
        val_path.write_text("\n".join(val_chunks), encoding="utf-8")

        self.logger.info(
            "Output: %d train chunks (%s), %d val chunks (%s)",
            len(train_chunks), train_path.name,
            len(val_chunks), val_path.name,
        )

        return len(train_chunks), len(val_chunks)

    def push_to_hub(self, repo_id: str | None = None) -> str:
        """Push train/val data to HuggingFace Hub as a dataset.

        Args:
            repo_id: HuggingFace repo (e.g. 'username/philosophy-corpus').
                     Falls back to config.yaml huggingface.repo_id.

        Returns:
            The repo URL.
        """
        from datasets import Dataset, DatasetDict

        if repo_id is None:
            hf_config = self.config.get("huggingface", {})
            repo_id = hf_config.get("repo_id", "")

        if not repo_id:
            raise ValueError(
                "No HuggingFace repo_id provided. Set it in config.yaml "
                "under huggingface.repo_id or pass --hf-repo."
            )

        train_path = self.output / "train.txt"
        val_path = self.output / "val.txt"

        if not train_path.exists() or not val_path.exists():
            raise FileNotFoundError(
                "train.txt/val.txt not found. Run the pipeline first."
            )

        self.logger.info("Preparing dataset for HuggingFace Hub...")

        def load_chunks(path: Path) -> list[dict]:
            lines = [
                l.strip()
                for l in path.read_text(encoding="utf-8").splitlines()
                if l.strip()
            ]
            return [{"text": line} for line in lines]

        train_data = load_chunks(train_path)
        val_data = load_chunks(val_path)

        ds = DatasetDict({
            "train": Dataset.from_list(train_data),
            "validation": Dataset.from_list(val_data),
        })

        self.logger.info(
            "Pushing to %s: %d train / %d val examples",
            repo_id, len(train_data), len(val_data),
        )

        ds.push_to_hub(repo_id)

        url = f"https://huggingface.co/datasets/{repo_id}"
        self.logger.info("Dataset pushed: %s", url)
        return url

    def stats(self):
        """Print corpus statistics."""
        parsed_files = sorted(self.parsed.glob("*.txt"))
        total_chunks = 0
        total_chars = 0

        print("\n=== Corpus Statistics ===\n")
        print(f"{'File':<40} {'Chunks':>8} {'Chars':>10}")
        print("-" * 60)

        for pf in parsed_files:
            lines = [l for l in pf.read_text(encoding="utf-8").splitlines() if l.strip()]
            chars = sum(len(l) for l in lines)
            total_chunks += len(lines)
            total_chars += chars
            print(f"{pf.name:<40} {len(lines):>8} {chars:>10}")

        print("-" * 60)
        print(f"{'TOTAL':<40} {total_chunks:>8} {total_chars:>10}")

        if total_chunks > 0:
            avg = total_chars / total_chunks
            print(f"\nAverage chunk length: {avg:.0f} chars")

        # Check output files
        train_path = self.output / "train.txt"
        val_path = self.output / "val.txt"
        if train_path.exists() and val_path.exists():
            train_lines = len([l for l in train_path.read_text(encoding="utf-8").splitlines() if l.strip()])
            val_lines = len([l for l in val_path.read_text(encoding="utf-8").splitlines() if l.strip()])
            print(f"\nOutput split: {train_lines} train / {val_lines} val")
        else:
            print("\nNo output files yet. Run pipeline to generate train.txt/val.txt")

        # Vocabulary check
        if train_path.exists():
            text = train_path.read_text(encoding="utf-8")
            vocab = sorted(set(text) - {"\n"})
            print(f"Vocabulary: {len(vocab)} chars -> {''.join(vocab)}")

        print()


def main():
    parser = argparse.ArgumentParser(description="MicroGPT Text Processing Pipeline")
    parser.add_argument("--rebuild", action="store_true", help="Only rebuild train/val from existing parsed chunks")
    parser.add_argument("--stats", action="store_true", help="Show corpus statistics")
    parser.add_argument("--push", action="store_true", help="Rebuild and push dataset to HuggingFace Hub")
    parser.add_argument("--hf-repo", type=str, default=None, help="HuggingFace repo ID (e.g. username/dataset)")
    parser.add_argument("--config", type=str, default=None, help="Path to config.yaml")
    args = parser.parse_args()

    config_path = Path(args.config) if args.config else None
    pipeline = Pipeline(config_path)

    if args.stats:
        pipeline.stats()
        return

    if args.push:
        print("Rebuilding output...")
        train_n, val_n = pipeline.rebuild_output()
        print(f"Output: {train_n} train / {val_n} val chunks")
        print("Pushing to HuggingFace Hub...")
        url = pipeline.push_to_hub(repo_id=args.hf_repo)
        print(f"Dataset available at: {url}")
        return

    if args.rebuild:
        print("Rebuilding output from existing parsed chunks...")
        train_n, val_n = pipeline.rebuild_output()
        print(f"Done: {train_n} train / {val_n} val chunks")
        return

    # Default: process inbox then rebuild
    print("Processing inbox...")
    new_chunks = pipeline.process_inbox()

    print("Rebuilding output...")
    train_n, val_n = pipeline.rebuild_output()

    print(f"\n{'='*50}")
    print(f"New chunks added: {new_chunks}")
    print(f"Total output: {train_n} train / {val_n} val chunks")
    print(f"Files: output/train.txt, output/val.txt")
    print(f"{'='*50}")


if __name__ == "__main__":
    main()