Update app/config.py
Browse files1234526789101112131415910111213141516171819201617181920211 unmodified line2324252829262728293132333430313233343537363738394041424341424344474845464751524849505155565758596061626364525354555657585960616263646569666771686973707172737475767778797677787980818283848081823 unmodified lines868788949589909192989910093949596103979899100107108101102103111112113104105106107108109110111112113120121114115116117118119120121122123124125126123124127128129130127128129131132133134135136137138139133134140141142143137138144145146147148149150151142143152153154155146147156157158159160150151152161162163164165155166167168169170171172159160173174175176163164177178179180181167168169170182183184185186172173187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243176177178179244"""Configuration settings for Universal Model Trainer"""
from pydantic_settings import BaseSettingsfrom typing import List, Optional, Dict, Anyfrom typing import Optional, Listfrom functools import lru_cacheimport os
class Settings(BaseSettings): """Application settings loaded from environment variables.""" # Application APP_NAME: str = "Universal Model Trainer" APP_VERSION: str = "1.0.0" DEBUG: bool = os.getenv("DEBUG", "false").lower() == "true" # Authentication APP_PASSWORD: str = os.getenv("APP_PASSWORD", "") # Set via HF Space secret SESSION_SECRET_KEY: str = os.getenv("SESSION_SECRET_KEY", "change-this-in-production-use-32-chars") SESSION_EXPIRE_HOURS: int = 24 @property def is_auth_configured(self) -> bool: """Check if password authentication is configured""" return bool(self.APP_PASSWORD) VERSION: str = "1.0.0" DEBUG: bool = False SECRET_KEY: str = "change-me-in-production" # Server HOST: str = "0.0.0.0"1 unmodified line WORKERS: int = 1 # HuggingFace HF_TOKEN: Optional[str] = os.getenv("HF_TOKEN") HF_HOME: str = os.getenv("HF_HOME", "/app/.cache/huggingface") HF_TOKEN: Optional[str] = None HF_USERNAME: Optional[str] = None HF_CACHE_DIR: str = "/app/cache" # Redis/Queue REDIS_URL: str = os.getenv("REDIS_URL", "redis://localhost:6379/0") MAX_CONCURRENT_JOBS: int = 1 JOB_TIMEOUT_HOURS: int = 24 # Redis / Queue REDIS_URL: str = "redis://localhost:6379/0" QUEUE_NAME: str = "training_queue" MAX_CONCURRENT_JOBS: int = 3 # Database DATABASE_URL: str = os.getenv("DATABASE_URL", "sqlite+aiosqlite:///./data/trainer.db") DATABASE_URL: str = "sqlite:///./data/trainer.db" # Storage Paths UPLOAD_DIR: str = "/app/uploads" MODELS_DIR: str = "/app/models" DATASETS_DIR: str = "/app/datasets" OUTPUT_DIR: str = "/app/outputs" CACHE_DIR: str = "/app/.cache" LOGS_DIR: str = "/app/logs" CACHE_DIR: str = "/app/cache" # Training Defaults DEFAULT_EPOCHS: int = 3 DEFAULT_BATCH_SIZE: int = 1 DEFAULT_BATCH_SIZE: int = 8 DEFAULT_LEARNING_RATE: float = 5e-5 DEFAULT_MAX_LENGTH: int = 512 DEFAULT_WARMUP_STEPS: int = 100 DEFAULT_WEIGHT_DECAY: float = 0.01 DEFAULT_EPOCHS: int = 3 DEFAULT_WARMUP_RATIO: float = 0.1 # Hardware DEVICE: str = "cpu" MIXED_PRECISION: bool = False GRADIENT_CHECKPOINTING: bool = True # PEFT/LoRA Defaults USE_PEFT_BY_DEFAULT: bool = True DEFAULT_LORA_R: int = 16 DEFAULT_LORA_ALPHA: int = 32 DEFAULT_LORA_DROPOUT: float = 0.05 DEFAULT_TARGET_MODULES: List[str] = ["q_proj", "v_proj"] DEVICE: str = "auto" # auto, cpu, cuda, mps DTYPE: str = "auto" # auto, float32, float16, bfloat16 # PEFT / LoRA Defaults PEFT_DEFAULTS: dict = { "lora_r": 16, "lora_alpha": 32, "lora_dropout": 0.05, "target_modules": ["q_proj", "v_proj"] } # Model Support SUPPORTED_TASKS: List[str] = [ "causal-lm", "seq2seq", "seq2seq", "token-classification", "text-classification", "sequence-classification", "question-answering", "reasoning" "summarization", "translation", "text-classification", "masked-lm", "vision-classification", "vision-segmentation", "audio-classification", "audio-transcription" ] 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 Sources DATASET_SOURCES: List[str] = [ "huggingface",3 unmodified lines ] # Logging LOG_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>" LOG_LEVEL: str = "INFO" LOG_FORMAT: str = "%(asctime)s - %(name)s - %(levelname)s - %(message)s" # Experiment Tracking WANDB_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") WANDB_API_KEY: Optional[str] = None WANDB_PROJECT: str = "universal-model-trainer" # Security CORS_ORIGINS: List[str] = ["*"] API_KEY_HEADER: str = "X-API-Key" ENABLE_AUTH: bool = False # Rate Limiting RATE_LIMIT_REQUESTS: int = 100 RATE_LIMIT_PERIOD_SECONDS: int = 60 RATE_LIMIT_PER_MINUTE: int = 60 # Resource Limits MAX_DATASET_SIZE_GB: float = 10.0 MAX_MODEL_SIZE_GB: float = 5.0 MAX_TRAINING_TIME_HOURS: int = 24 MAX_UPLOAD_SIZE_MB: int = 500 MAX_DATASET_SIZE_MB: int = 10000 MAX_TRAINING_HOURS: int = 24 class Config: env_file = ".env" env_file_encoding = "utf-8" case_sensitive = True
# Training templates for different tasksTRAINING_TEMPLATES: Dict[str, Dict[str, Any]] = {@lru_cache()def get_settings() -> Settings: """Get cached settings instance.""" return Settings()
# Export settings instancesettings = get_settings()
# Training Configuration TemplatesTRAINING_TEMPLATES = { "causal-lm": { "name": "Causal Language Model", "description": "Text generation with GPT-style models", "task": "causal-lm", "model_type": "decoder", "default_model": "gpt2", "default_args": { "max_length": 512, "truncate": True, "padding": "max_length" "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": { "name": "Sequence-to-Sequence", "description": "Summarization, translation, etc.", "task": "seq2seq", "model_type": "encoder-decoder", "default_model": "t5-small", "default_args": { "max_source_length": 512, "max_target_length": 128 "per_device_train_batch_size": 8, "learning_rate": 5e-5, "warmup_ratio": 0.1, "source_lang": "en", "target_lang": "en" } }, "token-classification": { "name": "Token Classification", "description": "Named Entity Recognition, POS Tagging", "task": "token-classification", "model_type": "encoder", "default_model": "bert-base-uncased", "default_args": { "max_length": 512, "label_all_tokens": False "per_device_train_batch_size": 16, "learning_rate": 3e-5, "label_column": "ner_tags" } }, "text-classification": { "name": "Text Classification", "description": "Sentiment analysis, categorization", "sequence-classification": { "task": "sequence-classification", "model_type": "encoder", "default_model": "distilbert-base-uncased", "default_args": { "max_length": 512 "per_device_train_batch_size": 16, "learning_rate": 2e-5, "num_labels": 2, "label_column": "label" } }, "question-answering": { "name": "Question Answering", "description": "Extractive question answering", "task": "question-answering", "model_type": "encoder", "default_model": "distilbert-base-uncased-distilled-squad", "default_args": { "max_length": 384, "doc_stride": 128 "per_device_train_batch_size": 12, "learning_rate": 3e-5, "max_answer_length": 30 } }, "reasoning": { "name": "Reasoning/Chain-of-Thought", "description": "Step-by-step reasoning models", "default_model": "gpt2", "text-classification": { "task": "text-classification", "model_type": "encoder", "default_model": "roberta-base", "default_args": { "max_length": 1024, "include_reasoning": True "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" } }}
settings = Settings()}app/config.py+158-93