import gradio as gr import joblib import torch import torch.nn as nn import numpy as np import pandas as pd from transformers import AutoTokenizer, AutoModel, AutoModelForSequenceClassification from sklearn.preprocessing import LabelEncoder from huggingface_hub import hf_hub_download import re import pyarabic.araby as araby # Constants MODEL_NAME = "aubmindlab/bert-base-arabertv02" MODEL_HUB = "batool0/arabic-speech-act-models" MAX_LEN = 64 CLASSES = ['Assertion', 'Expression', 'Question', 'Recommendation', 'Request'] CLASS_AR = { 'Assertion': 'تأكيد', 'Expression': 'تعبير', 'Question': 'سؤال', 'Recommendation': 'توصية', 'Request': 'طلب' } ERROR_PATTERNS = { ('Expression', 'Assertion'): "Expression/Assertion boundary: tweets describing events with emotional tone are often misclassified as Assertion.", ('Assertion', 'Expression'): "Expression/Assertion boundary: factual tweets with emotional vocabulary are sometimes misclassified as Expression.", ('Question', 'Expression'): "Implicit question: this question lacks an explicit interrogative particle (هل، ماذا), causing it to resemble an Expression.", ('Expression', 'Question'): "Implicit question: emotionally phrased tweet contains question-like vocabulary.", ('Request', 'Assertion'): "Analytical request: the request is framed as a logical argument, resembling an Assertion.", ('Request', 'Recommendation'): "Request vs Recommendation: the boundary between requesting and recommending is thin in Arabic.", ('Recommendation', 'Expression'): "Sarcastic recommendation misread as emotional Expression.", ('Assertion', 'Question'): "Assertion/Question boundary: the tweet may contain an implicit question structure without explicit particles.", ('Question', 'Assertion'): "Question/Assertion boundary: the question is phrased as a statement, common in Arabic rhetorical questions.", } CLASS_DESCRIPTIONS = { 'Assertion': "states a fact or conveys information objectively.", 'Expression': "expresses an opinion, emotion, or personal feeling.", 'Question': "asks for information or seeks clarification.", 'Recommendation': "suggests or advises a course of action.", 'Request': "asks someone to do something or take action.", } # Text Cleaning def clean_text(text): text = re.sub(r'http\S+|www\S+', '', text) text = re.sub(r'@\w+', '', text) text = re.sub(r'#\w+', '', text) text = re.sub(r'\d+', '', text) text = re.sub(r'[^\w\s\u0600-\u06FF]', '', text) text = araby.strip_tashkeel(text) text = araby.strip_tatweel(text) text = re.sub(r'[إأآا]', 'ا', text) text = re.sub(r'ة', 'ه', text) text = re.sub(r'ى', 'ي', text) text = re.sub(r'\s+', ' ', text).strip() return text # BiLSTM Architecture class AraBERTBiLSTM(nn.Module): def __init__(self, bert_model_name, hidden_dim, num_layers, num_classes, dropout=0.3): super(AraBERTBiLSTM, self).__init__() self.bert = AutoModel.from_pretrained(bert_model_name) for param in self.bert.parameters(): param.requires_grad = False bert_hidden_size = self.bert.config.hidden_size self.bilstm = nn.LSTM( input_size=bert_hidden_size, hidden_size=hidden_dim, num_layers=num_layers, batch_first=True, bidirectional=True, dropout=dropout if num_layers > 1 else 0.0 ) self.dropout = nn.Dropout(dropout) self.classifier = nn.Linear(hidden_dim * 2, num_classes) def forward(self, input_ids, attention_mask): with torch.no_grad(): bert_output = self.bert(input_ids=input_ids, attention_mask=attention_mask) token_embeddings = bert_output.last_hidden_state lstm_output, _ = self.bilstm(token_embeddings) mask = attention_mask.unsqueeze(-1).float() pooled = (lstm_output * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9) pooled = self.dropout(pooled) return self.classifier(pooled) # AraBERTClassifier Architecture class AraBERTClassifier(nn.Module): def __init__(self, model_name, num_classes, dropout=0.3): super(AraBERTClassifier, self).__init__() self.bert = AutoModel.from_pretrained(model_name) self.dropout = nn.Dropout(dropout) self.classifier = nn.Linear(self.bert.config.hidden_size, num_classes) def forward(self, input_ids, attention_mask): output = self.bert(input_ids=input_ids, attention_mask=attention_mask) cls_output = output.last_hidden_state[:, 0, :] cls_output = self.dropout(cls_output) logits = self.classifier(cls_output) return logits # Load Everything print("Loading models...") device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') le = LabelEncoder() le.fit(CLASSES) svm_model = joblib.load('svm_model.pkl') tokenizer = AutoTokenizer.from_pretrained(MODEL_NAME) bilstm_path = hf_hub_download(repo_id=MODEL_HUB, filename='best_bilstm_arabert.pt') bilstm_model = AraBERTBiLSTM(MODEL_NAME, hidden_dim=128, num_layers=2, num_classes=5) bilstm_model.load_state_dict(torch.load(bilstm_path, map_location=device)) bilstm_model.to(device) bilstm_model.eval() arabert_path = hf_hub_download(repo_id=MODEL_HUB, filename='best_arabert.pt') arabert_model = AraBERTClassifier(MODEL_NAME, num_classes=len(CLASSES), dropout=0.1).to(device) arabert_model.load_state_dict(torch.load(arabert_path, map_location=device)) arabert_model.eval() test_df = pd.read_csv('test_with_labels.csv') print("All models loaded") # Predict Functions def predict_svm(text): scores = svm_model.decision_function([text])[0] scores = (scores - scores.min()) / (scores.max() - scores.min() + 1e-9) pred_idx = scores.argmax() pred_class = svm_model.classes_[pred_idx] return pred_class, dict(zip(svm_model.classes_, scores.tolist())) def predict_bilstm(text): enc = tokenizer(text, max_length=MAX_LEN, padding='max_length', truncation=True, return_tensors='pt') with torch.no_grad(): logits = bilstm_model(enc['input_ids'].to(device), enc['attention_mask'].to(device)) probs = torch.softmax(logits, dim=1).cpu().numpy()[0] pred_class = le.classes_[probs.argmax()] return pred_class, dict(zip(le.classes_, probs.tolist())) def predict_arabert(text): enc = tokenizer(text, max_length=MAX_LEN, padding='max_length', truncation=True, return_tensors='pt') with torch.no_grad(): logits = arabert_model(input_ids=enc['input_ids'].to(device), attention_mask=enc['attention_mask'].to(device)) probs = torch.softmax(logits, dim=1).cpu().numpy()[0] pred_class = le.classes_[probs.argmax()] return pred_class, dict(zip(le.classes_, probs.tolist())) # Ground Truth Lookup def get_ground_truth(text): cleaned = clean_text(text) match = test_df[test_df['text'] == cleaned] if len(match) > 0: return match.iloc[0]['label'] return None # Error Analysis for known tweets def get_error_analysis(true_label, pred_label): key = (true_label, pred_label) return ERROR_PATTERNS.get(key, f"The model predicted {pred_label} instead of {true_label}. This may reflect lexical overlap between these classes in Arabic social media text.") # Smart Analysis for new tweets def get_smart_analysis(svm_pred, bilstm_pred, ara_pred, svm_probs, bilstm_probs, ara_probs): predictions = [svm_pred, bilstm_pred, ara_pred] unique_preds = set(predictions) if len(unique_preds) == 1: pred = ara_pred ara_conf = round(ara_probs.get(pred, 0) * 100) if ara_conf >= 80: return {'type': 'agree_high', 'message': f"All 3 models confidently agree: this tweet {CLASS_DESCRIPTIONS.get(pred, '')} ({ara_conf}% confidence by AraBERT). This is an unambiguous case."} else: return {'type': 'agree_low', 'message': f"All 3 models agree on {pred}, but with moderate confidence ({ara_conf}%). The tweet may have overlapping features with other classes."} if len(unique_preds) == 2: majority = max(set(predictions), key=predictions.count) minority_model = None minority_pred = None for model_name, pred in [("SVM", svm_pred), ("BiLSTM", bilstm_pred), ("AraBERT", ara_pred)]: if pred != majority: minority_model = model_name minority_pred = pred pattern_key = (majority, minority_pred) pattern_explanation = ERROR_PATTERNS.get(pattern_key, f"The boundary between {majority} and {minority_pred} can be ambiguous in Arabic social media text.") return {'type': 'partial_disagree', 'message': f"2 models agree on {majority} while {minority_model} predicts {minority_pred}. {pattern_explanation}"} return {'type': 'full_disagree', 'message': f"All 3 models disagree — SVM: {svm_pred}, BiLSTM: {bilstm_pred}, AraBERT: {ara_pred}. This tweet is inherently ambiguous, likely due to mixed communicative intent, sarcasm, or dialectal phrasing."} # SVM Top Features def get_top_features(text, pred_class): vec = svm_model.named_steps['tfidf'] clf = svm_model.named_steps['svm'] feature_names = vec.get_feature_names_out() transformed = vec.transform([text]) class_idx = list(svm_model.classes_).index(pred_class) scores = transformed.toarray()[0] * clf.coef_[class_idx] top_idx = scores.argsort()[-5:][::-1] return [feature_names[i] for i in top_idx if scores[i] > 0] # Main Classify Function def classify(text): if not text.strip(): return "
Please enter an Arabic tweet.
" cleaned = clean_text(text) svm_pred, svm_probs = predict_svm(cleaned) bilstm_pred, bilstm_probs = predict_bilstm(cleaned) ara_pred, ara_probs = predict_arabert(cleaned) ground_truth = get_ground_truth(cleaned) top_features = get_top_features(cleaned, svm_pred) def conf(probs, cls): return round(probs.get(cls, 0) * 100) def verdict(pred, gt): if gt is None: return "" return "Correct" if pred == gt else f"Wrong — True: {gt}" svm_v = verdict(svm_pred, ground_truth) bilstm_v = verdict(bilstm_pred, ground_truth) ara_v = verdict(ara_pred, ground_truth) # ── Colors: hard-coded to work on both light & dark themes ── # Cards use white background with dark text always card_bg = "#ffffff" card_text = "#111111" card_sub = "#555555" card_conf = "#333333" features_bg = "#d4edda" features_text = "#155724" features_title= "#0a4520" bar_bg = "#dddddd" bar_fill = "#2563eb" bar_label = "#222222" bar_pct = "#444444" breakdown_title = "#333333" correct_color = "#15803d" wrong_color = "#dc2626" features_html = " ".join([ f"{w}" for w in top_features ]) or "—" # Analysis Section if ground_truth: errors = [] for model_name, pred in [("SVM", svm_pred), ("BiLSTM", bilstm_pred), ("AraBERT", ara_pred)]: if pred != ground_truth: analysis = get_error_analysis(ground_truth, pred) errors.append(f"{model_name}: {analysis}") if errors: error_html = "SVM + TF-IDF
{svm_pred}
{CLASS_AR.get(svm_pred,'')}
{conf(svm_probs, svm_pred)}% confidence
{svm_v}
BiLSTM
{bilstm_pred}
{CLASS_AR.get(bilstm_pred,'')}
{conf(bilstm_probs, bilstm_pred)}% confidence
{bilstm_v}
AraBERT
{ara_pred}
{CLASS_AR.get(ara_pred,'')}
{conf(ara_probs, ara_pred)}% confidence
{ara_v}
Top signals
{features_html}All classes — confidence breakdown
{bars_html}