This model is a fine-tuned version of
cardiffnlp/twitter-xlm-roberta-base-sentiment specifically optimized for
Roman Urdu sentiment analysis. Roman Urdu refers to Urdu language written using the Latin/Roman script, commonly used in social media, messaging, and online forums.
The model uses a comprehensive Roman Urdu normalization pipeline with dictionary mapping for common variations.
1from transformers import AutoModelForSequenceClassification, AutoTokenizer
2import torch
3
4# Load model and tokenizer
5model = AutoModelForSequenceClassification.from_pretrained("Umair1710/xlmt-roberta-twitter-finetuned-sentimentalAnalysis")
6tokenizer = AutoTokenizer.from_pretrained("Umair1710/xlmt-roberta-twitter-finetuned-sentimentalAnalysis")
7
8# Preprocessing function
9def preprocess_text(text):
10 import re
11 roman_urdu_dict = {
12 'aj': 'aaj', 'acha': 'achha', 'bohat': 'bahut',
13 'nahi': 'nahin', 'kya': 'kya', 'yaar': 'yar',
14 }
15 text = text.lower()
16 words = text.split()
17 normalized = [roman_urdu_dict.get(word, word) for word in words]
18 return ' '.join(normalized)
19
20# Prediction function
21def predict_sentiment(text, model, tokenizer):
22 text = preprocess_text(text)
23 inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=128, padding=True)
24
25 with torch.no_grad():
26 outputs = model(**inputs)
27 probs = torch.nn.functional.softmax(outputs.logits, dim=-1)
28 pred_class = torch.argmax(probs, dim=-1).item()
29
30 labels = {0: "negative", 1: "neutral", 2: "positive"}
31 confidence = probs[0][pred_class].item()
32
33 return labels[pred_class], confidence
34
35# Example usage
36text = "aj bohat achha din tha jeet gaye"
37sentiment, confidence = predict_sentiment(text, model, tokenizer)
38print(f"Text: {text}")
39print(f"Sentiment: {sentiment} (confidence: {confidence:.3f})")
1@misc{umair2024xlmt,
2 author = {Umair},
3 title = {XLM-T RoBERTa Fine-tuned for Roman Urdu Sentiment Analysis},
4 year = {2024},
5 publisher = {Hugging Face},
6 url = {https://huggingface.co/Umair1710/xlmt-roberta-twitter-finetuned-sentimentalAnalysis}
7}
This model is released under the Apache 2.0 license.