Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,69 +3,75 @@ import pandas as pd
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.models import Sequential
|
| 5 |
from tensorflow.keras.layers import Dense
|
| 6 |
-
from tensorflow.keras.utils import to_categorical
|
| 7 |
from sklearn.preprocessing import StandardScaler
|
| 8 |
from sklearn.model_selection import train_test_split
|
|
|
|
| 9 |
import pickle
|
|
|
|
| 10 |
|
| 11 |
def load_data(files):
|
| 12 |
datasets = [pd.read_excel(file) for file in files]
|
| 13 |
combined_data = pd.concat(datasets, ignore_index=True)
|
| 14 |
return combined_data
|
| 15 |
|
| 16 |
-
def train_model(data, target_column,
|
| 17 |
-
# Prepare data
|
| 18 |
X = data.drop(target_column, axis=1)
|
| 19 |
y = data[target_column]
|
| 20 |
if y.dtype == 'object' or len(y.unique()) > 2:
|
| 21 |
y = pd.get_dummies(y)
|
| 22 |
|
| 23 |
-
|
| 24 |
scaler = StandardScaler()
|
| 25 |
-
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size/100)
|
| 26 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 27 |
X_test_scaled = scaler.transform(X_test)
|
| 28 |
|
| 29 |
-
|
| 30 |
-
model
|
| 31 |
-
for
|
| 32 |
-
model.add(Dense(
|
| 33 |
model.add(Dense(y.shape[1], activation='softmax'))
|
| 34 |
-
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 35 |
|
| 36 |
-
|
| 37 |
-
history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size,
|
| 38 |
-
accuracy =
|
| 39 |
|
| 40 |
-
return
|
| 41 |
|
| 42 |
-
def
|
| 43 |
-
|
|
|
|
| 44 |
prediction = model.predict(input_scaled)
|
| 45 |
-
predicted_class = np.argmax(prediction, axis=1)
|
| 46 |
-
return predicted_class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
-
# Gradio interface setup
|
| 49 |
with gr.Blocks() as app:
|
| 50 |
with gr.Row():
|
| 51 |
with gr.Column():
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
train_btn = gr.Button("Train Model")
|
| 60 |
-
|
| 61 |
with gr.Column():
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
-
|
| 71 |
-
app.launch()
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
from tensorflow.keras.models import Sequential
|
| 5 |
from tensorflow.keras.layers import Dense
|
|
|
|
| 6 |
from sklearn.preprocessing import StandardScaler
|
| 7 |
from sklearn.model_selection import train_test_split
|
| 8 |
+
from tensorflow.keras.utils import to_categorical
|
| 9 |
import pickle
|
| 10 |
+
import matplotlib.pyplot as plt
|
| 11 |
|
| 12 |
def load_data(files):
|
| 13 |
datasets = [pd.read_excel(file) for file in files]
|
| 14 |
combined_data = pd.concat(datasets, ignore_index=True)
|
| 15 |
return combined_data
|
| 16 |
|
| 17 |
+
def train_model(data, target_column, layers_info, test_size, epochs, batch_size):
|
|
|
|
| 18 |
X = data.drop(target_column, axis=1)
|
| 19 |
y = data[target_column]
|
| 20 |
if y.dtype == 'object' or len(y.unique()) > 2:
|
| 21 |
y = pd.get_dummies(y)
|
| 22 |
|
| 23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
|
| 24 |
scaler = StandardScaler()
|
|
|
|
| 25 |
X_train_scaled = scaler.fit_transform(X_train)
|
| 26 |
X_test_scaled = scaler.transform(X_test)
|
| 27 |
|
| 28 |
+
model = Sequential()
|
| 29 |
+
model.add(Dense(layers_info[0][1], activation=layers_info[0][0], input_dim=X_train_scaled.shape[1]))
|
| 30 |
+
for activation, neurons in layers_info[1:]:
|
| 31 |
+
model.add(Dense(neurons, activation=activation))
|
| 32 |
model.add(Dense(y.shape[1], activation='softmax'))
|
|
|
|
| 33 |
|
| 34 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 35 |
+
history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size, verbose=0)
|
| 36 |
+
accuracy = model.evaluate(X_test_scaled, y_test, verbose=0)[1]
|
| 37 |
|
| 38 |
+
return model, scaler, accuracy
|
| 39 |
|
| 40 |
+
def make_predictions(model, scaler, input_data):
|
| 41 |
+
input_df = pd.DataFrame([input_data], columns=input_data.keys())
|
| 42 |
+
input_scaled = scaler.transform(input_df)
|
| 43 |
prediction = model.predict(input_scaled)
|
| 44 |
+
predicted_class = np.argmax(prediction, axis=1)
|
| 45 |
+
return predicted_class[0]
|
| 46 |
+
|
| 47 |
+
def plot_data_distribution(data, target_column):
|
| 48 |
+
plt.figure(figsize=(10, 5))
|
| 49 |
+
data[target_column].value_counts().plot(kind='bar')
|
| 50 |
+
plt.title("Distribution of Target Variable")
|
| 51 |
+
plt.xlabel("Classes")
|
| 52 |
+
plt.ylabel("Frequency")
|
| 53 |
+
plt.grid(True)
|
| 54 |
+
plt.tight_layout()
|
| 55 |
+
return plt
|
| 56 |
|
|
|
|
| 57 |
with gr.Blocks() as app:
|
| 58 |
with gr.Row():
|
| 59 |
with gr.Column():
|
| 60 |
+
file_input = gr.File(label="Upload one or more datasets", type="file", accept_multiple_files=True)
|
| 61 |
+
target_input = gr.Textbox(label="Enter target column name")
|
| 62 |
+
layers_input = gr.Dynamic(label="Add Layer", value=["relu", 10])
|
| 63 |
+
test_size_input = gr.Slider(minimum=0.1, maximum=0.5, step=0.05, label="Test Size")
|
| 64 |
+
epochs_input = gr.Slider(minimum=10, maximum=100, step=10, label="Epochs")
|
| 65 |
+
batch_size_input = gr.Slider(minimum=10, maximum=128, step=10, label="Batch Size")
|
| 66 |
+
train_button = gr.Button("Train Model")
|
|
|
|
|
|
|
| 67 |
with gr.Column():
|
| 68 |
+
predict_button = gr.Button("Predict")
|
| 69 |
+
prediction_output = gr.Number(label="Predicted Class")
|
| 70 |
+
accuracy_output = gr.Number(label="Model Accuracy")
|
| 71 |
+
plot_output = gr.Plot(label="Data Distribution")
|
| 72 |
|
| 73 |
+
file_input.change(load_data, inputs=file_input, outputs=target_input)
|
| 74 |
+
train_button.click(train_model, inputs=[file_input, target_input, layers_input, test_size_input, epochs_input, batch_size_input], outputs=[prediction_output, plot_output, accuracy_output])
|
| 75 |
+
predict_button.click(make_predictions, inputs=[file_input, target_input, layers_input, test_size_input, epochs_input, batch_size_input], outputs=prediction_output)
|
| 76 |
|
| 77 |
+
app.launch()
|
|
|