File size: 1,005 Bytes
e0cfb62 1d384ae e0cfb62 | 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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | import torch
from transformers import (
AutoModelForSequenceClassification,
AutoTokenizer,
)
MODEL_PATH = "tmutton/wcag-accessibility-classifier"
tokenizer = AutoTokenizer.from_pretrained(MODEL_PATH)
model = AutoModelForSequenceClassification.from_pretrained(
MODEL_PATH
)
def predict(text):
inputs = tokenizer(
text,
return_tensors="pt",
truncation=True,
)
with torch.no_grad():
outputs = model(**inputs)
probabilities = torch.softmax(
outputs.logits,
dim=-1
)[0]
predicted_id = torch.argmax(probabilities).item()
predicted_label = model.config.id2label[predicted_id]
confidence = probabilities[predicted_id].item()
return predicted_label, confidence
while True:
text = input("\nDescribe an accessibility issue (or 'quit'): ")
if text.lower() == "quit":
break
label, confidence = predict(text)
print(f"\nPrediction: {label}")
print(f"Confidence: {confidence:.1%}") |