Update app.py
Browse files
app.py
CHANGED
@@ -43,22 +43,22 @@ try:
|
|
43 |
|
44 |
# Load the models
|
45 |
loaded_rf_model = load(rf_model_path)
|
46 |
-
print("
|
47 |
loaded_mlp_model = load_model(mlp_model_path)
|
48 |
-
print("MLP
|
49 |
loaded_meta_model = load(meta_model_path)
|
50 |
-
print("
|
51 |
except Exception as e:
|
52 |
-
print(f"
|
53 |
|
54 |
def predict_and_plot(velocity, temperature, precipitation, humidity):
|
55 |
try:
|
56 |
# Prepare the example data
|
57 |
example_data = pd.DataFrame({
|
58 |
-
'
|
59 |
-
'
|
60 |
-
'
|
61 |
-
'
|
62 |
})
|
63 |
|
64 |
# Scale the example data
|
@@ -113,20 +113,20 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
113 |
cleaning_times = calculate_cleaning_time(time_intervals, simulated_contamination_levels)
|
114 |
|
115 |
# Lidar names
|
116 |
-
lidar_names = ['
|
117 |
|
118 |
# Plot the graph
|
119 |
fig, ax = plt.subplots(figsize=(12, 8))
|
120 |
|
121 |
for i in range(simulated_contamination_levels.shape[1]):
|
122 |
ax.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
|
123 |
-
ax.axhline(y=0.4, color='r', linestyle='--', label='
|
124 |
if i < len(cleaning_times):
|
125 |
ax.scatter(cleaning_times[i], 0.4, color='k') # Mark the cleaning time point
|
126 |
|
127 |
-
ax.set_title('
|
128 |
-
ax.set_xlabel('
|
129 |
-
ax.set_ylabel('
|
130 |
ax.legend()
|
131 |
ax.grid(True)
|
132 |
|
@@ -139,80 +139,80 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
139 |
return [plot_output] + contamination_output + gradients_output + cleaning_time_output
|
140 |
|
141 |
except Exception as e:
|
142 |
-
print(f"
|
143 |
-
return [plt.figure()] + ["
|
144 |
|
145 |
inputs = [
|
146 |
-
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="
|
147 |
-
gr.Slider(minimum=-2, maximum=30, value=0, step=0.5, label="
|
148 |
-
gr.Slider(minimum=0, maximum=1, value=0, step=0.01, label="
|
149 |
-
gr.Slider(minimum=0, maximum=100, value=50, label="
|
150 |
]
|
151 |
|
152 |
contamination_outputs = [
|
153 |
-
gr.Textbox(label="
|
154 |
-
gr.Textbox(label="
|
155 |
-
gr.Textbox(label="
|
156 |
-
gr.Textbox(label="
|
157 |
-
gr.Textbox(label="
|
158 |
-
gr.Textbox(label="
|
159 |
]
|
160 |
|
161 |
gradients_outputs = [
|
162 |
-
gr.Textbox(label="
|
163 |
-
gr.Textbox(label="
|
164 |
-
gr.Textbox(label="
|
165 |
-
gr.Textbox(label="
|
166 |
-
gr.Textbox(label="
|
167 |
-
gr.Textbox(label="
|
168 |
]
|
169 |
|
170 |
cleaning_time_outputs = [
|
171 |
-
gr.Textbox(label="
|
172 |
-
gr.Textbox(label="
|
173 |
-
gr.Textbox(label="
|
174 |
-
gr.Textbox(label="
|
175 |
-
gr.Textbox(label="
|
176 |
-
gr.Textbox(label="
|
177 |
]
|
178 |
|
179 |
with gr.Blocks(css=".column-container {height: 100%; display: flex; flex-direction: column; justify-content: space-between;}") as demo:
|
180 |
-
gr.Markdown("<h1 style='text-align: center;'
|
181 |
-
gr.Markdown("
|
182 |
|
183 |
# Top Section: Inputs and Car Image
|
184 |
with gr.Row():
|
185 |
with gr.Column(scale=2, elem_classes="column-container"):
|
186 |
-
gr.Markdown("###
|
187 |
for inp in inputs:
|
188 |
inp.render()
|
189 |
-
submit_button = gr.Button(value="
|
190 |
-
clear_button = gr.Button(value="
|
191 |
|
192 |
with gr.Column(scale=1):
|
193 |
-
gr.Markdown("###
|
194 |
gr.Image(image_path)
|
195 |
|
196 |
# Bottom Section: Outputs (Three columns)
|
197 |
with gr.Row():
|
198 |
with gr.Column(scale=2):
|
199 |
-
gr.Markdown("###
|
200 |
for out in contamination_outputs:
|
201 |
out.render()
|
202 |
|
203 |
with gr.Column(scale=2):
|
204 |
-
gr.Markdown("###
|
205 |
for out in gradients_outputs:
|
206 |
out.render()
|
207 |
|
208 |
with gr.Column(scale=2):
|
209 |
-
gr.Markdown("###
|
210 |
for out in cleaning_time_outputs:
|
211 |
out.render()
|
212 |
|
213 |
# Graph below the outputs
|
214 |
with gr.Row():
|
215 |
-
plot_output = gr.Plot(label="
|
216 |
|
217 |
submit_button.click(
|
218 |
fn=predict_and_plot,
|
|
|
43 |
|
44 |
# Load the models
|
45 |
loaded_rf_model = load(rf_model_path)
|
46 |
+
print("Random Forest model loaded successfully.")
|
47 |
loaded_mlp_model = load_model(mlp_model_path)
|
48 |
+
print("MLP model loaded successfully.")
|
49 |
loaded_meta_model = load(meta_model_path)
|
50 |
+
print("Meta model loaded successfully.")
|
51 |
except Exception as e:
|
52 |
+
print(f"Error loading models or scaler: {e}")
|
53 |
|
54 |
def predict_and_plot(velocity, temperature, precipitation, humidity):
|
55 |
try:
|
56 |
# Prepare the example data
|
57 |
example_data = pd.DataFrame({
|
58 |
+
'Velocity(mph)': [velocity],
|
59 |
+
'Temperature': [temperature],
|
60 |
+
'Precipitation': [precipitation],
|
61 |
+
'Humidity': [humidity]
|
62 |
})
|
63 |
|
64 |
# Scale the example data
|
|
|
113 |
cleaning_times = calculate_cleaning_time(time_intervals, simulated_contamination_levels)
|
114 |
|
115 |
# Lidar names
|
116 |
+
lidar_names = ['F/L', 'F/R', 'Left', 'Right', 'Roof', 'Rear']
|
117 |
|
118 |
# Plot the graph
|
119 |
fig, ax = plt.subplots(figsize=(12, 8))
|
120 |
|
121 |
for i in range(simulated_contamination_levels.shape[1]):
|
122 |
ax.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
|
123 |
+
ax.axhline(y=0.4, color='r', linestyle='--', label='Contamination Threshold' if i == 0 else "")
|
124 |
if i < len(cleaning_times):
|
125 |
ax.scatter(cleaning_times[i], 0.4, color='k') # Mark the cleaning time point
|
126 |
|
127 |
+
ax.set_title('Contamination Levels Over Time for Each Lidar')
|
128 |
+
ax.set_xlabel('Time (seconds)')
|
129 |
+
ax.set_ylabel('Contamination Level')
|
130 |
ax.legend()
|
131 |
ax.grid(True)
|
132 |
|
|
|
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)"),
|
147 |
+
gr.Slider(minimum=-2, maximum=30, value=0, step=0.5, label="Temperature (ยฐC)"),
|
148 |
+
gr.Slider(minimum=0, maximum=1, value=0, step=0.01, label="Precipitation (inch)"),
|
149 |
+
gr.Slider(minimum=0, maximum=100, value=50, label="Humidity (%)")
|
150 |
]
|
151 |
|
152 |
contamination_outputs = [
|
153 |
+
gr.Textbox(label="Front Left Contamination"),
|
154 |
+
gr.Textbox(label="Front Right Contamination"),
|
155 |
+
gr.Textbox(label="Left Contamination"),
|
156 |
+
gr.Textbox(label="Right Contamination"),
|
157 |
+
gr.Textbox(label="Roof Contamination"),
|
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"),
|
173 |
+
gr.Textbox(label="Left Cleaning Time"),
|
174 |
+
gr.Textbox(label="Right Cleaning Time"),
|
175 |
+
gr.Textbox(label="Roof Cleaning Time"),
|
176 |
+
gr.Textbox(label="Rear Cleaning Time")
|
177 |
]
|
178 |
|
179 |
with gr.Blocks(css=".column-container {height: 100%; display: flex; flex-direction: column; justify-content: space-between;}") 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 |
# Top Section: Inputs and Car Image
|
184 |
with gr.Row():
|
185 |
with gr.Column(scale=2, elem_classes="column-container"):
|
186 |
+
gr.Markdown("### Input Parameters")
|
187 |
for inp in inputs:
|
188 |
inp.render()
|
189 |
+
submit_button = gr.Button(value="Submit", variant="primary")
|
190 |
+
clear_button = gr.Button(value="Clear")
|
191 |
|
192 |
with gr.Column(scale=1):
|
193 |
+
gr.Markdown("### Location of LiDARs")
|
194 |
gr.Image(image_path)
|
195 |
|
196 |
# Bottom Section: Outputs (Three columns)
|
197 |
with gr.Row():
|
198 |
with gr.Column(scale=2):
|
199 |
+
gr.Markdown("### Contamination Predictions")
|
200 |
for out in contamination_outputs:
|
201 |
out.render()
|
202 |
|
203 |
with gr.Column(scale=2):
|
204 |
+
gr.Markdown("### Gradient Predictions")
|
205 |
for out in gradients_outputs:
|
206 |
out.render()
|
207 |
|
208 |
with gr.Column(scale=2):
|
209 |
+
gr.Markdown("### Cleaning Time Predictions")
|
210 |
for out in cleaning_time_outputs:
|
211 |
out.render()
|
212 |
|
213 |
# Graph below the outputs
|
214 |
with gr.Row():
|
215 |
+
plot_output = gr.Plot(label="Contamination Levels Over Time")
|
216 |
|
217 |
submit_button.click(
|
218 |
fn=predict_and_plot,
|