Toxic Comment Detection for Semnan University Using BERT & Hugging Face

0
Toxic Comment Detection for Semnan University

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

Semnan University  |  Natural Language Processing (NLP)  |  Text Classification
Supervisor Notes Important Points — As Required by the Instructor

The following key points must be clearly communicated in every Deep Neural Networks project presentation and report:

1. Pre-trained Model: The model used in this project — unitary/toxic-bert — is already pre-trained. No training from scratch is required in Phase 1. The model was fine-tuned by Unitary on the Jigsaw dataset and is loaded directly from Hugging Face using the transformers pipeline.
2. Training Dataset (must be downloaded and cited): The dataset on which this model was trained is the Jigsaw Toxic Comment Classification Challenge (jigsaw-toxic-comment-classification-challenge). It must be downloaded from Kaggle and its source clearly documented: kaggle.com/c/jigsaw-toxic-comment-classification-challenge/data
3. Jupyter Notebook format: The project code must be saved and submitted as a Jupyter Notebook (.ipynb) file — not only as a plain .py script. This allows step-by-step execution, inline output, and clear documentation of each cell.
4. PowerPoint presentation: A high-quality PowerPoint (or equivalent slides) must explain the problem in an excellent and clear way — what the issue is, why it matters for Semnan University, and how the neural-network-based solution addresses it.
1 Project Overview
Universities receive hundreds of comments on their official Facebook pages and websites. Some comments are constructive feedback, while others contain offensive language, personal attacks, or cyberbullying. Manual moderation is slow and inconsistent.

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.

Social Media Comment toxic-bert Model Toxicity Score Safe / Toxic Label
Goal of Phase 1: Install the required libraries, load the unitary/toxic-bert model from Hugging Face, run five test comments, and verify that safe comments are classified correctly while toxic ones are flagged.
2 Step 1 — Selecting the Model on Hugging Face

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.


 Figure 1 — The unitary/toxic-bert model page on Hugging Face Hub, confirming the model selection for this project.
3 Step 2 — Environment Setup & Load Model 2.1 Install Dependencies
# Install required libraries (run once)
pip install transformers torch
2.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."
]
4 Step 2 — Classification Logic & Execution 2.3 Classification Logic

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")



 Figure 2 — The Python script loaded and executed in the development environment, showing the toxic-bert pipeline in action.
5 Step 3 — Analysis Results

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.
Validation passed: Comments 1, 2, and 5 received a Safe label because they either praise the university or offer respectful criticism. Comments 3 and 4 received a Toxic label due to offensive words such as trash, useless, stupid, and losers — confirming the security system successfully filters harmful content on the university page.
6 Technical Summary
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، يطلب الدكتور توضيح النقاط التالية في التقرير والعرض. فيما يلي شرح مبسّط لكل متطلب كما ينطبق على مشروعي الحالي:

  1. ١. تحميل مجموعة البيانات (Dataset) وذكر مصدرها
    يجب أن تحمّل الـ Dataset التي تدرب عليها النموذج (أو التي يعتمد عليها مشروعك) وتوضّح من أين حصلت عليها.
    في مشروعي: المصدر هو مسابقة Kaggle باسم Jigsaw Toxic Comment Classification Challenge (jigsaw-toxic-comment-classification-challenge). الرابط: تحميل البيانات من Kaggle
    هذه البيانات تحتوي على آلاف التعليقات من ويكيبيديا، موسومة إما مسيئة (Toxic) أو آمنة (Non-Toxic). نموذج unitary/toxic-bert تم تدريبه (Fine-tune) على هذه البيانات.
  2. ٢. شرح طريقة اختيار النموذج (Model)
    لا يكفي أن تقول «استخدمت موديل» — يجب أن تشرح لماذا اخترته.
    في مشروعي: اخترت unitary/toxic-bert للأسباب التالية:
    • مبني على BERT — شبكة عصبية عميقة قوية في فهم النصوص
    • مدرب مسبقاً على بيانات Jigsaw — مناسب لاكتشاف التعليقات المسيئة
    • جاهز على Hugging Face — لا حاجة لبناء النموذج من الصفر
    • يعطي نسبة ثقة (Confidence) — يسهل تحديد هل التعليق مسيء أم آمن
    • يناسب هدف المشروع: فلترة تعليقات صفحة جامعة سمنان على الفيسبوك والموقع
  3. ٣. شرح المشروع: ما المشكلة وما الحل؟
    عنوان المشروع: Toxic Comment Detection for Semnan University
    المشكلة: بعض التعليقات على صفحات الجامعة تحتوي على إساءة، تنمر إلكتروني، أو كلمات مهينة — والمراجعة اليدوية بطيئة.
    الحل: نظام آلي يفحص كل تعليق قبل نشره. إذا كانت نسبة الإساءة أعلى من 50% يُصنّف Toxic (مسيء)، وإلا يُصنّف Safe (آمن).
  4. ٤. استخدام Google Colab لتشغيل المشروع
    Colab من Google — بيئة مجانية على الإنترنت لتشغيل كود Python و Jupyter بدون تثبيت برامج على جهازك (ويمكن استخدام GPU مجاناً).
    خطوات التشغيل:
    • افتح الموقع: colab.research.google.com
    • أنشئ دفتر جديد (New Notebook) أو ارفع ملف .ipynb
    • في أول خلية (Cell) نفّذ: !pip install transformers torch
    • في خلية ثانية: الصق كود تحميل الموديل وفحص التعليقات (كما في هذا التقرير)
    • اضغط Run — Colab يحمّل النموذج تلقائياً من Hugging Face ويعرض النتائج
٥. الملفات المطلوب تسليمها (Deliverables)

📋 الملفات المطلوبة داخل الفلاش (Flash Drive):

  • ملف البيانات (Dataset): نسخة من قاعدة البيانات التي استُخدمت في المشروع.
    في مشروعي: jigsaw-toxic-comment-classification-challenge من Kaggle — رابط التحميل
  • ملف الكود (Google Colab): يُحمّل الكود من Google Colab ويُحفظ بالصيغة الرسمية .ipynb (Jupyter Notebook) — وليس ملف .py فقط.
  • العرض التقديمي (PowerPoint): ملف بصيغة .pptx يتضمن كافة تفاصيل المشروع (المشكلة، البيانات، النموذج، الكود، النتائج، والخلاصة).
📊 الهيكلية المطلوبة في ملف PowerPoint

يجب أن يغطي العرض التقديمي النقاط الست الأساسية التالية (نفس آلية مشروعي — ولديكم الحرية في الإضافة والتوسع):

  • 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 — الخلاصة الفنية والتقنية للمشروع
🌐 المواقع والمصادر التي ستحتاجونها منصة Hugging Face — للحصول على الموديلات والنماذج الذكية:
🔗 https://huggingface.co
منصة Google Colab — لكتابة وتشغيل الكود البرمجي:
🔗 https://colab.research.google.com/
منصة Kaggle — المصدر الأساسي للبحث عن الـ Dataset:
🔗 https://www.kaggle.com

ملاحظة: يمكنكم البحث عن اسم الـ Dataset في Kaggle، وفي حال تعذّر تحميلها مباشرة، يمكن البحث عنها وتحميلها من موقع GitHub.

بالتوفيق للجميع إن شاء الله 🤲

ملخص: هذا التقرير يلبّي متطلبات الدكتور: النموذج مدرب مسبقاً، مصدر Dataset موضّح، طريقة اختيار الموديل مشروحة، المشروع معرّف، طريقة التشغيل على Google Colab، وملفات التسليم (فلاش + Colab + PowerPoint) مع هيكلية العرض الست — بالإضافة إلى الكود والنتائج أعلاه.

إرسال تعليق

0 تعليقات
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
إرسال تعليق (0)

#buttons=( أقبل ! ) #days=(20)

يستخدم موقعنا ملفات تعريف الارتباط لتعزيز تجربتك. لمعرفة المزيد
أقبل !