Download runtime/graphkit/parquet.py from VISAI-AI/icj-citation-graph: direct link, hf CLI and curl.
- Browser
- Download file 1.9 kB
-
https://huggingface.co/datasets/VISAI-AI/icj-citation-graph/resolve/main/runtime/graphkit/parquet.py
- Command line
-
hf download hf://datasets/VISAI-AI/icj-citation-graph/runtime/graphkit/parquet.py
-
curl -L -o parquet.py https://huggingface.co/datasets/VISAI-AI/icj-citation-graph/resolve/main/runtime/graphkit/parquet.py
1.9 kB
| import json | |
| from pathlib import Path | |
| import pyarrow.parquet as pq | |
| from ._state import fingerprint | |
| from ._vectors import validate_vectors | |
| from .config import PRETRAINED_DIMENSION, PRETRAINED_MODEL, TARGETS | |
| def load_vectors(graph, targets, directory, batch_size, limit): | |
| root = Path(directory) | |
| manifest = json.loads((root / 'manifest.json').read_text()) | |
| source = fingerprint({'release': manifest['source_id'], 'model': PRETRAINED_MODEL, 'dimension': PRETRAINED_DIMENSION, 'storage': 'float16'}) | |
| counts = {} | |
| for name in targets: | |
| target = TARGETS[name] | |
| graph.check_source(target, target.property, source) | |
| paths = sorted((root / 'parquet').glob(target.files)) | |
| if not paths: | |
| raise FileNotFoundError(target.files) | |
| written = 0 | |
| for path in paths: | |
| for batch in pq.ParquetFile(path).iter_batches(batch_size=batch_size, columns=[target.key, target.column]): | |
| items = [row for row in batch.to_pylist() if row[target.column] is not None] | |
| missing = graph.missing(target, [row[target.key] for row in items], target.property) | |
| pending = [row for row in items if row[target.key] in missing] | |
| if limit: | |
| pending = pending[:limit - written] | |
| if pending: | |
| vectors = validate_vectors([row[target.column] for row in pending], PRETRAINED_DIMENSION) | |
| graph.write(target, target.property, source, [{'key': row[target.key], 'vector': vector} for row, vector in zip(pending, vectors)]) | |
| written += len(pending) | |
| if limit and written >= limit: | |
| break | |
| if limit and written >= limit: | |
| break | |
| counts[name] = written | |
| print(json.dumps({'target': name, 'vectors_added': written}), flush=True) | |
| return counts | |