File size: 755 Bytes
81bc0f3 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
from transformers import BertTokenizer, BertForSequenceClassification
import torch
model = BertForSequenceClassification.from_pretrained('./confidence_model')
tokenizer = BertTokenizer.from_pretrained('./confidence_tokenizer')
def predict_confidence(question, answer):
inputs = tokenizer(question, answer, return_tensors="pt", padding=True, truncation=True)
model.eval()
with torch.no_grad():
outputs = model(**inputs)
logits = outputs.logits
predictions = torch.argmax(logits, dim=-1)
return "Confident" if predictions.item() == 1 else "Not Confident"
# Example
question = "What is your experience with Python?"
answer = "I dont have any experience in Python"
print(predict_confidence(question, answer))
|