saja003 commited on
Commit
bf8331d
·
verified ·
1 Parent(s): a1f71ce

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +252 -0
app.py ADDED
@@ -0,0 +1,252 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
13
+ 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
+ # Load Dataset
38
+ # ==================================================
39
+
40
+ def load_dataset(task_type, dataset_name):
41
+
42
+ # Classification datasets
43
+ if task_type == "classification":
44
+
45
+ if dataset_name == "Iris":
46
+ data = load_iris(as_frame=True)
47
+
48
+ elif dataset_name == "Breast Cancer":
49
+ data = load_breast_cancer(as_frame=True)
50
+
51
+ else:
52
+ return None, None
53
+
54
+ # Regression datasets
55
+ else:
56
+
57
+ if dataset_name == "California Housing":
58
+ data = fetch_california_housing(as_frame=True)
59
+
60
+ elif dataset_name == "Diabetes":
61
+ data = load_diabetes(as_frame=True)
62
+
63
+ else:
64
+ return None, None
65
+
66
+ X = data.data
67
+ y = data.target
68
+
69
+ return X, y
70
+
71
+
72
+ # ==================================================
73
+ # Main Function
74
+ # ==================================================
75
+
76
+ def run_models(task_type, dataset_name):
77
+
78
+ # Load dataset
79
+ X, y = load_dataset(task_type, dataset_name)
80
+
81
+ if X is None:
82
+ return "Invalid dataset selection", ""
83
+
84
+ # Split
85
+ X_train, X_test, y_train, y_test = train_test_split(
86
+ X,
87
+ y,
88
+ test_size=0.2,
89
+ random_state=42
90
+ )
91
+
92
+ # Scaling
93
+ scaler = StandardScaler()
94
+
95
+ X_train = scaler.fit_transform(X_train)
96
+ X_test = scaler.transform(X_test)
97
+
98
+ # Classification Models
99
+ if task_type == "classification":
100
+
101
+ models = {
102
+ "Logistic Regression": LogisticRegression(max_iter=1000),
103
+ "SVM": SVC(),
104
+ "Decision Tree": DecisionTreeClassifier(),
105
+ "Random Forest": RandomForestClassifier()
106
+ }
107
+
108
+ # Regression Models
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)
124
+
125
+ predictions = model.predict(X_test)
126
+
127
+ # Classification Metrics
128
+ if task_type == "classification":
129
+
130
+ accuracy = accuracy_score(y_test, predictions)
131
+
132
+ f1 = f1_score(
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
+ name,
153
+ round(mse, 4),
154
+ round(r2, 4)
155
+ ])
156
+
157
+ # Results DataFrame
158
+ if task_type == "classification":
159
+
160
+ results_df = pd.DataFrame(
161
+ results,
162
+ columns=["Model", "Accuracy", "F1 Score"]
163
+ )
164
+
165
+ best_model = results_df.loc[
166
+ results_df["Accuracy"].idxmax()
167
+ ]["Model"]
168
+
169
+ else:
170
+
171
+ results_df = pd.DataFrame(
172
+ results,
173
+ columns=["Model", "MSE", "R2 Score"]
174
+ )
175
+
176
+ best_model = results_df.loc[
177
+ results_df["R2 Score"].idxmax()
178
+ ]["Model"]
179
+
180
+ return results_df, f"🏆 Best Model: {best_model}"
181
+
182
+
183
+ # ==================================================
184
+ # Update Dataset Choices
185
+ # ==================================================
186
+
187
+ def update_datasets(task_type):
188
+
189
+ if task_type == "classification":
190
+
191
+ return gr.Dropdown(
192
+ choices=[
193
+ "Iris",
194
+ "Breast Cancer"
195
+ ],
196
+ value="Iris"
197
+ )
198
+
199
+ else:
200
+
201
+ return gr.Dropdown(
202
+ choices=[
203
+ "California Housing",
204
+ "Diabetes"
205
+ ],
206
+ value="California Housing"
207
+ )
208
+
209
+
210
+ # ==================================================
211
+ # Gradio Interface
212
+ # ==================================================
213
+
214
+ with gr.Blocks() as demo:
215
+
216
+ gr.Markdown("# ML Model Comparison Tool")
217
+
218
+ task_type = gr.Radio(
219
+ ["classification", "regression"],
220
+ label="Select Task Type",
221
+ value="classification"
222
+ )
223
+
224
+ dataset_name = gr.Dropdown(
225
+ choices=[
226
+ "Iris",
227
+ "Breast Cancer"
228
+ ],
229
+ label="Select Dataset",
230
+ value="Iris"
231
+ )
232
+
233
+ task_type.change(
234
+ fn=update_datasets,
235
+ inputs=task_type,
236
+ outputs=dataset_name
237
+ )
238
+
239
+ run_button = gr.Button("Run Models")
240
+
241
+ results_output = gr.Dataframe(label="Results")
242
+
243
+ best_model_output = gr.Textbox(label="Best Model")
244
+
245
+ run_button.click(
246
+ fn=run_models,
247
+ inputs=[task_type, dataset_name],
248
+ outputs=[results_output, best_model_output]
249
+ )
250
+
251
+
252
+ demo.launch()