Spaces:
Build error
Build error
| import os | |
| import torch | |
| from datetime import datetime | |
| # hyperparameters | |
| DEVICE = "cuda" if torch.cuda.is_available() else "cpu" | |
| NUM_HEAD = 6 | |
| NUM_EMBED = NUM_HEAD * 128 | |
| NUM_LAYER = 8 | |
| DROPOUT = 0.3 | |
| MAX_SEQ_LEN = 4096 | |
| def encode(text_seq: str, tokenizer: any) -> torch.Tensor: | |
| """ | |
| Function to encode input text using a pre-trained tokenizer and vectorized lookups | |
| """ | |
| # tokenize the input text | |
| tokens = tokenizer.tokenize(text_seq) | |
| # convert the tokens to their corresponding ids | |
| token_indices = tokenizer.convert_tokens_to_ids(tokens) | |
| token_indices = torch.tensor(token_indices, dtype=torch.long) | |
| return token_indices | |
| def decode(enc_sec: torch.Tensor, tokenizer: any) -> str: | |
| """ | |
| Function to decode a sequence of token indices back to a string | |
| """ | |
| # convert the indices to a list | |
| enc_sec = enc_sec.tolist() | |
| # decode the indices to a string | |
| text = tokenizer.decode(enc_sec) | |
| return text | |