An automated cyber-security pipeline using Hugging Face Transformers and the unitary/toxic-bert model to classify social-media comments as safe or toxic before they appear on the university website.
By Wesam Kareem
The following key points must be clearly communicated in every Deep Neural Networks project presentation and report:
This project builds an automated content-moderation system that scans incoming comments in real time and flags toxic content before it is published. The first phase focuses on loading a pre-trained model and validating it against sample comments related to Semnan University.
The model chosen for this project is unitary/toxic-bert, hosted on the Hugging Face Model Hub. It is a BERT-based classifier fine-tuned specifically for detecting toxic, hateful, and abusive language in English text.
This model is based on the Jigsaw Toxic Comment Classification Challenge dataset (jigsaw-toxic-comment-classification-challenge), available on Kaggle: kaggle.com/c/jigsaw-toxic-comment-classification-challenge/data. The dataset contains thousands of Wikipedia talk-page comments labeled as toxic or non-toxic, which was used to train and fine-tune BERT models for automated content moderation.
Why toxic-bert?- Fine-tuned on the Jigsaw toxic comment dataset — a large, labeled corpus of toxic vs. non-toxic comments.
- Available as a ready-to-use pipeline via the transformers library — no custom training required for Phase 1.
- Returns a confidence score (0–100%) for each prediction, enabling a clear threshold rule.
- Widely used in industry for content moderation and cyberbullying detection.
# Install required libraries (run once) pip install transformers torch2.2 Load the Model & Prepare Test Comments
from transformers import pipeline
# Load the toxic-bert classifier from Hugging Face
cyber_detector = pipeline("text-classification", model="unitary/toxic-bert")
# Sample comments simulating Facebook posts about Semnan University
comments = [
"Semnan University has an amazing campus and the professors are very supportive. Proud to be a student here!",
"The engineering department at Semnan University is doing a great job. Thanks for the guidance.",
"The website of this university is absolute trash, and the IT staff are completely useless and stupid!",
"I really hate this place, the exams are unfair and the professors are terrible losers.",
"I faced some challenges with my registration, but the academic staff helped me resolve it quickly."
]
Each comment is passed through the model. The returned score represents the model's confidence that the text is toxic. A threshold of 50% is applied:
- Score > 50% → classified as Toxic (cyberbullying / offensive)
- Score ≤ 50% → classified as Safe (constructive / neutral)
print("--- Cyber Security: Semnan University Comment Analysis Results ---\n")
for comment in comments:
result = cyber_detector(comment)[0]
score = result['score'] * 100 # Convert to percentage
if score > 50.0:
status = "Toxic (Offensive / Cyberbullying)"
confidence = score
else:
status = "Safe (Constructive / Neutral)"
confidence = 100 - score
print(f"Comment: \"{comment}\"")
print(f"Result: {status} | Confidence: {confidence:.2f}%\n")
After running the script, the model correctly separated safe comments from toxic ones. The table below summarizes the expected classification for each test comment:
| # | Comment (summary) | Expected Label | Reason |
|---|---|---|---|
| 1 | Praise for campus and professors | Safe | Positive, supportive language |
| 2 | Thanks to engineering department | Safe | Gratitude and appreciation |
| 3 | "trash", "useless", "stupid" | Toxic | Offensive insults directed at staff |
| 4 | "hate", "terrible losers" | Toxic | Personal attacks and cyberbullying |
| 5 | Registration issue resolved by staff | Safe | Constructive criticism with positive resolution |
Figure 3 — Console output showing three Safe comments (green) and two Toxic comments (red) with confidence percentages.
| Component | Details |
|---|---|
| Platform | Hugging Face Model Hub |
| Model | unitary/toxic-bert |
| Training Dataset | Jigsaw Toxic Comment Classification Challenge (jigsaw-toxic-comment-classification-challenge) |
| Task | Text Classification (Toxic vs. Non-Toxic) |
| Library | transformers + torch |
| Threshold | 50% — score above threshold = Toxic |
| Test Set | 5 sample comments (3 safe, 2 toxic) |
| Accuracy (Phase 1) | 5 / 5 comments classified correctly |
عندما تختار مشروعاً في مادة Deep Neural Networks، يطلب الدكتور توضيح النقاط التالية في التقرير والعرض. فيما يلي شرح مبسّط لكل متطلب كما ينطبق على مشروعي الحالي:
-
١. تحميل مجموعة البيانات (Dataset) وذكر مصدرها
يجب أن تحمّل الـ Dataset التي تدرب عليها النموذج (أو التي يعتمد عليها مشروعك) وتوضّح من أين حصلت عليها.
في مشروعي: المصدر هو مسابقة Kaggle باسم Jigsaw Toxic Comment Classification Challenge (jigsaw-toxic-comment-classification-challenge). الرابط: تحميل البيانات من Kaggle
هذه البيانات تحتوي على آلاف التعليقات من ويكيبيديا، موسومة إما مسيئة (Toxic) أو آمنة (Non-Toxic). نموذج unitary/toxic-bert تم تدريبه (Fine-tune) على هذه البيانات. -
٢. شرح طريقة اختيار النموذج (Model)
لا يكفي أن تقول «استخدمت موديل» — يجب أن تشرح لماذا اخترته.
في مشروعي: اخترت unitary/toxic-bert للأسباب التالية:- مبني على BERT — شبكة عصبية عميقة قوية في فهم النصوص
- مدرب مسبقاً على بيانات Jigsaw — مناسب لاكتشاف التعليقات المسيئة
- جاهز على Hugging Face — لا حاجة لبناء النموذج من الصفر
- يعطي نسبة ثقة (Confidence) — يسهل تحديد هل التعليق مسيء أم آمن
- يناسب هدف المشروع: فلترة تعليقات صفحة جامعة سمنان على الفيسبوك والموقع
-
٣. شرح المشروع: ما المشكلة وما الحل؟
عنوان المشروع: Toxic Comment Detection for Semnan University
المشكلة: بعض التعليقات على صفحات الجامعة تحتوي على إساءة، تنمر إلكتروني، أو كلمات مهينة — والمراجعة اليدوية بطيئة.
الحل: نظام آلي يفحص كل تعليق قبل نشره. إذا كانت نسبة الإساءة أعلى من 50% يُصنّف Toxic (مسيء)، وإلا يُصنّف Safe (آمن). -
٤. استخدام Google Colab لتشغيل المشروع
Colab من Google — بيئة مجانية على الإنترنت لتشغيل كود Python و Jupyter بدون تثبيت برامج على جهازك (ويمكن استخدام GPU مجاناً).
خطوات التشغيل:- افتح الموقع: colab.research.google.com
- أنشئ دفتر جديد (New Notebook) أو ارفع ملف .ipynb
- في أول خلية (Cell) نفّذ: !pip install transformers torch
- في خلية ثانية: الصق كود تحميل الموديل وفحص التعليقات (كما في هذا التقرير)
- اضغط Run — Colab يحمّل النموذج تلقائياً من Hugging Face ويعرض النتائج
📋 الملفات المطلوبة داخل الفلاش (Flash Drive):
-
ملف البيانات (Dataset):
نسخة من قاعدة البيانات التي استُخدمت في المشروع.
في مشروعي: jigsaw-toxic-comment-classification-challenge من Kaggle — رابط التحميل - ملف الكود (Google Colab): يُحمّل الكود من Google Colab ويُحفظ بالصيغة الرسمية .ipynb (Jupyter Notebook) — وليس ملف .py فقط.
- العرض التقديمي (PowerPoint): ملف بصيغة .pptx يتضمن كافة تفاصيل المشروع (المشكلة، البيانات، النموذج، الكود، النتائج، والخلاصة).
يجب أن يغطي العرض التقديمي النقاط الست الأساسية التالية (نفس آلية مشروعي — ولديكم الحرية في الإضافة والتوسع):
- Project Overview — نظرة عامة على المشروع وأهدافه
- Step 1 — Selecting the Model on Hugging Face — آلية اختيار النموذج المناسب من المنصة
- Step 2 — Environment Setup & Load Model — إعداد البيئة البرمجية واستدعاء النموذج
- Step 3 — Classification Logic & Execution — منطق التصنيف وآلية التنفيذ برمجياً
- Step 4 — Analysis Results — تحليل النتائج والمخرجات التي تم التوصل إليها
- Technical Summary — الخلاصة الفنية والتقنية للمشروع
🔗 https://huggingface.co منصة Google Colab — لكتابة وتشغيل الكود البرمجي:
🔗 https://colab.research.google.com/ منصة Kaggle — المصدر الأساسي للبحث عن الـ Dataset:
🔗 https://www.kaggle.com
ملاحظة: يمكنكم البحث عن اسم الـ Dataset في Kaggle، وفي حال تعذّر تحميلها مباشرة، يمكن البحث عنها وتحميلها من موقع GitHub.


.png)
