File size: 6,928 Bytes
d7e53e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings(action="ignore")

from sklearn.model_selection import train_test_split
from sklearn.pipeline import Pipeline
from sklearn.compose import ColumnTransformer
from sklearn.preprocessing import (
    StandardScaler,
    OneHotEncoder,
    LabelEncoder,
)
from sklearn.metrics import (
    mean_absolute_error,
    mean_squared_error,
    r2_score,
    accuracy_score,
    precision_score,
    recall_score,
    f1_score,
    roc_auc_score,
)

# ======================
# Models
# ======================
from sklearn.linear_model import (
    LinearRegression,
    LogisticRegression,
    Perceptron,
)
from sklearn.neighbors import (
    KNeighborsClassifier,
    KNeighborsRegressor,
)
from sklearn.naive_bayes import GaussianNB
from sklearn.tree import (
    DecisionTreeClassifier,
    DecisionTreeRegressor,
)
from sklearn.svm import SVC, SVR
from sklearn.neural_network import (
    MLPClassifier,
    MLPRegressor,
)

from sklearn.utils.multiclass import type_of_target

# ======================
# Model Registry
# ======================
REGRESSION_MODELS = {
    "Linear Regression": LinearRegression(),
    "KNN Regressor": KNeighborsRegressor(),
    "Decision Tree Regressor": DecisionTreeRegressor(),
    "SVR": SVR(),
    "MLP Regressor": MLPRegressor(max_iter=1000),
}

CLASSIFICATION_MODELS = {
    "Logistic Regression": LogisticRegression(max_iter=500),
    "KNN Classifier": KNeighborsClassifier(),
    "Naive Bayes": GaussianNB(),
    "Perceptron": Perceptron(),
    "Decision Tree Classifier": DecisionTreeClassifier(),
    "SVM Classifier": SVC(probability=True),
    "MLP Classifier": MLPClassifier(max_iter=1000),
}

# ======================
# UI Helpers
# ======================
def update_models(task_type):
    if task_type == "Regression":
        return gr.update(choices=list(REGRESSION_MODELS.keys()), value=None)
    else:
        return gr.update(choices=list(CLASSIFICATION_MODELS.keys()), value=None)

def preview_csv(file):
    if file is None:
        return None
    return pd.read_csv(file.name)


def detect_target_type(y):
    # Categorical target
    if y.dtype == "object" or y.dtype.name == "category":
        return "Classification"

    # Numeric but low cardinality → classification
    if y.nunique() <= 20:
        return "Classification"

    return "Regression"


def auto_set_task(file):
    if file is None:
        return "Regression"
    df = pd.read_csv(file.name)
    y = df.iloc[:, -1]
    return detect_target_type(y)


# ======================
# Core Training Logic
# ======================
def train_model(file, task_type, model_name):
    df = pd.read_csv(file.name)

    # Target = last column
    X = df.iloc[:, :-1]
    y = df.iloc[:, -1]

    detected_task = detect_target_type(y)

    # 🚫 Mismatch validation
    if task_type != detected_task:
        return pd.DataFrame(
            {
                "Error": [
                    f"Dataset target detected as {detected_task}, "
                    f"but {task_type} model selected."
                ]
            }
        )

    # ---------- Automatic label encoding ----------
    if task_type == "Classification" and y.dtype == "object":
        y = LabelEncoder().fit_transform(y)

    # ---------- Feature preprocessing ----------
    num_cols = X.select_dtypes(include=["int64", "float64"]).columns
    cat_cols = X.select_dtypes(include=["object", "category"]).columns

    preprocessor = ColumnTransformer(
        transformers=[
            ("num", StandardScaler(), num_cols),
            ("cat", OneHotEncoder(handle_unknown="ignore"), cat_cols),
        ]
    )

    X_train, X_test, y_train, y_test = train_test_split(
        X, y, test_size=0.2, random_state=42
    )

    # ---------- Model selection ----------
    model = (
        REGRESSION_MODELS[model_name]
        if task_type == "Regression"
        else CLASSIFICATION_MODELS[model_name]
    )

    pipeline = Pipeline(
        steps=[
            ("preprocessing", preprocessor),
            ("model", model),
        ]
    )

    pipeline.fit(X_train, y_train)
    preds = pipeline.predict(X_test)

    # ---------- Metrics ----------
    if task_type == "Regression":
        metrics = {
            "MAE": mean_absolute_error(y_test, preds),
            "MSE": mean_squared_error(y_test, preds),
            "RMSE": np.sqrt(mean_squared_error(y_test, preds)),
            "R²": r2_score(y_test, preds),
        }

    else:
        metrics = {
            "Accuracy": accuracy_score(y_test, preds),
            "Precision": precision_score(y_test, preds, average="weighted"),
            "Recall": recall_score(y_test, preds, average="weighted"),
            "F1 Score": f1_score(y_test, preds, average="weighted"),
        }

        # ROC-AUC (safe handling)
        if hasattr(pipeline.named_steps["model"], "predict_proba"):
            probs = pipeline.predict_proba(X_test)
            target_type = type_of_target(y_test)

            # Binary classification
            if target_type == "binary":
                roc_auc = roc_auc_score(y_test, probs[:, 1])
                metrics["ROC-AUC"] = roc_auc

            # Multiclass classification
            elif target_type == "multiclass":
                roc_auc = roc_auc_score(
                    y_test,
                    probs,
                    multi_class="ovr",
                    average="weighted",
                )
                metrics["ROC-AUC"] = roc_auc


    # ---------- Metric table ----------
    result_df = pd.DataFrame(
        metrics.items(), columns=["Metric", "Value"]
    )

    return result_df

# ======================
# Gradio UI
# ======================
with gr.Blocks() as app:
    gr.Markdown("## Supervised Learning Model Trainer")
    gr.Markdown(
        "• Upload CSV\n"
        "• Last column is target\n"
        "• Automatic preprocessing & metrics"
    )

    file_input = gr.File(label="Upload CSV", file_types=[".csv"])

    csv_preview = gr.Dataframe(
        label="CSV Preview",
        interactive=False,
    )

    task_type = gr.Dropdown(
        ["Regression", "Classification"], label="Task Type", value="Regression"
    )
    model_name = gr.Dropdown(label="Model")
    output = gr.Dataframe(label="Evaluation Metrics")

    run_btn = gr.Button("Train & Evaluate")


    file_input.change(
        preview_csv,
        inputs=file_input,
        outputs=csv_preview,
    )

    file_input.change(
        auto_set_task,
        inputs=file_input,
        outputs=task_type,
    )

    task_type.change(
        update_models, inputs=task_type, outputs=model_name
    )

    app.load(
        update_models,
        inputs=task_type,
        outputs=model_name,
    )

    run_btn.click(
        train_model,
        inputs=[file_input, task_type, model_name],
        outputs=output,
    )

app.launch()