vectorplasticity commited on
Commit
bda3756
·
verified ·
1 Parent(s): 4857b08

Update app/config.py

Browse files
Files changed (1) hide show
  1. app/config.py +158 -93
app/config.py CHANGED
@@ -1,23 +1,21 @@
 
 
 
 
1
  from pydantic_settings import BaseSettings
2
- from typing import List, Optional, Dict, Any
 
3
  import os
4
 
5
 
6
  class Settings(BaseSettings):
 
 
7
  # Application
8
  APP_NAME: str = "Universal Model Trainer"
9
- APP_VERSION: str = "1.0.0"
10
- DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"
11
-
12
- # Authentication
13
- APP_PASSWORD: str = os.getenv("APP_PASSWORD", "") # Set via HF Space secret
14
- SESSION_SECRET_KEY: str = os.getenv("SESSION_SECRET_KEY", "change-this-in-production-use-32-chars")
15
- SESSION_EXPIRE_HOURS: int = 24
16
-
17
- @property
18
- def is_auth_configured(self) -> bool:
19
- """Check if password authentication is configured"""
20
- return bool(self.APP_PASSWORD)
21
 
22
  # Server
23
  HOST: str = "0.0.0.0"
@@ -25,63 +23,60 @@ class Settings(BaseSettings):
25
  WORKERS: int = 1
26
 
27
  # HuggingFace
28
- HF_TOKEN: Optional[str] = os.getenv("HF_TOKEN")
29
- HF_HOME: str = os.getenv("HF_HOME", "/app/.cache/huggingface")
 
30
 
31
- # Redis/Queue
32
- REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/0")
33
- MAX_CONCURRENT_JOBS: int = 1
34
- JOB_TIMEOUT_HOURS: int = 24
35
 
36
  # Database
37
- DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./data/trainer.db")
38
 
39
  # Storage Paths
 
40
  MODELS_DIR: str = "/app/models"
41
- DATASETS_DIR: str = "/app/datasets"
42
- OUTPUT_DIR: str = "/app/outputs"
43
- CACHE_DIR: str = "/app/.cache"
44
  LOGS_DIR: str = "/app/logs"
 
45
 
46
  # Training Defaults
47
- DEFAULT_EPOCHS: int = 3
48
- DEFAULT_BATCH_SIZE: int = 1
49
  DEFAULT_LEARNING_RATE: float = 5e-5
50
  DEFAULT_MAX_LENGTH: int = 512
51
- DEFAULT_WARMUP_STEPS: int = 100
52
- DEFAULT_WEIGHT_DECAY: float = 0.01
53
 
54
  # Hardware
55
- DEVICE: str = "cpu"
56
- MIXED_PRECISION: bool = False
57
- GRADIENT_CHECKPOINTING: bool = True
58
-
59
- # PEFT/LoRA Defaults
60
- USE_PEFT_BY_DEFAULT: bool = True
61
- DEFAULT_LORA_R: int = 16
62
- DEFAULT_LORA_ALPHA: int = 32
63
- DEFAULT_LORA_DROPOUT: float = 0.05
64
- DEFAULT_TARGET_MODULES: List[str] = ["q_proj", "v_proj"]
65
 
66
  # Model Support
67
  SUPPORTED_TASKS: List[str] = [
68
  "causal-lm",
69
- "seq2seq",
70
  "token-classification",
71
- "text-classification",
72
  "question-answering",
73
- "reasoning"
 
 
 
 
 
 
 
74
  ]
75
 
76
- DEFAULT_MODELS: Dict[str, str] = {
77
- "causal-lm": "gpt2",
78
- "seq2seq": "t5-small",
79
- "token-classification": "bert-base-uncased",
80
- "text-classification": "distilbert-base-uncased",
81
- "question-answering": "distilbert-base-uncased-distilled-squad",
82
- "reasoning": "gpt2"
83
- }
84
-
85
  # Dataset Sources
86
  DATASET_SOURCES: List[str] = [
87
  "huggingface",
@@ -91,89 +86,159 @@ class Settings(BaseSettings):
91
  ]
92
 
93
  # Logging
94
- LOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")
95
- LOG_FORMAT: str = "<green>{time:YYYY-MM-DD HH:mm:ss}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>"
96
 
97
  # Experiment Tracking
98
- WANDB_PROJECT: Optional[str] = os.getenv("WANDB_PROJECT")
99
- WANDB_API_KEY: Optional[str] = os.getenv("WANDB_API_KEY")
100
- MLFLOW_TRACKING_URI: Optional[str] = os.getenv("MLFLOW_TRACKING_URI")
101
 
102
  # Security
103
- CORS_ORIGINS: List[str] = ["*"]
104
  API_KEY_HEADER: str = "X-API-Key"
 
105
 
106
  # Rate Limiting
107
- RATE_LIMIT_REQUESTS: int = 100
108
- RATE_LIMIT_PERIOD_SECONDS: int = 60
109
 
110
  # Resource Limits
111
- MAX_DATASET_SIZE_GB: float = 10.0
112
- MAX_MODEL_SIZE_GB: float = 5.0
113
- MAX_TRAINING_TIME_HOURS: int = 24
114
 
115
  class Config:
116
  env_file = ".env"
 
117
  case_sensitive = True
118
 
119
 
120
- # Training templates for different tasks
121
- TRAINING_TEMPLATES: Dict[str, Dict[str, Any]] = {
 
 
 
 
 
 
 
 
 
 
122
  "causal-lm": {
123
- "name": "Causal Language Model",
124
- "description": "Text generation with GPT-style models",
125
  "default_model": "gpt2",
126
  "default_args": {
127
- "max_length": 512,
128
- "truncate": True,
129
- "padding": "max_length"
 
 
 
130
  }
131
  },
132
  "seq2seq": {
133
- "name": "Sequence-to-Sequence",
134
- "description": "Summarization, translation, etc.",
135
  "default_model": "t5-small",
136
  "default_args": {
137
- "max_source_length": 512,
138
- "max_target_length": 128
 
 
 
139
  }
140
  },
141
  "token-classification": {
142
- "name": "Token Classification",
143
- "description": "Named Entity Recognition, POS Tagging",
144
  "default_model": "bert-base-uncased",
145
  "default_args": {
146
- "max_length": 512,
147
- "label_all_tokens": False
 
148
  }
149
  },
150
- "text-classification": {
151
- "name": "Text Classification",
152
- "description": "Sentiment analysis, categorization",
153
  "default_model": "distilbert-base-uncased",
154
  "default_args": {
155
- "max_length": 512
 
 
 
156
  }
157
  },
158
  "question-answering": {
159
- "name": "Question Answering",
160
- "description": "Extractive question answering",
161
  "default_model": "distilbert-base-uncased-distilled-squad",
162
  "default_args": {
163
- "max_length": 384,
164
- "doc_stride": 128
 
165
  }
166
  },
167
- "reasoning": {
168
- "name": "Reasoning/Chain-of-Thought",
169
- "description": "Step-by-step reasoning models",
170
- "default_model": "gpt2",
171
  "default_args": {
172
- "max_length": 1024,
173
- "include_reasoning": True
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
  }
175
  }
176
- }
177
-
178
-
179
- settings = Settings()
 
1
+ """
2
+ Configuration settings for Universal Model Trainer
3
+ """
4
+
5
  from pydantic_settings import BaseSettings
6
+ from typing import Optional, List
7
+ from functools import lru_cache
8
  import os
9
 
10
 
11
  class Settings(BaseSettings):
12
+ """Application settings loaded from environment variables."""
13
+
14
  # Application
15
  APP_NAME: str = "Universal Model Trainer"
16
+ VERSION: str = "1.0.0"
17
+ DEBUG: bool = False
18
+ SECRET_KEY: str = "change-me-in-production"
 
 
 
 
 
 
 
 
 
19
 
20
  # Server
21
  HOST: str = "0.0.0.0"
 
23
  WORKERS: int = 1
24
 
25
  # HuggingFace
26
+ HF_TOKEN: Optional[str] = None
27
+ HF_USERNAME: Optional[str] = None
28
+ HF_CACHE_DIR: str = "/app/cache"
29
 
30
+ # Redis / Queue
31
+ REDIS_URL: str = "redis://localhost:6379/0"
32
+ QUEUE_NAME: str = "training_queue"
33
+ MAX_CONCURRENT_JOBS: int = 3
34
 
35
  # Database
36
+ DATABASE_URL: str = "sqlite:///./data/trainer.db"
37
 
38
  # Storage Paths
39
+ UPLOAD_DIR: str = "/app/uploads"
40
  MODELS_DIR: str = "/app/models"
 
 
 
41
  LOGS_DIR: str = "/app/logs"
42
+ CACHE_DIR: str = "/app/cache"
43
 
44
  # Training Defaults
45
+ DEFAULT_BATCH_SIZE: int = 8
 
46
  DEFAULT_LEARNING_RATE: float = 5e-5
47
  DEFAULT_MAX_LENGTH: int = 512
48
+ DEFAULT_EPOCHS: int = 3
49
+ DEFAULT_WARMUP_RATIO: float = 0.1
50
 
51
  # Hardware
52
+ DEVICE: str = "auto" # auto, cpu, cuda, mps
53
+ DTYPE: str = "auto" # auto, float32, float16, bfloat16
54
+
55
+ # PEFT / LoRA Defaults
56
+ PEFT_DEFAULTS: dict = {
57
+ "lora_r": 16,
58
+ "lora_alpha": 32,
59
+ "lora_dropout": 0.05,
60
+ "target_modules": ["q_proj", "v_proj"]
61
+ }
62
 
63
  # Model Support
64
  SUPPORTED_TASKS: List[str] = [
65
  "causal-lm",
66
+ "seq2seq",
67
  "token-classification",
68
+ "sequence-classification",
69
  "question-answering",
70
+ "summarization",
71
+ "translation",
72
+ "text-classification",
73
+ "masked-lm",
74
+ "vision-classification",
75
+ "vision-segmentation",
76
+ "audio-classification",
77
+ "audio-transcription"
78
  ]
79
 
 
 
 
 
 
 
 
 
 
80
  # Dataset Sources
81
  DATASET_SOURCES: List[str] = [
82
  "huggingface",
 
86
  ]
87
 
88
  # Logging
89
+ LOG_LEVEL: str = "INFO"
90
+ LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"
91
 
92
  # Experiment Tracking
93
+ WANDB_API_KEY: Optional[str] = None
94
+ WANDB_PROJECT: str = "universal-model-trainer"
 
95
 
96
  # Security
 
97
  API_KEY_HEADER: str = "X-API-Key"
98
+ ENABLE_AUTH: bool = False
99
 
100
  # Rate Limiting
101
+ RATE_LIMIT_PER_MINUTE: int = 60
 
102
 
103
  # Resource Limits
104
+ MAX_UPLOAD_SIZE_MB: int = 500
105
+ MAX_DATASET_SIZE_MB: int = 10000
106
+ MAX_TRAINING_HOURS: int = 24
107
 
108
  class Config:
109
  env_file = ".env"
110
+ env_file_encoding = "utf-8"
111
  case_sensitive = True
112
 
113
 
114
+ @lru_cache()
115
+ def get_settings() -> Settings:
116
+ """Get cached settings instance."""
117
+ return Settings()
118
+
119
+
120
+ # Export settings instance
121
+ settings = get_settings()
122
+
123
+
124
+ # Training Configuration Templates
125
+ TRAINING_TEMPLATES = {
126
  "causal-lm": {
127
+ "task": "causal-lm",
128
+ "model_type": "decoder",
129
  "default_model": "gpt2",
130
  "default_args": {
131
+ "per_device_train_batch_size": 4,
132
+ "gradient_accumulation_steps": 4,
133
+ "learning_rate": 5e-5,
134
+ "max_grad_norm": 1.0,
135
+ "warmup_ratio": 0.1,
136
+ "weight_decay": 0.01
137
  }
138
  },
139
  "seq2seq": {
140
+ "task": "seq2seq",
141
+ "model_type": "encoder-decoder",
142
  "default_model": "t5-small",
143
  "default_args": {
144
+ "per_device_train_batch_size": 8,
145
+ "learning_rate": 5e-5,
146
+ "warmup_ratio": 0.1,
147
+ "source_lang": "en",
148
+ "target_lang": "en"
149
  }
150
  },
151
  "token-classification": {
152
+ "task": "token-classification",
153
+ "model_type": "encoder",
154
  "default_model": "bert-base-uncased",
155
  "default_args": {
156
+ "per_device_train_batch_size": 16,
157
+ "learning_rate": 3e-5,
158
+ "label_column": "ner_tags"
159
  }
160
  },
161
+ "sequence-classification": {
162
+ "task": "sequence-classification",
163
+ "model_type": "encoder",
164
  "default_model": "distilbert-base-uncased",
165
  "default_args": {
166
+ "per_device_train_batch_size": 16,
167
+ "learning_rate": 2e-5,
168
+ "num_labels": 2,
169
+ "label_column": "label"
170
  }
171
  },
172
  "question-answering": {
173
+ "task": "question-answering",
174
+ "model_type": "encoder",
175
  "default_model": "distilbert-base-uncased-distilled-squad",
176
  "default_args": {
177
+ "per_device_train_batch_size": 12,
178
+ "learning_rate": 3e-5,
179
+ "max_answer_length": 30
180
  }
181
  },
182
+ "text-classification": {
183
+ "task": "text-classification",
184
+ "model_type": "encoder",
185
+ "default_model": "roberta-base",
186
  "default_args": {
187
+ "per_device_train_batch_size": 16,
188
+ "learning_rate": 1e-5,
189
+ "num_labels": 2
190
+ }
191
+ },
192
+ "summarization": {
193
+ "task": "summarization",
194
+ "model_type": "encoder-decoder",
195
+ "default_model": "facebook/bart-large-cnn",
196
+ "default_args": {
197
+ "per_device_train_batch_size": 4,
198
+ "learning_rate": 3e-5,
199
+ "max_source_length": 1024,
200
+ "max_target_length": 128
201
+ }
202
+ },
203
+ "translation": {
204
+ "task": "translation",
205
+ "model_type": "encoder-decoder",
206
+ "default_model": "Helsinki-NLP/opus-mt-en-fr",
207
+ "default_args": {
208
+ "per_device_train_batch_size": 8,
209
+ "learning_rate": 5e-5,
210
+ "source_lang": "en",
211
+ "target_lang": "fr"
212
+ }
213
+ },
214
+ "masked-lm": {
215
+ "task": "masked-lm",
216
+ "model_type": "encoder",
217
+ "default_model": "bert-base-uncased",
218
+ "default_args": {
219
+ "per_device_train_batch_size": 16,
220
+ "learning_rate": 5e-5,
221
+ "mlm_probability": 0.15
222
+ }
223
+ },
224
+ "vision-classification": {
225
+ "task": "vision-classification",
226
+ "model_type": "vision",
227
+ "default_model": "google/vit-base-patch16-224",
228
+ "default_args": {
229
+ "per_device_train_batch_size": 32,
230
+ "learning_rate": 2e-5,
231
+ "image_column": "image"
232
+ }
233
+ },
234
+ "audio-transcription": {
235
+ "task": "audio-transcription",
236
+ "model_type": "audio",
237
+ "default_model": "openai/whisper-small",
238
+ "default_args": {
239
+ "per_device_train_batch_size": 8,
240
+ "learning_rate": 1e-5,
241
+ "audio_column": "audio"
242
  }
243
  }
244
+ }