Skip to content

Commit

Permalink
Create BERT.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Jul 5, 2024
1 parent 4a2efda commit e6a854d
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions .ai/nlp/BERT.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import torch
from transformers import BertTokenizer, BertModel

class BERT:
def __init__(self, model_path):
self.tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
self.model = BertModel.from_pretrained(model_path)

def encode(self, text):
inputs = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=512,
return_attention_mask=True,
return_tensors='pt'
)
return inputs

def predict(self, inputs):
outputs = self.model(inputs['input_ids'], attention_mask=inputs['attention_mask'])
return outputs.last_hidden_state[:, 0, :]

0 comments on commit e6a854d

Please sign in to comment.