A specialized, lightweight AI model for detecting "Depression" vs. "Suicide" risk in Persian text, built using Sequence-Level Knowledge Distillation.
This project demonstrates an Enterprise-Grade Knowledge Distillation pipeline. Instead of relying on scarce or low-quality public datasets, we utilize a Teacher-Student Architecture to synthesize high-quality training data including clinical reasoning (Chain-of-Thought).
-
Teacher Model (
$\mathcal{T}$ ):Meta-Llama-3-70b-Instruct(via API). -
Student Model (
$\mathcal{S}$ ):Google-Gemma-3-1b-it(Local). - Method: Rationale Distillation & Supervised Fine-Tuning (SFT) with QLoRA.
The goal is to compress the reasoning capabilities of a 70B parameter model into a 1B parameter model that can run locally on consumer hardware while maintaining high diagnostic accuracy in Persian.
We implement Distillation via Synthetic Data with the following workflow:
- Seed Sampling: We fetch emotional seed text from the
go_emotionsdataset (Hugging Face). - Teacher Querying: The Teacher model translates seeds to informal Persian and generates a diagnosis with a Step-by-Step Rationale.
- Counterfactual Augmentation: The Teacher generates "hard negatives" (e.g., text that looks like suicide but is just depression) to improve Student robustness.
- Resilient Data Pipeline: Automatic recovery, cleaning, and formatting of data.
- Student Training: The Student is fine-tuned to mimic the Teacher's output format (Reasoning + Label).
Knowledge_Distillation_Project/
│
├── main.py # 🚀 Master Orchestrator (Run this!)
├── requirements.txt # Dependencies
├── README.md # Documentation
│
├── data/
│ └── processed/ # Generated datasets (CSV/Excel)
│
├── models/
│ └── student_gemma_adapter/ # Saved LoRA adapters after training
│
└── src/ # Source Code
├── __init__.py
├── teacher_pipeline.py # Llama-3 Interaction & Data Gen
├── recover_data.py # Crash Recovery & CSV Repair
├── preprocessing.py # Cleaning & Gemma Formatting
├── distillation_trainer.py # QLoRA Training Loop (SFT)
└── inference.py # Interactive CLI for Testing
-
Clone the repository:
git clone https://github.com/mdvr9980/persian-mental-health-distillation.git cd persian-mental-health-distillation -
Install dependencies:
pip install -r requirements.txt
-
Configuration:
- Open
main.pyand ensure yourAPI_KEY(OpenRouter/OpenAI) is set. - (Optional) Set your Hugging Face token in
src/distillation_trainer.pyif accessing gated models.
- Open
The project is controlled via the main.py orchestrator. You can run the entire pipeline or specific steps.
Generates data, cleans it, and trains the model in one go.
python main.py --step all- Phase 1: Teacher Generation
Generate synthetic data using Llama-3.
python main.py --step teacher
- Phase 2: Recovery (Optional)
Repair CSV files if the process was interrupted.
python main.py --step recover
- Phase 3: Preprocessing
Format data for the Gemma architecture.
python main.py --step preprocess
- Phase 4: Training (Student)
Start QLoRA fine-tuning.
python main.py --step student
Chat with your trained model interactively.
python src/inference.py"I feel empty and I don't want to get out of bed."
{
"persian_text": "خیلی احساس پوچی میکنم و اصلاً دلم نمیخواد از تخت بیام بیرون.",
"reasoning": "This sentence expresses severe lethargy and hopelessness, which are core symptoms of Depression. However, there is no active intent or plan to self-harm.",
"label": "Depression"
}<start_of_turn>user
خیلی احساس پوچی میکنم و اصلاً دلم نمیخواد از تخت بیام بیرون.<end_of_turn>
<start_of_turn>model
- LLMs: Meta Llama 3 (Teacher), Google Gemma (Student).
- Training:
bitsandbytes(4-bit Quantization),peft(LoRA),trl(SFTTrainer). - Data Processing:
pandas,datasets,regex. - Hardware: Optimized for Consumer GPUs (T4/RTX 3060+) using
bfloat16and gradient checkpointing.
This AI model is for research purposes only. It is not a substitute for professional medical advice, diagnosis, or treatment. If you or someone you know is in crisis, please contact emergency services.