Views
No views yet
1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model_name = "theluantran/cefr-bert-classifier"
5tokenizer = AutoTokenizer.from_pretrained(model_name)
6model = AutoModelForSequenceClassification.from_pretrained(model_name)
7
8text = "Your text here"
9inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512)
10
11with torch.no_grad():
12 outputs = model(**inputs)
13 predictions = torch.nn.functional.softmax(outputs.logits, dim=-1)
14 predicted_class = predictions.argmax().item()
15
16label_map = {0: 'A1', 1: 'A2', 2: 'B1', 3: 'B2', 4: 'C1/C2'}
17print(f"Predicted CEFR Level: {label_map[predicted_class]}")
18print(f"Confidence: {predictions[0][predicted_class].item():.2%}")