Update app.py
Browse files
app.py
CHANGED
|
@@ -64,8 +64,8 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
| 64 |
# Scale the example data
|
| 65 |
example_data_scaled = scaler_X.transform(example_data)
|
| 66 |
|
| 67 |
-
# Function to predict contamination levels
|
| 68 |
-
def
|
| 69 |
# Predict using MLP model
|
| 70 |
mlp_predictions_contamination, mlp_predictions_gradients = loaded_mlp_model.predict(example_data_scaled)
|
| 71 |
|
|
@@ -78,10 +78,10 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
| 78 |
# Predict using meta model
|
| 79 |
meta_predictions = loaded_meta_model.predict(combined_features)
|
| 80 |
|
| 81 |
-
return meta_predictions[:, :6] #
|
| 82 |
|
| 83 |
-
# Predict contamination levels for the single example
|
| 84 |
-
contamination_levels =
|
| 85 |
|
| 86 |
# Simulate contamination levels at multiple time intervals
|
| 87 |
time_intervals = np.arange(0, 601, 60) # Simulating time intervals from 0 to 600 seconds
|
|
@@ -130,16 +130,17 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
| 130 |
ax.legend()
|
| 131 |
ax.grid(True)
|
| 132 |
|
| 133 |
-
# Flatten the results into a single list of
|
| 134 |
plot_output = fig
|
| 135 |
contamination_output = [f"{val * 100:.2f}%" for val in contamination_levels[0]]
|
|
|
|
| 136 |
cleaning_time_output = [f"{val:.2f}" for val in cleaning_times]
|
| 137 |
|
| 138 |
-
return [plot_output] + contamination_output + cleaning_time_output
|
| 139 |
|
| 140 |
except Exception as e:
|
| 141 |
print(f"Error in Gradio interface: {e}")
|
| 142 |
-
return [plt.figure()] + ["Error"] *
|
| 143 |
|
| 144 |
inputs = [
|
| 145 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
@@ -157,6 +158,15 @@ contamination_outputs = [
|
|
| 157 |
gr.Textbox(label="Rear Contamination")
|
| 158 |
]
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
cleaning_time_outputs = [
|
| 161 |
gr.Textbox(label="Front Left Cleaning Time"),
|
| 162 |
gr.Textbox(label="Front Right Cleaning Time"),
|
|
@@ -167,8 +177,8 @@ cleaning_time_outputs = [
|
|
| 167 |
]
|
| 168 |
|
| 169 |
with gr.Blocks() as demo:
|
| 170 |
-
gr.Markdown("<h1 style='text-align: center;'>Environmental Factor-Based Contamination & Cleaning Time Prediction</h1>")
|
| 171 |
-
gr.Markdown("This application predicts the contamination levels,
|
| 172 |
|
| 173 |
with gr.Row():
|
| 174 |
with gr.Column():
|
|
@@ -184,18 +194,29 @@ with gr.Blocks() as demo:
|
|
| 184 |
gr.Button(value="Submit", variant="primary").click(
|
| 185 |
fn=predict_and_plot,
|
| 186 |
inputs=inputs,
|
| 187 |
-
outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + cleaning_time_outputs
|
| 188 |
)
|
| 189 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 190 |
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 200 |
|
| 201 |
demo.launch()
|
|
|
|
| 64 |
# Scale the example data
|
| 65 |
example_data_scaled = scaler_X.transform(example_data)
|
| 66 |
|
| 67 |
+
# Function to predict contamination levels and gradients
|
| 68 |
+
def predict_contamination_and_gradients(example_data_scaled):
|
| 69 |
# Predict using MLP model
|
| 70 |
mlp_predictions_contamination, mlp_predictions_gradients = loaded_mlp_model.predict(example_data_scaled)
|
| 71 |
|
|
|
|
| 78 |
# Predict using meta model
|
| 79 |
meta_predictions = loaded_meta_model.predict(combined_features)
|
| 80 |
|
| 81 |
+
return meta_predictions[:, :6], meta_predictions[:, 6:] # Split predictions into contamination and gradients
|
| 82 |
|
| 83 |
+
# Predict contamination levels and gradients for the single example
|
| 84 |
+
contamination_levels, gradients = predict_contamination_and_gradients(example_data_scaled)
|
| 85 |
|
| 86 |
# Simulate contamination levels at multiple time intervals
|
| 87 |
time_intervals = np.arange(0, 601, 60) # Simulating time intervals from 0 to 600 seconds
|
|
|
|
| 130 |
ax.legend()
|
| 131 |
ax.grid(True)
|
| 132 |
|
| 133 |
+
# Flatten the results into a single list of 19 outputs (1 plot + 6 contamination + 6 gradients + 6 cleaning times)
|
| 134 |
plot_output = fig
|
| 135 |
contamination_output = [f"{val * 100:.2f}%" for val in contamination_levels[0]]
|
| 136 |
+
gradients_output = [f"{val:.4f}" for val in gradients[0]]
|
| 137 |
cleaning_time_output = [f"{val:.2f}" for val in cleaning_times]
|
| 138 |
|
| 139 |
+
return [plot_output] + contamination_output + gradients_output + cleaning_time_output
|
| 140 |
|
| 141 |
except Exception as e:
|
| 142 |
print(f"Error in Gradio interface: {e}")
|
| 143 |
+
return [plt.figure()] + ["Error"] * 18
|
| 144 |
|
| 145 |
inputs = [
|
| 146 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
|
|
| 158 |
gr.Textbox(label="Rear Contamination")
|
| 159 |
]
|
| 160 |
|
| 161 |
+
gradients_outputs = [
|
| 162 |
+
gr.Textbox(label="Front Left Gradient"),
|
| 163 |
+
gr.Textbox(label="Front Right Gradient"),
|
| 164 |
+
gr.Textbox(label="Left Gradient"),
|
| 165 |
+
gr.Textbox(label="Right Gradient"),
|
| 166 |
+
gr.Textbox(label="Roof Gradient"),
|
| 167 |
+
gr.Textbox(label="Rear Gradient")
|
| 168 |
+
]
|
| 169 |
+
|
| 170 |
cleaning_time_outputs = [
|
| 171 |
gr.Textbox(label="Front Left Cleaning Time"),
|
| 172 |
gr.Textbox(label="Front Right Cleaning Time"),
|
|
|
|
| 177 |
]
|
| 178 |
|
| 179 |
with gr.Blocks() as demo:
|
| 180 |
+
gr.Markdown("<h1 style='text-align: center;'>Environmental Factor-Based Contamination, Gradient, & Cleaning Time Prediction</h1>")
|
| 181 |
+
gr.Markdown("This application predicts the contamination levels, gradients, and cleaning times for different parts of a car's LiDAR system based on environmental factors such as velocity, temperature, precipitation, and humidity.")
|
| 182 |
|
| 183 |
with gr.Row():
|
| 184 |
with gr.Column():
|
|
|
|
| 194 |
gr.Button(value="Submit", variant="primary").click(
|
| 195 |
fn=predict_and_plot,
|
| 196 |
inputs=inputs,
|
| 197 |
+
outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + gradients_outputs + cleaning_time_outputs
|
| 198 |
)
|
| 199 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 200 |
|
| 201 |
+
# Plot above the three columns of outputs
|
| 202 |
+
with gr.Column():
|
| 203 |
+
gr.Markdown("### Predictions")
|
| 204 |
+
gr.Plot(label="Contamination Levels Over Time").render()
|
| 205 |
+
|
| 206 |
+
with gr.Row():
|
| 207 |
+
with gr.Column():
|
| 208 |
+
gr.Markdown("#### Contamination Predictions")
|
| 209 |
+
for out in contamination_outputs:
|
| 210 |
+
out.render()
|
| 211 |
|
| 212 |
+
with gr.Column():
|
| 213 |
+
gr.Markdown("#### Gradient Predictions")
|
| 214 |
+
for out in gradients_outputs:
|
| 215 |
+
out.render()
|
| 216 |
+
|
| 217 |
+
with gr.Column():
|
| 218 |
+
gr.Markdown("#### Cleaning Time Predictions")
|
| 219 |
+
for out in cleaning_time_outputs:
|
| 220 |
+
out.render()
|
| 221 |
|
| 222 |
demo.launch()
|