File size: 12,289 Bytes
a949a72
 
e717e47
a949a72
c7a90a1
e717e47
 
a949a72
e717e47
a949a72
 
 
 
f23df61
 
 
a949a72
 
 
e717e47
a949a72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7a90a1
 
 
 
 
 
a949a72
 
e717e47
a949a72
 
 
 
 
 
 
 
 
e717e47
 
 
 
 
 
 
 
a949a72
 
 
 
 
e717e47
a949a72
 
 
 
 
 
 
e717e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a949a72
 
c7a90a1
 
 
 
 
 
 
a949a72
f23df61
 
 
a949a72
 
 
 
 
 
e717e47
 
 
 
 
 
a949a72
 
 
 
 
 
e717e47
 
 
a949a72
 
e717e47
 
 
 
 
a949a72
 
 
 
 
 
 
 
 
 
 
 
 
 
e717e47
 
 
 
 
a949a72
 
 
e717e47
a949a72
 
 
 
c7a90a1
 
 
 
 
 
a949a72
 
 
 
 
 
 
 
 
e717e47
 
a949a72
 
 
 
 
 
 
e717e47
 
 
 
 
 
 
 
 
 
 
 
 
 
a949a72
e717e47
a949a72
 
c7a90a1
 
 
 
 
 
 
a949a72
c7a90a1
e717e47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a949a72
c7a90a1
e717e47
a949a72
 
c7a90a1
 
 
e717e47
c7a90a1
 
 
a949a72
e717e47
 
 
 
 
 
f23df61
 
e717e47
f23df61
 
 
e717e47
f23df61
 
 
e717e47
 
 
 
 
f23df61
 
 
 
e717e47
 
 
 
 
 
 
 
 
 
 
 
f23df61
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
import json
import os
from typing import List, Optional, Union, Dict, Any, Tuple

from transformers import PreTrainedTokenizerFast
from transformers.tokenization_utils_base import AddedToken
from transformers.utils import logging

logger = logging.get_logger(__name__)

class TessarTokenizer(PreTrainedTokenizerFast):
    """
    Tessar Tokenizer implementation for Hugging Face Transformers
    
    This custom tokenizer extends the PreTrainedTokenizerFast with specialized 
    configuration and tokenization methods for the Tessar model.
    """
    
    model_input_names = ['input_ids', 'attention_mask']
    vocab_files_names = {"vocab_file": "vocab.json", "tokenizer_file": "tokenizer.json"}
    
    def __init__(
        self, 
        vocab_file=None,
        tokenizer_file=None,
        do_lower_case=True,
        unk_token="<unk>",
        sep_token="</s>",
        pad_token="<pad>",
        cls_token="<s>",
        mask_token="<mask>",
        bos_token="<s>",
        eos_token="</s>",
        max_cell_length=15,
        **kwargs
    ):
        """
        Initialize the Tessar Tokenizer with specific token configurations
        
        Args:
            vocab_file (str, optional): Path to the vocabulary file
            tokenizer_file (str, optional): Path to the pre-trained tokenizer file
            do_lower_case (bool, optional): Whether to lowercase the input. Defaults to True.
            max_cell_length (int, optional): Maximum length for cell tokenization. Defaults to 15.
        """
        # Prepare special tokens
        special_tokens_dict = {
            "unk_token": unk_token,
            "sep_token": sep_token,
            "pad_token": pad_token,
            "cls_token": cls_token,
            "mask_token": mask_token,
            "bos_token": bos_token,
            "eos_token": eos_token,
        }
        
        # Convert string tokens to AddedToken objects if they're not already
        for token_name, token_value in special_tokens_dict.items():
            if isinstance(token_value, str):
                special_tokens_dict[token_name] = AddedToken(token_value, 
                                                           lstrip=False, 
                                                           rstrip=False, 
                                                           normalized=True,
                                                           special=True)
        
        # Call parent constructor
        super().__init__(
            vocab_file=vocab_file,
            tokenizer_file=tokenizer_file,
            **special_tokens_dict,
            **kwargs
        )
        
        # Custom Tessar-specific attributes
        self.do_lower_case = do_lower_case
        self.max_cell_length = max_cell_length
    
    @property
    def vocab_size(self) -> int:
        """
        Return the size of vocabulary
        
        Returns:
            int: The vocabulary size
        """
        return len(self.vocab)
    
    def get_vocab(self) -> Dict[str, int]:
        """
        Return the vocabulary mapping
        
        Returns:
            Dict[str, int]: The vocabulary mapping
        """
        return dict(self.vocab)
    
    def save_vocabulary(self, save_directory: str, filename_prefix: Optional[str] = None) -> Tuple[str, ...]:
        """
        Save the tokenizer vocabulary and special tokens file
        
        Args:
            save_directory (str): Directory to save the vocabulary
            filename_prefix (str, optional): Prefix for the saved files
        
        Returns:
            tuple: Paths to the saved files
        """
        # Ensure the save directory exists
        os.makedirs(save_directory, exist_ok=True)
        
        # Prepare file paths
        vocab_file = os.path.join(
            save_directory, 
            f"{filename_prefix + '-' if filename_prefix else ''}vocab.json"
        )
        
        # Save tokenizer file
        tokenizer_file = os.path.join(
            save_directory, 
            f"{filename_prefix + '-' if filename_prefix else ''}tokenizer.json"
        )
        
        # Save special tokens configuration
        special_tokens_file = os.path.join(
            save_directory, 
            f"{filename_prefix + '-' if filename_prefix else ''}special_tokens.json"
        )
        
        # Get vocabulary from tokenizer
        vocab_dict = self.get_vocab()
        
        # Save vocabulary
        with open(vocab_file, 'w', encoding='utf-8') as f:
            json.dump(vocab_dict, f, ensure_ascii=False, indent=2)
        
        # Save the tokenizer file if it exists
        if hasattr(self, "backend_tokenizer") and hasattr(self.backend_tokenizer, "save"):
            self.backend_tokenizer.save(tokenizer_file)
        
        # Save special tokens configuration
        special_tokens_config = {
            "unk_token": self.unk_token,
            "sep_token": self.sep_token,
            "pad_token": self.pad_token,
            "cls_token": self.cls_token,
            "mask_token": self.mask_token,
            "bos_token": self.bos_token,
            "eos_token": self.eos_token,
            "do_lower_case": self.do_lower_case,
            "max_cell_length": self.max_cell_length
        }
        
        # Convert token objects to strings for JSON serialization
        for key, token in special_tokens_config.items():
            if hasattr(token, "content"):
                special_tokens_config[key] = token.content
        
        with open(special_tokens_file, 'w', encoding='utf-8') as f:
            json.dump(special_tokens_config, f, ensure_ascii=False, indent=2)
        
        return (vocab_file, tokenizer_file, special_tokens_file)
    
    def _tokenize(self, text: str) -> List[str]:
        """
        Custom tokenization method
        
        Args:
            text (str): Input text to tokenize
        
        Returns:
            List[str]: List of tokens
        """
        # Apply lowercase if required
        if self.do_lower_case:
            text = text.lower()
        
        # Use the parent tokenizer's tokenization method
        tokens = super()._tokenize(text)
        
        # Optional: Add custom cell-length truncation
        if self.max_cell_length > 0:
            tokens = tokens[:self.max_cell_length]
        
        return tokens
    
    def prepare_for_model(
        self, 
        ids: List[int], 
        pair_ids: Optional[List[int]] = None, 
        add_special_tokens: bool = True,
        padding: Union[bool, str] = False,
        truncation: Union[bool, str] = False,
        max_length: Optional[int] = None,
        stride: int = 0,
        pad_to_multiple_of: Optional[int] = None,
        return_tensors: Optional[str] = None,
        return_token_type_ids: Optional[bool] = None,
        return_attention_mask: Optional[bool] = None,
        return_overflowing_tokens: bool = False,
        return_special_tokens_mask: bool = False,
        return_offsets_mapping: bool = False,
        return_length: bool = False,
        verbose: bool = True,
        **kwargs
    ) -> Dict[str, Any]:
        """
        Prepare tokenized inputs for the model
        
        Args:
            ids (List[int]): List of input token ids
            pair_ids (Optional[List[int]], optional): List of pair token ids
        
        Returns:
            dict: Prepared model inputs
        """
        # Implement any Tessar-specific model preparation logic
        # For example, you might want to handle table data differently
        
        return super().prepare_for_model(
            ids,
            pair_ids=pair_ids,
            add_special_tokens=add_special_tokens,
            padding=padding,
            truncation=truncation,
            max_length=max_length,
            stride=stride,
            pad_to_multiple_of=pad_to_multiple_of,
            return_tensors=return_tensors,
            return_token_type_ids=return_token_type_ids,
            return_attention_mask=return_attention_mask,
            return_overflowing_tokens=return_overflowing_tokens,
            return_special_tokens_mask=return_special_tokens_mask,
            return_offsets_mapping=return_offsets_mapping,
            return_length=return_length,
            verbose=verbose,
            **kwargs
        )
    
    def batch_encode_tables(
        self,
        tables: List[List[List[str]]],
        max_length: Optional[int] = None,
        padding: Union[bool, str] = True,
        truncation: Union[bool, str] = True,
        return_tensors: Optional[str] = "pt",
        **kwargs
    ) -> Dict[str, Any]:
        """
        Encode a batch of tables for table question answering
        
        Args:
            tables (List[List[List[str]]]): List of tables, where each table is a list of rows,
                                          and each row is a list of cell values
            max_length (Optional[int], optional): Maximum sequence length
            padding (Union[bool, str], optional): Padding strategy
            truncation (Union[bool, str], optional): Truncation strategy
            return_tensors (Optional[str], optional): Type of tensors to return
        
        Returns:
            Dict[str, Any]: Encoded table batch
        """
        # Flatten tables into text sequences with appropriate format
        flattened_inputs = []
        
        for table in tables:
            # Convert table to a flattened text representation
            # This is a simplified example - real implementation would depend on your specific format
            table_text = ""
            
            for row_idx, row in enumerate(table):
                for col_idx, cell in enumerate(row):
                    # Apply cell-level processing
                    if self.do_lower_case:
                        cell = cell.lower()
                    
                    # Add cell with position information
                    table_text += f"[CELL_{row_idx}_{col_idx}] {cell} "
                
                # Add row separator
                table_text += "[ROW_END] "
            
            flattened_inputs.append(table_text.strip())
        
        # Encode the flattened text inputs
        return self(
            flattened_inputs,
            max_length=max_length,
            padding=padding,
            truncation=truncation,
            return_tensors=return_tensors,
            **kwargs
        )


def load_tessar_tokenizer(pretrained_model_name_or_path: str, **kwargs):
    """
    Load a pretrained Tessar tokenizer
    
    Args:
        pretrained_model_name_or_path (str): Path to the pretrained model
        **kwargs: Additional arguments to pass to from_pretrained
    
    Returns:
        TessarTokenizer: Initialized tokenizer
    """
    return TessarTokenizer.from_pretrained(pretrained_model_name_or_path, **kwargs)


# Register the tokenizer with the Transformers library
from transformers import AutoTokenizer
AutoTokenizer.register("SVECTOR-CORPORATION/Tessar-largest", TessarTokenizer)


# Example usage
if __name__ == "__main__":
    # Example of loading a pretrained tokenizer
    try:
        # Method 1: Direct loading with the class
        tokenizer = load_tessar_tokenizer("SVECTOR-CORPORATION/Tessar-largest")
        print("Tokenizer loaded successfully!")
        
        # Method 2: Loading through AutoTokenizer
        # This will work after the registration above
        auto_tokenizer = AutoTokenizer.from_pretrained("SVECTOR-CORPORATION/Tessar-largest")
        print("AutoTokenizer loaded successfully!")
        
        # Basic tokenization example
        text = "Hello, how are you doing today?"
        encoded = tokenizer(text, return_tensors="pt")
        print("Encoded Input:", encoded)
        
        # Example with table data
        table = [
            ["Header1", "Header2", "Header3"],
            ["Value1", "Value2", "Value3"],
            ["Value4", "Value5", "Value6"]
        ]
        
        # Example of batch encoding tables
        encoded_table = tokenizer.batch_encode_tables([table], return_tensors="pt")
        print("Encoded Table:", encoded_table)
        
    except Exception as e:
        print(f"Error loading tokenizer: {e}")