miguealguacil
Back to projects

Master's Thesis: Detecting Sexism on Social Media with BERT and LLMs

PythonPyTorchTransformersBERTModernBERTFastAPIReactSQLite

Sexism on social media shows up both explicitly and implicitly, which makes automatic moderation hard: current systems often confuse sexist content with posts that simply report or criticize it, and performance drops sharply when moving from one domain (e.g. Twitter) to another (e.g. forums or Reddit).

This thesis starts from the hypothesis that a BERT-style model fine-tuned specifically for this task can be competitive with, or even outperform, general-purpose LLMs used in zero-shot or few-shot settings, while needing significantly less training data.

Datasets

EDOS (SemEval-2023 Task 10): a Reddit and Gab dataset with 5,000 to 20,000 labeled examples, used in its binary, 3-class, and 4-class variants to evaluate different levels of granularity in sexism detection.

RedditBIAS: around 3,000 sentences focused on gender bias, used to evaluate how well the model generalizes to a domain different from the one it was trained on.

Synthetic sentence set: 15 sentences designed specifically for this thesis, spread across 3 classes, enabling a quick qualitative check of model behaviour on edge cases.

Methodology

Preprocessing of the datasets (text cleaning, balancing classes 50/50 to avoid bias from class imbalance) and splitting into training, validation, and test sets with a 70/10/20 ratio.

Hyperparameter search via a grid search over 54 combinations (learning rate, batch size, number of epochs, etc.) to find the optimal fine-tuning configuration for the BERT and ModernBERT models.

Evaluation of LLMs via few-shot prompting (k=6 examples for binary classification, k=12 for 4-class classification) using logits masking to restrict model output to valid classes.

Evaluation metrics: macro F1, precision, recall, and accuracy, computed consistently across all models and datasets.

Conclusions

Fine-tuned BERT-style models consistently outperform un-tuned LLMs: even the best result obtained with Mistral-7B-Instruct in few-shot (F1 = 0.413) is more than 40 percentage points below the fine-tuned models.

Reducing the EDOS training set from 20,000 to 10,000 examples is viable: it saves roughly 87% of GPU-hours at the cost of only 4 percentage points of F1.

Identified limitations: confusion between critical/reporting content and sexist content, difficulty detecting irony and sarcasm, and the lack of multilingual evaluation, which remains open as future work.

Web Application

The binary ModernBERT-base model (trained on reduced_10k) was integrated into a full-stack application that demonstrates its use in a real scenario: a FastAPI microservice with a React frontend, SQLite persistence, and JWT authentication with three roles: admin (full management of users and roles), sexism_detection (run and view analyses), and analytics (read-only access to analytics).

The sexism detector offers three analysis modes: free text (split into sentences, with an overall result and a per-sentence breakdown), URL (analyzes the text content of a page, with an optional filter by HTML tag), and full domain (respects robots.txt, locates the sitemap.xml, extracts indexable URLs, and runs inference on them in parallel).

The analytics module consolidates results in three views: a global dashboard (analyzed URLs and sentences, estimated sexism %, top-5 most sexist sentences, and a severity histogram), a list of analyzed domains, and a paginated list of URLs per domain with access to the sentence-by-sentence detail of each one.

As a proof of concept, the University of Granada's main portal (www.ugr.es) was analyzed: across 590 URLs and 12,643 sentences, only 22 sentences (≈0.002%) were flagged as sexist, and manual review confirmed they were false positives (sentences talking about gender discrimination, not perpetrating it). That's the expected result for an institutional website, and a sign that the model does not over-flag content indiscriminately.

Results

F1 (ModernBERT-base, EDOS-10k)0.843
Recall (ModernBERT-base, EDOS-10k)0.853
Accuracy (ModernBERT-base, EDOS-10k)0.843
F1 (bert-base-uncased, EDOS-10k)0.836
F1 (bert-base-uncased, EDOS-20k)0.7876
F1 (Mistral-7B-Instruct, few-shot)0.413
F1 (synthetic sentences, BERT)0.95-0.96

Screenshots

Bar chart comparing macro F1 across models on the EDOS, RedditBIAS, and synthetic-sentence datasets
F1 (macro) comparison by model and dataset in binary classification
Application login screen with username and password fields
Application login screen
Free-text analysis result showing the overall percentage of sexist sentences and a per-sentence breakdown
Free-text analysis with overall result and per-sentence breakdown
Analysis result of a URL's text content, with an overall summary and per-sentence detail
Text content analysis of a URL
Analysis result of a full domain, showing the located sitemaps and the detected URLs
Full domain analysis from its sitemap.xml
Global analytics dashboard with analyzed URLs and sentences, sexism percentage, top-5 most sexist sentences, and a severity histogram
Global analytics dashboard: analyzed URLs/sentences, sexism %, top-5 sentences, severity histogram
List of analyzed websites with search and access to each domain detail
List of analyzed domains
Paginated list of URLs for a domain with their sexism percentage and classification
Paginated list of URLs for a domain with their classification
Sentence-by-sentence detail of a URL's analysis, with search and classification filters
Sentence-by-sentence detail of a URL's analysis

Links