File size: 832 Bytes
c519328
 
 
3c64996
c519328
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 json
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
from ..app.utils import label_cols

tokenizer = AutoTokenizer.from_pretrained("final_model")
model = AutoModelForSequenceClassification.from_pretrained("final_model")

model.eval()

with open("final_model/thresholds.json") as f:
    thresholds = json.load(f)

def predict(text:str):
    inputs = tokenizer(
        text,
        return_tensors="pt",
        truncation=True,
        padding="max_length",
        max_length=512
    )

    with torch.no_grad():
        logits = model(**inputs).logits

    probs = torch.sigmoid(logits).squeeze().tolist()

    return{
        label: {
            "probability":round(prob, 4),
            "flagged": prob>=thresholds[label]
        } for label, prob in zip(label_cols, probs)
    }