import gradio as gr import numpy as np import json import joblib import tensorflow as tf import pandas as pd from joblib import load from tensorflow.keras.models import load_model from sklearn.preprocessing import MinMaxScaler import os import sklearn # Import sklearn # Display library versions print(f"Gradio version: {gr.__version__}") print(f"NumPy version: {np.__version__}") print(f"Scikit-learn version: {sklearn.__version__}") print(f"Joblib version: {joblib.__version__}") print(f"TensorFlow version: {tf.__version__}") print(f"Pandas version: {pd.__version__}") # Directory paths for the saved models script_dir = os.path.dirname(os.path.abspath(__file__)) scaler_path = os.path.join(script_dir, 'toolkit', 'scaler_X.json') rf_model_path = os.path.join(script_dir, 'toolkit', 'rf_model.joblib') mlp_model_path = os.path.join(script_dir, 'toolkit', 'mlp_model.keras') meta_model_path = os.path.join(script_dir, 'toolkit', 'meta_model.joblib') image_path = os.path.join(script_dir, 'toolkit', 'car.png') # Load the scaler and models try: # Load the scaler with open(scaler_path, 'r') as f: scaler_params = json.load(f) scaler_X = MinMaxScaler() scaler_X.scale_ = np.array(scaler_params["scale_"]) scaler_X.min_ = np.array(scaler_params["min_"]) scaler_X.data_min_ = np.array(scaler_params["data_min_"]) scaler_X.data_max_ = np.array(scaler_params["data_max_"]) scaler_X.data_range_ = np.array(scaler_params["data_range_"]) scaler_X.n_features_in_ = scaler_params["n_features_in_"] scaler_X.feature_names_in_ = np.array(scaler_params["feature_names_in_"]) # Load the models loaded_rf_model = load(rf_model_path) print("Random Forest model loaded successfully.") loaded_mlp_model = load_model(mlp_model_path) print("MLP model loaded successfully.") loaded_meta_model = load(meta_model_path) print("Meta model loaded successfully.") except Exception as e: print(f"Error loading models or scaler: {e}") def predict_new_values(new_input_data): try: # Ensure the new input data is in the correct format print(f"Raw Input Data: {new_input_data}") new_input_data = np.array(new_input_data).reshape(1, -1) # Scale the new input data new_input_scaled = scaler_X.transform(new_input_data) print(f"Scaled Input Data: {new_input_scaled}") # Make predictions with the MLP model contamination_predictions, gradients_predictions = loaded_mlp_model.predict(new_input_scaled) return contamination_predictions[0], gradients_predictions[0] except Exception as e: print(f"Error in prediction: {e}") return (["Error"] * 6, ["Error"] * 6) def gradio_interface(velocity, temperature, precipitation, humidity): try: input_data = [velocity, temperature, precipitation, humidity] print(f"Input Data: {input_data}") contamination_predictions, gradients_predictions = predict_new_values(input_data) print(f"Contamination Predictions: {contamination_predictions}") print(f"Gradients Predictions: {gradients_predictions}") return ( [f"{val * 100:.2f}%" if val != "Error" else "Error" for val in contamination_predictions], [f"{val:.2f}" if val != "Error" else "Error" for val in gradients_predictions] ) except Exception as e: print(f"Error in Gradio interface: {e}") return (["Error"] * 6, ["Error"] * 6) inputs = [ gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"), gr.Slider(minimum=-2, maximum=30, value=0, step=0.5, label="Temperature (°C)"), gr.Slider(minimum=0, maximum=1, value=0, step=0.01, label="Precipitation (inch)"), gr.Slider(minimum=0, maximum=100, value=50, label="Humidity (%)") ] contamination_outputs = [ gr.Textbox(label="Front Left Contamination"), gr.Textbox(label="Front Right Contamination"), gr.Textbox(label="Left Contamination"), gr.Textbox(label="Right Contamination"), gr.Textbox(label="Roof Contamination"), gr.Textbox(label="Rear Contamination") ] gradients_outputs = [ gr.Textbox(label="Front Left Gradient"), gr.Textbox(label="Front Right Gradient"), gr.Textbox(label="Left Gradient"), gr.Textbox(label="Right Gradient"), gr.Textbox(label="Roof Gradient"), gr.Textbox(label="Rear Gradient") ] with gr.Blocks() as demo: gr.Markdown("

Environmental Factor-Based Contamination & Gradient Prediction

") gr.Markdown("This application predicts the contamination levels and corresponding gradients for different parts of a car's LiDAR system based on environmental factors such as velocity, temperature, precipitation, and humidity.") with gr.Row(): with gr.Column(): gr.Markdown("### Input Parameters") for inp in inputs: inp.render() # Centered image display with gr.Row(): with gr.Column(scale=1, min_width=0): gr.Image(image_path) # Ensure the image is centered gr.Button(value="Submit", variant="primary").click(fn=gradio_interface, inputs=inputs, outputs=contamination_outputs + gradients_outputs) gr.Button(value="Clear").click(fn=lambda: None) with gr.Column(): gr.Markdown("### Contamination Predictions") for out in contamination_outputs: out.render() with gr.Column(): gr.Markdown("### Gradients Predictions") for out in gradients_outputs: out.render() demo.launch()