# Import necessary libraries import gradio as gr import numpy as np import pandas as pd import tensorflow as tf from joblib import load try: custom_objects = {'mae': tf.keras.losses.MeanAbsoluteError()} model = tf.keras.models.load_model('elevator_health_model.h5', custom_objects=custom_objects) scaler = load('elevator_data_scaler.joblib') print("Model and scaler loaded successfully.") except Exception as e: print(f"Error loading model or scaler: {e}") model = None scaler = None def predict_health(ball_bearing, humidity, vibration): """ This function takes the user's input, processes it, and returns the health score. """ if model is None or scaler is None: return "Error: Model or scaler not loaded. Please check the logs." input_df = pd.DataFrame([[ball_bearing, humidity, vibration]], columns=['ball-bearing', 'humidity', 'vibration']) # load data scaler input_scaled = scaler.transform(input_df) # prediksi engan model reconstruction = model.predict(input_scaled) health_index = np.mean(np.abs(input_scaled - reconstruction), axis=1)[0] threshold = 0.1 if health_index < threshold: status = "HEALTHY" explanation = "The elevator is operating within normal parameters." else: status = "WARNING! ANOMALY DETECTED" explanation = "The elevator is showing signs of abnormal behavior. An inspection is recommended." # Format output string result = ( f"Health Index Score: {health_index:.4f}\n" f"Status: {status}\n\n" f"Explanation: {explanation}" ) return result demo = gr.Interface( fn=predict_health, inputs=[ gr.Number(label="Ball Bearing Value"), gr.Number(label="Humidity Value"), gr.Number(label="Vibration Value") ], outputs=gr.Textbox(label="Health Status Result", lines=5), title="Elevator Health Monitoring", description="Enter the current sensor readings from the elevator to get its real-time health index. A low score indicates normal operation, while a high score suggests an anomaly." ) if __name__ == "__main__": demo.launch(server_port=7685)