Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,75 +3,72 @@ 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 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
|
| 13 |
-
datasets = [pd.read_excel(file) for file in
|
| 14 |
combined_data = pd.concat(datasets, ignore_index=True)
|
| 15 |
return combined_data
|
| 16 |
|
| 17 |
-
def
|
| 18 |
-
X =
|
| 19 |
-
y =
|
|
|
|
| 20 |
if y.dtype == 'object' or len(y.unique()) > 2:
|
| 21 |
-
y = pd.
|
| 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(
|
| 30 |
-
for
|
| 31 |
model.add(Dense(neurons, activation=activation))
|
| 32 |
model.add(Dense(y.shape[1], activation='softmax'))
|
| 33 |
|
| 34 |
-
model.compile(optimizer=
|
| 35 |
-
history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size,
|
| 36 |
-
|
|
|
|
| 37 |
|
| 38 |
-
return model, scaler, accuracy
|
| 39 |
|
| 40 |
-
def
|
| 41 |
-
|
| 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
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 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 |
-
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
|
|
|
| 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_and_prepare_data(uploaded_files):
|
| 12 |
+
datasets = [pd.read_excel(file) for file in uploaded_files]
|
| 13 |
combined_data = pd.concat(datasets, ignore_index=True)
|
| 14 |
return combined_data
|
| 15 |
|
| 16 |
+
def build_and_train_model(combined_data, target_column, num_layers, neurons_per_layer, activations, optimizer, epochs, batch_size, test_size):
|
| 17 |
+
X = combined_data.drop(target_column, axis=1)
|
| 18 |
+
y = combined_data[target_column]
|
| 19 |
+
|
| 20 |
if y.dtype == 'object' or len(y.unique()) > 2:
|
| 21 |
+
y = to_categorical(pd.factorize(y)[0])
|
| 22 |
|
| 23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size/100, 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(neurons_per_layer[0], activation=activations[0], input_shape=(X_train_scaled.shape[1],)))
|
| 30 |
+
for neurons, activation in zip(neurons_per_layer[1:], activations[1:]):
|
| 31 |
model.add(Dense(neurons, activation=activation))
|
| 32 |
model.add(Dense(y.shape[1], activation='softmax'))
|
| 33 |
|
| 34 |
+
model.compile(optimizer=optimizer, loss='categorical_crossentropy', metrics=['accuracy'])
|
| 35 |
+
history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2)
|
| 36 |
+
evaluation = model.evaluate(X_test_scaled, y_test)
|
| 37 |
+
accuracy = evaluation[1] * 100
|
| 38 |
|
| 39 |
+
return model, scaler, accuracy, history.history
|
| 40 |
|
| 41 |
+
def predict(model, scaler, input_features):
|
| 42 |
+
input_scaled = scaler.transform([input_features])
|
|
|
|
| 43 |
prediction = model.predict(input_scaled)
|
| 44 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 45 |
+
return predicted_class
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
+
def main(uploaded_files, target_column, num_layers, neurons, activations, optimizer, epochs, batch_size, test_size, input_features):
|
| 48 |
+
combined_data = load_and_prepare_data(uploaded_files)
|
| 49 |
+
model, scaler, accuracy, history = build_and_train_model(combined_data, target_column, num_layers, neurons, activations, optimizer, epochs, batch_size, test_size)
|
| 50 |
+
predicted_class = predict(model, scaler, input_features)
|
| 51 |
+
return predicted_class, accuracy, history
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
+
iface = gr.Interface(
|
| 54 |
+
fn=main,
|
| 55 |
+
inputs=[
|
| 56 |
+
gr.inputs.File(label="Upload one or more datasets", type="file", accept_multiple_files=True),
|
| 57 |
+
gr.inputs.Textbox(label="Target Column Name"),
|
| 58 |
+
gr.inputs.Slider(label="Number of Hidden Layers", minimum=1, maximum=5),
|
| 59 |
+
gr.inputs.Number(label="Neurons per Layer", default=64),
|
| 60 |
+
gr.inputs.Dropdown(label="Activation Functions", choices=["relu", "sigmoid", "tanh"], type="index"),
|
| 61 |
+
gr.inputs.Dropdown(label="Optimizer", choices=["adam", "sgd", "rmsprop"]),
|
| 62 |
+
gr.inputs.Slider(label="Epochs", minimum=10, maximum=100),
|
| 63 |
+
gr.inputs.Slider(label="Batch Size", minimum=10, maximum=100),
|
| 64 |
+
gr.inputs.Slider(label="Test Size (%)", minimum=10, maximum=50),
|
| 65 |
+
gr.inputs.Number(label="Input Features for Prediction")
|
| 66 |
+
],
|
| 67 |
+
outputs=[
|
| 68 |
+
gr.outputs.Textbox(label="Predicted Class"),
|
| 69 |
+
gr.outputs.Textbox(label="Model Accuracy"),
|
| 70 |
+
gr.outputs.Label(label="Training History")
|
| 71 |
+
]
|
| 72 |
+
)
|
| 73 |
|
| 74 |
+
iface.launch()
|