This is a prototype-based classifier for Swedish text CEFR level estimation with AutoConfig/AutoModel support, compatible with Hugging Face Transformers.
1import torch
2from transformers import AutoTokenizer
3
4# Load model and tokenizer
5model_name = "fffffwl/swe-cefr-sp"
6
7# If you have the model class locally:
8from convert_proto_model_to_hf import CEFRPrototypeModel
9model = CEFRPrototypeModel.from_pretrained(model_name)
10tokenizer = AutoTokenizer.from_pretrained(model_name)
11
12# Example text
13text = "Jag heter Anna och jag kommer från Sverige."
14
15# Tokenize and predict
16inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128)
17outputs = model(**inputs)
18
19# Get predictions
20probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
21predicted_class = torch.argmax(probs, dim=-1).item()
22
23# Map to CEFR level
24cefr_labels = ["A1", "A2", "B1", "B2", "C1", "C2"]
25print(f"Text: {text}")
26print(f"Predicted CEFR level: {cefr_labels[predicted_class]}")
27print(f"Confidence: {probs[0][predicted_class].item():.3f}")
1class CEFRProtoConfig(PretrainedConfig):
2 model_type = "cefr_prototype"
3
4 def __init__(
5 self,
6 encoder_name: str = "KB/bert-base-swedish-cased",
7 num_labels: int = 6,
8 prototypes_per_class: int = 3,
9 temperature: float = 10.0,
10 layer_index: int = -2,
11 hidden_size: int = 768,
12 **kwargs
13 ):
1class CEFRPrototypeModel(PreTrainedModel):
2 def encode(self, input_ids, attention_mask, token_type_ids=None) -> torch.Tensor:
3 # Mean pooling on BERT layer -2
4 # L2 normalization
5 pass
6
7 def forward(self, input_ids, attention_mask, token_type_ids=None, labels=None):
8 # Cosine similarity with prototypes
9 # Temperature scaling
10 pass
On the Swedish CEFR sentence dataset (10k sentences from COCTAILL, 8 Sidor, and SUC3):
1@misc{fan2024swedish,
2 title={Swedish Sentence-Level CEFR Classification with LLM Annotations},
3 author={Fan, Wenlin},
4 year={2024},
5 howpublished={\url{https://huggingface.co/fffffwl/swe-cefr-sp}}
6}
1@misc{fan2024swecefrsp,
2 title={Swedish CEFR Sentence-level Assessment using Large Language Models},
3 author={Fan, Wenlin},
4 year={2024},
5 publisher={GitHub},
6 howpublished={\url{https://github.com/fanwenlin/swe-cefr-sp}},
7 note={Dataset, LLM annotating codes and sentence-level assessment codes available}
8}
This model is released under the MIT License. See LICENSE file for details.
For more details, visit the
project repository.