|
from transformers import AutoTokenizer |
|
from transformers import AutoModelForSequenceClassification |
|
import torch |
|
from torch.nn import functional as F |
|
import numpy as np |
|
import json |
|
|
|
|
|
|
|
label2id= json.load( |
|
open('data/categories_refined.json', 'r') |
|
) |
|
id2label= {} |
|
for key in label2id.keys(): |
|
id2label[label2id[key]] = key |
|
|
|
|
|
|
|
model_name= "intent_classification_model/checkpoint-1216" |
|
tokenizer = AutoTokenizer.from_pretrained(model_name) |
|
|
|
model = AutoModelForSequenceClassification.from_pretrained(model_name).to("cuda") |
|
|
|
|
|
|
|
def logit2prob(logit): |
|
|
|
|
|
prob= 1/(1+ np.exp(-logit)) |
|
return np.round(prob, 3) |
|
|
|
|
|
|
|
|
|
def get_top_intent(keyword: str): |
|
''' |
|
Returns score list |
|
''' |
|
inputs = tokenizer(keyword, return_tensors="pt").to("cuda") |
|
with torch.no_grad(): |
|
logits = model(**inputs).logits |
|
|
|
|
|
|
|
|
|
|
|
|
|
individual_probabilities_scores = logit2prob(logits.cpu().numpy()[0]) |
|
|
|
score_list= [] |
|
|
|
for i in range(5): |
|
label= model.config.id2label[i] |
|
|
|
score= individual_probabilities_scores[i] |
|
score_list.append( |
|
(label, score) |
|
) |
|
|
|
|
|
|
|
|
|
|
|
|
|
score_list.sort( |
|
key= lambda x: x[1], reverse=True |
|
) |
|
|
|
return score_list |