Spaces:
Sleeping
Sleeping
Download app.py from Allmen/Occupancy-Detection: direct link, hf CLI and curl.
- Browser
- Download file 2.28 kB
-
https://huggingface.co/spaces/Allmen/Occupancy-Detection/resolve/main/app.py
- Command line
-
hf download hf://spaces/Allmen/Occupancy-Detection/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Allmen/Occupancy-Detection/resolve/main/app.py
2.28 kB
| import gradio as gr | |
| import pandas as pd | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| from sklearn.preprocessing import MinMaxScaler | |
| import tensorflow as tf | |
| import tensorflow_probability as tfp | |
| from sklearn.metrics import accuracy_score | |
| # Load the dataset | |
| df = pd.read_csv('./Dataset/datatraining.csv') | |
| scaler = MinMaxScaler() | |
| def prepare_dataset(df): | |
| X = df[['Temperature', 'Humidity', 'Light', 'CO2']].values | |
| y = df['Occupancy'].values | |
| X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.15, random_state=42) | |
| X_train_scaled = scaler.fit_transform(X_train) | |
| X_test_scaled = scaler.transform(X_test) | |
| return X_train_scaled, X_test_scaled, y_train, y_test | |
| X_train_scaled, X_test_scaled, y_train, y_test = prepare_dataset(df) | |
| # Build the Bayesian Neural Network model | |
| def build_bnn_model(input_shape): | |
| model = tf.keras.models.Sequential([ | |
| tf.keras.layers.Input(shape=(input_shape,)), | |
| tf.keras.layers.BatchNormalization(), | |
| tfp.layers.DenseFlipout(64, activation='relu'), | |
| tf.keras.layers.BatchNormalization(), | |
| tfp.layers.DenseFlipout(64, activation='relu'), | |
| tf.keras.layers.BatchNormalization(), | |
| tfp.layers.DenseFlipout(1, activation='sigmoid'), | |
| ]) | |
| model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.01)) | |
| return model | |
| bnn_model = build_bnn_model(input_shape=X_train_scaled.shape[1]) | |
| # Train the model | |
| bnn_model.fit(X_train_scaled, y_train, epochs=50, verbose=0) | |
| def predict_occupancy(Temperature, Humidity, Light, CO2): | |
| input_data = np.array([[Temperature, Humidity, Light, CO2]]) | |
| # Reshape the input data to 2-dimensional array | |
| input_data_reshaped = input_data.reshape(1, -1) | |
| input_data_scaled = scaler.transform(input_data_reshaped) | |
| y_pred = bnn_model.predict(input_data_scaled)[0][0] | |
| return 'Occupied' if y_pred >= 0.5 else 'Not Occupied' | |
| iface = gr.Interface( | |
| fn=predict_occupancy, | |
| inputs=['number', 'number', 'number', 'number'], | |
| outputs='text', | |
| title="Enhancing Building Energy Efficiency through Regularized Bayesian Neural Networks for Precise Occupancy Detection and Energy Optimization", | |
| description="Predict occupancy status based on sensor readings." | |
| ) | |
| iface.launch() |