MouezYazidi/campSentiment-Bilingual
Viewer • Updated • 2k • 15
How to use MouezYazidi/modernBERT-base-bilingual-CampingReviewsSentiment with Transformers:
# Use a pipeline as a high-level helper
from transformers import pipeline
pipe = pipeline("text-classification", model="MouezYazidi/modernBERT-base-bilingual-CampingReviewsSentiment") # Load model directly
from transformers import AutoTokenizer, AutoModelForSequenceClassification
tokenizer = AutoTokenizer.from_pretrained("MouezYazidi/modernBERT-base-bilingual-CampingReviewsSentiment")
model = AutoModelForSequenceClassification.from_pretrained("MouezYazidi/modernBERT-base-bilingual-CampingReviewsSentiment", device_map="auto")modernBERT-base-bilingual-CampingReviewsSentiment is a fine-tuned version of answerdotai/ModernBERT-base using a bilingual sentiment dataset MouezYazidi/campSentiment-Bilingual.
Model supports bilingual sentiment classification ( english & french )
After fine-tuning the model, we evaluate its performance on the test dataset from MouezYazidi/campSentiment-Bilingual
| Class | Precision | Recall | F1-Score | Support |
|---|---|---|---|---|
| 0 | 0.88 | 0.67 | 0.76 | 102 |
| 1 | 0.89 | 0.94 | 0.93 | 298 |
| Accuracy | 0.89 | 400 | ||
| Macro Avg | 0.89 | 0.82 | 0.85 | 400 |
| Weighted Avg | 0.89 | 0.89 | 0.89 | 400 |
Since transformers only supports the ModernBERT architecture from version 4.48.0.dev0, use the following
command to get the required version:
pip install "git+https://github.com/huggingface/transformers.git@6e0515e99c39444caae39472ee1b2fd76ece32f1" --upgrade
Install FlashAttention to accelerate inference performance
pip install flash-attn==2.7.2.post1
import torch
from transformers import AutoTokenizer, AutoModelForSequenceClassification
# Set device (GPU if available, else CPU)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# Load model and tokenizer
model_id = "MouezYazidi/modernBERT-base-bilingual-CampingReviewsSentiment"
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForSequenceClassification.from_pretrained(model_id, torch_dtype=torch.float16).to(device)
model.eval()
def predict_sentiment(text: str):
"""Predicts sentiment of the given text using the model."""
inputs = tokenizer([text], return_tensors="pt").to(device)
with torch.no_grad(): # Use no_grad for inference optimization
outputs = model(**inputs)
prediction = outputs.logits.argmax(dim=-1).item()
return 'positive' if prediction==1 else 'negative'
# Example usage
text = """
Place is amazing. Entertainment is next level brilliant
Pool areas excellent
Literally no complaints at all. Staff so friendly everywhere. Brought 2 teenagers they had a great time aswell as 3 and 9 year old
Fantastic time had by us all
"""
prediction = predict_sentiment(text)
print(f"Predicted Sentiment: {prediction}")
The following hyperparameters were used during training:
Base model
answerdotai/ModernBERT-base