Update app/config.py
Browse files12345678910111213141516171819202122231 unmodified line2526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586873 unmodified lines919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179from pydantic_settings import BaseSettingsfrom typing import List, Optional, Dict, Anyimport osclass Settings(BaseSettings):# ApplicationAPP_NAME: str = "Universal Model Trainer"APP_VERSION: str = "1.0.0"DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true"# AuthenticationAPP_PASSWORD: str = os.getenv("APP_PASSWORD", "") # Set via HF Space secretSESSION_SECRET_KEY: str = os.getenv("SESSION_SECRET_KEY", "change-this-in-production-use-32-chars")SESSION_EXPIRE_HOURS: int = 24@propertydef is_auth_configured(self) -> bool:"""Check if password authentication is configured"""return bool(self.APP_PASSWORD)# ServerHOST: str = "0.0.0.0"1 unmodified lineWORKERS: int = 1# HuggingFaceHF_TOKEN: Optional[str] = os.getenv("HF_TOKEN")HF_HOME: str = os.getenv("HF_HOME", "/app/.cache/huggingface")# Redis/QueueREDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/0")MAX_CONCURRENT_JOBS: int = 1JOB_TIMEOUT_HOURS: int = 24# DatabaseDATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./data/trainer.db")# Storage PathsMODELS_DIR: str = "/app/models"DATASETS_DIR: str = "/app/datasets"OUTPUT_DIR: str = "/app/outputs"CACHE_DIR: str = "/app/.cache"LOGS_DIR: str = "/app/logs"# Training DefaultsDEFAULT_EPOCHS: int = 3DEFAULT_BATCH_SIZE: int = 1DEFAULT_LEARNING_RATE: float = 5e-5DEFAULT_MAX_LENGTH: int = 512DEFAULT_WARMUP_STEPS: int = 100DEFAULT_WEIGHT_DECAY: float = 0.01# HardwareDEVICE: str = "cpu"MIXED_PRECISION: bool = FalseGRADIENT_CHECKPOINTING: bool = True# PEFT/LoRA DefaultsUSE_PEFT_BY_DEFAULT: bool = TrueDEFAULT_LORA_R: int = 16DEFAULT_LORA_ALPHA: int = 32DEFAULT_LORA_DROPOUT: float = 0.05DEFAULT_TARGET_MODULES: List[str] = ["q_proj", "v_proj"]# Model SupportSUPPORTED_TASKS: List[str] = ["causal-lm","seq2seq","token-classification","text-classification","question-answering","reasoning"]DEFAULT_MODELS: Dict[str, str] = {"causal-lm": "gpt2","seq2seq": "t5-small","token-classification": "bert-base-uncased","text-classification": "distilbert-base-uncased","question-answering": "distilbert-base-uncased-distilled-squad","reasoning": "gpt2"}# Dataset SourcesDATASET_SOURCES: List[str] = ["huggingface",3 unmodified lines]# LoggingLOG_LEVEL: str = os.getenv("LOG_LEVEL", "INFO")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>"# Experiment TrackingWANDB_PROJECT: Optional[str] = os.getenv("WANDB_PROJECT")WANDB_API_KEY: Optional[str] = os.getenv("WANDB_API_KEY")MLFLOW_TRACKING_URI: Optional[str] = os.getenv("MLFLOW_TRACKING_URI")# SecurityCORS_ORIGINS: List[str] = ["*"]API_KEY_HEADER: str = "X-API-Key"# Rate LimitingRATE_LIMIT_REQUESTS: int = 100RATE_LIMIT_PERIOD_SECONDS: int = 60# Resource LimitsMAX_DATASET_SIZE_GB: float = 10.0MAX_MODEL_SIZE_GB: float = 5.0MAX_TRAINING_TIME_HOURS: int = 24class Config:env_file = ".env"case_sensitive = True# Training templates for different tasksTRAINING_TEMPLATES: Dict[str, Dict[str, Any]] = {"causal-lm": {"name": "Causal Language Model","description": "Text generation with GPT-style models","default_model": "gpt2","default_args": {"max_length": 512,"truncate": True,"padding": "max_length"}},"seq2seq": {"name": "Sequence-to-Sequence","description": "Summarization, translation, etc.","default_model": "t5-small","default_args": {"max_source_length": 512,"max_target_length": 128}},"token-classification": {"name": "Token Classification","description": "Named Entity Recognition, POS Tagging","default_model": "bert-base-uncased","default_args": {"max_length": 512,"label_all_tokens": False}},"text-classification": {"name": "Text Classification","description": "Sentiment analysis, categorization","default_model": "distilbert-base-uncased","default_args": {"max_length": 512}},"question-answering": {"name": "Question Answering","description": "Extractive question answering","default_model": "distilbert-base-uncased-distilled-squad","default_args": {"max_length": 384,"doc_stride": 128}},"reasoning": {"name": "Reasoning/Chain-of-Thought","description": "Step-by-step reasoning models","default_model": "gpt2","default_args": {"max_length": 1024,"include_reasoning": True}}}settings = Settings()1234567891011121314151617181920211 unmodified line2324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081823 unmodified lines8687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244"""Configuration settings for Universal Model Trainer"""from pydantic_settings import BaseSettingsfrom typing import Optional, Listfrom functools import lru_cacheimport osclass Settings(BaseSettings):"""Application settings loaded from environment variables."""# ApplicationAPP_NAME: str = "Universal Model Trainer"VERSION: str = "1.0.0"DEBUG: bool = FalseSECRET_KEY: str = "change-me-in-production"# ServerHOST: str = "0.0.0.0"1 unmodified lineWORKERS: int = 1# HuggingFaceHF_TOKEN: Optional[str] = NoneHF_USERNAME: Optional[str] = NoneHF_CACHE_DIR: str = "/app/cache"# Redis / QueueREDIS_URL: str = "redis://localhost:6379/0"QUEUE_NAME: str = "training_queue"MAX_CONCURRENT_JOBS: int = 3# DatabaseDATABASE_URL: str = "sqlite:///./data/trainer.db"# Storage PathsUPLOAD_DIR: str = "/app/uploads"MODELS_DIR: str = "/app/models"LOGS_DIR: str = "/app/logs"CACHE_DIR: str = "/app/cache"# Training DefaultsDEFAULT_BATCH_SIZE: int = 8DEFAULT_LEARNING_RATE: float = 5e-5DEFAULT_MAX_LENGTH: int = 512DEFAULT_EPOCHS: int = 3DEFAULT_WARMUP_RATIO: float = 0.1# HardwareDEVICE: str = "auto" # auto, cpu, cuda, mpsDTYPE: str = "auto" # auto, float32, float16, bfloat16# PEFT / LoRA DefaultsPEFT_DEFAULTS: dict = {"lora_r": 16,"lora_alpha": 32,"lora_dropout": 0.05,"target_modules": ["q_proj", "v_proj"]}# Model SupportSUPPORTED_TASKS: List[str] = ["causal-lm","seq2seq","token-classification","sequence-classification","question-answering","summarization","translation","text-classification","masked-lm","vision-classification","vision-segmentation","audio-classification","audio-transcription"]# Dataset SourcesDATASET_SOURCES: List[str] = ["huggingface",3 unmodified lines]# LoggingLOG_LEVEL: str = "INFO"LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s"# Experiment TrackingWANDB_API_KEY: Optional[str] = NoneWANDB_PROJECT: str = "universal-model-trainer"# SecurityAPI_KEY_HEADER: str = "X-API-Key"ENABLE_AUTH: bool = False# Rate LimitingRATE_LIMIT_PER_MINUTE: int = 60# Resource LimitsMAX_UPLOAD_SIZE_MB: int = 500MAX_DATASET_SIZE_MB: int = 10000MAX_TRAINING_HOURS: int = 24class Config:env_file = ".env"env_file_encoding = "utf-8"case_sensitive = True@lru_cache()def get_settings() -> Settings:"""Get cached settings instance."""return Settings()# Export settings instancesettings = get_settings()# Training Configuration TemplatesTRAINING_TEMPLATES = {"causal-lm": {"task": "causal-lm","model_type": "decoder","default_model": "gpt2","default_args": {"per_device_train_batch_size": 4,"gradient_accumulation_steps": 4,"learning_rate": 5e-5,"max_grad_norm": 1.0,"warmup_ratio": 0.1,"weight_decay": 0.01}},"seq2seq": {"task": "seq2seq","model_type": "encoder-decoder","default_model": "t5-small","default_args": {"per_device_train_batch_size": 8,"learning_rate": 5e-5,"warmup_ratio": 0.1,"source_lang": "en","target_lang": "en"}},"token-classification": {"task": "token-classification","model_type": "encoder","default_model": "bert-base-uncased","default_args": {"per_device_train_batch_size": 16,"learning_rate": 3e-5,"label_column": "ner_tags"}},"sequence-classification": {"task": "sequence-classification","model_type": "encoder","default_model": "distilbert-base-uncased","default_args": {"per_device_train_batch_size": 16,"learning_rate": 2e-5,"num_labels": 2,"label_column": "label"}},"question-answering": {"task": "question-answering","model_type": "encoder","default_model": "distilbert-base-uncased-distilled-squad","default_args": {"per_device_train_batch_size": 12,"learning_rate": 3e-5,"max_answer_length": 30}},"text-classification": {"task": "text-classification","model_type": "encoder","default_model": "roberta-base","default_args": {"per_device_train_batch_size": 16,"learning_rate": 1e-5,"num_labels": 2}},"summarization": {"task": "summarization","model_type": "encoder-decoder","default_model": "facebook/bart-large-cnn","default_args": {"per_device_train_batch_size": 4,"learning_rate": 3e-5,"max_source_length": 1024,"max_target_length": 128}},"translation": {"task": "translation","model_type": "encoder-decoder","default_model": "Helsinki-NLP/opus-mt-en-fr","default_args": {"per_device_train_batch_size": 8,"learning_rate": 5e-5,"source_lang": "en","target_lang": "fr"}},"masked-lm": {"task": "masked-lm","model_type": "encoder","default_model": "bert-base-uncased","default_args": {"per_device_train_batch_size": 16,"learning_rate": 5e-5,"mlm_probability": 0.15}},"vision-classification": {"task": "vision-classification","model_type": "vision","default_model": "google/vit-base-patch16-224","default_args": {"per_device_train_batch_size": 32,"learning_rate": 2e-5,"image_column": "image"}},"audio-transcription": {"task": "audio-transcription","model_type": "audio","default_model": "openai/whisper-small","default_args": {"per_device_train_batch_size": 8,"learning_rate": 1e-5,"audio_column": "audio"}}}
app/config.py+158-93