1# test_model_3_2.py
2import os
3
4# CRITICAL: Disable TensorFlow before importing transformers
5os.environ['USE_TF'] = '0'
6os.environ['USE_TORCH'] = '1'
7
8from transformers import AutoTokenizer, AutoModelForSequenceClassification
9import torch
10
11# Load from HuggingFace Hub
12REPO_ID = "aurelius2023/xlm-roberta-algerian-misinformation"
13
14print("="*70)
15print("MODEL 3.2: XLM-RoBERTa Algerian Misinformation Detection")
16print("="*70)
17
18print("
19Loading model from Hugging Face Hub...")
20tokenizer = AutoTokenizer.from_pretrained(REPO_ID)
21model = AutoModelForSequenceClassification.from_pretrained(REPO_ID)
22
23print("✓ Model loaded successfully!")
24print(f"✓ Model: {REPO_ID}")
25print(f"✓ Base: xlm-roberta-base")
26print(f"✓ Performance: F1=0.6733, Accuracy=0.7617")
27
28# Label mappings
29label_map = {0: 'F', 1: 'R', 2: 'N', 3: 'M', 4: 'S'}
30label_names = {
31 'F': 'Fake',
32 'R': 'Real',
33 'N': 'Non-news',
34 'M': 'Misleading',
35 'S': 'Satire'
36}
37
38# Test with multiple examples
39test_examples = [
40 "وزير الشباب الجزائري يكشف ان الدول الاوروبيه تطلب من الجزائر حلولًا لمعالجه المشكلات الاجتماعيه لشبابها",
41 "الجزائر فازت بكأس العالم 2024",
42 "الحكومة أعلنت عن إصلاحات جديدة في قطاع التعليم"
43]
44
45print("
46" + "="*70)
47print("TEST PREDICTIONS")
48print("="*70)
49
50for i, text in enumerate(test_examples, 1):
51 print(f"
52--- Test {i} ---")
53 print(f"Text: {text[:80]}{'...' if len(text) > 80 else ''}")
54
55 # Tokenize
56 inputs = tokenizer(text, return_tensors="pt", max_length=128,
57 truncation=True, padding=True)
58
59 # Predict
60 with torch.no_grad():
61 outputs = model(**inputs)
62 probs = torch.softmax(outputs.logits, dim=1)[0]
63 pred = torch.argmax(probs).item()
64 confidence = probs[pred].item()
65
66 # Display results
67 predicted_label = label_map[pred]
68 predicted_name = label_names[predicted_label]
69
70 print(f"Predicted: {predicted_name} ({predicted_label})")
71 print(f"Confidence: {confidence:.2%}")
72
73 # Show all probabilities
74 print("All probabilities:")
75 for idx in range(5):
76 label = label_map[idx]
77 name = label_names[label]
78 prob = probs[idx].item()
79 bar = "█" * int(prob * 20)
80 print(f" {label} ({name:12s}): {prob:6.2%} {bar}")
81
82print("
83" + "="*70)
84print("✅ ALL TESTS COMPLETED SUCCESSFULLY!")
85print("="*70)
86
87print("
88📊 Model Performance Summary:")
89print(" • Accuracy: 76.17%")
90print(" • Macro F1: 67.33%")
91print(" • Best classes: F (84.16%), R (77.69%), N (80.67%)")
92print(" • Challenge: S class (30.12%) - limited training data")
93
94print(f"
95🔗 Model Card: https://huggingface.co/{REPO_ID}")
For questions, issues, or collaboration opportunities, please open an issue on the model repository.