File size: 15,182 Bytes
facf506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
{% extends "base.html" %}

{% block title %}Training Results - Data2Model{% endblock %}

{% block content %}
<div class="container my-5">
    <div class="text-center mb-5">
        <h1 class="display-4">
            <i class="fas fa-check-circle text-success"></i> Training Complete!
        </h1>
        <p class="lead">Your machine learning model has been successfully trained</p>
    </div>

    <!-- Model Information -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow">
                <div class="card-header bg-success text-white">
                    <h4 class="mb-0">
                        <i class="fas fa-trophy"></i> Best Model: {{ pipeline.model_name }}
                    </h4>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <p><strong>Problem Type:</strong> 
                                <span class="badge bg-primary">{{ pipeline.problem_type|title }}</span>
                            </p>
                            <p><strong>Algorithm:</strong> {{ pipeline.model_name }}</p>
                        </div>
                        <div class="col-md-6">
                            <p><strong>Target Variable:</strong> {{ pipeline.target_name }}</p>
                            <p><strong>Features Used:</strong> {{ pipeline.feature_names|length }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Performance Metrics -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow">
                <div class="card-header bg-primary text-white">
                    <h4 class="mb-0">
                        <i class="fas fa-chart-bar"></i> Performance Metrics
                    </h4>
                </div>
                <div class="card-body">
                    {% if pipeline.problem_type == 'classification' %}
                    <!-- Classification Metrics -->
                    <div class="row text-center">
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-success">{{ "%.2f%%"|format(results.results.accuracy * 100) }}</h3>
                                <p class="text-muted">Test Accuracy</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-info">{{ "%.2f%%"|format(results.results.precision * 100) }}</h3>
                                <p class="text-muted">Precision</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-warning">{{ "%.2f%%"|format(results.results.recall * 100) }}</h3>
                                <p class="text-muted">Recall</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-primary">{{ "%.2f%%"|format(results.results.f1_score * 100) }}</h3>
                                <p class="text-muted">F1 Score</p>
                            </div>
                        </div>
                    </div>
                    
                    {% if results.results.validation_accuracy %}
                    <div class="alert alert-info mt-3">
                        <strong>Validation Set Accuracy:</strong> {{ "%.2f%%"|format(results.results.validation_accuracy * 100) }}
                        <br><small>Large dataset detected - used 60% training, 20% testing, 20% validation split</small>
                    </div>
                    {% endif %}

                    {% elif pipeline.problem_type == 'regression' %}
                    <!-- Regression Metrics -->
                    <div class="row text-center">
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-success">{{ "%.4f"|format(results.results.r2_score) }}</h3>
                                <p class="text-muted">Test R² Score</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-info">{{ "%.4f"|format(results.results.rmse) }}</h3>
                                <p class="text-muted">RMSE</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-warning">{{ "%.4f"|format(results.results.mae) }}</h3>
                                <p class="text-muted">MAE</p>
                            </div>
                        </div>
                        <div class="col-md-3 mb-3">
                            <div class="metric-box">
                                <h3 class="text-danger">{{ "%.4f"|format(results.results.mse) }}</h3>
                                <p class="text-muted">MSE</p>
                            </div>
                        </div>
                    </div>
                    
                    {% if results.results.validation_r2 %}
                    <div class="alert alert-info mt-3">
                        <strong>Validation Set Metrics:</strong> R² = {{ "%.4f"|format(results.results.validation_r2) }}, 
                        RMSE = {{ "%.4f"|format(results.results.validation_rmse) }}
                        <br><small>Large dataset detected - used 60% training, 20% testing, 20% validation split</small>
                    </div>
                    {% endif %}
                    {% endif %}

                    <div class="mt-4">
                        <h6>Metrics Explained:</h6>
                        <ul class="small text-muted">
                            {% if pipeline.problem_type == 'classification' %}
                            <li><strong>Accuracy:</strong> Percentage of correct predictions</li>
                            <li><strong>Precision:</strong> Of predicted positives, how many were correct</li>
                            <li><strong>Recall:</strong> Of actual positives, how many were found</li>
                            <li><strong>F1 Score:</strong> Harmonic mean of precision and recall</li>
                            {% else %}
                            <li><strong>R² Score:</strong> Proportion of variance explained (closer to 1 is better)</li>
                            <li><strong>RMSE:</strong> Root Mean Square Error (lower is better)</li>
                            <li><strong>MAE:</strong> Mean Absolute Error (lower is better)</li>
                            <li><strong>MSE:</strong> Mean Squared Error (lower is better)</li>
                            {% endif %}
                        </ul>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Preprocessing Steps -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow">
                <div class="card-header bg-info text-white">
                    <h4 class="mb-0">
                        <i class="fas fa-tasks"></i> Preprocessing Steps Applied
                    </h4>
                </div>
                <div class="card-body">
                    <ol>
                        {% for step in pipeline.preprocessing_steps %}
                        <li>{{ step }}</li>
                        {% endfor %}
                    </ol>
                </div>
            </div>
        </div>
    </div>

    <!-- Split Info Banner -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            {% if results.use_val %}
            <div class="alert alert-success d-flex align-items-center gap-3">
                <i class="fas fa-check-circle fa-2x"></i>
                <div>
                    <strong>3-Way Split Used:</strong> {{ results.split_info }}<br>
                    <span class="small">Model trained, tested, and validated — Prediction is fully unlocked!</span>
                </div>
            </div>
            {% else %}
            <div class="alert alert-success d-flex align-items-center gap-3">
                <i class="fas fa-check-circle fa-2x"></i>
                <div>
                    <strong>2-Way Split Used:</strong> {{ results.split_info }}<br>
                    <span class="small">Model trained and tested — Prediction is fully unlocked!</span>
                </div>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Download Section -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow border-success">
                <div class="card-body text-center">
                    <h4>
                        <i class="fas fa-download"></i> Download Your Model
                    </h4>
                    <p class="text-muted">
                        Save the trained model for future predictions
                    </p>
                    <a href="{{ url_for('download_model') }}" class="btn btn-success btn-lg">
                        <i class="fas fa-download"></i> Download Model (.pkl)
                    </a>
                </div>
            </div>
        </div>
    </div>

    <!-- Deployable App Download Section -->
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow border-primary" style="background: linear-gradient(135deg, #1e293b, #0f172a); border-width: 2px;">
                <div class="card-body text-center py-4">
                    <div class="mb-3">
                        <span style="font-size: 3rem;">🚀</span>
                    </div>
                    <h4 class="text-white">
                        <i class="fas fa-box-open"></i> Download Deployable Web App
                    </h4>
                    <p class="text-white-50 mb-1">
                        Get a <strong class="text-info">complete, ready-to-run Flask application</strong> with your trained model built-in.
                    </p>
                    <p class="text-white-50 small mb-3">
                        Includes <code>app.py</code>, a beautiful <code>index.html</code>, model artifacts, and <code>requirements.txt</code>.<br>
                        Just unzip → <code>pip install -r requirements.txt</code> → <code>python app.py</code> and you're live!
                    </p>
                    <a href="{{ url_for('download_app') }}" class="btn btn-lg text-white px-5 py-3" style="background: linear-gradient(135deg, #6366f1, #4f46e5); border: none; border-radius: 14px; font-weight: 700; font-size: 1.1rem; transition: all 0.3s;" onmouseover="this.style.transform='translateY(-2px)'; this.style.boxShadow='0 12px 30px rgba(99,102,241,0.35)';" onmouseout="this.style.transform=''; this.style.boxShadow='';">
                        <i class="fas fa-file-zipper"></i> Download App (.zip)
                    </a>
                    <div class="mt-3">
                        <span class="badge bg-info"><i class="fas fa-code"></i> app.py</span>
                        <span class="badge bg-warning text-dark"><i class="fas fa-palette"></i> index.html</span>
                        <span class="badge bg-success"><i class="fas fa-brain"></i> model.pkl</span>
                        <span class="badge bg-secondary"><i class="fas fa-list"></i> requirements.txt</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Preprocessing Artifacts Download Section (only shown when applicable) -->
    {% if model_state.scaling_applied or model_state.encoding_applied %}
    <div class="row mb-4">
        <div class="col-lg-8 mx-auto">
            <div class="card shadow border-info">
                <div class="card-header bg-info text-white">
                    <h5 class="mb-0">
                        <i class="fas fa-cogs"></i> Download Preprocessing Artifacts
                    </h5>
                </div>
                <div class="card-body">
                    <p class="text-muted small mb-3">
                        These fitted preprocessing objects were used during training.
                        Download and reuse them during inference to ensure consistent transformations.
                    </p>
                    <div class="d-flex flex-wrap gap-3 justify-content-center">
                        {% if model_state.scaling_applied %}
                        <a href="{{ url_for('download_scaler') }}" class="btn btn-outline-primary btn-lg">
                            <i class="fas fa-balance-scale"></i> Download Scaler (.pkl)
                            <span class="badge bg-primary ms-2">Numeric</span>
                        </a>
                        {% endif %}
                        {% if model_state.encoding_applied %}
                        <a href="{{ url_for('download_encoder') }}" class="btn btn-outline-warning btn-lg">
                            <i class="fas fa-tag"></i> Download Encoder (.pkl)
                            <span class="badge bg-warning text-dark ms-2">Categorical</span>
                        </a>
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    {% endif %}


    <!-- Actions -->
    <div class="row">
        <div class="col-lg-8 mx-auto">
            <div class="d-grid gap-2">
                {% if predict_unlocked %}
                <a href="{{ url_for('predict') }}" class="btn btn-primary btn-lg">
                    <i class="fas fa-chart-line"></i> Make Predictions
                    <span class="badge bg-success ms-2">Unlocked</span>
                </a>
                {% else %}
                <button class="btn btn-secondary btn-lg" disabled title="Retrain with 3-way split to unlock">
                    <i class="fas fa-lock"></i> Make Predictions
                    <span class="badge bg-dark ms-2">Locked — needs Train/Test/Val split</span>
                </button>
                {% endif %}
                <a href="{{ url_for('index') }}" class="btn btn-outline-secondary">
                    <i class="fas fa-plus"></i> Train Another Model
                </a>
            </div>
        </div>
    </div>
</div>

<style>
.metric-box {
    padding: 20px;
    border-radius: 10px;
    background-color: #f8f9fa;
}

.metric-box h3 {
    font-size: 2.5rem;
    font-weight: bold;
    margin-bottom: 5px;
}

.metric-box p {
    margin-bottom: 0;
    font-weight: 500;
}
</style>
{% endblock %}