Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import json
|
| 4 |
+
import joblib
|
| 5 |
+
import tensorflow as tf
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from joblib import load
|
| 8 |
+
from tensorflow.keras.models import load_model
|
| 9 |
+
from sklearn.preprocessing import MinMaxScaler
|
| 10 |
+
import os
|
| 11 |
+
import sklearn # Import sklearn
|
| 12 |
+
|
| 13 |
+
# Display library versions
|
| 14 |
+
print(f"Gradio version: {gr.__version__}")
|
| 15 |
+
print(f"NumPy version: {np.__version__}")
|
| 16 |
+
print(f"Scikit-learn version: {sklearn.__version__}")
|
| 17 |
+
print(f"Joblib version: {joblib.__version__}")
|
| 18 |
+
print(f"TensorFlow version: {tf.__version__}")
|
| 19 |
+
print(f"Pandas version: {pd.__version__}")
|
| 20 |
+
|
| 21 |
+
# Directory paths for the saved models
|
| 22 |
+
script_dir = os.path.dirname(os.path.abspath(__file__))
|
| 23 |
+
scaler_path = os.path.join(script_dir, 'toolkit', 'scaler_X.json')
|
| 24 |
+
rf_model_path = os.path.join(script_dir, 'toolkit', 'rf_model.joblib')
|
| 25 |
+
mlp_model_path = os.path.join(script_dir, 'toolkit', 'mlp_model.keras')
|
| 26 |
+
meta_model_path = os.path.join(script_dir, 'toolkit', 'meta_model.joblib')
|
| 27 |
+
image_path = os.path.join(script_dir, 'toolkit', 'car.png')
|
| 28 |
+
|
| 29 |
+
# Load the scaler and models
|
| 30 |
+
try:
|
| 31 |
+
# Load the scaler
|
| 32 |
+
with open(scaler_path, 'r') as f:
|
| 33 |
+
scaler_params = json.load(f)
|
| 34 |
+
scaler_X = MinMaxScaler()
|
| 35 |
+
scaler_X.scale_ = np.array(scaler_params["scale_"])
|
| 36 |
+
scaler_X.min_ = np.array(scaler_params["min_"])
|
| 37 |
+
scaler_X.data_min_ = np.array(scaler_params["data_min_"])
|
| 38 |
+
scaler_X.data_max_ = np.array(scaler_params["data_max_"])
|
| 39 |
+
scaler_X.data_range_ = np.array(scaler_params["data_range_"])
|
| 40 |
+
scaler_X.n_features_in_ = scaler_params["n_features_in_"]
|
| 41 |
+
scaler_X.feature_names_in_ = np.array(scaler_params["feature_names_in_"])
|
| 42 |
+
|
| 43 |
+
# Load the models
|
| 44 |
+
loaded_rf_model = load(rf_model_path)
|
| 45 |
+
print("Random Forest model loaded successfully.")
|
| 46 |
+
loaded_mlp_model = load_model(mlp_model_path)
|
| 47 |
+
print("MLP model loaded successfully.")
|
| 48 |
+
loaded_meta_model = load(meta_model_path)
|
| 49 |
+
print("Meta model loaded successfully.")
|
| 50 |
+
except Exception as e:
|
| 51 |
+
print(f"Error loading models or scaler: {e}")
|
| 52 |
+
|
| 53 |
+
def predict_new_values(new_input_data):
|
| 54 |
+
try:
|
| 55 |
+
# Ensure the new input data is in the correct format
|
| 56 |
+
print(f"Raw Input Data: {new_input_data}")
|
| 57 |
+
new_input_data = np.array(new_input_data).reshape(1, -1)
|
| 58 |
+
|
| 59 |
+
# Scale the new input data
|
| 60 |
+
new_input_scaled = scaler_X.transform(new_input_data)
|
| 61 |
+
print(f"Scaled Input Data: {new_input_scaled}")
|
| 62 |
+
|
| 63 |
+
# Make predictions with the MLP model
|
| 64 |
+
contamination_predictions, gradients_predictions = loaded_mlp_model.predict(new_input_scaled)
|
| 65 |
+
|
| 66 |
+
return contamination_predictions[0], gradients_predictions[0]
|
| 67 |
+
except Exception as e:
|
| 68 |
+
print(f"Error in prediction: {e}")
|
| 69 |
+
return (["Error"] * 6, ["Error"] * 6)
|
| 70 |
+
|
| 71 |
+
def gradio_interface(velocity, temperature, precipitation, humidity):
|
| 72 |
+
try:
|
| 73 |
+
input_data = [velocity, temperature, precipitation, humidity]
|
| 74 |
+
print(f"Input Data: {input_data}")
|
| 75 |
+
contamination_predictions, gradients_predictions = predict_new_values(input_data)
|
| 76 |
+
print(f"Contamination Predictions: {contamination_predictions}")
|
| 77 |
+
print(f"Gradients Predictions: {gradients_predictions}")
|
| 78 |
+
return (
|
| 79 |
+
[f"{val * 100:.2f}%" if val != "Error" else "Error" for val in contamination_predictions],
|
| 80 |
+
[f"{val:.2f}" if val != "Error" else "Error" for val in gradients_predictions]
|
| 81 |
+
)
|
| 82 |
+
except Exception as e:
|
| 83 |
+
print(f"Error in Gradio interface: {e}")
|
| 84 |
+
return (["Error"] * 6, ["Error"] * 6)
|
| 85 |
+
|
| 86 |
+
inputs = [
|
| 87 |
+
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
| 88 |
+
gr.Slider(minimum=-2, maximum=30, value=0, step=0.5, label="Temperature (°C)"),
|
| 89 |
+
gr.Slider(minimum=0, maximum=1, value=0, step=0.01, label="Precipitation (inch)"),
|
| 90 |
+
gr.Slider(minimum=0, maximum=100, value=50, label="Humidity (%)")
|
| 91 |
+
]
|
| 92 |
+
|
| 93 |
+
contamination_outputs = [
|
| 94 |
+
gr.Textbox(label="Front Left Contamination"),
|
| 95 |
+
gr.Textbox(label="Front Right Contamination"),
|
| 96 |
+
gr.Textbox(label="Left Contamination"),
|
| 97 |
+
gr.Textbox(label="Right Contamination"),
|
| 98 |
+
gr.Textbox(label="Roof Contamination"),
|
| 99 |
+
gr.Textbox(label="Rear Contamination")
|
| 100 |
+
]
|
| 101 |
+
|
| 102 |
+
gradients_outputs = [
|
| 103 |
+
gr.Textbox(label="Front Left Gradient"),
|
| 104 |
+
gr.Textbox(label="Front Right Gradient"),
|
| 105 |
+
gr.Textbox(label="Left Gradient"),
|
| 106 |
+
gr.Textbox(label="Right Gradient"),
|
| 107 |
+
gr.Textbox(label="Roof Gradient"),
|
| 108 |
+
gr.Textbox(label="Rear Gradient")
|
| 109 |
+
]
|
| 110 |
+
|
| 111 |
+
with gr.Blocks() as demo:
|
| 112 |
+
gr.Markdown("<h1 style='text-align: center;'>Environmental Factor-Based Contamination & Gradient Prediction</h1>")
|
| 113 |
+
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.")
|
| 114 |
+
|
| 115 |
+
with gr.Row():
|
| 116 |
+
with gr.Column():
|
| 117 |
+
gr.Markdown("### Input Parameters")
|
| 118 |
+
for inp in inputs:
|
| 119 |
+
inp.render()
|
| 120 |
+
|
| 121 |
+
# Centered image display
|
| 122 |
+
with gr.Row():
|
| 123 |
+
with gr.Column(scale=1, min_width=0):
|
| 124 |
+
gr.Image(image_path) # Ensure the image is centered
|
| 125 |
+
|
| 126 |
+
gr.Button(value="Submit", variant="primary").click(fn=gradio_interface, inputs=inputs, outputs=contamination_outputs + gradients_outputs)
|
| 127 |
+
gr.Button(value="Clear").click(fn=lambda: None)
|
| 128 |
+
|
| 129 |
+
with gr.Column():
|
| 130 |
+
gr.Markdown("### Contamination Predictions")
|
| 131 |
+
for out in contamination_outputs:
|
| 132 |
+
out.render()
|
| 133 |
+
|
| 134 |
+
with gr.Column():
|
| 135 |
+
gr.Markdown("### Gradients Predictions")
|
| 136 |
+
for out in gradients_outputs:
|
| 137 |
+
out.render()
|
| 138 |
+
|
| 139 |
+
demo.launch()
|