Spaces:
Sleeping
Sleeping
Commit ·
9c720d9
1
Parent(s): 4e7fa1a
feat: add support for multiple AutoML frameworks (TPOT, H2O, AutoGluon, FLAML) including data preprocessing and MLflow integration.
Browse files- README.md +527 -10
- app.py +286 -209
- src/autogluon_utils.py +9 -6
- src/data_utils.py +1 -1
- src/flaml_utils.py +16 -12
- src/h2o_utils.py +110 -112
- src/mlflow_cache.py +21 -21
- src/mlflow_utils.py +8 -8
- src/tpot_utils.py +14 -14
README.md
CHANGED
|
@@ -1,13 +1,530 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
---
|
| 13 |
|
|
|
|
|
|
| 1 |
+
# 🚀 Multi-AutoML Interface
|
| 2 |
+
|
| 3 |
+
**A unified interface for experimenting with AutoML, allowing you to compare multiple frameworks (AutoGluon, FLAML, H2O, TPOT) with integrated MLOps via MLflow.**
|
| 4 |
+
|
| 5 |
+
---
|
| 6 |
+
|
| 7 |
+
## 🎯 **Overview**
|
| 8 |
+
|
| 9 |
+
The Multi-AutoML Interface is a web/desktop application that simplifies the use of AutoML frameworks, enabling:
|
| 10 |
+
|
| 11 |
+
- **Side-by-side comparison** of different AutoML engines
|
| 12 |
+
- **Integrated MLOps** with complete tracking via MLflow
|
| 13 |
+
- **Unified interface** for training, evaluation, and prediction
|
| 14 |
+
- **Flexible deployment** (web, Docker, desktop)
|
| 15 |
+
- **Detailed metrics and logging**
|
| 16 |
+
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
## ✨ **Key Features**
|
| 20 |
+
|
| 21 |
+
### 🤖 **Supported AutoML Frameworks:**
|
| 22 |
+
- **AutoGluon** (Amazon) - Exceptional performance
|
| 23 |
+
- **FLAML** (Microsoft) - Fast and efficient
|
| 24 |
+
- **H2O AutoML** (Enterprise) - Robust and comprehensive
|
| 25 |
+
- **TPOT** (Open Source) - Pipelines generated by Genetic Algorithms
|
| 26 |
+
|
| 27 |
+
### 📊 **Integrated MLOps:**
|
| 28 |
+
- **Complete MLflow tracking**
|
| 29 |
+
- **Automatic Data Lake versioning** with DVC
|
| 30 |
+
- **Automatic experiment logging**
|
| 31 |
+
- **Centralized model registry**
|
| 32 |
+
- **Detailed performance metrics**
|
| 33 |
+
- **Artifact management**
|
| 34 |
+
|
| 35 |
+
### 🖥️ **Multi-Deploy:**
|
| 36 |
+
- **Web interface** (Streamlit)
|
| 37 |
+
- **Docker container** (production)
|
| 38 |
+
- **Desktop app** (Electron)
|
| 39 |
+
- **Hugging Face Spaces** (Live Demo)
|
| 40 |
+
- **Local development**
|
| 41 |
+
|
| 42 |
+
### 🎛️ **Advanced Interface:**
|
| 43 |
+
- **Upload multiple datasets** (Train, Validation, Test)
|
| 44 |
+
- **Advanced parameter configuration**
|
| 45 |
+
- **Real-time monitoring**
|
| 46 |
+
- **Results visualization**
|
| 47 |
+
- **Interactive prediction**
|
| 48 |
+
|
| 49 |
+
---
|
| 50 |
+
|
| 51 |
+
## 🏗️ **Architecture**
|
| 52 |
+
|
| 53 |
+
```
|
| 54 |
+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
|
| 55 |
+
│ Frontend │ │ Backend API │ │ ML Engines │
|
| 56 |
+
│ │ │ │ │ │
|
| 57 |
+
│ • Streamlit │◄──►│ • Python │◄──►│ • AutoGluon │
|
| 58 |
+
│ • Electron │ │ • FastAPI │ │ • FLAML │
|
| 59 |
+
│ • React │ │ • MLflow │ │ • H2O AutoML │
|
| 60 |
+
│ • Custom UI │ │ • Logging │ │ • TPOT │
|
| 61 |
+
└─────────────────┘ └──────────────────┘ └─────────────────┘
|
| 62 |
+
│ │ │
|
| 63 |
+
▼ ▼ ▼
|
| 64 |
+
┌─────────────────┐ ┌──────────────────┐ ┌─────────────────┐
|
| 65 |
+
│ Storage │ │ Monitoring │ │ Deployment │
|
| 66 |
+
│ │ │ │ │ │
|
| 67 |
+
│ • File System │ │ • MLflow UI │ │ • Docker Hub │
|
| 68 |
+
│ • MLflow Artifacts│ │ • Logs │ │ • GitHub │
|
| 69 |
+
│ • Model Registry│ │ • Metrics │ │ • Electron Store│
|
| 70 |
+
└─────────────────┘ └──────────────────┘ └─────────────────┘
|
| 71 |
+
```
|
| 72 |
+
|
| 73 |
+
---
|
| 74 |
+
|
| 75 |
+
## 🚀 **Quick Start**
|
| 76 |
+
|
| 77 |
+
### 📋 **Prerequisites:**
|
| 78 |
+
- **Python 3.11+**
|
| 79 |
+
- **Node.js 16+** (for desktop app)
|
| 80 |
+
- **Java 11+** (for H2O AutoML)
|
| 81 |
+
- **Git**
|
| 82 |
+
|
| 83 |
+
### 🔧 **Installation:**
|
| 84 |
+
|
| 85 |
+
#### **1. Clone the Repository:**
|
| 86 |
+
```bash
|
| 87 |
+
git clone https://github.com/PedroM2626/Multi-AutoML-Interface.git
|
| 88 |
+
cd Multi-AutoML-Interface
|
| 89 |
+
```
|
| 90 |
+
|
| 91 |
+
#### **2. Python Environment:**
|
| 92 |
+
```bash
|
| 93 |
+
# Create virtual environment
|
| 94 |
+
python -m venv venv
|
| 95 |
+
|
| 96 |
+
# Activate (Windows)
|
| 97 |
+
venv\Scripts\activate
|
| 98 |
+
|
| 99 |
+
# Activate (Mac/Linux)
|
| 100 |
+
source venv/bin/activate
|
| 101 |
+
|
| 102 |
+
# Install dependencies
|
| 103 |
+
pip install -r requirements.txt
|
| 104 |
+
```
|
| 105 |
+
|
| 106 |
+
#### **3. Start MLflow:**
|
| 107 |
+
```bash
|
| 108 |
+
# Start MLflow server
|
| 109 |
+
mlflow server --host 0.0.0.0 --port 5000
|
| 110 |
+
```
|
| 111 |
+
|
| 112 |
+
#### **4. Run the Application:**
|
| 113 |
+
```bash
|
| 114 |
+
# Option 1: Web interface
|
| 115 |
+
streamlit run app.py --server.port 8501
|
| 116 |
+
|
| 117 |
+
# Option 2: Desktop app (requires Node.js)
|
| 118 |
+
npm install && npm run dev
|
| 119 |
+
|
| 120 |
+
# Option 3: Docker
|
| 121 |
+
docker-compose up
|
| 122 |
+
```
|
| 123 |
+
|
| 124 |
+
---
|
| 125 |
+
|
| 126 |
+
## 📖 **User Guide**
|
| 127 |
+
|
| 128 |
+
### 🎯 **Basic Workflow:**
|
| 129 |
+
|
| 130 |
+
#### **1. Data Upload:**
|
| 131 |
+
- Supported formats: CSV, Excel
|
| 132 |
+
- **Multiple splits supported**: Train (mandatory), Validation (optional), and Test (optional)
|
| 133 |
+
- Automatic type detection
|
| 134 |
+
- **Automatic Data Lake**: When processing data, it is copied to the `data_lake/` folder and versioned via DVC, generating hashes for version control.
|
| 135 |
+
|
| 136 |
+
#### **2. Experiment Configuration:**
|
| 137 |
+
- **Framework**: AutoGluon, FLAML, H2O, TPOT
|
| 138 |
+
- **Target variable**: Target column
|
| 139 |
+
- **Advanced parameters**: seed, time limits, folds, max textual features (TF-IDF), CV, etc.
|
| 140 |
+
|
| 141 |
+
#### **3. Training:**
|
| 142 |
+
- **Real-time monitoring**
|
| 143 |
+
- **Detailed logs**
|
| 144 |
+
- **Progress tracking**
|
| 145 |
+
|
| 146 |
+
#### **4. Results Analysis:**
|
| 147 |
+
- **Comparative leaderboards**
|
| 148 |
+
- **Performance metrics**
|
| 149 |
+
- **Model insights**
|
| 150 |
+
|
| 151 |
+
#### **5. Prediction:**
|
| 152 |
+
- **Upload new data**
|
| 153 |
+
- **Batch prediction**
|
| 154 |
+
- **Real-time inference**
|
| 155 |
+
|
| 156 |
+
---
|
| 157 |
+
|
| 158 |
+
## 🛠️ **Advanced Configuration**
|
| 159 |
+
|
| 160 |
+
### ⚙️ **Framework Parameters:**
|
| 161 |
+
|
| 162 |
+
#### **AutoGluon:**
|
| 163 |
+
```python
|
| 164 |
+
{
|
| 165 |
+
'presets': 'best_quality',
|
| 166 |
+
'time_limit': 3600,
|
| 167 |
+
'seed': 42,
|
| 168 |
+
'num_bag_folds': 5,
|
| 169 |
+
'num_bag_sets': 1
|
| 170 |
+
}
|
| 171 |
+
```
|
| 172 |
+
|
| 173 |
+
#### **FLAML:**
|
| 174 |
+
```python
|
| 175 |
+
{
|
| 176 |
+
'time_budget': 3600,
|
| 177 |
+
'seed': 42,
|
| 178 |
+
'ensemble': True,
|
| 179 |
+
'metric': 'accuracy',
|
| 180 |
+
'estimator_list': ['lgbm', 'xgboost', 'rf']
|
| 181 |
+
}
|
| 182 |
+
```
|
| 183 |
+
|
| 184 |
+
#### **H2O AutoML:**
|
| 185 |
+
```python
|
| 186 |
+
{
|
| 187 |
+
'max_runtime_secs': 3600,
|
| 188 |
+
'max_models': 20,
|
| 189 |
+
'seed': 42,
|
| 190 |
+
'nfolds': 5,
|
| 191 |
+
'balance_classes': True,
|
| 192 |
+
'sort_metric': 'AUTO'
|
| 193 |
+
}
|
| 194 |
+
```
|
| 195 |
+
|
| 196 |
+
#### **TPOT:**
|
| 197 |
+
```python
|
| 198 |
+
{
|
| 199 |
+
'generations': 5,
|
| 200 |
+
'population_size': 20,
|
| 201 |
+
'cv': 5,
|
| 202 |
+
'max_time_mins': 30,
|
| 203 |
+
'config_dict': 'TPOT sparse',
|
| 204 |
+
'tfidf_max_features': 500,
|
| 205 |
+
'tfidf_ngram_range': (1, 2)
|
| 206 |
+
}
|
| 207 |
+
```
|
| 208 |
+
|
| 209 |
+
### 🎛️ **MLflow Configuration:**
|
| 210 |
+
```python
|
| 211 |
+
# Experiments
|
| 212 |
+
mlflow.set_experiment("AutoGluon_Experiments")
|
| 213 |
+
mlflow.set_experiment("FLAML_Experiments")
|
| 214 |
+
mlflow.set_experiment("H2O_Experiments")
|
| 215 |
+
|
| 216 |
+
# Tracking
|
| 217 |
+
mlflow.log_param("framework", "autogluon")
|
| 218 |
+
mlflow.log_metric("accuracy", 0.95)
|
| 219 |
+
mlflow.log_artifact("model.pkl")
|
| 220 |
+
```
|
| 221 |
+
|
| 222 |
+
---
|
| 223 |
+
|
| 224 |
+
## 🐳 **Deploy with Docker**
|
| 225 |
+
|
| 226 |
+
### 📦 **Build and Run:**
|
| 227 |
+
|
| 228 |
+
#### **1. Build Image:**
|
| 229 |
+
```bash
|
| 230 |
+
docker build -t multi-automl:latest .
|
| 231 |
+
```
|
| 232 |
+
|
| 233 |
+
#### **2. Docker Compose:**
|
| 234 |
+
```bash
|
| 235 |
+
# Start all services
|
| 236 |
+
docker-compose up -d
|
| 237 |
+
|
| 238 |
+
# Logs
|
| 239 |
+
docker-compose logs -f
|
| 240 |
+
|
| 241 |
+
# Stop
|
| 242 |
+
docker-compose down
|
| 243 |
+
```
|
| 244 |
+
|
| 245 |
+
#### **3. Ports:**
|
| 246 |
+
- **8501**: Streamlit UI
|
| 247 |
+
- **5000**: MLflow UI
|
| 248 |
+
- **54321**: H2O Cluster
|
| 249 |
+
|
| 250 |
+
---
|
| 251 |
+
|
| 252 |
+
## 🖥️ **Desktop App (Electron)**
|
| 253 |
+
|
| 254 |
+
### 📦 **Installation and Build:**
|
| 255 |
+
|
| 256 |
+
#### **1. Install Node.js:**
|
| 257 |
+
```bash
|
| 258 |
+
# Download: https://nodejs.org/
|
| 259 |
+
node --version
|
| 260 |
+
npm --version
|
| 261 |
+
```
|
| 262 |
+
|
| 263 |
+
#### **2. Install Dependencies:**
|
| 264 |
+
```bash
|
| 265 |
+
npm install
|
| 266 |
+
```
|
| 267 |
+
|
| 268 |
+
#### **3. Development Mode:**
|
| 269 |
+
```bash
|
| 270 |
+
npm run dev
|
| 271 |
+
```
|
| 272 |
+
|
| 273 |
+
#### **4. Production Build:**
|
| 274 |
+
```bash
|
| 275 |
+
# Windows
|
| 276 |
+
npm run build-win
|
| 277 |
+
|
| 278 |
+
# Mac
|
| 279 |
+
npm run build-mac
|
| 280 |
+
|
| 281 |
+
# Linux
|
| 282 |
+
npm run build-linux
|
| 283 |
+
```
|
| 284 |
+
|
| 285 |
+
#### **5. Desktop Features:**
|
| 286 |
+
- **Native window** (without browser)
|
| 287 |
+
- **Professional menu** with shortcuts
|
| 288 |
+
- **Native file dialogs**
|
| 289 |
+
- **System integration**
|
| 290 |
+
- **Offline mode**
|
| 291 |
+
|
| 292 |
+
---
|
| 293 |
+
|
| 294 |
+
## 📊 **Performance and Benchmarks**
|
| 295 |
+
|
| 296 |
+
### 🏆 **Framework Comparison:**
|
| 297 |
+
|
| 298 |
+
| Framework | Speed | Performance | Memory | Ease of Use |
|
| 299 |
+
|-----------|-------|-------------|--------|-------------|
|
| 300 |
+
| **AutoGluon** | ⚡⚡⚡ | 🏆🏆 | 🏆🏆 | 🏆🏆🏆 |
|
| 301 |
+
| **FLAML** | ⚡⚡⚡⚡ | 🏆🏆 | 🏆🏆🏆 | 🏆🏆 |
|
| 302 |
+
| **H2O** | ⚡⚡ | 🏆🏆🏆 | 🏆 | 🏆 |
|
| 303 |
+
| **TPOT** | ⚡ | 🏆🏆🏆 | 🏆🏆 | 🏆 |
|
| 304 |
+
|
| 305 |
+
### 📈 **Performance Metrics:**
|
| 306 |
+
|
| 307 |
+
#### **Test Dataset (10k rows, 50 columns):**
|
| 308 |
+
```
|
| 309 |
+
AutoGluon: 2.5 min, 94.2% accuracy
|
| 310 |
+
FLAML: 1.8 min, 93.8% accuracy
|
| 311 |
+
H2O: 4.2 min, 94.0% accuracy
|
| 312 |
+
```
|
| 313 |
+
|
| 314 |
+
#### **Memory Usage:**
|
| 315 |
+
```
|
| 316 |
+
AutoGluon: ~2GB RAM
|
| 317 |
+
FLAML: ~1.5GB RAM
|
| 318 |
+
H2O: ~3GB RAM
|
| 319 |
+
TPOT: ~1GB RAM (Optimized)
|
| 320 |
+
```
|
| 321 |
+
|
| 322 |
+
---
|
| 323 |
+
|
| 324 |
+
## 🔧 **Troubleshooting**
|
| 325 |
+
|
| 326 |
+
### ❌ **Common Issues:**
|
| 327 |
+
|
| 328 |
+
#### **"Java not found" (H2O):**
|
| 329 |
+
```bash
|
| 330 |
+
# Windows: Add JAVA_HOME
|
| 331 |
+
set JAVA_HOME="C:\Program Files\Java\jdk-11"
|
| 332 |
+
|
| 333 |
+
# Mac/Linux: Export variable
|
| 334 |
+
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk
|
| 335 |
+
```
|
| 336 |
+
|
| 337 |
+
#### **"Port already in use":**
|
| 338 |
+
```bash
|
| 339 |
+
# Check ports
|
| 340 |
+
netstat -an | findstr 8501
|
| 341 |
+
|
| 342 |
+
# Kill process
|
| 343 |
+
taskkill /PID <PID> /F
|
| 344 |
+
|
| 345 |
+
# Use another port
|
| 346 |
+
streamlit run app.py --server.port 8502
|
| 347 |
+
```
|
| 348 |
+
|
| 349 |
+
#### **"Memory error":**
|
| 350 |
+
```bash
|
| 351 |
+
# Increase H2O memory
|
| 352 |
+
export H2O_MAX_MEM_SIZE="8G"
|
| 353 |
+
|
| 354 |
+
# Or reduce dataset
|
| 355 |
+
```
|
| 356 |
+
|
| 357 |
+
#### **"MLflow connection error" / "Missing mlruns":**
|
| 358 |
+
```bash
|
| 359 |
+
# In the new version, the mlruns/.trash directory is automatically healed and recreated if broken.
|
| 360 |
+
# For other issues:
|
| 361 |
+
mlflow server --host 0.0.0.0 --port 5000
|
| 362 |
+
```
|
| 363 |
+
|
| 364 |
+
---
|
| 365 |
+
|
| 366 |
+
## 🧪 **Testing**
|
| 367 |
+
|
| 368 |
+
### 📋 **Test Suite:**
|
| 369 |
+
|
| 370 |
+
#### **1. Integration Tests:**
|
| 371 |
+
```bash
|
| 372 |
+
# Test H2O integration
|
| 373 |
+
python tests/test_h2o_integration.py
|
| 374 |
+
|
| 375 |
+
# Test MLflow integration
|
| 376 |
+
python tests/test_mlflow_integration.py
|
| 377 |
+
```
|
| 378 |
+
|
| 379 |
+
#### **2. Unit Tests:**
|
| 380 |
+
```bash
|
| 381 |
+
# Test utils
|
| 382 |
+
pytest tests/test_utils.py
|
| 383 |
+
|
| 384 |
+
# Test interface
|
| 385 |
+
pytest tests/test_interface.py
|
| 386 |
+
```
|
| 387 |
+
|
| 388 |
+
#### **3. Performance Tests:**
|
| 389 |
+
```bash
|
| 390 |
+
# Benchmark frameworks
|
| 391 |
+
python tests/benchmark_frameworks.py
|
| 392 |
+
```
|
| 393 |
+
|
| 394 |
+
---
|
| 395 |
+
|
| 396 |
+
## 📁 **Project Structure**
|
| 397 |
+
|
| 398 |
+
```
|
| 399 |
+
Multi-AutoML-Interface/
|
| 400 |
+
├── 📁 src/ # Main source code
|
| 401 |
+
│ ├── 📄 autogluon_utils.py # AutoGluon integration
|
| 402 |
+
│ ├── 📄 flaml_utils.py # FLAML integration
|
| 403 |
+
│ ├── 📄 h2o_utils.py # H2O integration
|
| 404 |
+
│ ├── 📄 tpot_utils.py # TPOT integration
|
| 405 |
+
│ ├── 📄 mlflow_utils.py # MLflow helpers and auto-healing
|
| 406 |
+
│ ├── 📄 mlflow_cache.py # Cache optimization
|
| 407 |
+
│ ├── 📄 data_utils.py # Data processing
|
| 408 |
+
│ └── 📄 log_utils.py # Logging utilities
|
| 409 |
+
├── 📁 tests/ # Automated tests
|
| 410 |
+
│ ├── 📄 test_h2o_integration.py
|
| 411 |
+
│ ├── 📄 test_mlflow_integration.py
|
| 412 |
+
│ └── 📄 test_performance.py
|
| 413 |
+
├── 📁 electron/ # Desktop app (Electron)
|
| 414 |
+
│ ├── 📄 main.js # Main process
|
| 415 |
+
│ ├── 📄 preload.js # Security bridge
|
| 416 |
+
│ ├── 📄 renderer.js # UI enhancements
|
| 417 |
+
│ └── 📁 assets/ # Icons and resources
|
| 418 |
+
├── 📄 app.py # Streamlit main app
|
| 419 |
+
├── 📄 requirements.txt # Python dependencies
|
| 420 |
+
├── 📄 package.json # Node.js dependencies
|
| 421 |
+
├── 🐳 Dockerfile # Docker configuration
|
| 422 |
+
├── 🐳 docker-compose.yml # Multi-service setup
|
| 423 |
+
└── 📄 README.md # This file
|
| 424 |
+
```
|
| 425 |
+
|
| 426 |
+
---
|
| 427 |
+
|
| 428 |
+
## 🤝 **Contributing**
|
| 429 |
+
|
| 430 |
+
### 🎯 **How to Contribute:**
|
| 431 |
+
|
| 432 |
+
#### **1. Fork and Clone:**
|
| 433 |
+
```bash
|
| 434 |
+
git clone https://github.com/PedroM2626/Multi-AutoML-Interface.git
|
| 435 |
+
cd Multi-AutoML-Interface
|
| 436 |
+
```
|
| 437 |
+
|
| 438 |
+
#### **2. Create Branch:**
|
| 439 |
+
```bash
|
| 440 |
+
git checkout -b feature/new-feature
|
| 441 |
+
```
|
| 442 |
+
|
| 443 |
+
#### **3. Develop:**
|
| 444 |
+
- Follow existing code style
|
| 445 |
+
- Add tests
|
| 446 |
+
- Document changes
|
| 447 |
+
|
| 448 |
+
#### **4. Commit and Push:**
|
| 449 |
+
```bash
|
| 450 |
+
git add .
|
| 451 |
+
git commit -m "feat: add new feature"
|
| 452 |
+
git push origin feature/new-feature
|
| 453 |
+
```
|
| 454 |
+
|
| 455 |
+
#### **5. Pull Request:**
|
| 456 |
+
- Describe changes
|
| 457 |
+
- Link issues
|
| 458 |
+
- Await review
|
| 459 |
+
|
| 460 |
+
### 📝 **Guidelines:**
|
| 461 |
+
- **Python**: PEP 8
|
| 462 |
+
- **JavaScript**: ESLint
|
| 463 |
+
- **Commits**: Conventional Commits
|
| 464 |
+
- **Docs**: Clear Markdown
|
| 465 |
+
|
| 466 |
+
---
|
| 467 |
+
|
| 468 |
+
## 📄 **License**
|
| 469 |
+
|
| 470 |
+
This project is licensed under the **MIT License** - see the [LICENSE](LICENSE) file for details.
|
| 471 |
+
|
| 472 |
+
---
|
| 473 |
+
|
| 474 |
+
## 🙏 **Credits and Acknowledgements**
|
| 475 |
+
|
| 476 |
+
### 🤖 **Frameworks:**
|
| 477 |
+
- **AutoGluon** - Amazon Web Services
|
| 478 |
+
- **FLAML** - Microsoft Research
|
| 479 |
+
- **H2O AutoML** - H2O.ai
|
| 480 |
+
- **TPOT** - Rhodes Lab
|
| 481 |
+
- **MLflow** - Databricks
|
| 482 |
+
|
| 483 |
+
### 🛠️ **Technologies:**
|
| 484 |
+
- **Streamlit** - Web interface
|
| 485 |
+
- **Electron** - Desktop app
|
| 486 |
+
- **Docker** - Containerization
|
| 487 |
+
- **FastAPI** - Backend API
|
| 488 |
+
|
| 489 |
+
### 📚 **Resources:**
|
| 490 |
+
- **AutoML Documentation**
|
| 491 |
+
- **MLflow Tracking**
|
| 492 |
+
- **Streamlit Components**
|
| 493 |
+
- **Electron Security**
|
| 494 |
+
|
| 495 |
+
---
|
| 496 |
+
|
| 497 |
+
## 🗺️ **Future Roadmap**
|
| 498 |
+
|
| 499 |
+
### 🚀 **Upcoming Features**
|
| 500 |
+
- [ ] **Auto-sklearn** (meta-learning)
|
| 501 |
+
- [ ] **Model explainability** (SHAP, LIME)
|
| 502 |
+
- [ ] **Advanced visualizations**
|
| 503 |
+
- [ ] **Batch processing**
|
| 504 |
+
|
| 505 |
---
|
| 506 |
+
|
| 507 |
+
### 🌐 **Live Demo:**
|
| 508 |
+
[Hugging Face Spaces - Multi-AutoML Interface](https://huggingface.co/spaces/PedroM2626/Multi-AutoML-Interface)
|
| 509 |
+
|
| 510 |
+
---
|
| 511 |
+
|
| 512 |
+
## 🎉 **Conclusion**
|
| 513 |
+
|
| 514 |
+
The **Multi-AutoML Interface** represents a complete and professional solution for AutoML experimentation, combining:
|
| 515 |
+
|
| 516 |
+
- **🤖 Multiple frameworks** in a unified interface
|
| 517 |
+
- **📊 Integrated MLOps** with full tracking
|
| 518 |
+
- **🖥️ Flexible deployment** (web, desktop, container)
|
| 519 |
+
- **🎛️ Intuitive interface** for technical users
|
| 520 |
+
- **🔧 Advanced configuration** for experts
|
| 521 |
+
- **📈 Optimized performance** for production
|
| 522 |
+
|
| 523 |
+
**Ideal for:**
|
| 524 |
+
- **Data Scientists** wanting to compare frameworks
|
| 525 |
+
- **Researchers** experimenting with different approaches
|
| 526 |
+
- **Students** learning about AutoML
|
| 527 |
+
|
| 528 |
---
|
| 529 |
|
| 530 |
+
*Developed by Pedro Morato Lahoz*
|
app.py
CHANGED
|
@@ -9,19 +9,35 @@ import matplotlib.pyplot as plt
|
|
| 9 |
import seaborn as sns
|
| 10 |
import importlib
|
| 11 |
import queue
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
from src.data_utils import load_data, get_data_summary, save_to_data_lake, init_dvc, get_data_lake_files, get_dvc_hash
|
| 27 |
from src.autogluon_utils import train_model as train_autogluon, load_model_from_mlflow as load_autogluon
|
|
@@ -52,19 +68,19 @@ if 'log_queue' not in st.session_state:
|
|
| 52 |
st.title("🚀 AutoML Multi-Framework Interface")
|
| 53 |
|
| 54 |
# Sidebar navigation
|
| 55 |
-
st.sidebar.title("
|
| 56 |
-
menu = st.sidebar.selectbox("Menu", ["
|
| 57 |
|
| 58 |
st.sidebar.markdown("---")
|
| 59 |
-
st.sidebar.header("🔗
|
| 60 |
-
use_dagshub = st.sidebar.checkbox("
|
| 61 |
|
| 62 |
if use_dagshub:
|
| 63 |
-
dagshub_user = st.sidebar.text_input("
|
| 64 |
-
dagshub_repo = st.sidebar.text_input("
|
| 65 |
-
dagshub_token = st.sidebar.text_input("Token
|
| 66 |
|
| 67 |
-
if st.sidebar.button("
|
| 68 |
if dagshub_user and dagshub_repo and dagshub_token:
|
| 69 |
try:
|
| 70 |
import dagshub
|
|
@@ -72,80 +88,81 @@ if use_dagshub:
|
|
| 72 |
os.environ["MLFLOW_TRACKING_USERNAME"] = dagshub_user
|
| 73 |
os.environ["MLFLOW_TRACKING_PASSWORD"] = dagshub_token
|
| 74 |
dagshub.init(repo_owner=dagshub_user, repo_name=dagshub_repo, mlflow=True)
|
| 75 |
-
st.sidebar.success("
|
| 76 |
except ImportError:
|
| 77 |
-
st.sidebar.error("
|
| 78 |
except Exception as e:
|
| 79 |
-
st.sidebar.error(f"
|
| 80 |
else:
|
| 81 |
-
st.sidebar.warning("
|
| 82 |
st.sidebar.markdown("---")
|
| 83 |
|
| 84 |
-
if menu == "
|
| 85 |
-
st.header("📂 Upload
|
| 86 |
|
| 87 |
-
st.markdown("
|
| 88 |
-
uploaded_file = st.file_uploader("
|
| 89 |
-
filename_prefix = st.text_input("
|
| 90 |
|
| 91 |
-
if st.button("
|
| 92 |
if uploaded_file is not None:
|
| 93 |
try:
|
| 94 |
-
with st.spinner("
|
| 95 |
init_dvc()
|
| 96 |
-
df =
|
| 97 |
t_path, t_tag, t_hash = save_to_data_lake(df, filename_prefix)
|
| 98 |
-
st.
|
|
|
|
| 99 |
|
| 100 |
-
st.subheader("
|
| 101 |
st.dataframe(df.head())
|
| 102 |
|
| 103 |
-
st.subheader("
|
| 104 |
-
summary =
|
| 105 |
s_col1, s_col2 = st.columns(2)
|
| 106 |
-
s_col1.metric("
|
| 107 |
-
s_col2.metric("
|
| 108 |
|
| 109 |
-
st.write("
|
| 110 |
summary_df = pd.DataFrame({
|
| 111 |
-
"
|
| 112 |
-
"
|
| 113 |
})
|
| 114 |
st.table(summary_df)
|
| 115 |
|
| 116 |
except Exception as e:
|
| 117 |
-
st.error(f"
|
| 118 |
else:
|
| 119 |
-
st.error("
|
| 120 |
|
| 121 |
-
elif menu == "
|
| 122 |
-
st.header("🧠
|
| 123 |
|
| 124 |
-
available_files =
|
| 125 |
|
| 126 |
if not available_files:
|
| 127 |
-
st.warning("
|
| 128 |
st.stop()
|
| 129 |
|
| 130 |
-
st.subheader("1.
|
| 131 |
|
| 132 |
# UI mapping filenames
|
| 133 |
-
file_options = ["
|
| 134 |
file_paths_map = {os.path.basename(f): f for f in available_files}
|
| 135 |
|
| 136 |
col1, col2, col3 = st.columns(3)
|
| 137 |
with col1:
|
| 138 |
-
train_file_selection = st.selectbox("
|
| 139 |
with col2:
|
| 140 |
-
valid_file_selection = st.selectbox("
|
| 141 |
with col3:
|
| 142 |
-
test_file_selection = st.selectbox("
|
| 143 |
|
| 144 |
if train_file_selection:
|
| 145 |
try:
|
| 146 |
# Load Train
|
| 147 |
train_path = file_paths_map[train_file_selection]
|
| 148 |
-
df =
|
| 149 |
|
| 150 |
# Fetch Hash
|
| 151 |
t_hash_full, t_hash_short = get_dvc_hash(train_path)
|
|
@@ -153,17 +170,17 @@ elif menu == "Treinamento":
|
|
| 153 |
|
| 154 |
# Load Valid
|
| 155 |
valid_df = None
|
| 156 |
-
if valid_file_selection != "
|
| 157 |
valid_path = file_paths_map[valid_file_selection]
|
| 158 |
-
valid_df =
|
| 159 |
v_hash_full, v_hash_short = get_dvc_hash(valid_path)
|
| 160 |
dvc_hashes["dvc_valid_hash"] = v_hash_short
|
| 161 |
|
| 162 |
# Load Test
|
| 163 |
test_df = None
|
| 164 |
-
if test_file_selection != "
|
| 165 |
test_path = file_paths_map[test_file_selection]
|
| 166 |
-
test_df =
|
| 167 |
te_hash_full, te_hash_short = get_dvc_hash(test_path)
|
| 168 |
dvc_hashes["dvc_test_hash"] = te_hash_short
|
| 169 |
|
|
@@ -174,10 +191,67 @@ elif menu == "Treinamento":
|
|
| 174 |
st.session_state['dvc_hashes'] = dvc_hashes
|
| 175 |
|
| 176 |
except Exception as e:
|
| 177 |
-
st.error(f"
|
| 178 |
|
| 179 |
st.markdown("---")
|
| 180 |
-
st.subheader("2.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 181 |
|
| 182 |
if st.session_state['df'] is not None:
|
| 183 |
df = st.session_state['df']
|
|
@@ -186,36 +260,36 @@ elif menu == "Treinamento":
|
|
| 186 |
|
| 187 |
columns = df.columns.tolist()
|
| 188 |
|
| 189 |
-
framework = st.selectbox("
|
| 190 |
-
target = st.selectbox("
|
| 191 |
-
run_name = st.text_input("
|
| 192 |
|
| 193 |
# Datasets info
|
| 194 |
-
st.info(f"Datasets
|
| 195 |
|
| 196 |
# Framework specific options
|
| 197 |
-
st.subheader(f"
|
| 198 |
|
| 199 |
-
#
|
| 200 |
-
seed = st.number_input("Seed (
|
| 201 |
|
| 202 |
-
#
|
| 203 |
time_limit = time_budget = max_runtime_secs = 60
|
| 204 |
presets = task = metric = estimator_list = None
|
| 205 |
nfolds = balance_classes = sort_metric = exclude_algos = None
|
| 206 |
|
| 207 |
if framework == "AutoGluon":
|
| 208 |
-
time_limit = st.slider("
|
| 209 |
presets = st.selectbox("Presets", ['medium_quality', 'best_quality', 'high_quality', 'good_quality', 'optimize_for_deployment'])
|
| 210 |
elif framework == "FLAML":
|
| 211 |
-
time_budget = st.slider("
|
| 212 |
-
task = st.selectbox("
|
| 213 |
|
| 214 |
# Smart metric selection for FLAML
|
| 215 |
num_classes = df[target].nunique()
|
| 216 |
if task == 'classification':
|
| 217 |
if num_classes > 2:
|
| 218 |
-
st.warning(f"
|
| 219 |
metric_options = ['auto', 'accuracy', 'macro_f1', 'micro_f1', 'roc_auc_ovr', 'roc_auc_ovo', 'log_loss']
|
| 220 |
else:
|
| 221 |
metric_options = ['auto', 'accuracy', 'roc_auc', 'f1', 'log_loss']
|
|
@@ -224,69 +298,74 @@ elif menu == "Treinamento":
|
|
| 224 |
else:
|
| 225 |
metric_options = ['auto']
|
| 226 |
|
| 227 |
-
metric = st.selectbox("
|
| 228 |
-
estimators = st.multiselect("
|
| 229 |
estimator_list = estimators if estimators else 'auto'
|
| 230 |
elif framework == "H2O AutoML":
|
| 231 |
-
st.warning("⚠️ H2O AutoML
|
| 232 |
-
st.info("💡
|
| 233 |
|
| 234 |
-
max_runtime_secs = st.slider("
|
| 235 |
-
max_models = st.slider("
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
sort_metric = st.selectbox("Métrica de ordenação", ["AUTO", "AUC", "logloss", "RMSE", "MAE", "F1"])
|
| 242 |
|
| 243 |
-
|
| 244 |
-
|
|
|
|
|
|
|
| 245 |
elif framework == "TPOT":
|
| 246 |
-
st.info("🧬 TPOT
|
| 247 |
-
st.warning("⚠️ TPOT
|
| 248 |
|
| 249 |
-
generations = st.slider("
|
| 250 |
-
population_size = st.slider("
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
|
| 257 |
-
#
|
| 258 |
-
with st.expander("⚙️
|
| 259 |
-
config_dict = st.selectbox("
|
| 260 |
'TPOT light', 'TPOT MDR', 'TPOT sparse', 'TPOT NN'
|
| 261 |
-
], help="
|
| 262 |
|
| 263 |
-
tfidf_max_features = st.number_input("
|
| 264 |
-
ngram_max = st.slider("
|
| 265 |
tfidf_ngram_range = (1, ngram_max)
|
| 266 |
|
| 267 |
-
#
|
| 268 |
problem_type = 'classification' if df[target].nunique() <= 20 or df[target].dtype == 'object' else 'regression'
|
| 269 |
-
st.info(f"🎯
|
| 270 |
|
| 271 |
-
#
|
| 272 |
if problem_type == 'classification':
|
| 273 |
scoring_options = ['accuracy', 'balanced_accuracy', 'f1_macro', 'f1_micro', 'f1_weighted', 'roc_auc_ovr', 'roc_auc_ovo', 'precision_macro', 'recall_macro']
|
| 274 |
else:
|
| 275 |
scoring_options = ['neg_mean_squared_error', 'neg_root_mean_squared_error', 'neg_mean_absolute_error', 'r2', 'explained_variance']
|
| 276 |
|
| 277 |
-
scoring = st.selectbox("
|
| 278 |
|
| 279 |
-
if st.button("
|
| 280 |
-
st.subheader("📺
|
| 281 |
|
| 282 |
col_logs, col_chart = st.columns([1, 1])
|
| 283 |
|
| 284 |
with col_logs:
|
| 285 |
-
st.write("📋
|
| 286 |
log_placeholder = st.empty()
|
| 287 |
|
| 288 |
with col_chart:
|
| 289 |
-
st.write("📈
|
| 290 |
chart_placeholder = st.empty()
|
| 291 |
|
| 292 |
# Shared state for thread communication
|
|
@@ -331,10 +410,10 @@ elif menu == "Treinamento":
|
|
| 331 |
with redirect_stdout(LogIO()), redirect_stderr(LogIO()):
|
| 332 |
try:
|
| 333 |
if framework == "AutoGluon":
|
| 334 |
-
res_predictor, res_run_id = train_autogluon(df, target, run_name, valid_df, test_df, time_limit, presets, seed)
|
| 335 |
result_queue.put({"predictor": res_predictor, "run_id": res_run_id, "type": "autogluon", "success": True})
|
| 336 |
elif framework == "FLAML":
|
| 337 |
-
res_automl, res_run_id = train_flaml_model(df, target, run_name, valid_df, test_df, time_budget, task, metric, estimator_list, seed)
|
| 338 |
result_queue.put({"predictor": res_automl, "run_id": res_run_id, "type": "flaml", "success": True})
|
| 339 |
elif framework == "H2O AutoML":
|
| 340 |
res_automl, res_run_id = train_h2o_model(
|
|
@@ -369,7 +448,7 @@ elif menu == "Treinamento":
|
|
| 369 |
result_queue.put({"predictor": res_tpot, "pipeline": res_pipeline, "run_id": res_run_id, "info": res_info, "type": "tpot", "success": True})
|
| 370 |
except Exception as e:
|
| 371 |
import traceback
|
| 372 |
-
error_msg = f"
|
| 373 |
log_queue.put(error_msg)
|
| 374 |
result_queue.put({"success": False, "error": str(e)})
|
| 375 |
finally:
|
|
@@ -452,26 +531,26 @@ elif menu == "Treinamento":
|
|
| 452 |
st.session_state['predictor'] = final_result["predictor"]
|
| 453 |
st.session_state['run_id'] = final_result["run_id"]
|
| 454 |
st.session_state['model_type'] = final_result["type"]
|
| 455 |
-
st.success(f"
|
| 456 |
|
| 457 |
# Log DVC hashes to MLflow run
|
| 458 |
if 'dvc_hashes' in st.session_state and st.session_state['dvc_hashes']:
|
| 459 |
try:
|
| 460 |
with mlflow.start_run(run_id=final_result["run_id"]):
|
| 461 |
mlflow.log_params(st.session_state['dvc_hashes'])
|
| 462 |
-
st.info("🧬
|
| 463 |
except Exception as e:
|
| 464 |
-
st.warning(f"
|
| 465 |
|
| 466 |
else:
|
| 467 |
-
st.error(f"
|
| 468 |
|
| 469 |
# Show all logs at the end
|
| 470 |
while not log_queue.empty():
|
| 471 |
all_logs.append(log_queue.get())
|
| 472 |
|
| 473 |
if all_logs:
|
| 474 |
-
with st.expander("
|
| 475 |
st.code("\n".join(all_logs))
|
| 476 |
|
| 477 |
# Post-training visualizations
|
|
@@ -479,17 +558,17 @@ elif menu == "Treinamento":
|
|
| 479 |
if final_result['type'] == "flaml":
|
| 480 |
predictor = final_result['predictor']
|
| 481 |
|
| 482 |
-
st.subheader("🏆
|
| 483 |
col1, col2, col3 = st.columns(3)
|
| 484 |
-
col1.metric("
|
| 485 |
-
col2.metric("
|
| 486 |
-
col3.metric("
|
| 487 |
|
| 488 |
-
with st.expander("⚙️
|
| 489 |
st.json(predictor.best_config)
|
| 490 |
|
| 491 |
if hasattr(predictor, 'best_config_per_estimator') and predictor.best_config_per_estimator:
|
| 492 |
-
with st.expander("📊
|
| 493 |
st.json(predictor.best_config_per_estimator)
|
| 494 |
|
| 495 |
if hasattr(predictor, 'feature_importances_') and predictor.feature_importances_ is not None:
|
|
@@ -507,30 +586,29 @@ elif menu == "Treinamento":
|
|
| 507 |
plt.title("Top 10 Feature Importances (FLAML)")
|
| 508 |
st.pyplot(fig)
|
| 509 |
else:
|
| 510 |
-
st.info(f"
|
| 511 |
except Exception as feat_err:
|
| 512 |
-
st.warning(f"
|
| 513 |
-
|
| 514 |
elif final_result['type'] == "autogluon":
|
| 515 |
predictor = final_result['predictor']
|
| 516 |
-
st.subheader("🏆
|
| 517 |
|
| 518 |
-
st.subheader("
|
| 519 |
leaderboard = predictor.leaderboard(silent=True)
|
| 520 |
st.dataframe(leaderboard)
|
| 521 |
|
| 522 |
best_model = leaderboard.iloc[0]['model'] if not leaderboard.empty else "Modelo principal"
|
| 523 |
-
st.success(f"
|
| 524 |
|
| 525 |
-
with st.expander("⚙️
|
| 526 |
try:
|
| 527 |
info = predictor.info()
|
| 528 |
st.json(info)
|
| 529 |
except:
|
| 530 |
-
st.write("
|
| 531 |
|
| 532 |
-
if st.checkbox("
|
| 533 |
-
with st.spinner("
|
| 534 |
try:
|
| 535 |
fi = predictor.feature_importance(df)
|
| 536 |
st.dataframe(fi)
|
|
@@ -539,31 +617,31 @@ elif menu == "Treinamento":
|
|
| 539 |
plt.title("Feature Importance (AutoGluon)")
|
| 540 |
st.pyplot(fig)
|
| 541 |
except Exception as e:
|
| 542 |
-
st.error(f"
|
| 543 |
|
| 544 |
elif final_result['type'] == "h2o":
|
| 545 |
automl = final_result['predictor']
|
| 546 |
-
st.subheader("🏆
|
| 547 |
|
| 548 |
-
#
|
| 549 |
try:
|
| 550 |
best_model = automl.leader
|
| 551 |
if best_model is not None:
|
| 552 |
-
st.success(f"
|
| 553 |
|
| 554 |
-
st.subheader("
|
| 555 |
try:
|
| 556 |
leaderboard = automl.leaderboard.as_data_frame()
|
| 557 |
st.dataframe(leaderboard)
|
| 558 |
except Exception as e:
|
| 559 |
-
st.warning(f"
|
| 560 |
-
#
|
| 561 |
try:
|
| 562 |
st.text(str(automl.leaderboard.head(10)))
|
| 563 |
except:
|
| 564 |
-
st.info("Leaderboard
|
| 565 |
|
| 566 |
-
with st.expander("⚙️
|
| 567 |
try:
|
| 568 |
model_params = {
|
| 569 |
"model_id": best_model.model_id,
|
|
@@ -572,35 +650,34 @@ elif menu == "Treinamento":
|
|
| 572 |
}
|
| 573 |
st.json(model_params)
|
| 574 |
except Exception as e:
|
| 575 |
-
st.warning(f"
|
| 576 |
else:
|
| 577 |
-
st.warning("⚠️
|
| 578 |
-
st.info("
|
| 579 |
-
st.info("•
|
| 580 |
-
st.info("•
|
| 581 |
-
st.info("•
|
| 582 |
|
| 583 |
-
#
|
| 584 |
try:
|
| 585 |
-
st.subheader("📊
|
| 586 |
-
st.info(f"•
|
| 587 |
st.info(f"• Run ID: {final_result['run_id']}")
|
| 588 |
-
st.info(f"• Status:
|
| 589 |
-
st.info(f"•
|
| 590 |
-
st.info(f"• Recomendação: Aumentar tempo máximo ou reduzir complexidade dos dados")
|
| 591 |
except:
|
| 592 |
pass
|
| 593 |
except Exception as e:
|
| 594 |
-
st.error(f"⚠️
|
| 595 |
-
st.info("
|
| 596 |
|
| 597 |
-
#
|
| 598 |
try:
|
| 599 |
-
st.info(f"📊 **
|
| 600 |
-
st.info(f"•
|
| 601 |
st.info(f"• Run ID: {final_result['run_id']}")
|
| 602 |
-
st.info(f"• Status:
|
| 603 |
-
st.info(f"•
|
| 604 |
except:
|
| 605 |
pass
|
| 606 |
|
|
@@ -609,16 +686,16 @@ elif menu == "Treinamento":
|
|
| 609 |
pipeline = final_result['pipeline']
|
| 610 |
info = final_result['info']
|
| 611 |
|
| 612 |
-
st.subheader("🧬
|
| 613 |
|
| 614 |
-
#
|
| 615 |
col1, col2, col3, col4 = st.columns(4)
|
| 616 |
-
col1.metric("
|
| 617 |
-
col2.metric("
|
| 618 |
-
col3.metric("
|
| 619 |
col4.metric("Features", info['n_features'])
|
| 620 |
|
| 621 |
-
#
|
| 622 |
if info['problem_type'] == 'classification':
|
| 623 |
col1, col2, col3 = st.columns(3)
|
| 624 |
col1.metric("Accuracy", f"{info.get('accuracy', 0):.4f}")
|
|
@@ -630,40 +707,40 @@ elif menu == "Treinamento":
|
|
| 630 |
col2.metric("R²", f"{info.get('r2', 0):.4f}")
|
| 631 |
col3.metric("MSE", f"{info.get('mse', 0):.4f}")
|
| 632 |
|
| 633 |
-
#
|
| 634 |
-
with st.expander("🧬
|
| 635 |
st.code(str(tpot.fitted_pipeline_), language="python")
|
| 636 |
|
| 637 |
-
#
|
| 638 |
-
with st.expander("📊
|
| 639 |
st.json(info)
|
| 640 |
|
| 641 |
-
#
|
| 642 |
-
st.info(f"⏱️ **
|
| 643 |
-
st.info(f"🎯 **
|
| 644 |
|
| 645 |
except Exception as e:
|
| 646 |
import traceback
|
| 647 |
error_details = traceback.format_exc()
|
| 648 |
-
st.error(f"
|
| 649 |
-
with st.expander("
|
| 650 |
st.code(error_details)
|
| 651 |
finally:
|
| 652 |
pass
|
| 653 |
else:
|
| 654 |
-
st.warning("
|
| 655 |
|
| 656 |
-
elif menu == "
|
| 657 |
-
st.header("🔮
|
| 658 |
|
| 659 |
-
load_option = st.radio("
|
| 660 |
|
| 661 |
-
if load_option == "
|
| 662 |
col1, col2 = st.columns(2)
|
| 663 |
-
m_type = col1.selectbox("
|
| 664 |
run_id_input = col2.text_input("Run ID")
|
| 665 |
|
| 666 |
-
if st.button("
|
| 667 |
try:
|
| 668 |
if m_type == "AutoGluon":
|
| 669 |
st.session_state['predictor'] = load_autogluon(run_id_input)
|
|
@@ -677,27 +754,27 @@ elif menu == "Predição":
|
|
| 677 |
elif m_type == "TPOT":
|
| 678 |
st.session_state['predictor'] = load_tpot_model(run_id_input)
|
| 679 |
st.session_state['model_type'] = "tpot"
|
| 680 |
-
st.success("
|
| 681 |
except Exception as e:
|
| 682 |
-
st.error(f"
|
| 683 |
|
| 684 |
if st.session_state['predictor'] is not None:
|
| 685 |
predictor = st.session_state['predictor']
|
| 686 |
m_type = st.session_state['model_type']
|
| 687 |
|
| 688 |
-
st.info(f"
|
| 689 |
|
| 690 |
-
predict_file = st.file_uploader("
|
| 691 |
|
| 692 |
if predict_file is not None:
|
| 693 |
predict_df = load_data(predict_file)
|
| 694 |
st.dataframe(predict_df.head())
|
| 695 |
|
| 696 |
-
if st.button("
|
| 697 |
try:
|
| 698 |
-
#
|
| 699 |
if predictor is None:
|
| 700 |
-
st.error("
|
| 701 |
st.stop()
|
| 702 |
|
| 703 |
if m_type == "autogluon":
|
|
@@ -711,51 +788,51 @@ elif menu == "Predição":
|
|
| 711 |
result_df = predict_df.copy()
|
| 712 |
result_df['Predictions'] = predictions
|
| 713 |
|
| 714 |
-
st.success("
|
| 715 |
st.dataframe(result_df)
|
| 716 |
|
| 717 |
csv = result_df.to_csv(index=False).encode('utf-8')
|
| 718 |
-
st.download_button("Download CSV", csv, "predictions.csv", "text/csv")
|
| 719 |
except Exception as e:
|
| 720 |
-
st.error(f"
|
| 721 |
|
| 722 |
-
elif menu == "
|
| 723 |
-
st.header("📊
|
| 724 |
|
| 725 |
# Button to clean corrupted MLflow metadata
|
| 726 |
-
if st.sidebar.button("
|
| 727 |
import shutil
|
| 728 |
if os.path.exists("mlruns"):
|
| 729 |
# Instead of deleting everything, we could try to find the malformed ones
|
| 730 |
# but deleting is safer for a local "repair"
|
| 731 |
shutil.rmtree("mlruns")
|
| 732 |
-
st.sidebar.success("Cache
|
| 733 |
st.rerun()
|
| 734 |
|
| 735 |
-
#
|
| 736 |
-
if st.sidebar.button("
|
| 737 |
mlflow_cache.clear_cache()
|
| 738 |
-
st.sidebar.success("Cache
|
| 739 |
st.rerun()
|
| 740 |
|
| 741 |
-
#
|
| 742 |
experiment_list = get_cached_experiment_list()
|
| 743 |
-
exp_name = st.selectbox("
|
| 744 |
|
| 745 |
try:
|
| 746 |
-
#
|
| 747 |
runs = mlflow_cache.get_cached_all_runs(exp_name)
|
| 748 |
|
| 749 |
if not runs.empty:
|
| 750 |
st.dataframe(runs)
|
| 751 |
|
| 752 |
-
#
|
| 753 |
-
with st.expander("📊
|
| 754 |
-
st.write(f"
|
| 755 |
-
st.write(f"Total
|
| 756 |
-
st.write(f"Cache TTL: 5
|
| 757 |
else:
|
| 758 |
-
st.write("
|
| 759 |
except Exception as e:
|
| 760 |
-
st.error(f"
|
| 761 |
-
st.warning("
|
|
|
|
| 9 |
import seaborn as sns
|
| 10 |
import importlib
|
| 11 |
import queue
|
| 12 |
+
from sklearn.model_selection import train_test_split
|
| 13 |
|
| 14 |
+
# Development Cache Optimization (optional via URL ?dev=true)
|
| 15 |
+
dev_mode = st.query_params.get("dev", "false").lower() == "true"
|
| 16 |
+
if dev_mode:
|
| 17 |
+
st.sidebar.info("🛠️ Dev Mode: Reload active")
|
| 18 |
+
modules_to_reload = [
|
| 19 |
+
'src.autogluon_utils',
|
| 20 |
+
'src.flaml_utils',
|
| 21 |
+
'src.h2o_utils',
|
| 22 |
+
'src.tpot_utils',
|
| 23 |
+
'src.mlflow_cache'
|
| 24 |
+
]
|
| 25 |
+
for module in modules_to_reload:
|
| 26 |
+
if module in sys.modules:
|
| 27 |
+
importlib.reload(sys.modules[module])
|
| 28 |
|
| 29 |
+
# Functions with cache for Performance
|
| 30 |
+
@st.cache_data(show_spinner="Loading data...")
|
| 31 |
+
def cached_load_data(file_path_or_obj):
|
| 32 |
+
return load_data(file_path_or_obj)
|
| 33 |
+
|
| 34 |
+
@st.cache_data
|
| 35 |
+
def cached_get_data_summary(df):
|
| 36 |
+
return get_data_summary(df)
|
| 37 |
+
|
| 38 |
+
@st.cache_data(ttl=60) # 1 Minute Cache for file list
|
| 39 |
+
def cached_get_data_lake_files():
|
| 40 |
+
return get_data_lake_files()
|
| 41 |
|
| 42 |
from src.data_utils import load_data, get_data_summary, save_to_data_lake, init_dvc, get_data_lake_files, get_dvc_hash
|
| 43 |
from src.autogluon_utils import train_model as train_autogluon, load_model_from_mlflow as load_autogluon
|
|
|
|
| 68 |
st.title("🚀 AutoML Multi-Framework Interface")
|
| 69 |
|
| 70 |
# Sidebar navigation
|
| 71 |
+
st.sidebar.title("Navigation")
|
| 72 |
+
menu = st.sidebar.selectbox("Menu", ["Data Upload", "Training", "Prediction", "History (MLflow)"])
|
| 73 |
|
| 74 |
st.sidebar.markdown("---")
|
| 75 |
+
st.sidebar.header("🔗 DagsHub Integration (Optional)")
|
| 76 |
+
use_dagshub = st.sidebar.checkbox("Enable DagsHub")
|
| 77 |
|
| 78 |
if use_dagshub:
|
| 79 |
+
dagshub_user = st.sidebar.text_input("DagsHub Username")
|
| 80 |
+
dagshub_repo = st.sidebar.text_input("Repository Name")
|
| 81 |
+
dagshub_token = st.sidebar.text_input("Access Token (DagsHub)", type="password")
|
| 82 |
|
| 83 |
+
if st.sidebar.button("Connect to DagsHub"):
|
| 84 |
if dagshub_user and dagshub_repo and dagshub_token:
|
| 85 |
try:
|
| 86 |
import dagshub
|
|
|
|
| 88 |
os.environ["MLFLOW_TRACKING_USERNAME"] = dagshub_user
|
| 89 |
os.environ["MLFLOW_TRACKING_PASSWORD"] = dagshub_token
|
| 90 |
dagshub.init(repo_owner=dagshub_user, repo_name=dagshub_repo, mlflow=True)
|
| 91 |
+
st.sidebar.success("Successfully connected to DagsHub!")
|
| 92 |
except ImportError:
|
| 93 |
+
st.sidebar.error("dagshub library not found. Add 'dagshub' to requirements.txt and install it.")
|
| 94 |
except Exception as e:
|
| 95 |
+
st.sidebar.error(f"Connection error: {e}")
|
| 96 |
else:
|
| 97 |
+
st.sidebar.warning("Please fill all DagsHub fields.")
|
| 98 |
st.sidebar.markdown("---")
|
| 99 |
|
| 100 |
+
if menu == "Data Upload":
|
| 101 |
+
st.header("📂 Data Upload and Data Lake")
|
| 102 |
|
| 103 |
+
st.markdown("Upload new files to the Data Lake. They'll become available on the Training and Prediction tabs.")
|
| 104 |
+
uploaded_file = st.file_uploader("New CSV/Excel File", type=["csv", "xlsx", "xls"])
|
| 105 |
+
filename_prefix = st.text_input("Data Lake file prefix", value="dataset")
|
| 106 |
|
| 107 |
+
if st.button("Process and Save to Data Lake"):
|
| 108 |
if uploaded_file is not None:
|
| 109 |
try:
|
| 110 |
+
with st.spinner("Initializing Data Lake and processing data..."):
|
| 111 |
init_dvc()
|
| 112 |
+
df = cached_load_data(uploaded_file)
|
| 113 |
t_path, t_tag, t_hash = save_to_data_lake(df, filename_prefix)
|
| 114 |
+
st.cache_data.clear() # Clear cache because new data was injected
|
| 115 |
+
st.success(f"File loaded and versioned in the Data Lake with DVC! Generated Hash: {t_hash}")
|
| 116 |
|
| 117 |
+
st.subheader("Data Preview")
|
| 118 |
st.dataframe(df.head())
|
| 119 |
|
| 120 |
+
st.subheader("Data Summary")
|
| 121 |
+
summary = cached_get_data_summary(df)
|
| 122 |
s_col1, s_col2 = st.columns(2)
|
| 123 |
+
s_col1.metric("Rows", summary['rows'])
|
| 124 |
+
s_col2.metric("Columns", summary['columns'])
|
| 125 |
|
| 126 |
+
st.write("Data Types and Missing Values:")
|
| 127 |
summary_df = pd.DataFrame({
|
| 128 |
+
"Type": summary['dtypes'],
|
| 129 |
+
"Missing": summary['missing_values']
|
| 130 |
})
|
| 131 |
st.table(summary_df)
|
| 132 |
|
| 133 |
except Exception as e:
|
| 134 |
+
st.error(f"Error loading file: {e}")
|
| 135 |
else:
|
| 136 |
+
st.error("No file selected!")
|
| 137 |
|
| 138 |
+
elif menu == "Training":
|
| 139 |
+
st.header("🧠 Model Training")
|
| 140 |
|
| 141 |
+
available_files = cached_get_data_lake_files()
|
| 142 |
|
| 143 |
if not available_files:
|
| 144 |
+
st.warning("No datasets found in Data Lake. Please add them in the 'Data Upload' tab first.")
|
| 145 |
st.stop()
|
| 146 |
|
| 147 |
+
st.subheader("1. Data Lake Dataset Selection")
|
| 148 |
|
| 149 |
# UI mapping filenames
|
| 150 |
+
file_options = ["None"] + [os.path.basename(f) for f in available_files]
|
| 151 |
file_paths_map = {os.path.basename(f): f for f in available_files}
|
| 152 |
|
| 153 |
col1, col2, col3 = st.columns(3)
|
| 154 |
with col1:
|
| 155 |
+
train_file_selection = st.selectbox("Training (Required)", file_options[1:])
|
| 156 |
with col2:
|
| 157 |
+
valid_file_selection = st.selectbox("Validation (Optional)", file_options)
|
| 158 |
with col3:
|
| 159 |
+
test_file_selection = st.selectbox("Test/Holdout (Optional)", file_options)
|
| 160 |
|
| 161 |
if train_file_selection:
|
| 162 |
try:
|
| 163 |
# Load Train
|
| 164 |
train_path = file_paths_map[train_file_selection]
|
| 165 |
+
df = cached_load_data(train_path)
|
| 166 |
|
| 167 |
# Fetch Hash
|
| 168 |
t_hash_full, t_hash_short = get_dvc_hash(train_path)
|
|
|
|
| 170 |
|
| 171 |
# Load Valid
|
| 172 |
valid_df = None
|
| 173 |
+
if valid_file_selection != "None":
|
| 174 |
valid_path = file_paths_map[valid_file_selection]
|
| 175 |
+
valid_df = cached_load_data(valid_path)
|
| 176 |
v_hash_full, v_hash_short = get_dvc_hash(valid_path)
|
| 177 |
dvc_hashes["dvc_valid_hash"] = v_hash_short
|
| 178 |
|
| 179 |
# Load Test
|
| 180 |
test_df = None
|
| 181 |
+
if test_file_selection != "None":
|
| 182 |
test_path = file_paths_map[test_file_selection]
|
| 183 |
+
test_df = cached_load_data(test_path)
|
| 184 |
te_hash_full, te_hash_short = get_dvc_hash(test_path)
|
| 185 |
dvc_hashes["dvc_test_hash"] = te_hash_short
|
| 186 |
|
|
|
|
| 191 |
st.session_state['dvc_hashes'] = dvc_hashes
|
| 192 |
|
| 193 |
except Exception as e:
|
| 194 |
+
st.error(f"Error loading datasets from Data Lake: {e}")
|
| 195 |
|
| 196 |
st.markdown("---")
|
| 197 |
+
st.subheader("2. Data Splitting and Validation Strategy")
|
| 198 |
+
|
| 199 |
+
cv_folds = 0
|
| 200 |
+
if st.session_state['df'] is not None:
|
| 201 |
+
df = st.session_state['df']
|
| 202 |
+
valid_df_session = st.session_state.get('valid_df', None)
|
| 203 |
+
test_df_session = st.session_state.get('test_df', None)
|
| 204 |
+
|
| 205 |
+
col1, col2 = st.columns(2)
|
| 206 |
+
|
| 207 |
+
with col1:
|
| 208 |
+
st.markdown("**Final Test Set**")
|
| 209 |
+
if test_df_session is None:
|
| 210 |
+
test_size_pct = st.slider("Percentage extracted for Test (%)", 0, 50, 15, 5, help="Size of the test set retained for final model evaluation.")
|
| 211 |
+
else:
|
| 212 |
+
st.success("Test-set provided through a dedicated Data Lake file.")
|
| 213 |
+
test_size_pct = 0
|
| 214 |
+
|
| 215 |
+
with col2:
|
| 216 |
+
st.markdown("**Internal Validation Strategy**")
|
| 217 |
+
if valid_df_session is None:
|
| 218 |
+
val_strategy = st.radio("Method", ["Simple Holdout", "Cross-Validation"], horizontal=True, help="Holdout will physically split the Dataset. CV instructs engines to use Folds.")
|
| 219 |
+
if val_strategy == "Simple Holdout":
|
| 220 |
+
val_size_pct = st.slider("Percentage extracted for Validation (%)", 0, 50, 20, 5)
|
| 221 |
+
else:
|
| 222 |
+
cv_folds = st.slider("Number of Folds (K)", 2, 10, 5)
|
| 223 |
+
val_size_pct = 0
|
| 224 |
+
else:
|
| 225 |
+
st.success("Validation-set provided via file in Data Lake.")
|
| 226 |
+
val_size_pct = 0
|
| 227 |
+
|
| 228 |
+
# Apply Splits if needed and store on UI refresh safely
|
| 229 |
+
# We need a pristine copy or just track the original df length to not shrink infinitely on UI refreshes
|
| 230 |
+
# We'll use the current st.session_state['df'] as base, but this requires we cache original on selection.
|
| 231 |
+
if 'original_df' not in st.session_state or len(st.session_state['original_df']) != len(df) and ('has_split' not in st.session_state):
|
| 232 |
+
# Keep track of original selection payload
|
| 233 |
+
st.session_state['original_df'] = df.copy()
|
| 234 |
+
|
| 235 |
+
base_df = st.session_state['original_df'].copy()
|
| 236 |
+
|
| 237 |
+
if test_size_pct > 0:
|
| 238 |
+
base_df, fresh_test_df = train_test_split(base_df, test_size=(test_size_pct/100.0), random_state=42)
|
| 239 |
+
test_df_session = fresh_test_df
|
| 240 |
+
st.session_state['test_df'] = test_df_session
|
| 241 |
+
|
| 242 |
+
if val_size_pct > 0:
|
| 243 |
+
if len(base_df) > 100: # Safe margin
|
| 244 |
+
base_df, fresh_val_df = train_test_split(base_df, test_size=(val_size_pct/100.0), random_state=42)
|
| 245 |
+
valid_df_session = fresh_val_df
|
| 246 |
+
st.session_state['valid_df'] = valid_df_session
|
| 247 |
+
|
| 248 |
+
# Update current working df
|
| 249 |
+
df = base_df
|
| 250 |
+
st.session_state['active_df'] = df
|
| 251 |
+
st.session_state['cv_folds'] = cv_folds
|
| 252 |
+
|
| 253 |
+
st.markdown("---")
|
| 254 |
+
st.subheader("3. AutoML Configuration")
|
| 255 |
|
| 256 |
if st.session_state['df'] is not None:
|
| 257 |
df = st.session_state['df']
|
|
|
|
| 260 |
|
| 261 |
columns = df.columns.tolist()
|
| 262 |
|
| 263 |
+
framework = st.selectbox("Select AutoML Framework", ["AutoGluon", "FLAML", "H2O AutoML", "TPOT"])
|
| 264 |
+
target = st.selectbox("Select Target Column", columns)
|
| 265 |
+
run_name = st.text_input("Run Name", value=f"{framework.lower()}_run_{int(time.time())}")
|
| 266 |
|
| 267 |
# Datasets info
|
| 268 |
+
st.info(f"Active Datasets - Training: {len(df)} rows | Validation: {'N/A' if valid_df is None else str(len(valid_df)) + ' rows'} | Test: {'N/A' if test_df is None else str(len(test_df)) + ' rows'}")
|
| 269 |
|
| 270 |
# Framework specific options
|
| 271 |
+
st.subheader(f"{framework} Configurations")
|
| 272 |
|
| 273 |
+
# Common framework options
|
| 274 |
+
seed = st.number_input("Seed (reproducibility)", value=42, min_value=0, max_value=9999)
|
| 275 |
|
| 276 |
+
# Init vars
|
| 277 |
time_limit = time_budget = max_runtime_secs = 60
|
| 278 |
presets = task = metric = estimator_list = None
|
| 279 |
nfolds = balance_classes = sort_metric = exclude_algos = None
|
| 280 |
|
| 281 |
if framework == "AutoGluon":
|
| 282 |
+
time_limit = st.slider("Time limit (seconds)", 30, 3600, 60)
|
| 283 |
presets = st.selectbox("Presets", ['medium_quality', 'best_quality', 'high_quality', 'good_quality', 'optimize_for_deployment'])
|
| 284 |
elif framework == "FLAML":
|
| 285 |
+
time_budget = st.slider("Time budget (seconds)", 30, 3600, 60)
|
| 286 |
+
task = st.selectbox("Task", ['classification', 'regression', 'ts_forecast', 'rank'])
|
| 287 |
|
| 288 |
# Smart metric selection for FLAML
|
| 289 |
num_classes = df[target].nunique()
|
| 290 |
if task == 'classification':
|
| 291 |
if num_classes > 2:
|
| 292 |
+
st.warning(f"Multiclass problem detected ({num_classes} classes).")
|
| 293 |
metric_options = ['auto', 'accuracy', 'macro_f1', 'micro_f1', 'roc_auc_ovr', 'roc_auc_ovo', 'log_loss']
|
| 294 |
else:
|
| 295 |
metric_options = ['auto', 'accuracy', 'roc_auc', 'f1', 'log_loss']
|
|
|
|
| 298 |
else:
|
| 299 |
metric_options = ['auto']
|
| 300 |
|
| 301 |
+
metric = st.selectbox("Metric", metric_options)
|
| 302 |
+
estimators = st.multiselect("Estimators", ['lgbm', 'rf', 'catboost', 'xgboost', 'extra_tree', 'lrl1', 'lrl2'], default=['lgbm', 'rf'])
|
| 303 |
estimator_list = estimators if estimators else 'auto'
|
| 304 |
elif framework == "H2O AutoML":
|
| 305 |
+
st.warning("⚠️ H2O AutoML requires Java. If Java is not installed, use AutoGluon or FLAML.")
|
| 306 |
+
st.info("💡 To run H2O without Java installed locally, run via Docker.")
|
| 307 |
|
| 308 |
+
max_runtime_secs = st.slider("Max runtime (seconds)", 60, 3600, 300)
|
| 309 |
+
max_models = st.slider("Max number of models", 5, 50, 10)
|
| 310 |
+
if cv_folds == 0:
|
| 311 |
+
nfolds = st.slider("CV folds (H2O Native)", 2, 10, 3)
|
| 312 |
+
else:
|
| 313 |
+
nfolds = cv_folds
|
| 314 |
+
st.info(f"H2O native folds logic is overriden by the global CV configuration ({cv_folds} folds).")
|
|
|
|
| 315 |
|
| 316 |
+
balance_classes = st.checkbox("Balance classes", value=True)
|
| 317 |
+
|
| 318 |
+
exclude_options = ['DeepLearning', 'GLM', 'GBM', 'DRF', 'XGBoost', 'GLRM']
|
| 319 |
+
exclude_algos = st.multiselect("Exclude Algorithms", exclude_options, help="Algorithms to exclude from AutoML")
|
| 320 |
elif framework == "TPOT":
|
| 321 |
+
st.info("🧬 TPOT uses genetic algorithms to optimize machine learning pipelines.")
|
| 322 |
+
st.warning("⚠️ TPOT can be slower, but often finds highly optimal pipelines.")
|
| 323 |
|
| 324 |
+
generations = st.slider("Generations", 1, 20, 5, help="Number of generations for genetic evolution")
|
| 325 |
+
population_size = st.slider("Population Size", 10, 100, 20, help="Population size in each generation")
|
| 326 |
+
if cv_folds == 0:
|
| 327 |
+
cv = st.slider("Cross Validation Folds (TPOT)", 2, 10, 5)
|
| 328 |
+
else:
|
| 329 |
+
cv = cv_folds
|
| 330 |
+
st.info(f"TPOT CV folds override by global CV settings ({cv_folds} folds).")
|
| 331 |
+
max_time_mins = st.slider("Max time (minutes)", 5, 120, 30, help="Maximum training time in minutes")
|
| 332 |
+
max_eval_time_mins = st.slider("Max time per evaluation (minutes)", 1, 20, 5, help="Maximum time per pipeline evaluation")
|
| 333 |
+
verbosity = st.slider("Log verbosity level", 0, 3, 2, help="TPOT feedback verbosity")
|
| 334 |
+
n_jobs = st.slider("Parallel jobs", -1, 8, -1, help="Number of parallel processes (-1 to use all)")
|
| 335 |
|
| 336 |
+
# Advanced TPOT Options
|
| 337 |
+
with st.expander("⚙️ Advanced TPOT Options"):
|
| 338 |
+
config_dict = st.selectbox("TPOT Configuration", [
|
| 339 |
'TPOT light', 'TPOT MDR', 'TPOT sparse', 'TPOT NN'
|
| 340 |
+
], help="Predefined TPOT configuration for different types of problems")
|
| 341 |
|
| 342 |
+
tfidf_max_features = st.number_input("Text features max dimensions (TF-IDF)", min_value=100, max_value=10000, value=500, step=100)
|
| 343 |
+
ngram_max = st.slider("Max text N-Gram size", 1, 3, 2, help="If 2, evaluates unigrams and bigrams. If 3, unigrams, bigrams, and trigrams.")
|
| 344 |
tfidf_ngram_range = (1, ngram_max)
|
| 345 |
|
| 346 |
+
# Auto problem detection
|
| 347 |
problem_type = 'classification' if df[target].nunique() <= 20 or df[target].dtype == 'object' else 'regression'
|
| 348 |
+
st.info(f"🎯 Problem type detected: **{problem_type}**")
|
| 349 |
|
| 350 |
+
# Metrics based on problem type
|
| 351 |
if problem_type == 'classification':
|
| 352 |
scoring_options = ['accuracy', 'balanced_accuracy', 'f1_macro', 'f1_micro', 'f1_weighted', 'roc_auc_ovr', 'roc_auc_ovo', 'precision_macro', 'recall_macro']
|
| 353 |
else:
|
| 354 |
scoring_options = ['neg_mean_squared_error', 'neg_root_mean_squared_error', 'neg_mean_absolute_error', 'r2', 'explained_variance']
|
| 355 |
|
| 356 |
+
scoring = st.selectbox("Optimization Metric", scoring_options, help="Metric used to optimize the pipelines")
|
| 357 |
|
| 358 |
+
if st.button("Start Training"):
|
| 359 |
+
st.subheader("📺 Real-time Monitoring")
|
| 360 |
|
| 361 |
col_logs, col_chart = st.columns([1, 1])
|
| 362 |
|
| 363 |
with col_logs:
|
| 364 |
+
st.write("📋 Training Logs")
|
| 365 |
log_placeholder = st.empty()
|
| 366 |
|
| 367 |
with col_chart:
|
| 368 |
+
st.write("📈 Performance Evolution")
|
| 369 |
chart_placeholder = st.empty()
|
| 370 |
|
| 371 |
# Shared state for thread communication
|
|
|
|
| 410 |
with redirect_stdout(LogIO()), redirect_stderr(LogIO()):
|
| 411 |
try:
|
| 412 |
if framework == "AutoGluon":
|
| 413 |
+
res_predictor, res_run_id = train_autogluon(df, target, run_name, valid_df, test_df, time_limit, presets, seed, cv_folds)
|
| 414 |
result_queue.put({"predictor": res_predictor, "run_id": res_run_id, "type": "autogluon", "success": True})
|
| 415 |
elif framework == "FLAML":
|
| 416 |
+
res_automl, res_run_id = train_flaml_model(df, target, run_name, valid_df, test_df, time_budget, task, metric, estimator_list, seed, cv_folds)
|
| 417 |
result_queue.put({"predictor": res_automl, "run_id": res_run_id, "type": "flaml", "success": True})
|
| 418 |
elif framework == "H2O AutoML":
|
| 419 |
res_automl, res_run_id = train_h2o_model(
|
|
|
|
| 448 |
result_queue.put({"predictor": res_tpot, "pipeline": res_pipeline, "run_id": res_run_id, "info": res_info, "type": "tpot", "success": True})
|
| 449 |
except Exception as e:
|
| 450 |
import traceback
|
| 451 |
+
error_msg = f"CRITICAL TRAINING ERROR: {str(e)}\n{traceback.format_exc()}"
|
| 452 |
log_queue.put(error_msg)
|
| 453 |
result_queue.put({"success": False, "error": str(e)})
|
| 454 |
finally:
|
|
|
|
| 531 |
st.session_state['predictor'] = final_result["predictor"]
|
| 532 |
st.session_state['run_id'] = final_result["run_id"]
|
| 533 |
st.session_state['model_type'] = final_result["type"]
|
| 534 |
+
st.success(f"Training completed successfully! Run ID: {final_result['run_id']}")
|
| 535 |
|
| 536 |
# Log DVC hashes to MLflow run
|
| 537 |
if 'dvc_hashes' in st.session_state and st.session_state['dvc_hashes']:
|
| 538 |
try:
|
| 539 |
with mlflow.start_run(run_id=final_result["run_id"]):
|
| 540 |
mlflow.log_params(st.session_state['dvc_hashes'])
|
| 541 |
+
st.info("🧬 Data Lake (DVC) metadata successfully attached to Run!")
|
| 542 |
except Exception as e:
|
| 543 |
+
st.warning(f"Could not save DVC hashes to MLflow: {e}")
|
| 544 |
|
| 545 |
else:
|
| 546 |
+
st.error(f"Training failed: {final_result['error']}")
|
| 547 |
|
| 548 |
# Show all logs at the end
|
| 549 |
while not log_queue.empty():
|
| 550 |
all_logs.append(log_queue.get())
|
| 551 |
|
| 552 |
if all_logs:
|
| 553 |
+
with st.expander("View Full Training Logs"):
|
| 554 |
st.code("\n".join(all_logs))
|
| 555 |
|
| 556 |
# Post-training visualizations
|
|
|
|
| 558 |
if final_result['type'] == "flaml":
|
| 559 |
predictor = final_result['predictor']
|
| 560 |
|
| 561 |
+
st.subheader("🏆 Best Model (FLAML)")
|
| 562 |
col1, col2, col3 = st.columns(3)
|
| 563 |
+
col1.metric("Best Estimator", predictor.best_estimator)
|
| 564 |
+
col2.metric("Best Loss", f"{predictor.best_loss:.4f}")
|
| 565 |
+
col3.metric("Best Iteration", predictor.best_iteration)
|
| 566 |
|
| 567 |
+
with st.expander("⚙️ Best Configuration (Hyperparameters)"):
|
| 568 |
st.json(predictor.best_config)
|
| 569 |
|
| 570 |
if hasattr(predictor, 'best_config_per_estimator') and predictor.best_config_per_estimator:
|
| 571 |
+
with st.expander("📊 Best Configurations per Estimator"):
|
| 572 |
st.json(predictor.best_config_per_estimator)
|
| 573 |
|
| 574 |
if hasattr(predictor, 'feature_importances_') and predictor.feature_importances_ is not None:
|
|
|
|
| 586 |
plt.title("Top 10 Feature Importances (FLAML)")
|
| 587 |
st.pyplot(fig)
|
| 588 |
else:
|
| 589 |
+
st.info(f"Feature importance available, but columns mismatch ({len(importances)} vs {len(feature_names)}).")
|
| 590 |
except Exception as feat_err:
|
| 591 |
+
st.warning(f"Error generating importance chart: {feat_err}")
|
|
|
|
| 592 |
elif final_result['type'] == "autogluon":
|
| 593 |
predictor = final_result['predictor']
|
| 594 |
+
st.subheader("🏆 AutoGluon Results")
|
| 595 |
|
| 596 |
+
st.subheader("Final Leaderboard")
|
| 597 |
leaderboard = predictor.leaderboard(silent=True)
|
| 598 |
st.dataframe(leaderboard)
|
| 599 |
|
| 600 |
best_model = leaderboard.iloc[0]['model'] if not leaderboard.empty else "Modelo principal"
|
| 601 |
+
st.success(f"Best model found: **{best_model}**")
|
| 602 |
|
| 603 |
+
with st.expander("⚙️ Training Details (AutoGluon Info)"):
|
| 604 |
try:
|
| 605 |
info = predictor.info()
|
| 606 |
st.json(info)
|
| 607 |
except:
|
| 608 |
+
st.write("Detailed info not available for this model.")
|
| 609 |
|
| 610 |
+
if st.checkbox("Generate Feature Importance (AutoGluon)"):
|
| 611 |
+
with st.spinner("Calculating importance (this may take a while)..."):
|
| 612 |
try:
|
| 613 |
fi = predictor.feature_importance(df)
|
| 614 |
st.dataframe(fi)
|
|
|
|
| 617 |
plt.title("Feature Importance (AutoGluon)")
|
| 618 |
st.pyplot(fig)
|
| 619 |
except Exception as e:
|
| 620 |
+
st.error(f"Error calculating importance: {e}")
|
| 621 |
|
| 622 |
elif final_result['type'] == "h2o":
|
| 623 |
automl = final_result['predictor']
|
| 624 |
+
st.subheader("🏆 H2O AutoML Results")
|
| 625 |
|
| 626 |
+
# Verify if H2O is still connected before accessing the model
|
| 627 |
try:
|
| 628 |
best_model = automl.leader
|
| 629 |
if best_model is not None:
|
| 630 |
+
st.success(f"Best model found: **{best_model.model_id}**")
|
| 631 |
|
| 632 |
+
st.subheader("Final Leaderboard")
|
| 633 |
try:
|
| 634 |
leaderboard = automl.leaderboard.as_data_frame()
|
| 635 |
st.dataframe(leaderboard)
|
| 636 |
except Exception as e:
|
| 637 |
+
st.warning(f"Could not display leaderboard: {e}")
|
| 638 |
+
# Fallback to textual representation
|
| 639 |
try:
|
| 640 |
st.text(str(automl.leaderboard.head(10)))
|
| 641 |
except:
|
| 642 |
+
st.info("Leaderboard unavailable (H2O connection closed)")
|
| 643 |
|
| 644 |
+
with st.expander("⚙️ Best Model Details (H2O)"):
|
| 645 |
try:
|
| 646 |
model_params = {
|
| 647 |
"model_id": best_model.model_id,
|
|
|
|
| 650 |
}
|
| 651 |
st.json(model_params)
|
| 652 |
except Exception as e:
|
| 653 |
+
st.warning(f"Could not retrieve model details: {e}")
|
| 654 |
else:
|
| 655 |
+
st.warning("⚠️ No models were trained during this execution.")
|
| 656 |
+
st.info("This might happen when:")
|
| 657 |
+
st.info("• The max runtime is severely constrained for the dataset size")
|
| 658 |
+
st.info("• The data format was rejected by the active algorithms")
|
| 659 |
+
st.info("• Bad algorithm exclusion constraints")
|
| 660 |
|
| 661 |
+
# Try showing fallback info
|
| 662 |
try:
|
| 663 |
+
st.subheader("📊 Training Information")
|
| 664 |
+
st.info(f"• Type: H2O AutoML")
|
| 665 |
st.info(f"• Run ID: {final_result['run_id']}")
|
| 666 |
+
st.info(f"• Status: Completed, but without trained models")
|
| 667 |
+
st.info(f"• Recommendation: Increase maximum runtime or decrease data constraints")
|
|
|
|
| 668 |
except:
|
| 669 |
pass
|
| 670 |
except Exception as e:
|
| 671 |
+
st.error(f"⚠️ Could not access H2O model details: {e}")
|
| 672 |
+
st.info("This commonly happens when the H2O local cluster terminates after training. Check MLflow UI for saved metrics!")
|
| 673 |
|
| 674 |
+
# Fallback training info
|
| 675 |
try:
|
| 676 |
+
st.info(f"📊 **Training Information:**")
|
| 677 |
+
st.info(f"• Type: H2O AutoML")
|
| 678 |
st.info(f"• Run ID: {final_result['run_id']}")
|
| 679 |
+
st.info(f"• Status: Completed successfully")
|
| 680 |
+
st.info(f"• Metrics properly recorded in MLflow")
|
| 681 |
except:
|
| 682 |
pass
|
| 683 |
|
|
|
|
| 686 |
pipeline = final_result['pipeline']
|
| 687 |
info = final_result['info']
|
| 688 |
|
| 689 |
+
st.subheader("🧬 TPOT AutoML Results")
|
| 690 |
|
| 691 |
+
# General information
|
| 692 |
col1, col2, col3, col4 = st.columns(4)
|
| 693 |
+
col1.metric("Problem Type", info['problem_type'].title())
|
| 694 |
+
col2.metric("Generations", info['generations'])
|
| 695 |
+
col3.metric("Population", info['population_size'])
|
| 696 |
col4.metric("Features", info['n_features'])
|
| 697 |
|
| 698 |
+
# Metrics
|
| 699 |
if info['problem_type'] == 'classification':
|
| 700 |
col1, col2, col3 = st.columns(3)
|
| 701 |
col1.metric("Accuracy", f"{info.get('accuracy', 0):.4f}")
|
|
|
|
| 707 |
col2.metric("R²", f"{info.get('r2', 0):.4f}")
|
| 708 |
col3.metric("MSE", f"{info.get('mse', 0):.4f}")
|
| 709 |
|
| 710 |
+
# Optimized pipeline
|
| 711 |
+
with st.expander("🧬 Optimized Pipeline"):
|
| 712 |
st.code(str(tpot.fitted_pipeline_), language="python")
|
| 713 |
|
| 714 |
+
# Detailed information
|
| 715 |
+
with st.expander("📊 Detailed Information"):
|
| 716 |
st.json(info)
|
| 717 |
|
| 718 |
+
# Training time
|
| 719 |
+
st.info(f"⏱️ **Training Duration:** {info['training_duration']:.2f} seconds")
|
| 720 |
+
st.info(f"🎯 **Optimization Metric:** {info['scoring']}")
|
| 721 |
|
| 722 |
except Exception as e:
|
| 723 |
import traceback
|
| 724 |
error_details = traceback.format_exc()
|
| 725 |
+
st.error(f"Error during training: {e}")
|
| 726 |
+
with st.expander("View error details (Traceback)"):
|
| 727 |
st.code(error_details)
|
| 728 |
finally:
|
| 729 |
pass
|
| 730 |
else:
|
| 731 |
+
st.warning("Please upload or select Data Lake training sets first.")
|
| 732 |
|
| 733 |
+
elif menu == "Prediction":
|
| 734 |
+
st.header("🔮 Prediction")
|
| 735 |
|
| 736 |
+
load_option = st.radio("Choose the model source", ["Current session model", "Load from MLflow runs"])
|
| 737 |
|
| 738 |
+
if load_option == "Load from MLflow runs":
|
| 739 |
col1, col2 = st.columns(2)
|
| 740 |
+
m_type = col1.selectbox("Model Framework", ["AutoGluon", "FLAML", "H2O AutoML", "TPOT"])
|
| 741 |
run_id_input = col2.text_input("Run ID")
|
| 742 |
|
| 743 |
+
if st.button("Load Model"):
|
| 744 |
try:
|
| 745 |
if m_type == "AutoGluon":
|
| 746 |
st.session_state['predictor'] = load_autogluon(run_id_input)
|
|
|
|
| 754 |
elif m_type == "TPOT":
|
| 755 |
st.session_state['predictor'] = load_tpot_model(run_id_input)
|
| 756 |
st.session_state['model_type'] = "tpot"
|
| 757 |
+
st.success("Model loaded successfully!")
|
| 758 |
except Exception as e:
|
| 759 |
+
st.error(f"Loading error: {e}")
|
| 760 |
|
| 761 |
if st.session_state['predictor'] is not None:
|
| 762 |
predictor = st.session_state['predictor']
|
| 763 |
m_type = st.session_state['model_type']
|
| 764 |
|
| 765 |
+
st.info(f"Active model: {m_type}")
|
| 766 |
|
| 767 |
+
predict_file = st.file_uploader("Upload prediction dataset", type=["csv", "xlsx", "xls"])
|
| 768 |
|
| 769 |
if predict_file is not None:
|
| 770 |
predict_df = load_data(predict_file)
|
| 771 |
st.dataframe(predict_df.head())
|
| 772 |
|
| 773 |
+
if st.button("Execute Prediction"):
|
| 774 |
try:
|
| 775 |
+
# Validate predictor payload
|
| 776 |
if predictor is None:
|
| 777 |
+
st.error("No model is loaded. Please train or load a model first.")
|
| 778 |
st.stop()
|
| 779 |
|
| 780 |
if m_type == "autogluon":
|
|
|
|
| 788 |
result_df = predict_df.copy()
|
| 789 |
result_df['Predictions'] = predictions
|
| 790 |
|
| 791 |
+
st.success("Predictions concluded!")
|
| 792 |
st.dataframe(result_df)
|
| 793 |
|
| 794 |
csv = result_df.to_csv(index=False).encode('utf-8')
|
| 795 |
+
st.download_button("Download predictions CSV", csv, "predictions.csv", "text/csv")
|
| 796 |
except Exception as e:
|
| 797 |
+
st.error(f"Prediction error: {e}")
|
| 798 |
|
| 799 |
+
elif menu == "History (MLflow)":
|
| 800 |
+
st.header("📊 Experiments History")
|
| 801 |
|
| 802 |
# Button to clean corrupted MLflow metadata
|
| 803 |
+
if st.sidebar.button("Hard Reset MLflow (Repair MLRuns tracking)"):
|
| 804 |
import shutil
|
| 805 |
if os.path.exists("mlruns"):
|
| 806 |
# Instead of deleting everything, we could try to find the malformed ones
|
| 807 |
# but deleting is safer for a local "repair"
|
| 808 |
shutil.rmtree("mlruns")
|
| 809 |
+
st.sidebar.success("Cache cleared! Please restart your training processes.")
|
| 810 |
st.rerun()
|
| 811 |
|
| 812 |
+
# Soft cache clear
|
| 813 |
+
if st.sidebar.button("Clear Python MLflow Cache"):
|
| 814 |
mlflow_cache.clear_cache()
|
| 815 |
+
st.sidebar.success("Cache cleared!")
|
| 816 |
st.rerun()
|
| 817 |
|
| 818 |
+
# Cached experiment list
|
| 819 |
experiment_list = get_cached_experiment_list()
|
| 820 |
+
exp_name = st.selectbox("Select Experiment Node", experiment_list)
|
| 821 |
|
| 822 |
try:
|
| 823 |
+
# Request cached runs
|
| 824 |
runs = mlflow_cache.get_cached_all_runs(exp_name)
|
| 825 |
|
| 826 |
if not runs.empty:
|
| 827 |
st.dataframe(runs)
|
| 828 |
|
| 829 |
+
# Cache statistics insight
|
| 830 |
+
with st.expander("📊 Cache Statistics"):
|
| 831 |
+
st.write(f"Experiment: {exp_name}")
|
| 832 |
+
st.write(f"Total runs: {len(runs)}")
|
| 833 |
+
st.write(f"Cache TTL cycle: 5 minutes")
|
| 834 |
else:
|
| 835 |
+
st.write("No recorded runs found for this experiment tracking node.")
|
| 836 |
except Exception as e:
|
| 837 |
+
st.error(f"Error reading MLflow cache: {e}")
|
| 838 |
+
st.warning("This is commonly caused by corrupted trailing database traces or manually deleted runs folders. Use the Hard Reset button to fix locally.")
|
src/autogluon_utils.py
CHANGED
|
@@ -9,7 +9,7 @@ logger = logging.getLogger(__name__)
|
|
| 9 |
|
| 10 |
def train_model(train_data: pd.DataFrame, target: str, run_name: str,
|
| 11 |
valid_data: pd.DataFrame = None, test_data: pd.DataFrame = None,
|
| 12 |
-
time_limit: int = 60, presets: str = 'medium_quality', seed: int = 42):
|
| 13 |
"""
|
| 14 |
Trains an AutoGluon model and logs results to MLflow using generic artifact logging.
|
| 15 |
"""
|
|
@@ -35,12 +35,12 @@ def train_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 35 |
# Clean validation and test formats if present
|
| 36 |
if valid_data is not None:
|
| 37 |
if target not in valid_data.columns:
|
| 38 |
-
raise ValueError(f"
|
| 39 |
valid_data = valid_data.dropna(subset=[target])
|
| 40 |
mlflow.log_param("has_validation_data", True)
|
| 41 |
if test_data is not None:
|
| 42 |
if target not in test_data.columns:
|
| 43 |
-
raise ValueError(f"
|
| 44 |
test_data = test_data.dropna(subset=[target])
|
| 45 |
mlflow.log_param("has_test_data", True)
|
| 46 |
|
|
@@ -50,14 +50,17 @@ def train_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 50 |
"time_limit": time_limit,
|
| 51 |
"presets": presets
|
| 52 |
}
|
| 53 |
-
if
|
|
|
|
|
|
|
|
|
|
| 54 |
fit_args["tuning_data"] = valid_data
|
| 55 |
|
| 56 |
predictor = TabularPredictor(label=target, path=model_path).fit(**fit_args)
|
| 57 |
|
| 58 |
# Log metrics (leaderboard)
|
| 59 |
-
#
|
| 60 |
-
#
|
| 61 |
eval_data = test_data if test_data is not None else (valid_data if valid_data is not None else train_data)
|
| 62 |
leaderboard = predictor.leaderboard(eval_data, silent=True)
|
| 63 |
# Log the best model's score
|
|
|
|
| 9 |
|
| 10 |
def train_model(train_data: pd.DataFrame, target: str, run_name: str,
|
| 11 |
valid_data: pd.DataFrame = None, test_data: pd.DataFrame = None,
|
| 12 |
+
time_limit: int = 60, presets: str = 'medium_quality', seed: int = 42, cv_folds: int = 0):
|
| 13 |
"""
|
| 14 |
Trains an AutoGluon model and logs results to MLflow using generic artifact logging.
|
| 15 |
"""
|
|
|
|
| 35 |
# Clean validation and test formats if present
|
| 36 |
if valid_data is not None:
|
| 37 |
if target not in valid_data.columns:
|
| 38 |
+
raise ValueError(f"Target column '{target}' not found in Validation data. Make sure it has the same structure as the training dataset.")
|
| 39 |
valid_data = valid_data.dropna(subset=[target])
|
| 40 |
mlflow.log_param("has_validation_data", True)
|
| 41 |
if test_data is not None:
|
| 42 |
if target not in test_data.columns:
|
| 43 |
+
raise ValueError(f"Target column '{target}' not found in Test data. Make sure the test set includes the target variable.")
|
| 44 |
test_data = test_data.dropna(subset=[target])
|
| 45 |
mlflow.log_param("has_test_data", True)
|
| 46 |
|
|
|
|
| 50 |
"time_limit": time_limit,
|
| 51 |
"presets": presets
|
| 52 |
}
|
| 53 |
+
if cv_folds > 0:
|
| 54 |
+
fit_args["num_bag_folds"] = cv_folds
|
| 55 |
+
|
| 56 |
+
if valid_data is not None and cv_folds == 0:
|
| 57 |
fit_args["tuning_data"] = valid_data
|
| 58 |
|
| 59 |
predictor = TabularPredictor(label=target, path=model_path).fit(**fit_args)
|
| 60 |
|
| 61 |
# Log metrics (leaderboard)
|
| 62 |
+
# If test_data is provided, leaderboard and scoring will strictly use it,
|
| 63 |
+
# otherwise fallback to training data
|
| 64 |
eval_data = test_data if test_data is not None else (valid_data if valid_data is not None else train_data)
|
| 65 |
leaderboard = predictor.leaderboard(eval_data, silent=True)
|
| 66 |
# Log the best model's score
|
src/data_utils.py
CHANGED
|
@@ -16,7 +16,7 @@ def load_data(file):
|
|
| 16 |
elif filename.endswith(('.xls', '.xlsx')):
|
| 17 |
return pd.read_excel(file)
|
| 18 |
else:
|
| 19 |
-
raise ValueError("
|
| 20 |
|
| 21 |
def get_data_summary(df):
|
| 22 |
"""
|
|
|
|
| 16 |
elif filename.endswith(('.xls', '.xlsx')):
|
| 17 |
return pd.read_excel(file)
|
| 18 |
else:
|
| 19 |
+
raise ValueError("Unsupported file format. Please use CSV or Excel.")
|
| 20 |
|
| 21 |
def get_data_summary(df):
|
| 22 |
"""
|
src/flaml_utils.py
CHANGED
|
@@ -12,12 +12,12 @@ logger = logging.getLogger(__name__)
|
|
| 12 |
|
| 13 |
def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
| 14 |
valid_data: pd.DataFrame = None, test_data: pd.DataFrame = None,
|
| 15 |
-
time_budget: int = 60, task: str = 'classification', metric: str = 'auto', estimator_list: list = 'auto', seed: int = 42):
|
| 16 |
"""
|
| 17 |
Trains a FLAML model and logs results to MLflow.
|
| 18 |
"""
|
| 19 |
safe_set_experiment("FLAML_Experiments")
|
| 20 |
-
logging.info(f"
|
| 21 |
|
| 22 |
# Ensure flaml logger is also at INFO level
|
| 23 |
import flaml
|
|
@@ -28,7 +28,7 @@ def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 28 |
with mlflow.start_run(run_name=run_name) as run:
|
| 29 |
# Data cleaning: drop rows where target is NaN
|
| 30 |
train_data = train_data.dropna(subset=[target])
|
| 31 |
-
logging.info(f"
|
| 32 |
|
| 33 |
# Log parameters
|
| 34 |
mlflow.log_param("target", target)
|
|
@@ -44,7 +44,7 @@ def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 44 |
X_val, y_val = None, None
|
| 45 |
if valid_data is not None:
|
| 46 |
if target not in valid_data.columns:
|
| 47 |
-
raise ValueError(f"
|
| 48 |
valid_data = valid_data.dropna(subset=[target])
|
| 49 |
X_val = valid_data.drop(columns=[target])
|
| 50 |
y_val = valid_data[target]
|
|
@@ -52,7 +52,7 @@ def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 52 |
|
| 53 |
if test_data is not None:
|
| 54 |
if target not in test_data.columns:
|
| 55 |
-
raise ValueError(f"
|
| 56 |
mlflow.log_param("has_test_data", True)
|
| 57 |
|
| 58 |
automl = AutoML()
|
|
@@ -69,27 +69,31 @@ def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 69 |
"log_file_name": "flaml.log",
|
| 70 |
"seed": seed,
|
| 71 |
"n_jobs": 1,
|
| 72 |
-
"verbose": 0, #
|
| 73 |
}
|
| 74 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 75 |
if X_val is not None:
|
| 76 |
settings["X_val"] = X_val
|
| 77 |
settings["y_val"] = y_val
|
| 78 |
|
| 79 |
# Train model
|
| 80 |
-
logging.info("
|
| 81 |
try:
|
| 82 |
automl.fit(X_train=X_train, y_train=y_train, **settings)
|
| 83 |
-
logging.info("
|
| 84 |
except StopIteration:
|
| 85 |
-
logging.info("
|
| 86 |
if not hasattr(automl, 'best_estimator') or automl.best_estimator is None:
|
| 87 |
-
raise RuntimeError("FLAML
|
| 88 |
|
| 89 |
# Log metrics
|
| 90 |
if hasattr(automl, 'best_loss'):
|
| 91 |
mlflow.log_metric("best_loss", automl.best_loss)
|
| 92 |
-
logging.info(f"
|
| 93 |
|
| 94 |
# Save best model
|
| 95 |
model_path = os.path.join("models", f"flaml_{run_name}.pkl")
|
|
@@ -118,4 +122,4 @@ def load_flaml_model(run_id: str):
|
|
| 118 |
if file.endswith(".pkl"):
|
| 119 |
with open(os.path.join(root, file), "rb") as f:
|
| 120 |
return pickle.load(f)
|
| 121 |
-
raise FileNotFoundError("
|
|
|
|
| 12 |
|
| 13 |
def train_flaml_model(train_data: pd.DataFrame, target: str, run_name: str,
|
| 14 |
valid_data: pd.DataFrame = None, test_data: pd.DataFrame = None,
|
| 15 |
+
time_budget: int = 60, task: str = 'classification', metric: str = 'auto', estimator_list: list = 'auto', seed: int = 42, cv_folds: int = 0):
|
| 16 |
"""
|
| 17 |
Trains a FLAML model and logs results to MLflow.
|
| 18 |
"""
|
| 19 |
safe_set_experiment("FLAML_Experiments")
|
| 20 |
+
logging.info(f"Starting FLAML training for run: {run_name}")
|
| 21 |
|
| 22 |
# Ensure flaml logger is also at INFO level
|
| 23 |
import flaml
|
|
|
|
| 28 |
with mlflow.start_run(run_name=run_name) as run:
|
| 29 |
# Data cleaning: drop rows where target is NaN
|
| 30 |
train_data = train_data.dropna(subset=[target])
|
| 31 |
+
logging.info(f"Data ready: {len(train_data)} rows.")
|
| 32 |
|
| 33 |
# Log parameters
|
| 34 |
mlflow.log_param("target", target)
|
|
|
|
| 44 |
X_val, y_val = None, None
|
| 45 |
if valid_data is not None:
|
| 46 |
if target not in valid_data.columns:
|
| 47 |
+
raise ValueError(f"Target column '{target}' not found in Validation data.")
|
| 48 |
valid_data = valid_data.dropna(subset=[target])
|
| 49 |
X_val = valid_data.drop(columns=[target])
|
| 50 |
y_val = valid_data[target]
|
|
|
|
| 52 |
|
| 53 |
if test_data is not None:
|
| 54 |
if target not in test_data.columns:
|
| 55 |
+
raise ValueError(f"Target column '{target}' not found in Test data.")
|
| 56 |
mlflow.log_param("has_test_data", True)
|
| 57 |
|
| 58 |
automl = AutoML()
|
|
|
|
| 69 |
"log_file_name": "flaml.log",
|
| 70 |
"seed": seed,
|
| 71 |
"n_jobs": 1,
|
| 72 |
+
"verbose": 0, # Reduce internal verbosity to avoid pollution, progress goes to flaml.log
|
| 73 |
}
|
| 74 |
|
| 75 |
+
if cv_folds > 0:
|
| 76 |
+
settings["eval_method"] = "cv"
|
| 77 |
+
settings["n_splits"] = cv_folds
|
| 78 |
+
|
| 79 |
if X_val is not None:
|
| 80 |
settings["X_val"] = X_val
|
| 81 |
settings["y_val"] = y_val
|
| 82 |
|
| 83 |
# Train model
|
| 84 |
+
logging.info("Executing hyperparameter search (automl.fit)...")
|
| 85 |
try:
|
| 86 |
automl.fit(X_train=X_train, y_train=y_train, **settings)
|
| 87 |
+
logging.info("Search finished successfully.")
|
| 88 |
except StopIteration:
|
| 89 |
+
logging.info("Search interrupted (time limit reached).")
|
| 90 |
if not hasattr(automl, 'best_estimator') or automl.best_estimator is None:
|
| 91 |
+
raise RuntimeError("FLAML stopped without finding a valid model.")
|
| 92 |
|
| 93 |
# Log metrics
|
| 94 |
if hasattr(automl, 'best_loss'):
|
| 95 |
mlflow.log_metric("best_loss", automl.best_loss)
|
| 96 |
+
logging.info(f"Best final Loss: {automl.best_loss:.4f}")
|
| 97 |
|
| 98 |
# Save best model
|
| 99 |
model_path = os.path.join("models", f"flaml_{run_name}.pkl")
|
|
|
|
| 122 |
if file.endswith(".pkl"):
|
| 123 |
with open(os.path.join(root, file), "rb") as f:
|
| 124 |
return pickle.load(f)
|
| 125 |
+
raise FileNotFoundError("FLAML model not found in artifacts.")
|
src/h2o_utils.py
CHANGED
|
@@ -11,18 +11,18 @@ from src.mlflow_utils import safe_set_experiment
|
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
def check_java_availability():
|
| 14 |
-
"""
|
| 15 |
try:
|
| 16 |
import subprocess
|
| 17 |
import os
|
| 18 |
|
| 19 |
-
#
|
| 20 |
result = subprocess.run(['java', '-version'],
|
| 21 |
capture_output=True, text=True, timeout=5)
|
| 22 |
if result.returncode == 0:
|
| 23 |
return True
|
| 24 |
|
| 25 |
-
#
|
| 26 |
java_paths = [
|
| 27 |
r"C:\Program Files\Eclipse Adoptium\jdk-11.0.30.7-hotspot\bin\java.exe",
|
| 28 |
r"C:\Program Files\Eclipse Adoptium\jdk-11.0.23.9-hotspot\bin\java.exe",
|
|
@@ -43,65 +43,65 @@ def check_java_availability():
|
|
| 43 |
return False
|
| 44 |
|
| 45 |
def initialize_h2o():
|
| 46 |
-
"""
|
| 47 |
if not check_java_availability():
|
| 48 |
raise RuntimeError(
|
| 49 |
-
"Java
|
| 50 |
-
"
|
| 51 |
-
"1.
|
| 52 |
-
"2.
|
| 53 |
-
"3.
|
| 54 |
-
"\
|
| 55 |
-
"-
|
| 56 |
-
"-
|
| 57 |
)
|
| 58 |
|
| 59 |
try:
|
| 60 |
import h2o
|
| 61 |
h2o.init(max_mem_size="4G", nthreads=-1)
|
| 62 |
-
logger.info("
|
| 63 |
return h2o
|
| 64 |
except Exception as e:
|
| 65 |
-
logger.error(f"
|
| 66 |
raise
|
| 67 |
|
| 68 |
def cleanup_h2o():
|
| 69 |
-
"""
|
| 70 |
try:
|
| 71 |
import h2o
|
| 72 |
h2o.cluster().shutdown()
|
| 73 |
-
logger.info("
|
| 74 |
except Exception as e:
|
| 75 |
-
logger.warning(f"
|
| 76 |
|
| 77 |
def prepare_data_for_h2o(train_data: pd.DataFrame, target: str):
|
| 78 |
-
"""
|
| 79 |
import h2o
|
| 80 |
|
| 81 |
-
#
|
| 82 |
train_data_clean = train_data.dropna(subset=[target])
|
| 83 |
|
| 84 |
-
#
|
| 85 |
if train_data_clean.select_dtypes(include=['object']).shape[1] > 0:
|
| 86 |
-
logger.info("
|
| 87 |
|
| 88 |
-
#
|
| 89 |
for col in train_data_clean.select_dtypes(include=['object']).columns:
|
| 90 |
if col != target:
|
| 91 |
-
#
|
| 92 |
train_data_clean[f'{col}_length'] = train_data_clean[col].astype(str).str.len()
|
| 93 |
-
#
|
| 94 |
train_data_clean[f'{col}_word_count'] = train_data_clean[col].astype(str).str.split().str.len()
|
| 95 |
|
| 96 |
-
#
|
| 97 |
text_cols = train_data_clean.select_dtypes(include=['object']).columns
|
| 98 |
text_cols = [col for col in text_cols if col != target]
|
| 99 |
train_data_clean = train_data_clean.drop(columns=text_cols)
|
| 100 |
|
| 101 |
-
#
|
| 102 |
h2o_frame = h2o.H2OFrame(train_data_clean)
|
| 103 |
|
| 104 |
-
#
|
| 105 |
if train_data_clean[target].dtype == 'object' or train_data_clean[target].nunique() < 20:
|
| 106 |
h2o_frame[target] = h2o_frame[target].asfactor()
|
| 107 |
|
|
@@ -113,23 +113,23 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 113 |
nfolds: int = 3, balance_classes: bool = True, seed: int = 42,
|
| 114 |
sort_metric: str = "AUTO", exclude_algos: list = None):
|
| 115 |
"""
|
| 116 |
-
|
| 117 |
"""
|
| 118 |
import h2o
|
| 119 |
from h2o.automl import H2OAutoML
|
| 120 |
|
| 121 |
safe_set_experiment("H2O_Experiments")
|
| 122 |
-
logging.info(f"
|
| 123 |
|
| 124 |
-
#
|
| 125 |
h2o_instance = initialize_h2o()
|
| 126 |
|
| 127 |
try:
|
| 128 |
with mlflow.start_run(run_name=run_name) as run:
|
| 129 |
-
#
|
| 130 |
h2o_frame, clean_data = prepare_data_for_h2o(train_data, target)
|
| 131 |
|
| 132 |
-
# Log
|
| 133 |
mlflow.log_param("target", target)
|
| 134 |
mlflow.log_param("max_runtime_secs", max_runtime_secs)
|
| 135 |
mlflow.log_param("max_models", max_models)
|
|
@@ -141,7 +141,7 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 141 |
if exclude_algos:
|
| 142 |
mlflow.log_param("exclude_algos", exclude_algos)
|
| 143 |
|
| 144 |
-
#
|
| 145 |
features = [col for col in clean_data.columns if col != target]
|
| 146 |
mlflow.log_param("features", features)
|
| 147 |
|
|
@@ -159,11 +159,11 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 159 |
exclude_algos=exclude_algos or []
|
| 160 |
)
|
| 161 |
|
| 162 |
-
#
|
| 163 |
h2o_valid = None
|
| 164 |
if valid_data is not None:
|
| 165 |
if target not in valid_data.columns:
|
| 166 |
-
raise ValueError(f"
|
| 167 |
valid_data = valid_data.dropna(subset=[target])
|
| 168 |
h2o_valid, _ = prepare_data_for_h2o(valid_data, target)
|
| 169 |
mlflow.log_param("has_validation_data", True)
|
|
@@ -171,13 +171,13 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 171 |
h2o_test = None
|
| 172 |
if test_data is not None:
|
| 173 |
if target not in test_data.columns:
|
| 174 |
-
raise ValueError(f"
|
| 175 |
test_data = test_data.dropna(subset=[target])
|
| 176 |
h2o_test, _ = prepare_data_for_h2o(test_data, target)
|
| 177 |
mlflow.log_param("has_test_data", True)
|
| 178 |
|
| 179 |
-
#
|
| 180 |
-
logger.info("
|
| 181 |
start_time = time.time()
|
| 182 |
train_kwargs = {"x": features, "y": target, "training_frame": h2o_frame}
|
| 183 |
if h2o_valid is not None:
|
|
@@ -188,87 +188,87 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 188 |
aml.train(**train_kwargs)
|
| 189 |
training_duration = time.time() - start_time
|
| 190 |
|
| 191 |
-
logger.info(f"
|
| 192 |
|
| 193 |
-
#
|
| 194 |
leaderboard = aml.leaderboard
|
| 195 |
|
| 196 |
-
#
|
| 197 |
if leaderboard.nrow == 0:
|
| 198 |
-
logger.warning("⚠️
|
| 199 |
-
logger.warning("
|
| 200 |
-
logger.warning("1.
|
| 201 |
-
logger.warning("2.
|
| 202 |
-
logger.warning("3.
|
| 203 |
|
| 204 |
-
#
|
| 205 |
mlflow.log_metric("total_models_trained", 0)
|
| 206 |
mlflow.log_metric("training_duration", training_duration)
|
| 207 |
mlflow.log_metric("best_model_score", 0.0)
|
| 208 |
|
| 209 |
-
#
|
| 210 |
return aml, run.info.run_id
|
| 211 |
|
| 212 |
-
logger.info("\nTop 5
|
| 213 |
print(leaderboard.head(5))
|
| 214 |
|
| 215 |
-
#
|
| 216 |
try:
|
| 217 |
-
#
|
| 218 |
leaderboard_df = None
|
| 219 |
try:
|
| 220 |
leaderboard_df = leaderboard.as_data_frame()
|
| 221 |
-
logger.info(f"
|
| 222 |
except Exception as e:
|
| 223 |
-
logger.warning(f"
|
| 224 |
|
| 225 |
-
#
|
| 226 |
best_model_score = 0.0
|
| 227 |
if leaderboard_df is not None and len(leaderboard_df) > 0:
|
| 228 |
-
#
|
| 229 |
for metric in ['auc', 'logloss', 'rmse', 'mae', 'r2']:
|
| 230 |
if metric in leaderboard_df.columns:
|
| 231 |
best_model_score = leaderboard_df.iloc[0][metric]
|
| 232 |
-
logger.info(f"
|
| 233 |
break
|
| 234 |
|
| 235 |
mlflow.log_metric("total_models_trained", len(leaderboard_df))
|
| 236 |
else:
|
| 237 |
-
# Fallback:
|
| 238 |
try:
|
| 239 |
available_columns = leaderboard.columns
|
| 240 |
-
logger.info(f"
|
| 241 |
|
| 242 |
-
#
|
| 243 |
if len(available_columns) > 0:
|
| 244 |
first_col = available_columns[0]
|
| 245 |
best_model_score = leaderboard[0, first_col]
|
| 246 |
-
logger.info(f"
|
| 247 |
|
| 248 |
mlflow.log_metric("total_models_trained", leaderboard.nrow)
|
| 249 |
except Exception as e:
|
| 250 |
-
logger.warning(f"
|
| 251 |
mlflow.log_metric("total_models_trained", 0)
|
| 252 |
|
| 253 |
mlflow.log_metric("best_model_score", best_model_score)
|
| 254 |
mlflow.log_metric("training_duration", training_duration)
|
| 255 |
|
| 256 |
except Exception as e:
|
| 257 |
-
logger.warning(f"
|
| 258 |
-
#
|
| 259 |
mlflow.log_metric("best_model_score", 0.0)
|
| 260 |
mlflow.log_metric("training_duration", training_duration)
|
| 261 |
mlflow.log_metric("total_models_trained", 0)
|
| 262 |
|
| 263 |
-
#
|
| 264 |
try:
|
| 265 |
leaderboard_df = leaderboard.as_data_frame()
|
| 266 |
leaderboard_path = f"h2o_leaderboard_{run_name}.csv"
|
| 267 |
leaderboard_df.to_csv(leaderboard_path, index=False)
|
| 268 |
mlflow.log_artifact(leaderboard_path)
|
| 269 |
except Exception as e:
|
| 270 |
-
logger.warning(f"
|
| 271 |
-
#
|
| 272 |
try:
|
| 273 |
leaderboard_text = str(leaderboard.head(10))
|
| 274 |
leaderboard_path = f"h2o_leaderboard_{run_name}.txt"
|
|
@@ -278,47 +278,47 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 278 |
f.write(leaderboard_text)
|
| 279 |
mlflow.log_artifact(leaderboard_path)
|
| 280 |
except Exception as e2:
|
| 281 |
-
logger.warning(f"
|
| 282 |
|
| 283 |
-
#
|
| 284 |
if hasattr(aml, 'leader') and aml.leader is not None:
|
| 285 |
model_dir = "models/h2o_models"
|
| 286 |
os.makedirs(model_dir, exist_ok=True)
|
| 287 |
model_path = f"{model_dir}/h2o_model_{run_name}"
|
| 288 |
|
| 289 |
-
#
|
| 290 |
best_model = aml.leader
|
| 291 |
h2o.save_model(best_model, path=model_path)
|
| 292 |
-
logger.info(f"
|
| 293 |
|
| 294 |
-
#
|
| 295 |
temp_model_path = f"temp_h2o_model_{run_name}"
|
| 296 |
os.makedirs(temp_model_path, exist_ok=True)
|
| 297 |
h2o.save_model(best_model, path=temp_model_path)
|
| 298 |
mlflow.log_artifacts(temp_model_path, artifact_path="model")
|
| 299 |
|
| 300 |
-
#
|
| 301 |
import shutil
|
| 302 |
if os.path.exists(temp_model_path):
|
| 303 |
shutil.rmtree(temp_model_path)
|
| 304 |
else:
|
| 305 |
-
logger.warning("⚠️
|
| 306 |
|
| 307 |
-
#
|
| 308 |
no_model_path = f"no_model_{run_name}.txt"
|
| 309 |
with open(no_model_path, "w") as f:
|
| 310 |
f.write(f"H2O AutoML - {run_name}\n")
|
| 311 |
f.write("=" * 50 + "\n")
|
| 312 |
-
f.write("
|
| 313 |
-
f.write("
|
| 314 |
-
f.write("1.
|
| 315 |
-
f.write("2.
|
| 316 |
-
f.write("3.
|
| 317 |
-
f.write(f"
|
| 318 |
|
| 319 |
mlflow.log_artifact(no_model_path)
|
| 320 |
|
| 321 |
-
#
|
| 322 |
if (clean_data[target].dtype == 'object' or clean_data[target].nunique() < 20) and hasattr(aml, 'leader') and aml.leader is not None:
|
| 323 |
try:
|
| 324 |
best_model = aml.leader
|
|
@@ -326,22 +326,22 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 326 |
pred_array = predictions['predict'].as_data_frame()['predict'].values
|
| 327 |
true_labels = clean_data[target].values
|
| 328 |
|
| 329 |
-
#
|
| 330 |
accuracy = accuracy_score(true_labels, pred_array)
|
| 331 |
f1_macro = f1_score(true_labels, pred_array, average='macro')
|
| 332 |
f1_weighted = f1_score(true_labels, pred_array, average='weighted')
|
| 333 |
|
| 334 |
-
logger.info(f"\
|
| 335 |
logger.info(f"Accuracy: {accuracy:.4f}")
|
| 336 |
logger.info(f"F1-Score (macro): {f1_macro:.4f}")
|
| 337 |
logger.info(f"F1-Score (weighted): {f1_weighted:.4f}")
|
| 338 |
|
| 339 |
-
# Log
|
| 340 |
mlflow.log_metric("validation_accuracy", accuracy)
|
| 341 |
mlflow.log_metric("validation_f1_macro", f1_macro)
|
| 342 |
mlflow.log_metric("validation_f1_weighted", f1_weighted)
|
| 343 |
|
| 344 |
-
#
|
| 345 |
class_report = classification_report(true_labels, pred_array)
|
| 346 |
report_path = f"classification_report_{run_name}.txt"
|
| 347 |
with open(report_path, "w") as f:
|
|
@@ -352,11 +352,11 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 352 |
mlflow.log_artifact(report_path)
|
| 353 |
|
| 354 |
except Exception as e:
|
| 355 |
-
logger.warning(f"
|
| 356 |
else:
|
| 357 |
-
logger.info("
|
| 358 |
|
| 359 |
-
#
|
| 360 |
if os.path.exists(leaderboard_path):
|
| 361 |
os.remove(leaderboard_path)
|
| 362 |
|
|
@@ -367,76 +367,74 @@ def train_h2o_model(train_data: pd.DataFrame, target: str, run_name: str,
|
|
| 367 |
return aml, run.info.run_id
|
| 368 |
|
| 369 |
except Exception as e:
|
| 370 |
-
logger.error(f"
|
| 371 |
raise
|
| 372 |
-
finally:
|
| 373 |
-
cleanup_h2o()
|
| 374 |
|
| 375 |
def load_h2o_model(run_id: str):
|
| 376 |
"""
|
| 377 |
-
|
| 378 |
"""
|
| 379 |
import h2o
|
| 380 |
|
| 381 |
-
#
|
| 382 |
try:
|
| 383 |
h2o.init(max_mem_size="2G", nthreads=-1)
|
| 384 |
except:
|
| 385 |
-
pass # H2O
|
| 386 |
|
| 387 |
try:
|
| 388 |
-
# Download
|
| 389 |
local_path = mlflow.artifacts.download_artifacts(run_id=run_id, artifact_path="model")
|
| 390 |
|
| 391 |
-
#
|
| 392 |
for root, dirs, files in os.walk(local_path):
|
| 393 |
for file in files:
|
| 394 |
if file.endswith(".zip"):
|
| 395 |
model_path = os.path.join(root, file)
|
| 396 |
-
logger.info(f"
|
| 397 |
model = h2o.load_model(model_path)
|
| 398 |
|
| 399 |
-
#
|
| 400 |
if model is None:
|
| 401 |
-
raise ValueError("
|
| 402 |
|
| 403 |
-
logger.info(f"
|
| 404 |
return model
|
| 405 |
|
| 406 |
-
raise FileNotFoundError("
|
| 407 |
|
| 408 |
except Exception as e:
|
| 409 |
-
logger.error(f"
|
| 410 |
raise
|
| 411 |
|
| 412 |
def predict_with_h2o(model, data: pd.DataFrame):
|
| 413 |
"""
|
| 414 |
-
|
| 415 |
"""
|
| 416 |
import h2o
|
| 417 |
|
| 418 |
-
#
|
| 419 |
if model is None:
|
| 420 |
-
raise ValueError("
|
| 421 |
|
| 422 |
try:
|
| 423 |
-
logger.info(f"
|
| 424 |
|
| 425 |
-
#
|
| 426 |
-
h2o_frame, _ = prepare_data_for_h2o(data, target="dummy") # target
|
| 427 |
|
| 428 |
-
#
|
| 429 |
predictions = model.predict(h2o_frame)
|
| 430 |
pred_array = predictions['predict'].as_data_frame()['predict'].values
|
| 431 |
|
| 432 |
-
logger.info(f"
|
| 433 |
return pred_array
|
| 434 |
|
| 435 |
except Exception as e:
|
| 436 |
-
logger.error(f"
|
| 437 |
raise
|
| 438 |
finally:
|
| 439 |
-
#
|
| 440 |
try:
|
| 441 |
if 'h2o_frame' in locals():
|
| 442 |
h2o_frame = None
|
|
|
|
| 11 |
logger = logging.getLogger(__name__)
|
| 12 |
|
| 13 |
def check_java_availability():
|
| 14 |
+
"""Checks if Java is available in the system"""
|
| 15 |
try:
|
| 16 |
import subprocess
|
| 17 |
import os
|
| 18 |
|
| 19 |
+
# Try to find Java in PATH
|
| 20 |
result = subprocess.run(['java', '-version'],
|
| 21 |
capture_output=True, text=True, timeout=5)
|
| 22 |
if result.returncode == 0:
|
| 23 |
return True
|
| 24 |
|
| 25 |
+
# If not found in PATH, try common paths on Windows
|
| 26 |
java_paths = [
|
| 27 |
r"C:\Program Files\Eclipse Adoptium\jdk-11.0.30.7-hotspot\bin\java.exe",
|
| 28 |
r"C:\Program Files\Eclipse Adoptium\jdk-11.0.23.9-hotspot\bin\java.exe",
|
|
|
|
| 43 |
return False
|
| 44 |
|
| 45 |
def initialize_h2o():
|
| 46 |
+
"""Initializes the H2O cluster with Java check"""
|
| 47 |
if not check_java_availability():
|
| 48 |
raise RuntimeError(
|
| 49 |
+
"Java is not installed on the system. H2O AutoML requires Java to function.\n\n"
|
| 50 |
+
"Options:\n"
|
| 51 |
+
"1. Install Java locally (JRE/JDK)\n"
|
| 52 |
+
"2. Use Docker: docker build -t multi-automl-interface . && docker run -p 8501:8501 multi-automl-interface\n"
|
| 53 |
+
"3. Use AutoGluon or FLAML as alternatives (they do not require Java)\n"
|
| 54 |
+
"\nTo install Java on Windows:\n"
|
| 55 |
+
"- Download from: https://adoptium.net/\n"
|
| 56 |
+
"- Or use: winget install EclipseAdoptium.Temurin.11.JDK"
|
| 57 |
)
|
| 58 |
|
| 59 |
try:
|
| 60 |
import h2o
|
| 61 |
h2o.init(max_mem_size="4G", nthreads=-1)
|
| 62 |
+
logger.info("H2O Cluster initialized successfully")
|
| 63 |
return h2o
|
| 64 |
except Exception as e:
|
| 65 |
+
logger.error(f"Error initializing H2O: {e}")
|
| 66 |
raise
|
| 67 |
|
| 68 |
def cleanup_h2o():
|
| 69 |
+
"""Finalizes the H2O cluster"""
|
| 70 |
try:
|
| 71 |
import h2o
|
| 72 |
h2o.cluster().shutdown()
|
| 73 |
+
logger.info("H2O Cluster finalized")
|
| 74 |
except Exception as e:
|
| 75 |
+
logger.warning(f"Error finalizing H2O: {e}")
|
| 76 |
|
| 77 |
def prepare_data_for_h2o(train_data: pd.DataFrame, target: str):
|
| 78 |
+
"""Prepares data for H2O AutoML"""
|
| 79 |
import h2o
|
| 80 |
|
| 81 |
+
# Drop null values
|
| 82 |
train_data_clean = train_data.dropna(subset=[target])
|
| 83 |
|
| 84 |
+
# For textual data, create basic numerical features
|
| 85 |
if train_data_clean.select_dtypes(include=['object']).shape[1] > 0:
|
| 86 |
+
logger.info("Text columns detected, generating basic numerical features...")
|
| 87 |
|
| 88 |
+
# For each text column, build basic features
|
| 89 |
for col in train_data_clean.select_dtypes(include=['object']).columns:
|
| 90 |
if col != target:
|
| 91 |
+
# Text length
|
| 92 |
train_data_clean[f'{col}_length'] = train_data_clean[col].astype(str).str.len()
|
| 93 |
+
# Word count
|
| 94 |
train_data_clean[f'{col}_word_count'] = train_data_clean[col].astype(str).str.split().str.len()
|
| 95 |
|
| 96 |
+
# Drop text columns except target
|
| 97 |
text_cols = train_data_clean.select_dtypes(include=['object']).columns
|
| 98 |
text_cols = [col for col in text_cols if col != target]
|
| 99 |
train_data_clean = train_data_clean.drop(columns=text_cols)
|
| 100 |
|
| 101 |
+
# Convert to H2OFrame
|
| 102 |
h2o_frame = h2o.H2OFrame(train_data_clean)
|
| 103 |
|
| 104 |
+
# Convert target to factor (categorical) if classification
|
| 105 |
if train_data_clean[target].dtype == 'object' or train_data_clean[target].nunique() < 20:
|
| 106 |
h2o_frame[target] = h2o_frame[target].asfactor()
|
| 107 |
|
|
|
|
| 113 |
nfolds: int = 3, balance_classes: bool = True, seed: int = 42,
|
| 114 |
sort_metric: str = "AUTO", exclude_algos: list = None):
|
| 115 |
"""
|
| 116 |
+
Trains H2O AutoML model and registers in MLflow
|
| 117 |
"""
|
| 118 |
import h2o
|
| 119 |
from h2o.automl import H2OAutoML
|
| 120 |
|
| 121 |
safe_set_experiment("H2O_Experiments")
|
| 122 |
+
logging.info(f"Starting H2O AutoML training for run: {run_name}")
|
| 123 |
|
| 124 |
+
# Initialize H2O
|
| 125 |
h2o_instance = initialize_h2o()
|
| 126 |
|
| 127 |
try:
|
| 128 |
with mlflow.start_run(run_name=run_name) as run:
|
| 129 |
+
# Prepare data
|
| 130 |
h2o_frame, clean_data = prepare_data_for_h2o(train_data, target)
|
| 131 |
|
| 132 |
+
# Log parameters
|
| 133 |
mlflow.log_param("target", target)
|
| 134 |
mlflow.log_param("max_runtime_secs", max_runtime_secs)
|
| 135 |
mlflow.log_param("max_models", max_models)
|
|
|
|
| 141 |
if exclude_algos:
|
| 142 |
mlflow.log_param("exclude_algos", exclude_algos)
|
| 143 |
|
| 144 |
+
# Define features (all except target)
|
| 145 |
features = [col for col in clean_data.columns if col != target]
|
| 146 |
mlflow.log_param("features", features)
|
| 147 |
|
|
|
|
| 159 |
exclude_algos=exclude_algos or []
|
| 160 |
)
|
| 161 |
|
| 162 |
+
# Prepare test and validation data if present
|
| 163 |
h2o_valid = None
|
| 164 |
if valid_data is not None:
|
| 165 |
if target not in valid_data.columns:
|
| 166 |
+
raise ValueError(f"Target column '{target}' not found in Validation data.")
|
| 167 |
valid_data = valid_data.dropna(subset=[target])
|
| 168 |
h2o_valid, _ = prepare_data_for_h2o(valid_data, target)
|
| 169 |
mlflow.log_param("has_validation_data", True)
|
|
|
|
| 171 |
h2o_test = None
|
| 172 |
if test_data is not None:
|
| 173 |
if target not in test_data.columns:
|
| 174 |
+
raise ValueError(f"Target column '{target}' not found in Test data.")
|
| 175 |
test_data = test_data.dropna(subset=[target])
|
| 176 |
h2o_test, _ = prepare_data_for_h2o(test_data, target)
|
| 177 |
mlflow.log_param("has_test_data", True)
|
| 178 |
|
| 179 |
+
# Train model
|
| 180 |
+
logger.info("Starting H2O AutoML training...")
|
| 181 |
start_time = time.time()
|
| 182 |
train_kwargs = {"x": features, "y": target, "training_frame": h2o_frame}
|
| 183 |
if h2o_valid is not None:
|
|
|
|
| 188 |
aml.train(**train_kwargs)
|
| 189 |
training_duration = time.time() - start_time
|
| 190 |
|
| 191 |
+
logger.info(f"Training completed in {training_duration:.2f} seconds")
|
| 192 |
|
| 193 |
+
# Get leaderboard
|
| 194 |
leaderboard = aml.leaderboard
|
| 195 |
|
| 196 |
+
# Check if leaderboard is empty
|
| 197 |
if leaderboard.nrow == 0:
|
| 198 |
+
logger.warning("⚠️ No models trained. Leaderboard is empty.")
|
| 199 |
+
logger.warning("This can happen if:")
|
| 200 |
+
logger.warning("1. Max runtime is too short")
|
| 201 |
+
logger.warning("2. Data is not adequate for algorithms")
|
| 202 |
+
logger.warning("3. Data has underlying issues")
|
| 203 |
|
| 204 |
+
# Log basic metrics even without models
|
| 205 |
mlflow.log_metric("total_models_trained", 0)
|
| 206 |
mlflow.log_metric("training_duration", training_duration)
|
| 207 |
mlflow.log_metric("best_model_score", 0.0)
|
| 208 |
|
| 209 |
+
# Return AutoML even without models
|
| 210 |
return aml, run.info.run_id
|
| 211 |
|
| 212 |
+
logger.info("\nTop 5 models:")
|
| 213 |
print(leaderboard.head(5))
|
| 214 |
|
| 215 |
+
# Save leaderboard as metric with safe wrapper
|
| 216 |
try:
|
| 217 |
+
# Check available columns in leaderboard
|
| 218 |
leaderboard_df = None
|
| 219 |
try:
|
| 220 |
leaderboard_df = leaderboard.as_data_frame()
|
| 221 |
+
logger.info(f"Available columns: {list(leaderboard_df.columns)}")
|
| 222 |
except Exception as e:
|
| 223 |
+
logger.warning(f"Could not convert leaderboard to DataFrame: {e}")
|
| 224 |
|
| 225 |
+
# Try to get the best available metric
|
| 226 |
best_model_score = 0.0
|
| 227 |
if leaderboard_df is not None and len(leaderboard_df) > 0:
|
| 228 |
+
# Search for metrics in preference order
|
| 229 |
for metric in ['auc', 'logloss', 'rmse', 'mae', 'r2']:
|
| 230 |
if metric in leaderboard_df.columns:
|
| 231 |
best_model_score = leaderboard_df.iloc[0][metric]
|
| 232 |
+
logger.info(f"Using metric '{metric}': {best_model_score}")
|
| 233 |
break
|
| 234 |
|
| 235 |
mlflow.log_metric("total_models_trained", len(leaderboard_df))
|
| 236 |
else:
|
| 237 |
+
# Fallback: use the first value in H2O leaderboard
|
| 238 |
try:
|
| 239 |
available_columns = leaderboard.columns
|
| 240 |
+
logger.info(f"Available H2O columns: {available_columns}")
|
| 241 |
|
| 242 |
+
# Try accessing first row, first metric col
|
| 243 |
if len(available_columns) > 0:
|
| 244 |
first_col = available_columns[0]
|
| 245 |
best_model_score = leaderboard[0, first_col]
|
| 246 |
+
logger.info(f"Using first available column '{first_col}': {best_model_score}")
|
| 247 |
|
| 248 |
mlflow.log_metric("total_models_trained", leaderboard.nrow)
|
| 249 |
except Exception as e:
|
| 250 |
+
logger.warning(f"Could not extract metrics from leaderboard: {e}")
|
| 251 |
mlflow.log_metric("total_models_trained", 0)
|
| 252 |
|
| 253 |
mlflow.log_metric("best_model_score", best_model_score)
|
| 254 |
mlflow.log_metric("training_duration", training_duration)
|
| 255 |
|
| 256 |
except Exception as e:
|
| 257 |
+
logger.warning(f"Error processing leaderboard metrics: {e}")
|
| 258 |
+
# Default fallback
|
| 259 |
mlflow.log_metric("best_model_score", 0.0)
|
| 260 |
mlflow.log_metric("training_duration", training_duration)
|
| 261 |
mlflow.log_metric("total_models_trained", 0)
|
| 262 |
|
| 263 |
+
# Try saving leaderboard with error handling
|
| 264 |
try:
|
| 265 |
leaderboard_df = leaderboard.as_data_frame()
|
| 266 |
leaderboard_path = f"h2o_leaderboard_{run_name}.csv"
|
| 267 |
leaderboard_df.to_csv(leaderboard_path, index=False)
|
| 268 |
mlflow.log_artifact(leaderboard_path)
|
| 269 |
except Exception as e:
|
| 270 |
+
logger.warning(f"Could not save leaderboard as CSV: {e}")
|
| 271 |
+
# Save as plain text if CSV fails
|
| 272 |
try:
|
| 273 |
leaderboard_text = str(leaderboard.head(10))
|
| 274 |
leaderboard_path = f"h2o_leaderboard_{run_name}.txt"
|
|
|
|
| 278 |
f.write(leaderboard_text)
|
| 279 |
mlflow.log_artifact(leaderboard_path)
|
| 280 |
except Exception as e2:
|
| 281 |
+
logger.warning(f"Could not save leaderboard as text: {e2}")
|
| 282 |
|
| 283 |
+
# Save local model (only if there are models)
|
| 284 |
if hasattr(aml, 'leader') and aml.leader is not None:
|
| 285 |
model_dir = "models/h2o_models"
|
| 286 |
os.makedirs(model_dir, exist_ok=True)
|
| 287 |
model_path = f"{model_dir}/h2o_model_{run_name}"
|
| 288 |
|
| 289 |
+
# Save best model (leader) rather than AutoML object
|
| 290 |
best_model = aml.leader
|
| 291 |
h2o.save_model(best_model, path=model_path)
|
| 292 |
+
logger.info(f"Model saved at: {model_path}")
|
| 293 |
|
| 294 |
+
# Log model to MLflow
|
| 295 |
temp_model_path = f"temp_h2o_model_{run_name}"
|
| 296 |
os.makedirs(temp_model_path, exist_ok=True)
|
| 297 |
h2o.save_model(best_model, path=temp_model_path)
|
| 298 |
mlflow.log_artifacts(temp_model_path, artifact_path="model")
|
| 299 |
|
| 300 |
+
# Clean temp directory
|
| 301 |
import shutil
|
| 302 |
if os.path.exists(temp_model_path):
|
| 303 |
shutil.rmtree(temp_model_path)
|
| 304 |
else:
|
| 305 |
+
logger.warning("⚠️ No model to save (no models were trained)")
|
| 306 |
|
| 307 |
+
# Create a placeholder file explaining the situation
|
| 308 |
no_model_path = f"no_model_{run_name}.txt"
|
| 309 |
with open(no_model_path, "w") as f:
|
| 310 |
f.write(f"H2O AutoML - {run_name}\n")
|
| 311 |
f.write("=" * 50 + "\n")
|
| 312 |
+
f.write("No models were trained during this run.\n")
|
| 313 |
+
f.write("Possible causes:\n")
|
| 314 |
+
f.write("1. Insufficient training time\n")
|
| 315 |
+
f.write("2. Data inadequate for algorithms\n")
|
| 316 |
+
f.write("3. Data quality issues\n")
|
| 317 |
+
f.write(f"Training time: {training_duration:.2f} seconds\n")
|
| 318 |
|
| 319 |
mlflow.log_artifact(no_model_path)
|
| 320 |
|
| 321 |
+
# Generate classification report for classification tasks (only if models exist)
|
| 322 |
if (clean_data[target].dtype == 'object' or clean_data[target].nunique() < 20) and hasattr(aml, 'leader') and aml.leader is not None:
|
| 323 |
try:
|
| 324 |
best_model = aml.leader
|
|
|
|
| 326 |
pred_array = predictions['predict'].as_data_frame()['predict'].values
|
| 327 |
true_labels = clean_data[target].values
|
| 328 |
|
| 329 |
+
# Calculate metrics
|
| 330 |
accuracy = accuracy_score(true_labels, pred_array)
|
| 331 |
f1_macro = f1_score(true_labels, pred_array, average='macro')
|
| 332 |
f1_weighted = f1_score(true_labels, pred_array, average='weighted')
|
| 333 |
|
| 334 |
+
logger.info(f"\nValidation metrics:")
|
| 335 |
logger.info(f"Accuracy: {accuracy:.4f}")
|
| 336 |
logger.info(f"F1-Score (macro): {f1_macro:.4f}")
|
| 337 |
logger.info(f"F1-Score (weighted): {f1_weighted:.4f}")
|
| 338 |
|
| 339 |
+
# Log validation metrics
|
| 340 |
mlflow.log_metric("validation_accuracy", accuracy)
|
| 341 |
mlflow.log_metric("validation_f1_macro", f1_macro)
|
| 342 |
mlflow.log_metric("validation_f1_weighted", f1_weighted)
|
| 343 |
|
| 344 |
+
# Generate report
|
| 345 |
class_report = classification_report(true_labels, pred_array)
|
| 346 |
report_path = f"classification_report_{run_name}.txt"
|
| 347 |
with open(report_path, "w") as f:
|
|
|
|
| 352 |
mlflow.log_artifact(report_path)
|
| 353 |
|
| 354 |
except Exception as e:
|
| 355 |
+
logger.warning(f"Could not generate classification report: {e}")
|
| 356 |
else:
|
| 357 |
+
logger.info("Skipping report generation (no models trained or not a classification problem)")
|
| 358 |
|
| 359 |
+
# Clean temporary files
|
| 360 |
if os.path.exists(leaderboard_path):
|
| 361 |
os.remove(leaderboard_path)
|
| 362 |
|
|
|
|
| 367 |
return aml, run.info.run_id
|
| 368 |
|
| 369 |
except Exception as e:
|
| 370 |
+
logger.error(f"Error during H2O training: {e}")
|
| 371 |
raise
|
|
|
|
|
|
|
| 372 |
|
| 373 |
def load_h2o_model(run_id: str):
|
| 374 |
"""
|
| 375 |
+
Loads H2O model from MLflow
|
| 376 |
"""
|
| 377 |
import h2o
|
| 378 |
|
| 379 |
+
# Initialize H2O if not active
|
| 380 |
try:
|
| 381 |
h2o.init(max_mem_size="2G", nthreads=-1)
|
| 382 |
except:
|
| 383 |
+
pass # H2O might already be active
|
| 384 |
|
| 385 |
try:
|
| 386 |
+
# Download artifact
|
| 387 |
local_path = mlflow.artifacts.download_artifacts(run_id=run_id, artifact_path="model")
|
| 388 |
|
| 389 |
+
# Find and load the model
|
| 390 |
for root, dirs, files in os.walk(local_path):
|
| 391 |
for file in files:
|
| 392 |
if file.endswith(".zip"):
|
| 393 |
model_path = os.path.join(root, file)
|
| 394 |
+
logger.info(f"Loading H2O model from: {model_path}")
|
| 395 |
model = h2o.load_model(model_path)
|
| 396 |
|
| 397 |
+
# Check if model loaded correctly
|
| 398 |
if model is None:
|
| 399 |
+
raise ValueError("Loaded model is None")
|
| 400 |
|
| 401 |
+
logger.info(f"H2O model loaded successfully: {type(model)}")
|
| 402 |
return model
|
| 403 |
|
| 404 |
+
raise FileNotFoundError("H2O model not found in artifacts.")
|
| 405 |
|
| 406 |
except Exception as e:
|
| 407 |
+
logger.error(f"Error loading H2O model: {e}")
|
| 408 |
raise
|
| 409 |
|
| 410 |
def predict_with_h2o(model, data: pd.DataFrame):
|
| 411 |
"""
|
| 412 |
+
Makes predictions using an H2O model
|
| 413 |
"""
|
| 414 |
import h2o
|
| 415 |
|
| 416 |
+
# Check if model is valid
|
| 417 |
if model is None:
|
| 418 |
+
raise ValueError("H2O model is None. Ensure the model was loaded correctly.")
|
| 419 |
|
| 420 |
try:
|
| 421 |
+
logger.info(f"Starting prediction with H2O model: {type(model)}")
|
| 422 |
|
| 423 |
+
# Prepare data the same way as training
|
| 424 |
+
h2o_frame, _ = prepare_data_for_h2o(data, target="dummy") # target not used for prediction
|
| 425 |
|
| 426 |
+
# Do predictions
|
| 427 |
predictions = model.predict(h2o_frame)
|
| 428 |
pred_array = predictions['predict'].as_data_frame()['predict'].values
|
| 429 |
|
| 430 |
+
logger.info(f"Prediction complete: {len(pred_array)} predictions")
|
| 431 |
return pred_array
|
| 432 |
|
| 433 |
except Exception as e:
|
| 434 |
+
logger.error(f"Error in H2O prediction: {e}")
|
| 435 |
raise
|
| 436 |
finally:
|
| 437 |
+
# Clean H2O frame to release memory
|
| 438 |
try:
|
| 439 |
if 'h2o_frame' in locals():
|
| 440 |
h2o_frame = None
|
src/mlflow_cache.py
CHANGED
|
@@ -7,57 +7,57 @@ import logging
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
class MLflowCache:
|
| 10 |
-
"""Cache
|
| 11 |
|
| 12 |
-
def __init__(self, ttl: int = 300): #
|
| 13 |
self._cache = {}
|
| 14 |
self._timestamps = {}
|
| 15 |
self.ttl = ttl
|
| 16 |
|
| 17 |
def _is_expired(self, key: str) -> bool:
|
| 18 |
-
"""
|
| 19 |
if key not in self._timestamps:
|
| 20 |
return True
|
| 21 |
return time.time() - self._timestamps[key] > self.ttl
|
| 22 |
|
| 23 |
def _set_cache(self, key: str, value):
|
| 24 |
-
"""
|
| 25 |
self._cache[key] = value
|
| 26 |
self._timestamps[key] = time.time()
|
| 27 |
|
| 28 |
def get_cached_all_runs(self, experiment_name: str) -> pd.DataFrame:
|
| 29 |
-
"""
|
| 30 |
cache_key = f"all_runs_{experiment_name}"
|
| 31 |
|
| 32 |
if not self._is_expired(cache_key) and cache_key in self._cache:
|
| 33 |
-
logger.info(f"
|
| 34 |
return self._cache[cache_key]
|
| 35 |
|
| 36 |
try:
|
| 37 |
-
#
|
| 38 |
experiment = mlflow.get_experiment_by_name(experiment_name)
|
| 39 |
if experiment is None:
|
| 40 |
return pd.DataFrame()
|
| 41 |
|
| 42 |
-
#
|
| 43 |
runs = mlflow.search_runs(experiment_ids=[experiment.experiment_id])
|
| 44 |
|
| 45 |
-
# Cache
|
| 46 |
self._set_cache(cache_key, runs)
|
| 47 |
-
logger.info(f"Cache
|
| 48 |
|
| 49 |
return runs
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
-
logger.error(f"
|
| 53 |
return pd.DataFrame()
|
| 54 |
|
| 55 |
def get_cached_experiment(self, experiment_name: str):
|
| 56 |
-
"""
|
| 57 |
cache_key = f"experiment_{experiment_name}"
|
| 58 |
|
| 59 |
if not self._is_expired(cache_key) and cache_key in self._cache:
|
| 60 |
-
logger.info(f"
|
| 61 |
return self._cache[cache_key]
|
| 62 |
|
| 63 |
try:
|
|
@@ -66,32 +66,32 @@ class MLflowCache:
|
|
| 66 |
return experiment
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
-
logger.error(f"
|
| 70 |
return None
|
| 71 |
|
| 72 |
def clear_cache(self):
|
| 73 |
-
"""
|
| 74 |
self._cache.clear()
|
| 75 |
self._timestamps.clear()
|
| 76 |
-
logger.info("Cache
|
| 77 |
|
| 78 |
def clear_experiment_cache(self, experiment_name: str):
|
| 79 |
-
"""
|
| 80 |
keys_to_remove = [key for key in self._cache.keys() if experiment_name in key]
|
| 81 |
for key in keys_to_remove:
|
| 82 |
self._cache.pop(key, None)
|
| 83 |
self._timestamps.pop(key, None)
|
| 84 |
-
logger.info(f"Cache
|
| 85 |
|
| 86 |
-
#
|
| 87 |
mlflow_cache = MLflowCache()
|
| 88 |
|
| 89 |
@lru_cache(maxsize=128)
|
| 90 |
def get_cached_experiment_list():
|
| 91 |
-
"""
|
| 92 |
try:
|
| 93 |
experiments = mlflow.search_experiments()
|
| 94 |
return [exp.name for exp in experiments]
|
| 95 |
except Exception as e:
|
| 96 |
-
logger.error(f"
|
| 97 |
return ["AutoGluon_Experiments", "FLAML_Experiments", "H2O_Experiments"]
|
|
|
|
| 7 |
logger = logging.getLogger(__name__)
|
| 8 |
|
| 9 |
class MLflowCache:
|
| 10 |
+
"""Cache to optimize MLflow data loading"""
|
| 11 |
|
| 12 |
+
def __init__(self, ttl: int = 300): # 5 minutes TTL
|
| 13 |
self._cache = {}
|
| 14 |
self._timestamps = {}
|
| 15 |
self.ttl = ttl
|
| 16 |
|
| 17 |
def _is_expired(self, key: str) -> bool:
|
| 18 |
+
"""Checks if cache is expired"""
|
| 19 |
if key not in self._timestamps:
|
| 20 |
return True
|
| 21 |
return time.time() - self._timestamps[key] > self.ttl
|
| 22 |
|
| 23 |
def _set_cache(self, key: str, value):
|
| 24 |
+
"""Sets value in cache"""
|
| 25 |
self._cache[key] = value
|
| 26 |
self._timestamps[key] = time.time()
|
| 27 |
|
| 28 |
def get_cached_all_runs(self, experiment_name: str) -> pd.DataFrame:
|
| 29 |
+
"""Gets all runs with cache"""
|
| 30 |
cache_key = f"all_runs_{experiment_name}"
|
| 31 |
|
| 32 |
if not self._is_expired(cache_key) and cache_key in self._cache:
|
| 33 |
+
logger.info(f"Using cache for experiment {experiment_name}")
|
| 34 |
return self._cache[cache_key]
|
| 35 |
|
| 36 |
try:
|
| 37 |
+
# Get experiment
|
| 38 |
experiment = mlflow.get_experiment_by_name(experiment_name)
|
| 39 |
if experiment is None:
|
| 40 |
return pd.DataFrame()
|
| 41 |
|
| 42 |
+
# Search runs
|
| 43 |
runs = mlflow.search_runs(experiment_ids=[experiment.experiment_id])
|
| 44 |
|
| 45 |
+
# Cache the result
|
| 46 |
self._set_cache(cache_key, runs)
|
| 47 |
+
logger.info(f"Cache updated for experiment {experiment_name} ({len(runs)} runs)")
|
| 48 |
|
| 49 |
return runs
|
| 50 |
|
| 51 |
except Exception as e:
|
| 52 |
+
logger.error(f"Error fetching runs for experiment {experiment_name}: {e}")
|
| 53 |
return pd.DataFrame()
|
| 54 |
|
| 55 |
def get_cached_experiment(self, experiment_name: str):
|
| 56 |
+
"""Gets experiment with cache"""
|
| 57 |
cache_key = f"experiment_{experiment_name}"
|
| 58 |
|
| 59 |
if not self._is_expired(cache_key) and cache_key in self._cache:
|
| 60 |
+
logger.info(f"Using cache for experiment {experiment_name}")
|
| 61 |
return self._cache[cache_key]
|
| 62 |
|
| 63 |
try:
|
|
|
|
| 66 |
return experiment
|
| 67 |
|
| 68 |
except Exception as e:
|
| 69 |
+
logger.error(f"Error fetching experiment {experiment_name}: {e}")
|
| 70 |
return None
|
| 71 |
|
| 72 |
def clear_cache(self):
|
| 73 |
+
"""Clears all cache"""
|
| 74 |
self._cache.clear()
|
| 75 |
self._timestamps.clear()
|
| 76 |
+
logger.info("Cache cleared")
|
| 77 |
|
| 78 |
def clear_experiment_cache(self, experiment_name: str):
|
| 79 |
+
"""Clears cache for a specific experiment"""
|
| 80 |
keys_to_remove = [key for key in self._cache.keys() if experiment_name in key]
|
| 81 |
for key in keys_to_remove:
|
| 82 |
self._cache.pop(key, None)
|
| 83 |
self._timestamps.pop(key, None)
|
| 84 |
+
logger.info(f"Cache cleared for experiment {experiment_name}")
|
| 85 |
|
| 86 |
+
# Global cache instance
|
| 87 |
mlflow_cache = MLflowCache()
|
| 88 |
|
| 89 |
@lru_cache(maxsize=128)
|
| 90 |
def get_cached_experiment_list():
|
| 91 |
+
"""Gets experiment list with cache"""
|
| 92 |
try:
|
| 93 |
experiments = mlflow.search_experiments()
|
| 94 |
return [exp.name for exp in experiments]
|
| 95 |
except Exception as e:
|
| 96 |
+
logger.error(f"Error fetching experiment list: {e}")
|
| 97 |
return ["AutoGluon_Experiments", "FLAML_Experiments", "H2O_Experiments"]
|
src/mlflow_utils.py
CHANGED
|
@@ -19,11 +19,11 @@ def heal_mlruns(mlruns_path="mlruns"):
|
|
| 19 |
if os.path.isdir(item_path) and item.isdigit():
|
| 20 |
meta_path = os.path.join(item_path, "meta.yaml")
|
| 21 |
if not os.path.exists(meta_path):
|
| 22 |
-
logger.warning(f"
|
| 23 |
try:
|
| 24 |
shutil.rmtree(item_path)
|
| 25 |
except Exception as e:
|
| 26 |
-
logger.error(f"
|
| 27 |
|
| 28 |
def safe_set_experiment(experiment_name):
|
| 29 |
"""Safely set MLflow experiment"""
|
|
@@ -31,15 +31,15 @@ def safe_set_experiment(experiment_name):
|
|
| 31 |
import mlflow
|
| 32 |
import os
|
| 33 |
|
| 34 |
-
#
|
| 35 |
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 36 |
mlruns_path = os.path.join(project_root, "mlruns")
|
| 37 |
|
| 38 |
-
#
|
| 39 |
os.makedirs(mlruns_path, exist_ok=True)
|
| 40 |
os.makedirs(os.path.join(mlruns_path, ".trash"), exist_ok=True)
|
| 41 |
|
| 42 |
-
#
|
| 43 |
normalized_path = mlruns_path.replace('\\', '/')
|
| 44 |
tracking_uri = f"file:///{normalized_path}"
|
| 45 |
mlflow.set_tracking_uri(tracking_uri)
|
|
@@ -47,11 +47,11 @@ def safe_set_experiment(experiment_name):
|
|
| 47 |
# Set experiment
|
| 48 |
mlflow.set_experiment(experiment_name)
|
| 49 |
|
| 50 |
-
logger.info(f"MLflow tracking URI
|
| 51 |
-
logger.info(f"
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
-
logger.error(f"
|
| 55 |
if "MissingConfigException" in str(type(e)) or "meta.yaml" in str(e):
|
| 56 |
heal_mlruns()
|
| 57 |
mlflow.set_experiment(experiment_name)
|
|
|
|
| 19 |
if os.path.isdir(item_path) and item.isdigit():
|
| 20 |
meta_path = os.path.join(item_path, "meta.yaml")
|
| 21 |
if not os.path.exists(meta_path):
|
| 22 |
+
logger.warning(f"Removing malformed experiment: {item_path}")
|
| 23 |
try:
|
| 24 |
shutil.rmtree(item_path)
|
| 25 |
except Exception as e:
|
| 26 |
+
logger.error(f"Error removing {item_path}: {e}")
|
| 27 |
|
| 28 |
def safe_set_experiment(experiment_name):
|
| 29 |
"""Safely set MLflow experiment"""
|
|
|
|
| 31 |
import mlflow
|
| 32 |
import os
|
| 33 |
|
| 34 |
+
# Configure tracking URI to project directory
|
| 35 |
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
| 36 |
mlruns_path = os.path.join(project_root, "mlruns")
|
| 37 |
|
| 38 |
+
# Ensure directory and trash exist
|
| 39 |
os.makedirs(mlruns_path, exist_ok=True)
|
| 40 |
os.makedirs(os.path.join(mlruns_path, ".trash"), exist_ok=True)
|
| 41 |
|
| 42 |
+
# Configure tracking URI
|
| 43 |
normalized_path = mlruns_path.replace('\\', '/')
|
| 44 |
tracking_uri = f"file:///{normalized_path}"
|
| 45 |
mlflow.set_tracking_uri(tracking_uri)
|
|
|
|
| 47 |
# Set experiment
|
| 48 |
mlflow.set_experiment(experiment_name)
|
| 49 |
|
| 50 |
+
logger.info(f"MLflow tracking URI configured to: {tracking_uri}")
|
| 51 |
+
logger.info(f"Experiment '{experiment_name}' configured successfully")
|
| 52 |
|
| 53 |
except Exception as e:
|
| 54 |
+
logger.error(f"Error configuring MLflow experiment: {e}")
|
| 55 |
if "MissingConfigException" in str(type(e)) or "meta.yaml" in str(e):
|
| 56 |
heal_mlruns()
|
| 57 |
mlflow.set_experiment(experiment_name)
|
src/tpot_utils.py
CHANGED
|
@@ -114,7 +114,7 @@ def prepare_data_for_tpot(df, target_column, test_data=None, test_size=0.2, rand
|
|
| 114 |
# Process test_data if provided
|
| 115 |
if test_data is not None:
|
| 116 |
if target_column not in test_data.columns:
|
| 117 |
-
raise ValueError(f"
|
| 118 |
test_clean = test_data.dropna(subset=[target_column]).copy()
|
| 119 |
for col in test_clean.columns:
|
| 120 |
if col != target_column:
|
|
@@ -173,7 +173,7 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 173 |
# TPOT handles validation automatically via CV. If validation is passed, concatenate to train for larger pool
|
| 174 |
if valid_data is not None:
|
| 175 |
if target_column not in valid_data.columns:
|
| 176 |
-
raise ValueError(f"
|
| 177 |
df = pd.concat([df, valid_data], ignore_index=True)
|
| 178 |
mlflow.log_param("has_validation_data", True)
|
| 179 |
|
|
@@ -231,12 +231,12 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 231 |
else:
|
| 232 |
scoring = 'neg_mean_squared_error'
|
| 233 |
|
| 234 |
-
#
|
| 235 |
while mlflow.active_run():
|
| 236 |
mlflow.end_run()
|
| 237 |
|
| 238 |
with mlflow.start_run(run_name=run_name) as run:
|
| 239 |
-
logger.info(f"
|
| 240 |
|
| 241 |
# Choose TPOT class based on problem type
|
| 242 |
if problem_type == 'classification':
|
|
@@ -290,13 +290,13 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 290 |
tpot.fit(X_train_processed, y_train)
|
| 291 |
training_duration = time.time() - start_time
|
| 292 |
|
| 293 |
-
logger.info(f"
|
| 294 |
|
| 295 |
except Exception as tpot_error:
|
| 296 |
-
logger.error(f"
|
| 297 |
|
| 298 |
# Try with simpler configuration
|
| 299 |
-
logger.info("
|
| 300 |
tpot = TPOTClassifier(
|
| 301 |
generations=1,
|
| 302 |
population_size=5,
|
|
@@ -312,7 +312,7 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 312 |
|
| 313 |
tpot.fit(X_train_processed, y_train)
|
| 314 |
training_duration = time.time() - start_time
|
| 315 |
-
logger.info(f"
|
| 316 |
|
| 317 |
# Predictions
|
| 318 |
y_pred = tpot.predict(X_test_processed)
|
|
@@ -359,7 +359,7 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 359 |
mlflow.log_artifact(report_path)
|
| 360 |
|
| 361 |
except Exception as e:
|
| 362 |
-
logger.warning(f"
|
| 363 |
|
| 364 |
else: # Regression
|
| 365 |
mse = mean_squared_error(y_test, y_pred)
|
|
@@ -416,7 +416,7 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 416 |
pipeline_path = f"tpot_models/best_pipeline_{run_name}.py"
|
| 417 |
os.makedirs("tpot_models", exist_ok=True)
|
| 418 |
tpot.export(pipeline_path)
|
| 419 |
-
logger.info(f"Pipeline
|
| 420 |
|
| 421 |
# Save model info
|
| 422 |
info_path = f"tpot_models/model_info_{run_name}.txt"
|
|
@@ -432,12 +432,12 @@ def train_tpot_model(df, target_column, run_name,
|
|
| 432 |
# Log the fitted pipeline
|
| 433 |
mlflow.sklearn.log_model(final_pipeline, "model", registered_model_name=f"TPOT_{run_name}")
|
| 434 |
|
| 435 |
-
logger.info("
|
| 436 |
|
| 437 |
return tpot, final_pipeline, run.info.run_id, model_info
|
| 438 |
|
| 439 |
except Exception as e:
|
| 440 |
-
logger.error(f"
|
| 441 |
raise
|
| 442 |
|
| 443 |
def load_tpot_model(run_id, model_path="model"):
|
|
@@ -446,7 +446,7 @@ def load_tpot_model(run_id, model_path="model"):
|
|
| 446 |
model = mlflow.sklearn.load_model(f"runs:/{run_id}/{model_path}")
|
| 447 |
return model
|
| 448 |
except Exception as e:
|
| 449 |
-
logger.error(f"
|
| 450 |
raise
|
| 451 |
|
| 452 |
def predict_with_tpot(model, data, preprocessor=None):
|
|
@@ -460,5 +460,5 @@ def predict_with_tpot(model, data, preprocessor=None):
|
|
| 460 |
predictions = model.predict(data_processed)
|
| 461 |
return predictions
|
| 462 |
except Exception as e:
|
| 463 |
-
logger.error(f"
|
| 464 |
raise
|
|
|
|
| 114 |
# Process test_data if provided
|
| 115 |
if test_data is not None:
|
| 116 |
if target_column not in test_data.columns:
|
| 117 |
+
raise ValueError(f"Target column '{target_column}' not found in Test data.")
|
| 118 |
test_clean = test_data.dropna(subset=[target_column]).copy()
|
| 119 |
for col in test_clean.columns:
|
| 120 |
if col != target_column:
|
|
|
|
| 173 |
# TPOT handles validation automatically via CV. If validation is passed, concatenate to train for larger pool
|
| 174 |
if valid_data is not None:
|
| 175 |
if target_column not in valid_data.columns:
|
| 176 |
+
raise ValueError(f"Target column '{target_column}' not found in Validation data.")
|
| 177 |
df = pd.concat([df, valid_data], ignore_index=True)
|
| 178 |
mlflow.log_param("has_validation_data", True)
|
| 179 |
|
|
|
|
| 231 |
else:
|
| 232 |
scoring = 'neg_mean_squared_error'
|
| 233 |
|
| 234 |
+
# Ensure there are no loose active runs that could cause errors on start
|
| 235 |
while mlflow.active_run():
|
| 236 |
mlflow.end_run()
|
| 237 |
|
| 238 |
with mlflow.start_run(run_name=run_name) as run:
|
| 239 |
+
logger.info(f"Starting TPOT training for run: {run_name}")
|
| 240 |
|
| 241 |
# Choose TPOT class based on problem type
|
| 242 |
if problem_type == 'classification':
|
|
|
|
| 290 |
tpot.fit(X_train_processed, y_train)
|
| 291 |
training_duration = time.time() - start_time
|
| 292 |
|
| 293 |
+
logger.info(f"Training completed in {training_duration:.2f} seconds")
|
| 294 |
|
| 295 |
except Exception as tpot_error:
|
| 296 |
+
logger.error(f"Error during TPOT training: {tpot_error}")
|
| 297 |
|
| 298 |
# Try with simpler configuration
|
| 299 |
+
logger.info("Trying with simpler configuration...")
|
| 300 |
tpot = TPOTClassifier(
|
| 301 |
generations=1,
|
| 302 |
population_size=5,
|
|
|
|
| 312 |
|
| 313 |
tpot.fit(X_train_processed, y_train)
|
| 314 |
training_duration = time.time() - start_time
|
| 315 |
+
logger.info(f"Simplified training completed in {training_duration:.2f} seconds")
|
| 316 |
|
| 317 |
# Predictions
|
| 318 |
y_pred = tpot.predict(X_test_processed)
|
|
|
|
| 359 |
mlflow.log_artifact(report_path)
|
| 360 |
|
| 361 |
except Exception as e:
|
| 362 |
+
logger.warning(f"Could not generate classification report: {e}")
|
| 363 |
|
| 364 |
else: # Regression
|
| 365 |
mse = mean_squared_error(y_test, y_pred)
|
|
|
|
| 416 |
pipeline_path = f"tpot_models/best_pipeline_{run_name}.py"
|
| 417 |
os.makedirs("tpot_models", exist_ok=True)
|
| 418 |
tpot.export(pipeline_path)
|
| 419 |
+
logger.info(f"Pipeline exported to {pipeline_path}")
|
| 420 |
|
| 421 |
# Save model info
|
| 422 |
info_path = f"tpot_models/model_info_{run_name}.txt"
|
|
|
|
| 432 |
# Log the fitted pipeline
|
| 433 |
mlflow.sklearn.log_model(final_pipeline, "model", registered_model_name=f"TPOT_{run_name}")
|
| 434 |
|
| 435 |
+
logger.info("TPOT model successfully registered in MLflow")
|
| 436 |
|
| 437 |
return tpot, final_pipeline, run.info.run_id, model_info
|
| 438 |
|
| 439 |
except Exception as e:
|
| 440 |
+
logger.error(f"Error during TPOT training: {e}")
|
| 441 |
raise
|
| 442 |
|
| 443 |
def load_tpot_model(run_id, model_path="model"):
|
|
|
|
| 446 |
model = mlflow.sklearn.load_model(f"runs:/{run_id}/{model_path}")
|
| 447 |
return model
|
| 448 |
except Exception as e:
|
| 449 |
+
logger.error(f"Error loading TPOT model: {e}")
|
| 450 |
raise
|
| 451 |
|
| 452 |
def predict_with_tpot(model, data, preprocessor=None):
|
|
|
|
| 460 |
predictions = model.predict(data_processed)
|
| 461 |
return predictions
|
| 462 |
except Exception as e:
|
| 463 |
+
logger.error(f"Error during TPOT prediction: {e}")
|
| 464 |
raise
|