Spaces:
Sleeping
Sleeping
File size: 954 Bytes
ad52a00 61ea8cf ad52a00 |
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 |
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
|