Spaces:
Runtime error
Runtime error
| import streamlit as st | |
| import pandas as pd | |
| import numpy as np | |
| import joblib | |
| # Load the saved model and scaler | |
| def load_model_and_scaler(): | |
| model = joblib.load('gradient_boosting_model.pkl') | |
| scaler = joblib.load('scaler.pkl') | |
| return model, scaler | |
| # List of features used in training | |
| FEATURES = [ | |
| "Longitude", "Latitude", "Speed", "Distance", "Acc X", "Acc Y", "Acc Z", "Heading", | |
| "gyro_x", "gyro_y", "gyro_z", "Acc_Magnitude", "Acc_Change", "Gyro_Magnitude", | |
| "Gyro_Change", "Net_Displacement", "Speed_Change", "Heading_Change", | |
| "Rolling_Acc_Mean", "Rolling_Acc_STD", "acc_mean", "acc_std", "gyro_mean", "gyro_std" | |
| ] | |
| # Streamlit app | |
| def main(): | |
| st.title("Anomaly Detection with Gradient Boosting") | |
| st.write("This app uses a pre-trained Gradient Boosting model to detect anomalies in data.") | |
| # Load the model and scaler | |
| model, scaler = load_model_and_scaler() | |
| # Sidebar for navigation | |
| menu = ["Overview", "Test with Custom Input", "Batch Prediction"] | |
| choice = st.sidebar.selectbox("Menu", menu) | |
| if choice == "Overview": | |
| st.subheader("Overview") | |
| st.write(""" | |
| This application uses a Gradient Boosting Classifier trained on various features related to sensor data, | |
| including accelerometer, gyroscope, speed, and heading information. It predicts whether a given sample | |
| is classified as 'Normal' or 'Anomaly.' | |
| """) | |
| # Display model performance metrics | |
| metrics = pd.read_csv("model_metrics.csv") | |
| st.table(metrics) | |
| elif choice == "Test with Custom Input": | |
| st.subheader("Test with Custom Input") | |
| st.write("Provide input values for the features:") | |
| # Collect user input for each feature | |
| user_input = {} | |
| for feature in FEATURES: | |
| user_input[feature] = st.number_input(f"{feature}", value=0.0) | |
| # Convert the user input to a DataFrame | |
| user_input_df = pd.DataFrame([user_input]) | |
| if st.button("Predict"): | |
| # Scale the input data | |
| scaled_input = scaler.transform(user_input_df) | |
| # Predict using the model | |
| prediction = model.predict(scaled_input) | |
| prediction_proba = model.predict_proba(scaled_input) | |
| # Display the results | |
| result = "Anomaly" if prediction[0] == 1 else "Normal" | |
| st.write(f"Prediction: **{result}**") | |
| st.write(f"Prediction Probability: Normal - {prediction_proba[0][0]:.2f}, Anomaly - {prediction_proba[0][1]:.2f}") | |
| elif choice == "Batch Prediction": | |
| st.subheader("Batch Prediction") | |
| st.write("Upload a CSV file containing the following columns:") | |
| st.write(FEATURES) | |
| # File upload for batch prediction | |
| uploaded_file = st.file_uploader("Upload CSV", type=["csv"]) | |
| if uploaded_file is not None: | |
| # Read the uploaded CSV file | |
| batch_data = pd.read_csv(uploaded_file) | |
| st.write("Uploaded Data Preview:") | |
| st.dataframe(batch_data) | |
| # Check if the uploaded data has the required features | |
| if not set(FEATURES).issubset(batch_data.columns): | |
| st.error("Uploaded file does not contain all the required features.") | |
| else: | |
| # Scale the data | |
| batch_scaled = scaler.transform(batch_data[FEATURES]) | |
| # Predict using the model | |
| batch_predictions = model.predict(batch_scaled) | |
| batch_data['Prediction'] = np.where(batch_predictions == 1, "Anomaly", "Normal") | |
| # Display predictions | |
| st.write("Predictions:") | |
| st.dataframe(batch_data[['Prediction']]) | |
| # Download the results as a CSV | |
| csv = batch_data.to_csv(index=False) | |
| st.download_button( | |
| label="Download Predictions", | |
| data=csv, | |
| file_name="batch_predictions.csv", | |
| mime="text/csv" | |
| ) | |
| if __name__ == "__main__": | |
| main() | |