|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +Script to initialize the embeddings tracker dataset by processing docs from hf-doc-build/doc-build. |
| 4 | +This reconstructs document IDs deterministically without needing Meilisearch. |
| 5 | +
|
| 6 | +Usage: |
| 7 | + uv run python migrations/init_embeddings_tracker.py --hf_token <token> |
| 8 | +
|
| 9 | +The dataset will be created at: hf-doc-build/doc-builder-embeddings-tracker |
| 10 | +""" |
| 11 | + |
| 12 | +import argparse |
| 13 | +import os |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +from datasets import Dataset |
| 17 | +from tqdm import tqdm |
| 18 | + |
| 19 | +from doc_builder.meilisearch_helper import generate_doc_id |
| 20 | +from doc_builder.process_hf_docs import process_all_libraries |
| 21 | + |
| 22 | +# Dataset repository for tracking embeddings |
| 23 | +EMBEDDINGS_TRACKER_REPO = "hf-doc-build/doc-builder-embeddings-tracker" |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + parser = argparse.ArgumentParser( |
| 28 | + description="Initialize embeddings tracker dataset from hf-doc-build/doc-build" |
| 29 | + ) |
| 30 | + parser.add_argument( |
| 31 | + "--hf_token", |
| 32 | + type=str, |
| 33 | + required=False, |
| 34 | + help="HuggingFace token with write access (or set HF_TOKEN env var)", |
| 35 | + ) |
| 36 | + parser.add_argument( |
| 37 | + "--repo", |
| 38 | + type=str, |
| 39 | + default=EMBEDDINGS_TRACKER_REPO, |
| 40 | + help=f"Dataset repository ID (default: {EMBEDDINGS_TRACKER_REPO})", |
| 41 | + ) |
| 42 | + parser.add_argument( |
| 43 | + "--output-dir", |
| 44 | + type=str, |
| 45 | + default=None, |
| 46 | + help="Directory for downloaded/extracted files (uses temp dir if not specified)", |
| 47 | + ) |
| 48 | + args = parser.parse_args() |
| 49 | + |
| 50 | + hf_token = args.hf_token or os.environ.get("HF_TOKEN") |
| 51 | + |
| 52 | + if not hf_token: |
| 53 | + raise ValueError("HF_TOKEN is required. Set via --hf_token or HF_TOKEN env var.") |
| 54 | + |
| 55 | + # Process all libraries from hf-doc-build/doc-build (same as populate-search-engine) |
| 56 | + print("=" * 80) |
| 57 | + print("DOWNLOADING AND PROCESSING DOCS FROM hf-doc-build/doc-build") |
| 58 | + print("=" * 80) |
| 59 | + |
| 60 | + results = process_all_libraries( |
| 61 | + output_dir=Path(args.output_dir) if args.output_dir else None, |
| 62 | + excerpts_max_length=2000, # Same as default in populate-search-engine |
| 63 | + ) |
| 64 | + |
| 65 | + # Generate document IDs for all chunks |
| 66 | + print("\n" + "=" * 80) |
| 67 | + print("GENERATING DOCUMENT IDS") |
| 68 | + print("=" * 80) |
| 69 | + |
| 70 | + entries = [] |
| 71 | + for library_name, chunks in tqdm(results.items(), desc="Processing libraries"): |
| 72 | + for chunk in chunks: |
| 73 | + doc_id = generate_doc_id(chunk.package_name, chunk.page, chunk.text) |
| 74 | + entries.append( |
| 75 | + { |
| 76 | + "id": doc_id, |
| 77 | + "library": chunk.package_name, |
| 78 | + "source_page_url": chunk.source_page_url, |
| 79 | + } |
| 80 | + ) |
| 81 | + |
| 82 | + print(f"\nTotal document IDs generated: {len(entries)}") |
| 83 | + |
| 84 | + # Deduplicate by ID (in case of any duplicates) |
| 85 | + seen_ids = set() |
| 86 | + unique_entries = [] |
| 87 | + for entry in entries: |
| 88 | + if entry["id"] not in seen_ids: |
| 89 | + seen_ids.add(entry["id"]) |
| 90 | + unique_entries.append(entry) |
| 91 | + |
| 92 | + print(f"Unique document IDs: {len(unique_entries)}") |
| 93 | + |
| 94 | + # Create and push dataset |
| 95 | + print("\n" + "=" * 80) |
| 96 | + print("PUSHING TO HUGGINGFACE") |
| 97 | + print("=" * 80) |
| 98 | + |
| 99 | + dataset = Dataset.from_list(unique_entries) |
| 100 | + print(f"Created dataset with {len(dataset)} entries") |
| 101 | + print(f"Columns: {dataset.column_names}") |
| 102 | + |
| 103 | + print(f"Pushing to {args.repo}...") |
| 104 | + dataset.push_to_hub(args.repo, token=hf_token, private=False) |
| 105 | + |
| 106 | + print("\n" + "=" * 80) |
| 107 | + print("✅ MIGRATION COMPLETE") |
| 108 | + print("=" * 80) |
| 109 | + print(f"Dataset created at: https://huggingface.co/datasets/{args.repo}") |
| 110 | + print(f"Total documents tracked: {len(unique_entries)}") |
| 111 | + |
| 112 | + |
| 113 | +if __name__ == "__main__": |
| 114 | + main() |
0 commit comments