File size: 1,047 Bytes
180d0f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from transformers import Pipeline
import torch
import joblib

class CustomPipeline(Pipeline):
    def __init__(self, model, tokenizer, device=-1, **kwargs):
        super().__init__(model=model, tokenizer=tokenizer, device=device, **kwargs)
        self.label_mapping = joblib.load("label_mapping.joblib")

    def _sanitize_parameters(self, **kwargs):
        return {}, {}, {}

    def preprocess(self, inputs):
        return self.tokenizer(inputs, return_tensors="pt", truncation=True, padding=True, max_length=512)

    def _forward(self, model_inputs):
        with torch.no_grad():
            outputs = self.model(**model_inputs)
        return outputs

    def postprocess(self, model_outputs):
        logits = model_outputs.logits
        predicted_class = torch.argmax(logits, dim=1).item()
        predicted_label = self.label_mapping[predicted_class]
        confidence = torch.softmax(logits, dim=1)[0][predicted_class].item()
        
        return {
            "label": predicted_label,
            "score": confidence
        }