File size: 978 Bytes
e27ee71
 
 
 
 
 
 
 
 
66e8bb3
 
 
e27ee71
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
from transformers import AutoConfig, AutoModelForSequenceClassification, AutoTokenizer
from transformers.tools import PipelineTool


class TextPairClassificationTool(PipelineTool):
    default_checkpoint = "sgugger/bert-finetuned-mrpc"
    pre_processor_class = AutoTokenizer
    model_class = AutoModelForSequenceClassification
    
    inputs = ["text", "text"]
    outputs = ["text"]

    description = (
        "This is a tool that classifies if two texts in English are similar or not using the labels 'equivalent' and "
        "'not_equivalent'. It takes two inputs named `text` and `second_text` which should be in English and returns "
        "the predicted label."
    )

    def encode(self, text, second_text):
        return self.pre_processor(text, second_text, return_tensors="pt")

    def decode(self, outputs):
        logits = outputs.logits
        label_id = torch.argmax(logits[0]).item()
        return self.model.config.id2label[label_id]