-
Notifications
You must be signed in to change notification settings - Fork 309
QA eval pipeline for retrieval #1754
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
KyleZheng1284
wants to merge
5
commits into
NVIDIA:main
Choose a base branch
from
KyleZheng1284:feature/qa-harness-fullpage-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| """Build a page-level markdown index from extracted Parquet files. | ||
|
|
||
| Loads extraction results saved by extract_bo767_parquet.py, groups records | ||
| by (source document, page number), renders each page via to_markdown_by_page, | ||
| and writes a JSON index mapping source_id -> page_number -> markdown. | ||
|
|
||
| Usage: | ||
| python build_page_markdown_index.py | ||
|
|
||
| Env vars: | ||
| PARQUET_DIR Directory containing Parquet files (default: data/bo767_extracted) | ||
| OUTPUT_FILE Where to write the JSON index (default: data/bo767_page_markdown.json) | ||
| """ | ||
|
|
||
| import json | ||
| import os | ||
| import sys | ||
| import time | ||
| from collections import defaultdict | ||
| from pathlib import Path | ||
|
|
||
| import numpy as np | ||
|
|
||
| _HERE = os.path.dirname(os.path.abspath(__file__)) | ||
|
|
||
|
|
||
| def main() -> int: | ||
| parquet_dir = os.environ.get( | ||
| "PARQUET_DIR", | ||
| os.path.join(_HERE, "data", "bo767_extracted"), | ||
| ) | ||
| output_file = os.environ.get( | ||
| "OUTPUT_FILE", | ||
| os.path.join(_HERE, "data", "bo767_page_markdown.json"), | ||
| ) | ||
|
|
||
| print("=" * 60) | ||
| print("Build Page Markdown Index") | ||
| print("=" * 60) | ||
| print(f"Parquet dir: {parquet_dir}") | ||
| print(f"Output file: {output_file}") | ||
|
|
||
| if not os.path.isdir(parquet_dir): | ||
| print(f"ERROR: Parquet directory not found: {parquet_dir}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| import pandas as pd | ||
| from nemo_retriever.io.markdown import to_markdown_by_page | ||
|
|
||
| parquet_files = sorted(Path(parquet_dir).rglob("*.parquet")) | ||
| if not parquet_files: | ||
| print(f"ERROR: No .parquet files found in {parquet_dir}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| print(f"Found {len(parquet_files)} Parquet file(s)") | ||
|
|
||
| t0 = time.monotonic() | ||
| dfs = [pd.read_parquet(f) for f in parquet_files] | ||
| df = pd.concat(dfs, ignore_index=True) | ||
| print(f"Loaded {len(df)} records in {time.monotonic() - t0:.1f}s") | ||
| print(f"Columns: {list(df.columns)}") | ||
|
|
||
| path_col = "path" if "path" in df.columns else "source_id" | ||
| if path_col not in df.columns: | ||
| print("ERROR: Neither 'path' nor 'source_id' found in columns", file=sys.stderr) | ||
| return 1 | ||
|
|
||
| def _ndarray_to_list(record: dict) -> dict: | ||
| """Pandas reads Parquet list columns as numpy arrays. | ||
| to_markdown_by_page checks isinstance(items, list), so convert them.""" | ||
| for key in ("table", "chart", "infographic", "tables", "charts", "infographics"): | ||
| val = record.get(key) | ||
| if isinstance(val, np.ndarray): | ||
| record[key] = val.tolist() | ||
| return record | ||
|
|
||
| docs_grouped = defaultdict(list) | ||
| for _, row in df.iterrows(): | ||
| source = str(row.get(path_col, "")) | ||
| if source: | ||
| docs_grouped[source].append(_ndarray_to_list(row.to_dict())) | ||
|
|
||
| print(f"Grouped into {len(docs_grouped)} documents") | ||
|
|
||
| t1 = time.monotonic() | ||
| index: dict[str, dict[str, str]] = {} | ||
| total_pages = 0 | ||
|
|
||
| for source_id, records in docs_grouped.items(): | ||
| try: | ||
| pages = to_markdown_by_page(records) | ||
| except Exception as exc: | ||
| print(f" WARNING: Failed to render {source_id}: {exc}") | ||
| continue | ||
|
|
||
| page_map: dict[str, str] = {} | ||
| for page_number, markdown in pages.items(): | ||
| page_map[str(page_number)] = markdown | ||
| total_pages += 1 | ||
|
|
||
| index[source_id] = page_map | ||
|
|
||
| elapsed_render = time.monotonic() - t1 | ||
| print(f"Rendered {total_pages} pages from {len(index)} documents in {elapsed_render:.1f}s") | ||
|
|
||
| os.makedirs(os.path.dirname(output_file), exist_ok=True) | ||
| with open(output_file, "w", encoding="utf-8") as f: | ||
| json.dump(index, f, ensure_ascii=False) | ||
|
|
||
| size_mb = os.path.getsize(output_file) / 1024 / 1024 | ||
| print(f"\nIndex written to {output_file} ({size_mb:.1f} MB)") | ||
| print(f" Documents: {len(index)}") | ||
| print(f" Pages: {total_pages}") | ||
|
|
||
| return 0 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
seems like this is also a tool, where I send it a parquet file (could be dataframe) and then you create the page level markdown. This definitely is useful outside of harness.