Download demo_generate_30.py from BitZone910/wiki_math: direct link, hf CLI and curl.
- Browser
- Download file 3.66 kB
-
https://huggingface.co/datasets/BitZone910/wiki_math/resolve/main/demo_generate_30.py
- Command line
-
hf download hf://datasets/BitZone910/wiki_math/demo_generate_30.py
-
curl -L -o demo_generate_30.py https://huggingface.co/datasets/BitZone910/wiki_math/resolve/main/demo_generate_30.py
3.66 kB
| """Generate and print a 30-passage Search-R1 corpus demo.""" | |
| import json | |
| import os | |
| import sys | |
| from collections import Counter | |
| import build_search_r1_corpus as corpus_builder | |
| DEMO_PASSAGE_LIMIT = 30 | |
| DEMO_OUTPUT_PATH = corpus_builder.BASE_DIR / "search_r1_corpus_demo.jsonl" | |
| DEMO_TEMP_PATH = corpus_builder.BASE_DIR / "search_r1_corpus_demo.jsonl.tmp" | |
| def main() -> None: | |
| if hasattr(sys.stdout, "reconfigure"): | |
| sys.stdout.reconfigure(encoding="utf-8", errors="replace") | |
| corpus_builder.verify_input_files(corpus_builder.INPUT_FILES) | |
| jsonl_lines = [] | |
| scanned_articles = 0 | |
| retained_articles = 0 | |
| filter_counts = Counter() | |
| try: | |
| with DEMO_TEMP_PATH.open( | |
| "w", encoding="utf-8", newline="\n" | |
| ) as output: | |
| for input_path in corpus_builder.INPUT_FILES: | |
| documents = corpus_builder.load_documents(input_path) | |
| for article in documents: | |
| scanned_articles += 1 | |
| reason = corpus_builder.initial_filter_reason(article) | |
| if reason is not None: | |
| filter_counts[reason] += 1 | |
| continue | |
| raw_content = str(article["content"]) | |
| raw_title = ( | |
| "" | |
| if article.get("title") is None | |
| else str(article["title"]) | |
| ) | |
| raw_link = ( | |
| "" | |
| if article.get("link") is None | |
| else str(article["link"]) | |
| ) | |
| cleaned_content = corpus_builder.clean_content(raw_content) | |
| passages = corpus_builder.make_passages(cleaned_content) | |
| if not passages: | |
| filter_counts["cleaned_content_under_50_words"] += 1 | |
| continue | |
| retained_articles += 1 | |
| title = corpus_builder.clean_title(raw_title, raw_link) | |
| for passage in passages: | |
| passage_id = str(len(jsonl_lines)) | |
| record = { | |
| "id": passage_id, | |
| "contents": f'"{title}"\n{passage}', | |
| } | |
| line = json.dumps(record, ensure_ascii=False) | |
| output.write(line) | |
| output.write("\n") | |
| jsonl_lines.append(line) | |
| if len(jsonl_lines) == DEMO_PASSAGE_LIMIT: | |
| break | |
| if len(jsonl_lines) == DEMO_PASSAGE_LIMIT: | |
| break | |
| if len(jsonl_lines) == DEMO_PASSAGE_LIMIT: | |
| break | |
| os.replace(DEMO_TEMP_PATH, DEMO_OUTPUT_PATH) | |
| except BaseException: | |
| try: | |
| DEMO_TEMP_PATH.unlink(missing_ok=True) | |
| except OSError: | |
| pass | |
| raise | |
| print("=" * 80) | |
| print(f"Search-R1 corpus demo: {len(jsonl_lines)} JSONL records") | |
| print("=" * 80) | |
| for line in jsonl_lines: | |
| print(line) | |
| print("=" * 80) | |
| print("Demo summary") | |
| print("=" * 80) | |
| print(f"Scanned source articles: {scanned_articles}") | |
| print(f"Articles contributing passages: {retained_articles}") | |
| print(f"Filtered while scanning: {sum(filter_counts.values())}") | |
| for reason, count in sorted(filter_counts.items()): | |
| print(f" {reason}: {count}") | |
| print(f"Generated passages: {len(jsonl_lines)}") | |
| print(f"Output file: {DEMO_OUTPUT_PATH}") | |
| if __name__ == "__main__": | |
| main() | |