Text Classification
Transformers
PyTorch
bert
Inference Endpoints
rttl commited on
Commit
362cab8
1 Parent(s): 79ff564

Upload pipeline.py

Browse files
Files changed (1) hide show
  1. pipeline.py +29 -0
pipeline.py ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ from transformers import BertTokenizer
3
+ from foody_bert import FoodyBertForSequenceClassification
4
+
5
+ class PreTrainedPipeline():
6
+
7
+ def __init__(self, path=""):
8
+
9
+ """
10
+ Initialize model
11
+ """
12
+ self.bert_tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")
13
+ self.model = FoodyBertForSequenceClassification.from_pretrained(".")
14
+ def __call__(self, inputs: str) -> List[float]:
15
+
16
+ """
17
+ Args:
18
+ inputs (:obj:`str`):
19
+ a string to get the features of.
20
+ Return:
21
+ A :obj:`list` of floats: The features computed by the model.
22
+ """
23
+ input_ids = self.bert_tokenizer.encode(inputs, add_special_tokens=True)
24
+ X = torch.tensor([input_ids])
25
+ with torch.no_grad():
26
+ predicted_class_id = self.model(X).logits.argmax().item()
27
+ labels = ['positive','neutral','negative']
28
+ reps = labels[predicted_class_id]
29
+ return self.model.get_sentence_vector(inputs).tolist()