Spaces:
Sleeping
Sleeping
Upload app5.py
Browse files
app5.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
from tensorflow.keras.models import Sequential, load_model
|
| 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 |
+
import matplotlib.pyplot as plt
|
| 11 |
+
|
| 12 |
+
# Streamlit app setup
|
| 13 |
+
st.title("Dynamic Fault Detection Dashboard")
|
| 14 |
+
|
| 15 |
+
# Sidebar for configuration
|
| 16 |
+
st.sidebar.title("Configuration")
|
| 17 |
+
|
| 18 |
+
# Dynamic Dataset Handling
|
| 19 |
+
uploaded_files = st.sidebar.file_uploader("Upload one or more datasets", accept_multiple_files=True, type=["xlsx"])
|
| 20 |
+
datasets = []
|
| 21 |
+
if uploaded_files:
|
| 22 |
+
for uploaded_file in uploaded_files:
|
| 23 |
+
data = pd.read_excel(uploaded_file)
|
| 24 |
+
datasets.append(data)
|
| 25 |
+
|
| 26 |
+
if datasets:
|
| 27 |
+
combined_data = pd.concat(datasets, ignore_index=True)
|
| 28 |
+
st.sidebar.header("Target Selection")
|
| 29 |
+
target_column = st.sidebar.selectbox("Select the target column:", combined_data.columns)
|
| 30 |
+
|
| 31 |
+
X = combined_data.drop(target_column, axis=1)
|
| 32 |
+
y = combined_data[target_column]
|
| 33 |
+
if y.dtype == 'object' or len(y.unique()) > 2:
|
| 34 |
+
y = to_categorical(pd.factorize(y)[0])
|
| 35 |
+
|
| 36 |
+
test_size = st.sidebar.slider("Test Size (as %)", 10, 50, 20) / 100
|
| 37 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size, random_state=42)
|
| 38 |
+
|
| 39 |
+
scaler = StandardScaler()
|
| 40 |
+
X_train_scaled = scaler.fit_transform(X_train)
|
| 41 |
+
X_test_scaled = scaler.transform(X_test)
|
| 42 |
+
|
| 43 |
+
with open("scaler.pkl", "wb") as f:
|
| 44 |
+
pickle.dump(scaler, f)
|
| 45 |
+
|
| 46 |
+
st.write("### Data Preview")
|
| 47 |
+
st.write(combined_data.head())
|
| 48 |
+
|
| 49 |
+
num_layers = st.sidebar.slider("Number of Hidden Layers", 1, 5, 2)
|
| 50 |
+
model = Sequential()
|
| 51 |
+
model.add(Dense(st.sidebar.slider(f"Number of Neurons (Layer 1)", 1, 256, 64), input_shape=(X_train_scaled.shape[1],), activation=st.sidebar.selectbox(f"Activation Function (Layer 1)", ["relu", "sigmoid", "tanh"], index=0)))
|
| 52 |
+
for i in range(1, num_layers):
|
| 53 |
+
model.add(Dense(st.sidebar.slider(f"Number of Neurons (Layer {i+1})", 1, 256, 64), activation=st.sidebar.selectbox(f"Activation Function (Layer {i+1})", ["relu", "sigmoid", "tanh"], index=0)))
|
| 54 |
+
model.add(Dense(y.shape[1], activation="softmax"))
|
| 55 |
+
|
| 56 |
+
model.compile(optimizer=st.sidebar.selectbox("Optimizer", ["adam", "sgd", "rmsprop"], index=0), loss="categorical_crossentropy", metrics=["accuracy"])
|
| 57 |
+
|
| 58 |
+
st.write("### Model Summary")
|
| 59 |
+
model.summary(print_fn=lambda x: st.text(x))
|
| 60 |
+
|
| 61 |
+
if st.sidebar.button("Train Model"):
|
| 62 |
+
with st.spinner("Training in progress..."):
|
| 63 |
+
history = model.fit(X_train_scaled, y_train, epochs=st.sidebar.slider("Epochs", 10, 100, 50), batch_size=st.sidebar.slider("Batch Size", 16, 128, 32), validation_split=0.2)
|
| 64 |
+
st.success("Model trained successfully!")
|
| 65 |
+
model.save("trained_model.h5")
|
| 66 |
+
st.write("### Training History")
|
| 67 |
+
st.line_chart({"Training Accuracy": history.history["accuracy"], "Validation Accuracy": history.history["val_accuracy"]})
|
| 68 |
+
test_loss, test_accuracy = model.evaluate(X_test_scaled, y_test)
|
| 69 |
+
st.write(f"### Test Accuracy: {test_accuracy * 100:.2f}%")
|
| 70 |
+
|
| 71 |
+
# Prediction Section
|
| 72 |
+
st.header("Make Predictions")
|
| 73 |
+
feature_names = X.columns.tolist() if datasets else []
|
| 74 |
+
input_data = [st.number_input(f"Enter value for {feature}", step=0.01) for feature in feature_names]
|
| 75 |
+
|
| 76 |
+
if st.button("Predict Fault Type"):
|
| 77 |
+
loaded_model = load_model("trained_model.h5")
|
| 78 |
+
loaded_scaler = pickle.load(open("scaler.pkl", "rb"))
|
| 79 |
+
input_scaled = loaded_scaler.transform([input_data])
|
| 80 |
+
prediction = loaded_model.predict(input_scaled)
|
| 81 |
+
predicted_class = np.argmax(prediction, axis=1)[0]
|
| 82 |
+
st.success(f"The predicted fault class is: {predicted_class}")
|
| 83 |
+
|
| 84 |
+
# Plot the entered data as a line graph
|
| 85 |
+
plt.figure(figsize=(10, 4))
|
| 86 |
+
plt.plot(feature_names, input_data, marker='o')
|
| 87 |
+
plt.title("Input Data Points")
|
| 88 |
+
plt.xlabel("Features")
|
| 89 |
+
plt.ylabel("Value")
|
| 90 |
+
plt.grid(True)
|
| 91 |
+
plt.xticks(rotation=45)
|
| 92 |
+
st.pyplot(plt)
|