Download M3Retrieve.py from Tensorists/M3Retrieve: direct link, hf CLI and curl.
- Browser
- Download file 4.07 kB
-
https://huggingface.co/datasets/Tensorists/M3Retrieve/resolve/8b7f5c2e4e04e3feeda8d68da44d64205a0a5a0e/M3Retrieve.py
- Command line
-
hf download hf://datasets/Tensorists/M3Retrieve@8b7f5c2e4e04e3feeda8d68da44d64205a0a5a0e/M3Retrieve.py
-
curl -L -o M3Retrieve.py https://huggingface.co/datasets/Tensorists/M3Retrieve/resolve/8b7f5c2e4e04e3feeda8d68da44d64205a0a5a0e/M3Retrieve.py
4.07 kB
| import json | |
| import os | |
| import datasets | |
| # Define the dataset | |
| class M3Retrieve(datasets.GeneratorBasedBuilder): | |
| VERSION = datasets.Version("1.0.0") | |
| # Define available subfolders (datasets) | |
| SUBFOLDERS = [ | |
| "Anatomy_and_Physiology", | |
| "Cardiology", | |
| "Dermatology", | |
| "Endocrinology_and_Diabetes", | |
| "Gastroenterology", | |
| "Hematology", | |
| "Microbiology_and_Cell_Biology", | |
| "Miscellaneous", | |
| "Neurology_and_Neuroscience", | |
| "Ophthalmology_and_Sensory_Systems", | |
| "Orthopedics_and_Musculoskeletal", | |
| "Pharmacology", | |
| "Psychiatry_and_Mental_Health", | |
| "Pubmed", | |
| "Radiology_and_Imaging", | |
| "Reproductive_System", | |
| "Respiratory_and_Pulmonology", | |
| "Surgical_Specialties", | |
| ] | |
| # Define Builder Configs for each subfolder | |
| BUILDER_CONFIGS = [ | |
| datasets.BuilderConfig( | |
| name=subfolder, | |
| version=datasets.Version("1.0.0"), | |
| description=f"Dataset for {subfolder.replace('_', ' ')}" | |
| ) | |
| for subfolder in SUBFOLDERS | |
| ] | |
| # def _info(self): | |
| # return datasets.DatasetInfo( | |
| # description="M3Retrieve: Benchmarking Multimodal Retrieval for Medicine", | |
| # features={ | |
| # "queries": { | |
| # "_id": datasets.Value("string"), | |
| # "caption": datasets.Value("string"), | |
| # "image_path": datasets.Value("string"), | |
| # }, | |
| # "corpus": { | |
| # "_id": datasets.Value("string"), | |
| # "text": datasets.Value("string"), | |
| # }, | |
| # "qrels": { | |
| # "query-id": datasets.Value("string"), | |
| # "corpus-id": datasets.Value("string"), | |
| # "score": datasets.Value("float32"), | |
| # }, | |
| # }, | |
| # supervised_keys=None, | |
| # ) | |
| def _info(self): | |
| return datasets.DatasetInfo( | |
| description="M3Retrieve: Benchmarking Multimodal Retrieval for Medicine", | |
| features=datasets.Features( | |
| { | |
| "_id": datasets.Value("string"), | |
| "caption": datasets.Value("string"), | |
| "image_path": datasets.Value("string"), | |
| "text": datasets.Value("string"), | |
| "query-id": datasets.Value("string"), | |
| "corpus-id": datasets.Value("string"), | |
| "score": datasets.Value("float32"), | |
| } | |
| ), | |
| supervised_keys=None, | |
| ) | |
| def _split_generators(self, dl_manager): | |
| """Returns SplitGenerators for the selected subfolder""" | |
| data_dir = os.path.join(dl_manager.download_and_extract(self.config.data_dir), self.config.name) | |
| return [ | |
| datasets.SplitGenerator( | |
| name="queries", | |
| gen_kwargs={"filepath": os.path.join(data_dir, "queries.jsonl"), "key": "queries"}, | |
| ), | |
| datasets.SplitGenerator( | |
| name="corpus", | |
| gen_kwargs={"filepath": os.path.join(data_dir, "corpus.jsonl"), "key": "corpus"}, | |
| ), | |
| datasets.SplitGenerator( | |
| name="qrels", | |
| gen_kwargs={"filepath": os.path.join(data_dir, "qrels/test.tsv"), "key": "qrels"}, | |
| ), | |
| ] | |
| def _generate_examples(self, filepath, key): | |
| """Yields examples as (key, example) tuples.""" | |
| if key in ["queries", "corpus"]: | |
| with open(filepath, "r", encoding="utf-8") as f: | |
| for i, line in enumerate(f): | |
| data = json.loads(line) | |
| yield i, data | |
| elif key == "qrels": | |
| with open(filepath, "r", encoding="utf-8") as f: | |
| for i, line in enumerate(f): | |
| query_id, corpus_id, score = line.strip().split("\t") | |
| yield i, {"query-id": query_id, "corpus-id": corpus_id, "score": float(score)} | |