Views
No views yet
myrkur/Persian-ModernBert-base for binary sentiment classification on the Taaghche Persian review dataset.myrkur/Persian-ModernBert-base1from transformers import AutoTokenizer, AutoModelForSequenceClassification
2import torch
3
4model_name = "aysangh/ModernBERT-base-fa-taghche"
5model = AutoModelForSequenceClassification.from_pretrained(model_name)
6tokenizer = AutoTokenizer.from_pretrained(model_name)
7
8def predict_sentiment(text):
9 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256, padding=True)
10 with torch.no_grad():
11 outputs = model(**inputs)
12 probs = torch.softmax(outputs.logits, dim=-1)
13 pred = torch.argmax(probs, dim=-1).item()
14 confidence = probs[0][pred].item()
15 return {
16 "label": "positive" if pred == 0 else "negative",
17 "confidence": confidence
18}
19
20text = "خیلی بد بود، اصلاً پیشنهاد نمیکنم."
21
22result = predict_sentiment(text)
23print(result)
24
25# Output:
26# {'label': 'negative', 'confidence': 0.9999935626983643}