Downloads · 30 days
0
Kenny-lalek/algerian-darija-misinformation-lr
algerian-darija-misinformation-lr is a machine learning model from Kenny-lalek. Use it for the machine learning task on the model card, and read the license before you ship it in a product.
This model is a Logistic Regression classifier trained to detect misinformation in Algerian Darija (Algerian Arabic dialect) text.
Downloads · 30 days
0
Access
Public
Updated Dec 29, 2025
Repo size
786 KB
Likes
0
Public
Click a slice to open those files.
.joblib786 KB · 99%
From the Hugging Face model README
This model is a Logistic Regression classifier trained to detect misinformation in Algerian Darija (Algerian Arabic dialect) text.
The model classifies text into 5 categories:
Vectorization: TF-IDF
Classifier: Logistic Regression
Features: TF-IDF + Metadata (6 features)
pip install huggingface_hub scikit-learn numpy scipy
import joblib
import numpy as np
import re
from huggingface_hub import hf_hub_download
from scipy.sparse import hstack, csr_matrix
# Load model artifacts
REPO_NAME = "Kenny-lalek/algerian-darija-misinformation-lr"
tfidf_vectorizer = joblib.load(
hf_hub_download(repo_id=REPO_NAME, filename="tfidf_vectorizer.joblib")
)
model = joblib.load(
hf_hub_download(repo_id=REPO_NAME, filename="logistic_regression_model.joblib")
)
metadata_scaler = joblib.load(
hf_hub_download(repo_id=REPO_NAME, filename="metadata_scaler.joblib")
)
# Preprocessing function
def remove_diacritics(text):
arabic_diacritics = re.compile("""
ّ | # Tashdid
َ | # Fatha
ً | # Tanwin Fath
ُ | # Damma
ٌ | # Tanwin Damm
ِ | # Kasra
ٍ | # Tanwin Kasr
ْ | # Sukun
ـ # Tatwil/Kashida
""", re.VERBOSE)
return re.sub(arabic_diacritics, '', text)
def preprocess_text(text):
if text is None or text == "":
return ""
text = str(text)
text = remove_diacritics(text)
text = ' '.join(text.split())
return text.strip()
def extract_metadata_features(text):
word_count = len(text.split())
punctuation_chars = ".,!?;:،؛"
punctuation_count = sum(1 for char in text if char in punctuation_chars)
punctuation_density = punctuation_count / len(text) if len(text) > 0 else 0
exclamation_count = text.count('!')
question_count = text.count('?') + text.count('؟')
letters = [c for c in text if c.isalpha()]
all_caps_ratio = sum(1 for c in letters if c.isupper()) / len(letters) if len(letters) > 0 else 0
emoji_count = 0
return np.array([[word_count, punctuation_density, exclamation_count,
question_count, all_caps_ratio, emoji_count]])
# Predict function
def predict(text):
# Preprocess
processed_text = preprocess_text(text)
# Vectorize
text_features = tfidf_vectorizer.transform([processed_text])
# Add metadata
metadata = extract_metadata_features(processed_text)
metadata_scaled = metadata_scaler.transform(metadata)
features = hstack([text_features, csr_matrix(metadata_scaled)])
# Predict
prediction = model.predict(features)[0]
probabilities = model.predict_proba(features)[0]
return {
'label': prediction,
'confidence': float(np.max(probabilities)),
'probabilities': {
label: float(prob)
for label, prob in zip(['F', 'R', 'N', 'M', 'S'], probabilities)
}
}
# Example usage
text = "النظام المافيوي العصاباباتي"
result = predict(text)
print(f"Predicted: {result['label']}")
print(f"Confidence: {result['confidence']:.4f}")
For easier usage, use the provided wrapper class from the test script:
import joblib
import numpy as np
import re
from huggingface_hub import hf_hub_download
class AlgerianDarijaClassifier:
"""Wrapper class for the trained model"""
def __init__(self, repo_name, model_type="enhanced"):
"""
Initialize and load model from Hugging Face Hub
Args:
repo_name: HuggingFace repo name
model_type: "enhanced" or "baseline"
"""
self.repo_name = repo_name
self.model_type = model_type
self.classes = ['F', 'R', 'N', 'M', 'S']
print(f"Loading model from {repo_name}...")
# Download and load artifacts
self.tfidf_vectorizer = joblib.load(
hf_hub_download(repo_id=repo_name, filename="tfidf_vectorizer.joblib")
)
self.model = joblib.load(
hf_hub_download(repo_id=repo_name, filename="logistic_regression_model.joblib")
)
if model_type == "enhanced":
self.metadata_scaler = joblib.load(
hf_hub_download(repo_id=repo_name, filename="metadata_scaler.joblib")
)
else:
self.metadata_scaler = None
print("Model loaded successfully!")
def predict(self, text):
"""
Predict the class of a single text
Args:
text: Input text in Algerian Darija
Returns:
dict: Prediction results with label and probabilities
"""
# Preprocess
processed_text = preprocess_text(text)
# Vectorize
text_features = self.tfidf_vectorizer.transform([processed_text])
# Add metadata if enhanced model
if self.model_type == "enhanced":
metadata = extract_metadata_features(processed_text)
metadata_scaled = self.metadata_scaler.transform(metadata)
# Combine features
from scipy.sparse import hstack, csr_matrix
features = hstack([text_features, csr_matrix(metadata_scaled)])
else:
features = text_features
# Predict
prediction = self.model.predict(features)[0]
probabilities = self.model.predict_proba(features)[0]
# Create result dictionary
result = {
'predicted_label': prediction,
'confidence': float(np.max(probabilities)),
'probabilities': {
class_label: float(prob)
for class_label, prob in zip(self.classes, probabilities)
}
}
return result
def predict_batch(self, texts):
"""
Predict classes for multiple texts
Args:
texts: List of input texts
Returns:
list: List of prediction dictionaries
"""
return [self.predict(text) for text in texts]
# Initialize
classifier = AlgerianDarijaClassifier(
repo_name="Kenny-lalek/algerian-darija-misinformation-lr",
model_type="enhanced"
)
# Predict single text
example = "insert a news statement in daridja"
result = classifier.predict(example)
print(result)
# Predict multiple texts
texts = ["text1", "text2", "text3"]
results = classifier.predict_batch(texts)
Apache 2.0
For questions or issues, please open an issue on the repository.