Spaces:
Running
on
Zero
Running
on
Zero
| """ | |
| Parts of the code is based on source code of memit | |
| MIT License | |
| Copyright (c) 2022 Kevin Meng | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| """ | |
| import json | |
| import typing | |
| from pathlib import Path | |
| import torch | |
| from torch.utils.data import Dataset | |
| REMOTE_ROOT_URL = "https://rome.baulab.info" | |
| REMOTE_ROOT = f"{REMOTE_ROOT_URL}/data/dsets" | |
| class CounterFactDataset(Dataset): | |
| def __init__( | |
| self, | |
| data_dir: str, | |
| multi: bool = False, | |
| size: typing.Optional[int] = None, | |
| *args, | |
| **kwargs, | |
| ): | |
| data_dir = Path(data_dir) | |
| cf_loc = data_dir / ( | |
| "counterfact.json" if not multi else "multi_counterfact.json" | |
| ) | |
| if not cf_loc.exists(): | |
| remote_url = f"{REMOTE_ROOT}/{'multi_' if multi else ''}counterfact.json" | |
| print(f"{cf_loc} does not exist. Downloading from {remote_url}") | |
| data_dir.mkdir(exist_ok=True, parents=True) | |
| torch.hub.download_url_to_file(remote_url, cf_loc) | |
| with open(cf_loc, "r") as f: | |
| self.data = json.load(f) | |
| if size is not None: | |
| self.data = self.data[:size] | |
| print(f"Loaded dataset with {len(self)} elements") | |
| def __len__(self): | |
| return len(self.data) | |
| def __getitem__(self, item): | |
| return self.data[item] | |
| class MultiCounterFactDataset(CounterFactDataset): | |
| def __init__( | |
| self, data_dir: str, size: typing.Optional[int] = None, *args, **kwargs | |
| ): | |
| super().__init__(data_dir, *args, multi=True, size=size, **kwargs) | |