Commit ·
c9ad1bf
1
Parent(s): 848842c
Update app.py
Browse files12 unmodified lines131415161617181920212223232425262712 unmodified lines
def predict(text=None) -> dict: model.eval() inputs = tokenizer(text, return_tensors="pt") inputs = tokenizer(str(text), return_tensors="pt") input_ids = inputs["input_ids"].to(device) attention_mask = inputs["attention_mask"].to(device) model.to(device) token_logits = model(input_ids, attention_mask=attention_mask).logits print(mask_token_index) mask_token_index = torch.where(inputs["input_ids"] == tokenizer.mask_token_id)[1] mask_token_logits = token_logits[0, mask_token_index, :] print(mask_token_logits.shape) print(mask_token_logits) top_5_tokens = torch.topk(mask_token_logits, NUM_CLASSES, dim=1).indices[0].tolist() score = torch.nn.functional.softmax(mask_token_logits)[0] top_5_score = torch.topk(score, NUM_CLASSES).values.tolist()app.py+3-2