|
| 1 | +import json |
| 2 | + |
| 3 | +from unitxt import add_to_catalog |
| 4 | +from unitxt.blocks import TaskCard |
| 5 | +from unitxt.collections_operators import Wrap |
| 6 | +from unitxt.image_operators import HashImage, ToImage |
| 7 | +from unitxt.loaders import LoadHF |
| 8 | +from unitxt.operators import ( |
| 9 | + AddIncrementalId, |
| 10 | + Cast, |
| 11 | + Copy, |
| 12 | + Deduplicate, |
| 13 | + FilterByCondition, |
| 14 | +) |
| 15 | +from unitxt.splitters import RenameSplits, SplitRandomMix |
| 16 | +from unitxt.templates import InputOutputTemplate |
| 17 | +from unitxt.test_utils.card import test_card |
| 18 | + |
| 19 | +description = ( |
| 20 | + "We introduced REAL-MM-RAG-Bench, a real-world multi-modal retrieval benchmark designed to evaluate " |
| 21 | + "retrieval models in reliable, challenging, and realistic settings. The benchmark was constructed using " |
| 22 | + "an automated pipeline, where queries were generated by a vision-language model (VLM), filtered by a large " |
| 23 | + "language model (LLM), and rephrased by an LLM to ensure high-quality retrieval evaluation. " |
| 24 | + "To simulate real-world retrieval challenges, we introduce multi-level query rephrasing, modifying queries " |
| 25 | + "at three distinct levels—from minor wording adjustments to significant structural changes—ensuring models " |
| 26 | + "are tested on their true semantic understanding rather than simple keyword matching." |
| 27 | +) |
| 28 | + |
| 29 | +datasets = [ |
| 30 | + {"hf_name": "REAL-MM-RAG_FinSlides", "subset": "fin_slides"}, |
| 31 | + {"hf_name": "REAL-MM-RAG_FinReport", "subset": "fin_report"}, |
| 32 | + {"hf_name": "REAL-MM-RAG_TechReport", "subset": "tech_report"}, |
| 33 | + {"hf_name": "REAL-MM-RAG_TechSlides", "subset": "tech_slides"}, |
| 34 | +] |
| 35 | + |
| 36 | +hf_ibm_research = "ibm-research" |
| 37 | +hf_url_base = "https://huggingface.co/datasets/" |
| 38 | + |
| 39 | +for dataset in datasets: |
| 40 | + hf_name = dataset["hf_name"] |
| 41 | + hf_dataset_id = f"{hf_ibm_research}/{hf_name}" |
| 42 | + hf_url = f"{hf_url_base}/{hf_dataset_id}" |
| 43 | + subset = dataset["subset"] |
| 44 | + |
| 45 | + # first we create the card for the benchmark |
| 46 | + card = TaskCard( |
| 47 | + loader=LoadHF( |
| 48 | + path=hf_dataset_id, |
| 49 | + name="default", |
| 50 | + split="test", |
| 51 | + data_classification_policy=["public"], |
| 52 | + ), |
| 53 | + preprocess_steps=[ |
| 54 | + FilterByCondition(values={"query": None}, condition="ne"), |
| 55 | + HashImage( |
| 56 | + field="image", |
| 57 | + to_field="reference_context_ids", |
| 58 | + ), |
| 59 | + Copy( field="query", to_field="question"), |
| 60 | + AddIncrementalId(to_field="question_id"), |
| 61 | + Cast(field="question_id", to="str"), |
| 62 | + SplitRandomMix( |
| 63 | + { |
| 64 | + "test": "test[30%]", |
| 65 | + "train": "test[70%]", |
| 66 | + }), |
| 67 | + Wrap( |
| 68 | + field="answer", |
| 69 | + inside="list", |
| 70 | + to_field="reference_answers", |
| 71 | + ), |
| 72 | + Wrap( |
| 73 | + field="reference_context_ids", |
| 74 | + inside="list", |
| 75 | + to_field="reference_context_ids", |
| 76 | + ), |
| 77 | + ], |
| 78 | + task="tasks.rag.end_to_end", |
| 79 | + templates={"default": "templates.rag.end_to_end.json_predictions"}, |
| 80 | + __tags__={"license": "cdla-permissive-2.0", "url": hf_url}, |
| 81 | + __title__=dataset["hf_name"].replace("-", "").replace("_", ": "), |
| 82 | + __description__=description, |
| 83 | + ) |
| 84 | + |
| 85 | + wrong_answer = { |
| 86 | + "contexts": ["hi"], |
| 87 | + "is_answerable": True, |
| 88 | + "answer": "Don't know", |
| 89 | + "context_ids": ["id0"], |
| 90 | + } |
| 91 | + |
| 92 | + test_card( |
| 93 | + card, |
| 94 | + strict=True, |
| 95 | + full_mismatch_prediction_values=[json.dumps(wrong_answer)], |
| 96 | + debug=False, |
| 97 | + ) |
| 98 | + |
| 99 | + add_to_catalog(card, f"cards.rag.benchmark.real_mm_rag_{subset}.en", overwrite=True) |
| 100 | + |
| 101 | + # next we create the card for the pages (documents) |
| 102 | + card = TaskCard( |
| 103 | + loader=LoadHF( |
| 104 | + path=hf_dataset_id, |
| 105 | + name="default", |
| 106 | + split="test", |
| 107 | + data_classification_policy=["public"], |
| 108 | + ), |
| 109 | + preprocess_steps=[ |
| 110 | + RenameSplits({"test": "train"}), |
| 111 | + HashImage( |
| 112 | + field="image", |
| 113 | + to_field="document_id", |
| 114 | + ), |
| 115 | + Deduplicate(by=["document_id"]), |
| 116 | + ToImage(field="image"), |
| 117 | + Wrap(field="image", inside="list", to_field="passages"), |
| 118 | + ], |
| 119 | + task="tasks.rag.corpora", |
| 120 | + templates={ |
| 121 | + "empty": InputOutputTemplate( |
| 122 | + input_format="", |
| 123 | + output_format="", |
| 124 | + ), |
| 125 | + }, |
| 126 | + __tags__={"license": "cdla-permissive-2.0", "url": hf_url}, |
| 127 | + __title__=dataset["hf_name"].replace("-", "").replace("_", ": "), |
| 128 | + __description__=description, |
| 129 | + ) |
| 130 | + # Not testing card, because documents are not evaluated. |
| 131 | + add_to_catalog(card, f"cards.rag.documents.real_mm_rag_{subset}.en", overwrite=True) |
0 commit comments