MLOps Hate Speech Project Documentation
Documentation of core classes and functions used in this project.
Data Preparation
load_and_prepare_dataset
Loads, merges, splits, and saves the hate speech dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seed
|
int
|
Random seed for shuffling the dataset. |
42
|
save_path
|
Optional[str]
|
Path to save the processed dataset. |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/mlops_hatespeech/data.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
|
Model Training
get_config
Get the configuration from Hydra.
Source code in src/mlops_hatespeech/train.py
33 34 35 36 |
|
train_model
Load configuration using Hydra with optional overrides.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
overrides
|
Optional[List[str]]
|
List of override strings |
required |
Returns:
Name | Type | Description |
---|---|---|
DictConfig |
Trainer
|
Composed configuration object. |
Source code in src/mlops_hatespeech/train.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
|
train
Entry point for training. Optionally override key hyperparameters.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lr
|
Optional[float]
|
Learning rate. |
None
|
wd
|
Optional[float]
|
Weight decay. |
None
|
epochs
|
Optional[int]
|
Number of training epochs. |
None
|
seed
|
Optional[int]
|
Random seed. |
None
|
Source code in src/mlops_hatespeech/train.py
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 |
|
Model Evaluation
find_latest_checkpoint
Finds the latest checkpoint folder in the given directory based on the highest checkpoint number.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
run_dir
|
str
|
Path to the directory containing checkpoint folders. |
'logs/run1'
|
Returns:
Name | Type | Description |
---|---|---|
str |
str
|
Path to the latest checkpoint folder. |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no checkpoints are found. |
Source code in src/mlops_hatespeech/evaluate.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
|
compute_metrics
Computes accuracy and weighted F1 score from model predictions and true labels.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eval_preds
|
Any
|
An object with 'predictions' (logits) and 'label_ids' (true labels). |
required |
Returns:
Type | Description |
---|---|
Dict[str, float]
|
Dict[str, float]: Dictionary with 'f1' and 'accuracy' scores. |
Source code in src/mlops_hatespeech/evaluate.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
|
main
Main function: Loads the latest checkpoint, prepares the dataset, runs evaluation, and uploads results as a JSON file to a GCS bucket.
Source code in src/mlops_hatespeech/evaluate.py
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
|
Drift Detection
get_bert_embeddings
Generates mean-pooled BERT embeddings for a list of texts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
texts
|
List[str]
|
Input texts. |
required |
tokenizer
|
PreTrainedTokenizer
|
Tokenizer for the model. |
required |
model
|
PreTrainedModel
|
Pretrained BERT model. |
required |
device
|
str
|
Device to run the model on, default is "cpu". |
'cpu'
|
Returns:
Type | Description |
---|---|
ndarray
|
np.ndarray: 2D numpy array with shape (n_texts, hidden_size). |
Source code in src/mlops_hatespeech/drift_detector.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
download_predictions_from_gcs
Downloads prediction JSON files from a Google Cloud Storage bucket.
Each JSON is expected to contain a 'tweet' (under 'input_text') and a 'label' (under 'prediction'). These are collected into a DataFrame for further processing.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
bucket_name
|
str
|
Name of the GCS bucket. |
required |
prefix
|
str
|
Prefix path under which prediction JSON files are stored. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: DataFrame with columns 'tweet' and 'label' from all valid JSON files. |
Source code in src/mlops_hatespeech/drift_detector.py
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
|
upload_report_to_gcs
Uploads a local file (e.g. an HTML report) to a specified location in a GCS bucket.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
local_path
|
str
|
Path to the local file to be uploaded. |
required |
bucket_name
|
str
|
Name of the target GCS bucket. |
required |
destination_path
|
str
|
Path (including filename) in the bucket where the file should be stored. |
required |
Returns:
Type | Description |
---|---|
None
|
None |
Source code in src/mlops_hatespeech/drift_detector.py
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
main
Main routine for detecting embedding-based data drift on tweet inputs using BERT embeddings and uploading the Evidently report to GCS.
Source code in src/mlops_hatespeech/drift_detector.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
|