Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
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, layers, neurons, epochs, batch_size, test_size):
|
| 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 |
+
# Split and scale data
|
| 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 |
+
# Build the model
|
| 30 |
+
model = Sequential([Dense(neurons[0], activation='relu', input_shape=(X_train_scaled.shape[1],))])
|
| 31 |
+
for n in neurons[1:]:
|
| 32 |
+
model.add(Dense(n, activation='relu'))
|
| 33 |
+
model.add(Dense(y.shape[1], activation='softmax'))
|
| 34 |
+
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
|
| 35 |
+
|
| 36 |
+
# Train the model
|
| 37 |
+
history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size, validation_data=(X_test_scaled, y_test))
|
| 38 |
+
accuracy = history.history['accuracy'][-1]
|
| 39 |
+
|
| 40 |
+
return f'Training completed with accuracy: {accuracy*100:.2f}%'
|
| 41 |
+
|
| 42 |
+
def predict(model, scaler, features):
|
| 43 |
+
input_scaled = scaler.transform([features])
|
| 44 |
+
prediction = model.predict(input_scaled)
|
| 45 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 46 |
+
return predicted_class
|
| 47 |
+
|
| 48 |
+
# Gradio interface setup
|
| 49 |
+
with gr.Blocks() as app:
|
| 50 |
+
with gr.Row():
|
| 51 |
+
with gr.Column():
|
| 52 |
+
file_upload = gr.File(label="Upload one or more datasets", type="file", accept=".xlsx", multiple=True)
|
| 53 |
+
target_column = gr.Textbox(label="Target Column Name")
|
| 54 |
+
num_layers = gr.Slider(minimum=1, maximum=5, label="Number of Layers")
|
| 55 |
+
neurons_per_layer = gr.Textbox(label="Neurons per Layer (comma-separated)")
|
| 56 |
+
epochs = gr.Number(label="Epochs", value=50)
|
| 57 |
+
batch_size = gr.Number(label="Batch Size", value=32)
|
| 58 |
+
test_size = gr.Slider(minimum=5, maximum=95, step=5, label="Test Size (%)")
|
| 59 |
+
train_btn = gr.Button("Train Model")
|
| 60 |
+
|
| 61 |
+
with gr.Column():
|
| 62 |
+
prediction_input = gr.Number(label="Input Features for Prediction", value=0)
|
| 63 |
+
predict_btn = gr.Button("Predict")
|
| 64 |
+
output = gr.Textbox(label="Output")
|
| 65 |
+
|
| 66 |
+
file_upload.change(load_data, inputs=[file_upload], outputs=[output])
|
| 67 |
+
train_btn.click(train_model, inputs=[output, target_column, num_layers, neurons_per_layer, epochs, batch_size, test_size], outputs=[output])
|
| 68 |
+
predict_btn.click(predict, inputs=[model, scaler, prediction_input], outputs=[output])
|
| 69 |
+
|
| 70 |
+
if __name__ == "__main__":
|
| 71 |
+
app.launch()
|