Spaces:
Sleeping
Sleeping
Download app.py from ZainKhan007/Dynamic_Approach: direct link, hf CLI and curl.
- Browser
- Download file 3.52 kB
-
https://huggingface.co/spaces/ZainKhan007/Dynamic_Approach/resolve/main/app.py
- Command line
-
hf download hf://spaces/ZainKhan007/Dynamic_Approach/app.py
-
curl -L -o app.py https://huggingface.co/spaces/ZainKhan007/Dynamic_Approach/resolve/main/app.py
3.52 kB
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| from tensorflow.keras.models import Sequential, load_model | |
| from tensorflow.keras.layers import Dense | |
| from tensorflow.keras.utils import to_categorical | |
| from sklearn.preprocessing import StandardScaler | |
| from sklearn.model_selection import train_test_split | |
| import pickle | |
| import matplotlib.pyplot as plt | |
| # Streamlit app setup | |
| st.title("Dynamic Fault Detection Dashboard") | |
| # Sidebar for configuration | |
| st.sidebar.title("Configuration") | |
| # Dynamic Dataset Handling | |
| uploaded_files = st.sidebar.file_uploader("Upload one or more datasets", accept_multiple_files=True, type=["xlsx"]) | |
| datasets = [] | |
| if uploaded_files: | |
| for uploaded_file in uploaded_files: | |
| data = pd.read_excel(uploaded_file) | |
| datasets.append(data) | |
| if datasets: | |
| # Combine all uploaded datasets into one DataFrame | |
| combined_data = pd.concat(datasets, ignore_index=True) | |
| st.write("### Data Preview", combined_data.head()) | |
| # Select target column dynamically | |
| target_column = st.sidebar.selectbox("Select the target column", combined_data.columns) | |
| # Prepare the data | |
| X = combined_data.drop(target_column, axis=1) | |
| y = combined_data[target_column] | |
| if y.dtype == 'object' or len(y.unique()) > 2: | |
| y = pd.get_dummies(y) # One-hot encode the target variable | |
| # Data splitting and scaling | |
| test_size = st.sidebar.slider("Test Size (as %)", min_value=10, max_value=90, value=20, step=5) | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=test_size/100, random_state=42) | |
| scaler = StandardScaler() | |
| X_train_scaled = scaler.fit_transform(X_train) | |
| X_test_scaled = scaler.transform(X_test) | |
| # Model Configuration | |
| num_layers = st.sidebar.slider("Number of Hidden Layers", min_value=1, max_value=5, value=2) | |
| model = Sequential() | |
| first_layer_neurons = st.sidebar.slider("Neurons in First Layer", min_value=10, max_value=256, step=10, value=50) | |
| model.add(Dense(first_layer_neurons, activation='relu', input_shape=(X_train_scaled.shape[1],))) | |
| for i in range(1, num_layers): | |
| neurons = st.sidebar.slider(f"Neurons in Layer {i+1}", min_value=10, max_value=256, step=10, value=50) | |
| model.add(Dense(neurons, activation='relu')) | |
| model.add(Dense(y.shape[1], activation='softmax')) | |
| model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) | |
| # Model Training | |
| if st.sidebar.button("Train Model"): | |
| epochs = st.sidebar.slider("Number of Epochs", 50, 100, 10) | |
| batch_size = st.sidebar.slider("Batch Size", 10, 100, 32) | |
| history = model.fit(X_train_scaled, y_train, epochs=epochs, batch_size=batch_size, validation_split=0.2) | |
| st.success("Model trained successfully!") | |
| # Display accuracy and loss graphs | |
| st.line_chart(history.history['accuracy']) | |
| st.line_chart(history.history['val_accuracy']) | |
| # Prediction Section | |
| if st.button("Make Predictions"): | |
| input_features = [st.number_input(f"Input {col}", step=0.00001) for col in X.columns] | |
| input_scaled = scaler.transform([input_features]) | |
| prediction = model.predict(input_scaled) | |
| predicted_class = np.argmax(prediction, axis=1) | |
| st.write(f"Predicted Fault Class: {predicted_class}") | |
| # Visualizing input data distribution | |
| fig, ax = plt.subplots() | |
| combined_data[target_column].value_counts().plot(kind='bar', ax=ax) | |
| ax.set_title("Distribution of Target Variable") | |
| st.pyplot(fig) | |