tejani commited on
Commit
bfaf0f2
·
verified ·
1 Parent(s): 00758c6

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +76 -19
app.py CHANGED
@@ -1,19 +1,23 @@
1
  import pandas as pd
2
  import numpy as np
3
- from sklearn.model_selection import train_test_split, GridSearchCV
4
  from xgboost import XGBClassifier
5
- from sklearn.metrics import accuracy_score, classification_report
6
  from sklearn.preprocessing import StandardScaler
7
  from imblearn.over_sampling import SMOTE
8
  import gradio as gr
 
 
9
  import io
 
 
10
  import warnings
11
  warnings.filterwarnings('ignore')
12
 
13
  # Function to load and preprocess data
14
- def load_and_preprocess_data(file_path):
15
  try:
16
- data = pd.read_csv(file_path)
17
 
18
  # Convert suits and ranks to numerical values
19
  suit_order = {'spades': 0, 'hearts': 1, 'clubs': 2, 'diamonds': 3}
@@ -27,9 +31,9 @@ def load_and_preprocess_data(file_path):
27
  data['Lion Suit Num'] = data['Lion Suit'].map(suit_order)
28
  data['Lion Rank Num'] = data['Lion Rank'].map(rank_order)
29
 
30
- return data
31
  except Exception as e:
32
- return f"Error loading data: {str(e)}"
33
 
34
  # Feature engineering
35
  def create_features(data, n_games=3):
@@ -67,17 +71,50 @@ def create_features(data, n_games=3):
67
  ['suit_mean', 'suit_std', 'rank_mean', 'rank_std'])
68
  return pd.DataFrame(features, columns=columns)
69
 
70
- # Training function
71
- def train_model(file_path, n_estimators, learning_rate, max_depth, subsample):
72
- output = io.StringIO()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73
 
74
  try:
75
  # Load and preprocess data
76
- data = load_and_preprocess_data(file_path)
77
- if isinstance(data, str):
78
- return data
 
79
 
80
  # Create features
 
81
  n_games = 3
82
  features = create_features(data, n_games)
83
  targets = {
@@ -90,12 +127,18 @@ def train_model(file_path, n_estimators, learning_rate, max_depth, subsample):
90
  }
91
 
92
  # Scale features
 
93
  scaler = StandardScaler()
94
  features_scaled = scaler.fit_transform(features)
95
  features_scaled = pd.DataFrame(features_scaled, columns=features.columns)
96
 
97
- results = []
98
- for target_name, target in targets.items():
 
 
 
 
 
99
  # Split data
100
  X_train, X_test, y_train, y_test = train_test_split(
101
  features_scaled, target, test_size=0.2, random_state=42
@@ -127,20 +170,30 @@ def train_model(file_path, n_estimators, learning_rate, max_depth, subsample):
127
  y_pred = model.predict(X_test)
128
  accuracy = accuracy_score(y_test, y_pred)
129
  report = classification_report(y_test, y_pred, zero_division=0)
 
130
 
131
  results.append(f"**{target_name} Results**\n")
132
  results.append(f"Accuracy: {accuracy:.2f}\n")
133
  results.append(f"Classification Report:\n{report}\n")
 
 
 
 
 
 
 
 
134
 
135
- return "\n".join(results)
 
136
 
137
  except Exception as e:
138
- return f"Error during training: {str(e)}"
139
 
140
  # Gradio interface
141
  with gr.Blocks() as demo:
142
  gr.Markdown("# Card Game Prediction Model Training")
143
- gr.Markdown("Upload the training dataset and configure hyperparameters to train the model.")
144
 
145
  file_input = gr.File(label="Upload TRAINING_CARD_DATA.csv")
146
  n_estimators = gr.Slider(50, 300, value=100, step=10, label="Number of Estimators")
@@ -149,12 +202,16 @@ with gr.Blocks() as demo:
149
  subsample = gr.Slider(0.5, 1.0, value=0.8, step=0.1, label="Subsample")
150
 
151
  train_button = gr.Button("Train Model")
152
- output = gr.Textbox(label="Training Results")
 
 
 
153
 
154
  train_button.click(
155
  fn=train_model,
156
  inputs=[file_input, n_estimators, learning_rate, max_depth, subsample],
157
- outputs=output
 
158
  )
159
 
160
  demo.launch()
 
1
  import pandas as pd
2
  import numpy as np
3
+ from sklearn.model_selection import train_test_split
4
  from xgboost import XGBClassifier
5
+ from sklearn.metrics import accuracy_score, classification_report, confusion_matrix
6
  from sklearn.preprocessing import StandardScaler
7
  from imblearn.over_sampling import SMOTE
8
  import gradio as gr
9
+ import matplotlib.pyplot as plt
10
+ import seaborn as sns
11
  import io
12
+ import base64
13
+ from PIL import Image
14
  import warnings
15
  warnings.filterwarnings('ignore')
16
 
17
  # Function to load and preprocess data
18
+ def load_and_preprocess_data(file):
19
  try:
20
+ data = pd.read_csv(file.name)
21
 
22
  # Convert suits and ranks to numerical values
23
  suit_order = {'spades': 0, 'hearts': 1, 'clubs': 2, 'diamonds': 3}
 
31
  data['Lion Suit Num'] = data['Lion Suit'].map(suit_order)
32
  data['Lion Rank Num'] = data['Lion Rank'].map(rank_order)
33
 
34
+ return data, None
35
  except Exception as e:
36
+ return None, f"Error loading data: {str(e)}"
37
 
38
  # Feature engineering
39
  def create_features(data, n_games=3):
 
71
  ['suit_mean', 'suit_std', 'rank_mean', 'rank_std'])
72
  return pd.DataFrame(features, columns=columns)
73
 
74
+ # Function to plot confusion matrix
75
+ def plot_confusion_matrix(y_true, y_pred, title):
76
+ cm = confusion_matrix(y_true, y_pred)
77
+ plt.figure(figsize=(6, 4))
78
+ sns.heatmap(cm, annot=True, fmt='d', cmap='Blues')
79
+ plt.title(title)
80
+ plt.xlabel('Predicted')
81
+ plt.ylabel('Actual')
82
+ buf = io.BytesIO()
83
+ plt.savefig(buf, format='png')
84
+ buf.seek(0)
85
+ img = Image.open(buf)
86
+ plt.close()
87
+ return img
88
+
89
+ # Function to plot accuracy bar chart
90
+ def plot_accuracy_chart(accuracies):
91
+ plt.figure(figsize=(8, 5))
92
+ plt.bar(accuracies.keys(), accuracies.values(), color='skyblue')
93
+ plt.title('Model Accuracy Comparison')
94
+ plt.ylabel('Accuracy')
95
+ plt.xticks(rotation=45)
96
+ plt.ylim(0, 1)
97
+ buf = io.BytesIO()
98
+ plt.savefig(buf, format='png')
99
+ buf.seek(0)
100
+ img = Image.open(buf)
101
+ plt.close()
102
+ return img
103
+
104
+ # Training function with progress tracking
105
+ def train_model(file, n_estimators, learning_rate, max_depth, subsample, progress=gr.Progress()):
106
+ progress(0, desc="Starting...")
107
+ results = []
108
 
109
  try:
110
  # Load and preprocess data
111
+ progress(0.1, desc="Loading and preprocessing data...")
112
+ data, error = load_and_preprocess_data(file)
113
+ if error:
114
+ return error, None, None
115
 
116
  # Create features
117
+ progress(0.2, desc="Engineering features...")
118
  n_games = 3
119
  features = create_features(data, n_games)
120
  targets = {
 
127
  }
128
 
129
  # Scale features
130
+ progress(0.3, desc="Scaling features...")
131
  scaler = StandardScaler()
132
  features_scaled = scaler.fit_transform(features)
133
  features_scaled = pd.DataFrame(features_scaled, columns=features.columns)
134
 
135
+ accuracies = {}
136
+ confusion_matrices = []
137
+
138
+ # Train models
139
+ for i, (target_name, target) in enumerate(targets.items()):
140
+ progress(0.4 + (i / len(targets)) * 0.4, desc=f"Training {target_name} model...")
141
+
142
  # Split data
143
  X_train, X_test, y_train, y_test = train_test_split(
144
  features_scaled, target, test_size=0.2, random_state=42
 
170
  y_pred = model.predict(X_test)
171
  accuracy = accuracy_score(y_test, y_pred)
172
  report = classification_report(y_test, y_pred, zero_division=0)
173
+ accuracies[target_name] = accuracy
174
 
175
  results.append(f"**{target_name} Results**\n")
176
  results.append(f"Accuracy: {accuracy:.2f}\n")
177
  results.append(f"Classification Report:\n{report}\n")
178
+
179
+ # Generate confusion matrix plot
180
+ cm_plot = plot_confusion_matrix(y_test, y_pred, f"Confusion Matrix - {target_name}")
181
+ confusion_matrices.append(cm_plot)
182
+
183
+ progress(0.9, desc="Generating visualizations...")
184
+ # Generate accuracy bar chart
185
+ accuracy_plot = plot_accuracy_chart(accuracies)
186
 
187
+ progress(1.0, desc="Completed!")
188
+ return "\n".join(results), accuracy_plot, confusion_matrices
189
 
190
  except Exception as e:
191
+ return f"Error during training: {str(e)}", None, None
192
 
193
  # Gradio interface
194
  with gr.Blocks() as demo:
195
  gr.Markdown("# Card Game Prediction Model Training")
196
+ gr.Markdown("Upload the training dataset and configure hyperparameters to train the model. Track progress and view results with visualizations.")
197
 
198
  file_input = gr.File(label="Upload TRAINING_CARD_DATA.csv")
199
  n_estimators = gr.Slider(50, 300, value=100, step=10, label="Number of Estimators")
 
202
  subsample = gr.Slider(0.5, 1.0, value=0.8, step=0.1, label="Subsample")
203
 
204
  train_button = gr.Button("Train Model")
205
+
206
+ output_text = gr.Textbox(label="Training Results")
207
+ accuracy_plot = gr.Image(label="Accuracy Comparison")
208
+ confusion_plots = gr.Gallery(label="Confusion Matrices")
209
 
210
  train_button.click(
211
  fn=train_model,
212
  inputs=[file_input, n_estimators, learning_rate, max_depth, subsample],
213
+ outputs=[output_text, accuracy_plot, confusion_plots],
214
+ _js="() => {return {progress: true}}"
215
  )
216
 
217
  demo.launch()