Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,12 +1,11 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
-
import numpy as np
|
| 4 |
|
| 5 |
from sklearn.datasets import (
|
| 6 |
load_iris,
|
| 7 |
load_breast_cancer,
|
| 8 |
fetch_california_housing,
|
| 9 |
-
load_diabetes
|
| 10 |
)
|
| 11 |
|
| 12 |
from sklearn.model_selection import train_test_split
|
|
@@ -14,110 +13,137 @@ from sklearn.preprocessing import StandardScaler
|
|
| 14 |
|
| 15 |
# Classification Models
|
| 16 |
from sklearn.linear_model import LogisticRegression
|
| 17 |
-
from sklearn.svm import SVC
|
| 18 |
from sklearn.tree import DecisionTreeClassifier
|
| 19 |
from sklearn.ensemble import RandomForestClassifier
|
|
|
|
| 20 |
|
| 21 |
# Regression Models
|
| 22 |
from sklearn.linear_model import LinearRegression
|
| 23 |
-
from sklearn.svm import SVR
|
| 24 |
from sklearn.tree import DecisionTreeRegressor
|
| 25 |
from sklearn.ensemble import RandomForestRegressor
|
|
|
|
| 26 |
|
| 27 |
# Metrics
|
| 28 |
from sklearn.metrics import (
|
| 29 |
accuracy_score,
|
| 30 |
f1_score,
|
| 31 |
mean_squared_error,
|
| 32 |
-
r2_score
|
| 33 |
)
|
| 34 |
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
def load_dataset(task_type, dataset_name):
|
| 41 |
|
| 42 |
-
|
| 43 |
-
if task_type == "classification":
|
| 44 |
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
elif dataset_name == "Breast Cancer":
|
| 49 |
-
data = load_breast_cancer(as_frame=True)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
|
| 54 |
-
|
| 55 |
-
else:
|
| 56 |
|
| 57 |
-
if dataset_name == "California Housing":
|
| 58 |
-
data = fetch_california_housing(as_frame=True)
|
| 59 |
|
| 60 |
-
|
| 61 |
-
|
|
|
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
X =
|
| 67 |
-
y =
|
| 68 |
|
| 69 |
return X, y
|
| 70 |
|
| 71 |
|
| 72 |
-
# ==================================================
|
| 73 |
-
#
|
| 74 |
-
# ==================================================
|
| 75 |
|
| 76 |
def run_models(task_type, dataset_name):
|
| 77 |
|
| 78 |
-
#
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
if
|
| 82 |
-
return "Invalid dataset selection", ""
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
test_size=0.2,
|
| 89 |
-
random_state=42
|
| 90 |
-
)
|
| 91 |
-
|
| 92 |
-
# Scaling
|
| 93 |
-
scaler = StandardScaler()
|
| 94 |
|
| 95 |
-
|
| 96 |
-
|
|
|
|
|
|
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 100 |
|
| 101 |
models = {
|
| 102 |
"Logistic Regression": LogisticRegression(max_iter=1000),
|
| 103 |
-
"SVM": SVC(),
|
| 104 |
"Decision Tree": DecisionTreeClassifier(),
|
| 105 |
-
"Random Forest": RandomForestClassifier()
|
|
|
|
| 106 |
}
|
| 107 |
|
| 108 |
-
#
|
|
|
|
|
|
|
|
|
|
| 109 |
else:
|
| 110 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
models = {
|
| 112 |
"Linear Regression": LinearRegression(),
|
| 113 |
-
"SVR": SVR(),
|
| 114 |
"Decision Tree": DecisionTreeRegressor(),
|
| 115 |
-
"Random Forest": RandomForestRegressor()
|
|
|
|
| 116 |
}
|
| 117 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 118 |
results = []
|
| 119 |
|
| 120 |
-
# Train Models
|
| 121 |
for name, model in models.items():
|
| 122 |
|
| 123 |
model.fit(X_train, y_train)
|
|
@@ -125,37 +151,26 @@ def run_models(task_type, dataset_name):
|
|
| 125 |
predictions = model.predict(X_test)
|
| 126 |
|
| 127 |
# Classification Metrics
|
| 128 |
-
if task_type == "
|
| 129 |
|
| 130 |
accuracy = accuracy_score(y_test, predictions)
|
|
|
|
| 131 |
|
| 132 |
-
|
| 133 |
-
y_test,
|
| 134 |
-
predictions,
|
| 135 |
-
average="weighted"
|
| 136 |
-
)
|
| 137 |
-
|
| 138 |
-
results.append([
|
| 139 |
-
name,
|
| 140 |
-
round(accuracy, 4),
|
| 141 |
-
round(f1, 4)
|
| 142 |
-
])
|
| 143 |
|
| 144 |
# Regression Metrics
|
| 145 |
else:
|
| 146 |
|
| 147 |
mse = mean_squared_error(y_test, predictions)
|
| 148 |
-
|
| 149 |
r2 = r2_score(y_test, predictions)
|
| 150 |
|
| 151 |
-
results.append([
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
|
| 157 |
-
|
| 158 |
-
if task_type == "classification":
|
| 159 |
|
| 160 |
results_df = pd.DataFrame(
|
| 161 |
results,
|
|
@@ -163,8 +178,9 @@ def run_models(task_type, dataset_name):
|
|
| 163 |
)
|
| 164 |
|
| 165 |
best_model = results_df.loc[
|
| 166 |
-
results_df["Accuracy"].idxmax()
|
| 167 |
-
|
|
|
|
| 168 |
|
| 169 |
else:
|
| 170 |
|
|
@@ -174,24 +190,26 @@ def run_models(task_type, dataset_name):
|
|
| 174 |
)
|
| 175 |
|
| 176 |
best_model = results_df.loc[
|
| 177 |
-
results_df["
|
| 178 |
-
|
|
|
|
| 179 |
|
| 180 |
return results_df, f"🏆 Best Model: {best_model}"
|
| 181 |
|
| 182 |
|
| 183 |
-
# ==================================================
|
| 184 |
-
#
|
| 185 |
-
# ==================================================
|
| 186 |
|
| 187 |
def update_datasets(task_type):
|
| 188 |
|
| 189 |
-
if task_type == "
|
| 190 |
|
| 191 |
return gr.Dropdown(
|
| 192 |
choices=[
|
| 193 |
"Iris",
|
| 194 |
-
"Breast Cancer"
|
|
|
|
| 195 |
],
|
| 196 |
value="Iris"
|
| 197 |
)
|
|
@@ -201,33 +219,35 @@ def update_datasets(task_type):
|
|
| 201 |
return gr.Dropdown(
|
| 202 |
choices=[
|
| 203 |
"California Housing",
|
| 204 |
-
"Diabetes"
|
|
|
|
| 205 |
],
|
| 206 |
value="California Housing"
|
| 207 |
)
|
| 208 |
|
| 209 |
|
| 210 |
-
# ==================================================
|
| 211 |
-
#
|
| 212 |
-
# ==================================================
|
| 213 |
|
| 214 |
with gr.Blocks() as demo:
|
| 215 |
|
| 216 |
-
gr.Markdown("#
|
| 217 |
|
| 218 |
task_type = gr.Radio(
|
| 219 |
-
["
|
| 220 |
-
|
| 221 |
-
|
| 222 |
)
|
| 223 |
|
| 224 |
dataset_name = gr.Dropdown(
|
| 225 |
choices=[
|
| 226 |
"Iris",
|
| 227 |
-
"Breast Cancer"
|
|
|
|
| 228 |
],
|
| 229 |
-
|
| 230 |
-
|
| 231 |
)
|
| 232 |
|
| 233 |
task_type.change(
|
|
@@ -238,15 +258,14 @@ with gr.Blocks() as demo:
|
|
| 238 |
|
| 239 |
run_button = gr.Button("Run Models")
|
| 240 |
|
| 241 |
-
|
| 242 |
|
| 243 |
-
|
| 244 |
|
| 245 |
run_button.click(
|
| 246 |
fn=run_models,
|
| 247 |
inputs=[task_type, dataset_name],
|
| 248 |
-
outputs=[
|
| 249 |
)
|
| 250 |
|
| 251 |
-
|
| 252 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
|
|
|
| 3 |
|
| 4 |
from sklearn.datasets import (
|
| 5 |
load_iris,
|
| 6 |
load_breast_cancer,
|
| 7 |
fetch_california_housing,
|
| 8 |
+
load_diabetes,
|
| 9 |
)
|
| 10 |
|
| 11 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 13 |
|
| 14 |
# Classification Models
|
| 15 |
from sklearn.linear_model import LogisticRegression
|
|
|
|
| 16 |
from sklearn.tree import DecisionTreeClassifier
|
| 17 |
from sklearn.ensemble import RandomForestClassifier
|
| 18 |
+
from sklearn.neighbors import KNeighborsClassifier
|
| 19 |
|
| 20 |
# Regression Models
|
| 21 |
from sklearn.linear_model import LinearRegression
|
|
|
|
| 22 |
from sklearn.tree import DecisionTreeRegressor
|
| 23 |
from sklearn.ensemble import RandomForestRegressor
|
| 24 |
+
from sklearn.svm import SVR
|
| 25 |
|
| 26 |
# Metrics
|
| 27 |
from sklearn.metrics import (
|
| 28 |
accuracy_score,
|
| 29 |
f1_score,
|
| 30 |
mean_squared_error,
|
| 31 |
+
r2_score,
|
| 32 |
)
|
| 33 |
|
| 34 |
+
# =====================================================
|
| 35 |
+
# TITANIC DATASET
|
| 36 |
+
# =====================================================
|
| 37 |
|
| 38 |
+
def load_titanic():
|
| 39 |
+
url = "https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv"
|
| 40 |
+
df = pd.read_csv(url)
|
|
|
|
|
|
|
| 41 |
|
| 42 |
+
df = df[["Pclass", "Sex", "Age", "Fare", "Survived"]]
|
|
|
|
| 43 |
|
| 44 |
+
df["Age"] = df["Age"].fillna(df["Age"].mean())
|
| 45 |
+
df["Sex"] = df["Sex"].map({"male": 0, "female": 1})
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
X = df.drop("Survived", axis=1)
|
| 48 |
+
y = df["Survived"]
|
| 49 |
|
| 50 |
+
return X, y
|
|
|
|
| 51 |
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
# =====================================================
|
| 54 |
+
# BOSTON DATASET
|
| 55 |
+
# =====================================================
|
| 56 |
|
| 57 |
+
def load_boston():
|
| 58 |
+
url = "https://raw.githubusercontent.com/selva86/datasets/master/BostonHousing.csv"
|
| 59 |
+
df = pd.read_csv(url)
|
| 60 |
|
| 61 |
+
X = df.drop("medv", axis=1)
|
| 62 |
+
y = df["medv"]
|
| 63 |
|
| 64 |
return X, y
|
| 65 |
|
| 66 |
|
| 67 |
+
# =====================================================
|
| 68 |
+
# MAIN FUNCTION
|
| 69 |
+
# =====================================================
|
| 70 |
|
| 71 |
def run_models(task_type, dataset_name):
|
| 72 |
|
| 73 |
+
# =========================
|
| 74 |
+
# CLASSIFICATION DATASETS
|
| 75 |
+
# =========================
|
| 76 |
|
| 77 |
+
if task_type == "Classification":
|
|
|
|
| 78 |
|
| 79 |
+
if dataset_name == "Iris":
|
| 80 |
+
data = load_iris()
|
| 81 |
+
X = pd.DataFrame(data.data, columns=data.feature_names)
|
| 82 |
+
y = data.target
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
elif dataset_name == "Breast Cancer":
|
| 85 |
+
data = load_breast_cancer()
|
| 86 |
+
X = pd.DataFrame(data.data, columns=data.feature_names)
|
| 87 |
+
y = data.target
|
| 88 |
|
| 89 |
+
elif dataset_name == "Titanic":
|
| 90 |
+
X, y = load_titanic()
|
| 91 |
|
| 92 |
models = {
|
| 93 |
"Logistic Regression": LogisticRegression(max_iter=1000),
|
|
|
|
| 94 |
"Decision Tree": DecisionTreeClassifier(),
|
| 95 |
+
"Random Forest": RandomForestClassifier(),
|
| 96 |
+
"KNN": KNeighborsClassifier(),
|
| 97 |
}
|
| 98 |
|
| 99 |
+
# =========================
|
| 100 |
+
# REGRESSION DATASETS
|
| 101 |
+
# =========================
|
| 102 |
+
|
| 103 |
else:
|
| 104 |
|
| 105 |
+
if dataset_name == "California Housing":
|
| 106 |
+
data = fetch_california_housing()
|
| 107 |
+
X = pd.DataFrame(data.data, columns=data.feature_names)
|
| 108 |
+
y = data.target
|
| 109 |
+
|
| 110 |
+
elif dataset_name == "Diabetes":
|
| 111 |
+
data = load_diabetes()
|
| 112 |
+
X = pd.DataFrame(data.data, columns=data.feature_names)
|
| 113 |
+
y = data.target
|
| 114 |
+
|
| 115 |
+
elif dataset_name == "Boston Housing":
|
| 116 |
+
X, y = load_boston()
|
| 117 |
+
|
| 118 |
models = {
|
| 119 |
"Linear Regression": LinearRegression(),
|
|
|
|
| 120 |
"Decision Tree": DecisionTreeRegressor(),
|
| 121 |
+
"Random Forest": RandomForestRegressor(),
|
| 122 |
+
"SVR": SVR(),
|
| 123 |
}
|
| 124 |
|
| 125 |
+
# =========================
|
| 126 |
+
# SPLIT + SCALE
|
| 127 |
+
# =========================
|
| 128 |
+
|
| 129 |
+
X_train, X_test, y_train, y_test = train_test_split(
|
| 130 |
+
X,
|
| 131 |
+
y,
|
| 132 |
+
test_size=0.2,
|
| 133 |
+
random_state=42,
|
| 134 |
+
)
|
| 135 |
+
|
| 136 |
+
scaler = StandardScaler()
|
| 137 |
+
|
| 138 |
+
X_train = scaler.fit_transform(X_train)
|
| 139 |
+
X_test = scaler.transform(X_test)
|
| 140 |
+
|
| 141 |
+
# =========================
|
| 142 |
+
# TRAIN MODELS
|
| 143 |
+
# =========================
|
| 144 |
+
|
| 145 |
results = []
|
| 146 |
|
|
|
|
| 147 |
for name, model in models.items():
|
| 148 |
|
| 149 |
model.fit(X_train, y_train)
|
|
|
|
| 151 |
predictions = model.predict(X_test)
|
| 152 |
|
| 153 |
# Classification Metrics
|
| 154 |
+
if task_type == "Classification":
|
| 155 |
|
| 156 |
accuracy = accuracy_score(y_test, predictions)
|
| 157 |
+
f1 = f1_score(y_test, predictions, average="weighted")
|
| 158 |
|
| 159 |
+
results.append([name, accuracy, f1])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
|
| 161 |
# Regression Metrics
|
| 162 |
else:
|
| 163 |
|
| 164 |
mse = mean_squared_error(y_test, predictions)
|
|
|
|
| 165 |
r2 = r2_score(y_test, predictions)
|
| 166 |
|
| 167 |
+
results.append([name, mse, r2])
|
| 168 |
+
|
| 169 |
+
# =========================
|
| 170 |
+
# RESULTS TABLE
|
| 171 |
+
# =========================
|
| 172 |
|
| 173 |
+
if task_type == "Classification":
|
|
|
|
| 174 |
|
| 175 |
results_df = pd.DataFrame(
|
| 176 |
results,
|
|
|
|
| 178 |
)
|
| 179 |
|
| 180 |
best_model = results_df.loc[
|
| 181 |
+
results_df["Accuracy"].idxmax(),
|
| 182 |
+
"Model"
|
| 183 |
+
]
|
| 184 |
|
| 185 |
else:
|
| 186 |
|
|
|
|
| 190 |
)
|
| 191 |
|
| 192 |
best_model = results_df.loc[
|
| 193 |
+
results_df["MSE"].idxmin(),
|
| 194 |
+
"Model"
|
| 195 |
+
]
|
| 196 |
|
| 197 |
return results_df, f"🏆 Best Model: {best_model}"
|
| 198 |
|
| 199 |
|
| 200 |
+
# =====================================================
|
| 201 |
+
# UPDATE DATASET OPTIONS
|
| 202 |
+
# =====================================================
|
| 203 |
|
| 204 |
def update_datasets(task_type):
|
| 205 |
|
| 206 |
+
if task_type == "Classification":
|
| 207 |
|
| 208 |
return gr.Dropdown(
|
| 209 |
choices=[
|
| 210 |
"Iris",
|
| 211 |
+
"Breast Cancer",
|
| 212 |
+
"Titanic"
|
| 213 |
],
|
| 214 |
value="Iris"
|
| 215 |
)
|
|
|
|
| 219 |
return gr.Dropdown(
|
| 220 |
choices=[
|
| 221 |
"California Housing",
|
| 222 |
+
"Diabetes",
|
| 223 |
+
"Boston Housing"
|
| 224 |
],
|
| 225 |
value="California Housing"
|
| 226 |
)
|
| 227 |
|
| 228 |
|
| 229 |
+
# =====================================================
|
| 230 |
+
# GRADIO UI
|
| 231 |
+
# =====================================================
|
| 232 |
|
| 233 |
with gr.Blocks() as demo:
|
| 234 |
|
| 235 |
+
gr.Markdown("# AI Model Comparison App")
|
| 236 |
|
| 237 |
task_type = gr.Radio(
|
| 238 |
+
choices=["Classification", "Regression"],
|
| 239 |
+
value="Classification",
|
| 240 |
+
label="Select Task Type"
|
| 241 |
)
|
| 242 |
|
| 243 |
dataset_name = gr.Dropdown(
|
| 244 |
choices=[
|
| 245 |
"Iris",
|
| 246 |
+
"Breast Cancer",
|
| 247 |
+
"Titanic"
|
| 248 |
],
|
| 249 |
+
value="Iris",
|
| 250 |
+
label="Select Dataset"
|
| 251 |
)
|
| 252 |
|
| 253 |
task_type.change(
|
|
|
|
| 258 |
|
| 259 |
run_button = gr.Button("Run Models")
|
| 260 |
|
| 261 |
+
output_table = gr.Dataframe()
|
| 262 |
|
| 263 |
+
output_text = gr.Textbox()
|
| 264 |
|
| 265 |
run_button.click(
|
| 266 |
fn=run_models,
|
| 267 |
inputs=[task_type, dataset_name],
|
| 268 |
+
outputs=[output_table, output_text]
|
| 269 |
)
|
| 270 |
|
|
|
|
| 271 |
demo.launch()
|