File size: 10,094 Bytes
168a930
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
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
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
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
"""
Configuration module for MLOps platform.
Contains all configuration classes and constants.
"""

from dataclasses import dataclass, field
from typing import Dict, List, Optional
from enum import Enum


class LanguageCode(str, Enum):
    """Supported language codes."""
    ENGLISH = "en"
    CHINESE = "zh"
    KHMER = "km"


class ClassificationType(str, Enum):
    """Classification task types."""
    BINARY = "binary"
    MULTICLASS = "multiclass"


# Supported languages with display names
SUPPORTED_LANGUAGES: Dict[str, Dict[str, str]] = {
    "en": {
        "name": "English",
        "native_name": "English",
        "description": "English language support with standard NLP preprocessing",
        "tokenizer_hint": "Uses standard word tokenization"
    },
    "zh": {
        "name": "Chinese",
        "native_name": "ไธญๆ–‡",
        "description": "Chinese language support with character-level tokenization",
        "tokenizer_hint": "Uses jieba for word segmentation"
    },
    "km": {
        "name": "Khmer",
        "native_name": "แž—แžถแžŸแžถแžแŸ’แž˜แŸ‚แžš",
        "description": "Khmer language support with specialized tokenization",
        "tokenizer_hint": "Uses ICU-based tokenization for Khmer script"
    }
}

# Model architectures supported with recommendations
MODEL_ARCHITECTURES = {
    "roberta-base": {
        "name": "RoBERTa Base",
        "description": "Robust BERT model, excellent for English text classification",
        "languages": ["en"],
        "max_length": 512,
        "recommended_for": "English only, high accuracy needed",
        "speed": "Medium",
        "size": "355MB",
        "best_use": "English binary/multiclass classification"
    },
    "bert-base-multilingual-cased": {
        "name": "mBERT (Multilingual BERT)",
        "description": "Supports 104 languages - Good balance of performance and multilingual support",
        "languages": ["en", "zh", "km"],
        "max_length": 512,
        "recommended_for": "Multilingual tasks, balanced performance",
        "speed": "Medium",
        "size": "665MB",
        "best_use": "Multilingual classification, good general-purpose model"
    },
    "xlm-roberta-base": {
        "name": "XLM-RoBERTa Base",
        "description": "Best multilingual model - Highest accuracy for Chinese, Khmer, and other languages",
        "languages": ["en", "zh", "km"],
        "max_length": 512,
        "recommended_for": "Best multilingual performance, recommended for Chinese/Khmer",
        "speed": "Medium-Slow",
        "size": "1.03GB",
        "best_use": "When you need the best accuracy across multiple languages"
    },
    "distilbert-base-multilingual-cased": {
        "name": "DistilBERT Multilingual (Recommended for CPU)",
        "description": "Lightweight and fast - Perfect for CPU training or quick experiments",
        "languages": ["en", "zh", "km"],
        "max_length": 512,
        "recommended_for": "CPU training, fast experiments, limited resources",
        "speed": "Fast",
        "size": "525MB",
        "best_use": "CPU-only systems, quick prototyping, limited GPU memory"
    }
}

# Model selection guide
MODEL_SELECTION_GUIDE = {
    "cpu_training": "distilbert-base-multilingual-cased",
    "gpu_training_english": "roberta-base",
    "gpu_training_multilingual": "xlm-roberta-base",
    "quick_experiment": "distilbert-base-multilingual-cased",
    "production_english": "roberta-base",
    "production_multilingual": "xlm-roberta-base"
}


@dataclass
class TrainingConfig:
    """Configuration for model training."""
    
    # Model settings
    model_name: str = "bert-base-multilingual-cased"
    num_labels: int = 2
    
    # Training hyperparameters
    learning_rate: float = 2e-5
    batch_size: int = 16
    num_epochs: int = 3
    warmup_ratio: float = 0.1
    weight_decay: float = 0.01
    max_length: int = 256
    
    # Data settings
    train_split: float = 0.8
    validation_split: float = 0.1
    test_split: float = 0.1
    shuffle_data: bool = True
    random_seed: int = 42
    
    # Language settings
    language: str = "en"
    
    # Output settings
    output_dir: str = "trained_models"
    save_best_model: bool = True
    logging_steps: int = 10
    eval_strategy: str = "epoch"
    
    # Performance settings
    use_fp16: bool = False  # Disabled for CPU compatibility
    gradient_accumulation_steps: int = 1
    
    # Labels configuration
    label_names: List[str] = field(default_factory=lambda: ["Legitimate", "Phishing"])
    
    def validate(self) -> List[str]:
        """Validate configuration and return list of warnings/errors."""
        issues = []
        
        if self.learning_rate <= 0:
            issues.append("Learning rate must be positive")
        if self.batch_size < 1:
            issues.append("Batch size must be at least 1")
        if self.num_epochs < 1:
            issues.append("Number of epochs must be at least 1")
        if self.train_split + self.validation_split + self.test_split > 1.0:
            issues.append("Sum of data splits cannot exceed 1.0")
        if self.language not in SUPPORTED_LANGUAGES:
            issues.append(f"Unsupported language: {self.language}")
            
        return issues
    
    def to_dict(self) -> dict:
        """Convert config to dictionary."""
        return {
            "model_name": self.model_name,
            "num_labels": self.num_labels,
            "learning_rate": self.learning_rate,
            "batch_size": self.batch_size,
            "num_epochs": self.num_epochs,
            "warmup_ratio": self.warmup_ratio,
            "weight_decay": self.weight_decay,
            "max_length": self.max_length,
            "train_split": self.train_split,
            "validation_split": self.validation_split,
            "test_split": self.test_split,
            "shuffle_data": self.shuffle_data,
            "random_seed": self.random_seed,
            "language": self.language,
            "output_dir": self.output_dir,
            "label_names": self.label_names
        }


@dataclass  
class ExperimentConfig:
    """Configuration for experiment tracking."""
    
    experiment_name: str = "content_detection"
    run_name: Optional[str] = None
    tags: Dict[str, str] = field(default_factory=dict)
    description: str = ""
    
    # MLflow settings (optional)
    use_mlflow: bool = False
    mlflow_tracking_uri: str = "mlruns"
    

# UI Translation strings
UI_TRANSLATIONS = {
    "en": {
        "app_title": "MLOps Training Platform",
        "sidebar_title": "Configuration",
        "language_select": "Select Target Language",
        "upload_data": "Upload Dataset",
        "training_config": "Training Configuration",
        "start_training": "Start Training",
        "training_progress": "Training Progress",
        "evaluation": "Model Evaluation",
        "download_model": "Download Model",
        "upload_help": "Upload a CSV file with 'text' and 'label' columns",
        "metrics_title": "Training Metrics",
        "confusion_matrix": "Confusion Matrix",
        "success_msg": "Training completed successfully!",
        "error_msg": "An error occurred during training",
        "welcome_msg": "Welcome to the MLOps Training Platform",
        "data_preview": "Data Preview",
        "class_distribution": "Class Distribution"
    },
    "zh": {
        "app_title": "๐Ÿค– ๆœบๅ™จๅญฆไน ่ฟ็ปด่ฎญ็ปƒๅนณๅฐ",
        "sidebar_title": "้…็ฝฎ",
        "language_select": "้€‰ๆ‹ฉ็›ฎๆ ‡่ฏญ่จ€",
        "upload_data": "ไธŠไผ ๆ•ฐๆฎ้›†",
        "training_config": "่ฎญ็ปƒ้…็ฝฎ",
        "start_training": "ๅผ€ๅง‹่ฎญ็ปƒ",
        "training_progress": "่ฎญ็ปƒ่ฟ›ๅบฆ",
        "evaluation": "ๆจกๅž‹่ฏ„ไผฐ",
        "download_model": "ไธ‹่ฝฝๆจกๅž‹",
        "upload_help": "ไธŠไผ ๅŒ…ๅซ 'text' ๅ’Œ 'label' ๅˆ—็š„CSVๆ–‡ไปถ",
        "metrics_title": "่ฎญ็ปƒๆŒ‡ๆ ‡",
        "confusion_matrix": "ๆททๆท†็Ÿฉ้˜ต",
        "success_msg": "่ฎญ็ปƒๆˆๅŠŸๅฎŒๆˆ๏ผ",
        "error_msg": "่ฎญ็ปƒ่ฟ‡็จ‹ไธญๅ‘็”Ÿ้”™่ฏฏ",
        "welcome_msg": "ๆฌข่ฟŽไฝฟ็”จๆœบๅ™จๅญฆไน ่ฟ็ปด่ฎญ็ปƒๅนณๅฐ",
        "data_preview": "ๆ•ฐๆฎ้ข„่งˆ",
        "class_distribution": "็ฑปๅˆซๅˆ†ๅธƒ"
    },
    "km": {
        "app_title": "๐Ÿค– แžœแŸแž‘แžทแž€แžถแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž› MLOps",
        "sidebar_title": "แž€แžถแžšแž€แŸ†แžŽแžแŸ‹",
        "language_select": "แž‡แŸ’แžšแžพแžŸแžšแžพแžŸแž—แžถแžŸแžถแž‚แŸ„แž›แžŠแŸ…",
        "upload_data": "แž•แŸ’แž‘แžปแž€แžกแžพแž„แžŸแŸ†แžŽแžปแŸ†แž‘แžทแž“แŸ’แž“แž“แŸแž™",
        "training_config": "แž€แžถแžšแž€แŸ†แžŽแžแŸ‹แž€แžถแžšแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›",
        "start_training": "แž…แžถแž”แŸ‹แž•แŸ’แžแžพแž˜แž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›",
        "training_progress": "แžœแžŒแŸ’แžแž“แž—แžถแž–แž“แŸƒแž€แžถแžšแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›",
        "evaluation": "แž€แžถแžšแžœแžถแž™แžแž˜แŸ’แž›แŸƒแž˜แŸ‰แžผแžŠแŸ‚แž›",
        "download_model": "แž‘แžถแž‰แž™แž€แž˜แŸ‰แžผแžŠแŸ‚แž›",
        "upload_help": "แž•แŸ’แž‘แžปแž€แžกแžพแž„แžฏแž€แžŸแžถแžš CSV แžŠแŸ‚แž›แž˜แžถแž“แž‡แžฝแžšแžˆแžš 'text' แž“แžทแž„ 'label'",
        "metrics_title": "แžšแž„แŸ’แžœแžถแžŸแŸ‹แž“แŸƒแž€แžถแžšแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›",
        "confusion_matrix": "แž˜แŸ‰แžถแž‘แŸ’แžšแžธแžŸแž—แžถแž–แž…แŸ’แžšแžกแŸ†",
        "success_msg": "แž€แžถแžšแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›แž”แžถแž“แž‡แŸ„แž‚แž‡แŸแž™!",
        "error_msg": "แž€แŸ†แž แžปแžŸแž˜แžฝแž™แž”แžถแž“แž€แžพแžแžกแžพแž„แž€แŸ’แž“แžปแž„แžขแŸ†แžกแžปแž„แž–แŸแž›แž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž›",
        "welcome_msg": "แžŸแžผแž˜แžŸแŸ’แžœแžถแž‚แž˜แž“แŸแž˜แž€แž€แžถแž“แŸ‹แžœแŸแž‘แžทแž€แžถแž”แžŽแŸ’แžแžปแŸ‡แž”แžŽแŸ’แžแžถแž› MLOps",
        "data_preview": "แž˜แžพแž›แž‘แžทแž“แŸ’แž“แž“แŸแž™แž‡แžถแž˜แžปแž“",
        "class_distribution": "แž€แžถแžšแž…แŸ‚แž€แž…แžถแž™แžแŸ’แž“แžถแž€แŸ‹"
    }
}


def get_translation(key: str, language: str = "en") -> str:
    """Get translated string for given key and language."""
    if language not in UI_TRANSLATIONS:
        language = "en"
    return UI_TRANSLATIONS[language].get(key, UI_TRANSLATIONS["en"].get(key, key))