| 676 | |
| 677 | |
| 678 | class SidItemFeatDataset(JSONBaseDataset): |
| 679 | def __init__(self, item_file, index_file, tokenizer=None, max_len=2048, sample=-1, test=False, seed=0, category=""): |
| 680 | """ |
| 681 | Dataset for sid2title and title2sid tasks. |
| 682 | |
| 683 | Args: |
| 684 | item_file: Path to .item.json file with item features |
| 685 | index_file: Path to .index.json file with item indices |
| 686 | tokenizer: Tokenizer for encoding text |
| 687 | max_len: Maximum sequence length |
| 688 | sample: Number of samples to use (-1 for all) |
| 689 | test: Whether this is test mode |
| 690 | seed: Random seed |
| 691 | category: Category name for prompts |
| 692 | """ |
| 693 | super().__init__(item_file=item_file, index_file=index_file, tokenizer=tokenizer, max_len=max_len, test=test, category=category, dedup=False, seed=seed) |
| 694 | |
| 695 | # Build sid2title and title2sid mappings |
| 696 | self.sid2title = {} |
| 697 | self.title2sid = {} |
| 698 | |
| 699 | for item_id, sids in self.indices.items(): |
| 700 | if item_id in self.item_feat: |
| 701 | title = self.item_feat[item_id]['title'] |
| 702 | # Concatenate all three semantic IDs as the key |
| 703 | if len(sids) >= 3: |
| 704 | combined_sid = sids[0] + sids[1] + sids[2] |
| 705 | self.sid2title[combined_sid] = title |
| 706 | self.title2sid[title] = combined_sid |
| 707 | |
| 708 | # Create data samples |
| 709 | self.data = [] |
| 710 | |
| 711 | # Create sid2title samples |
| 712 | for sid, title in self.sid2title.items(): |
| 713 | self.data.append({ |
| 714 | 'task': 'sid2title', |
| 715 | 'input': sid, |
| 716 | 'output': title |
| 717 | }) |
| 718 | |
| 719 | # Create title2sid samples |
| 720 | for title, sid in self.title2sid.items(): |
| 721 | self.data.append({ |
| 722 | 'task': 'title2sid', |
| 723 | 'input': title, |
| 724 | 'output': sid |
| 725 | }) |
| 726 | |
| 727 | if sample > 0 and sample < len(self.data): |
| 728 | self.data = random.sample(self.data, sample) |
| 729 | |
| 730 | if self.tokenizer is not None: |
| 731 | self.get_inputs() |
| 732 | |
| 733 | def generate_prompt(self, data_point): |
| 734 | if data_point['task'] == 'title2sid': |
| 735 | prompt = f"Which item has the title: {data_point['input']}?" |
no outgoing calls