Update app.py
Browse files
app.py
CHANGED
@@ -51,7 +51,7 @@ try:
|
|
51 |
except Exception as e:
|
52 |
print(f"Error loading models or scaler: {e}")
|
53 |
|
54 |
-
def
|
55 |
try:
|
56 |
# Prepare the example data
|
57 |
example_data = pd.DataFrame({
|
@@ -83,64 +83,45 @@ def predict_and_plot(velocity, temperature, precipitation, humidity):
|
|
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
|
88 |
|
89 |
# Generate simulated contamination levels (linear interpolation between predicted values)
|
90 |
simulated_contamination_levels = np.array([
|
91 |
-
np.linspace(contamination_levels[
|
92 |
-
for i in range(contamination_levels
|
93 |
]).T
|
94 |
|
95 |
-
# Function to calculate cleaning time using linear interpolation
|
96 |
-
def calculate_cleaning_time(time_intervals, contamination_levels, threshold=0.4):
|
97 |
-
cleaning_times = []
|
98 |
-
for i in range(contamination_levels.shape[1]):
|
99 |
-
levels = contamination_levels[:, i]
|
100 |
-
for j in range(1, len(levels)):
|
101 |
-
if levels[j-1] <= threshold <= levels[j]:
|
102 |
-
# Linear interpolation
|
103 |
-
t1, t2 = time_intervals[j-1], time_intervals[j]
|
104 |
-
c1, c2 = levels[j-1], levels[j]
|
105 |
-
cleaning_time = t1 + (threshold - c1) * (t2 - t1) / (c2 - c1)
|
106 |
-
cleaning_times.append(cleaning_time)
|
107 |
-
break
|
108 |
-
else:
|
109 |
-
cleaning_times.append(time_intervals[-1]) # If threshold is not reached
|
110 |
-
return cleaning_times
|
111 |
-
|
112 |
-
# Calculate cleaning times for all 6 lidars
|
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 |
-
|
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
|
140 |
|
141 |
except Exception as e:
|
142 |
-
print(f"Error in
|
143 |
-
return
|
144 |
|
145 |
inputs = [
|
146 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
@@ -189,9 +170,9 @@ with gr.Blocks() as demo:
|
|
189 |
# Submit and Clear Buttons under the inputs
|
190 |
with gr.Row():
|
191 |
gr.Button(value="Submit", variant="primary").click(
|
192 |
-
fn=
|
193 |
inputs=inputs,
|
194 |
-
outputs=
|
195 |
)
|
196 |
gr.Button(value="Clear").click(fn=lambda: None)
|
197 |
|
@@ -219,6 +200,10 @@ with gr.Blocks() as demo:
|
|
219 |
with gr.Row():
|
220 |
with gr.Column():
|
221 |
gr.Markdown("### Contamination Levels Over Time")
|
222 |
-
gr.Plot(label="Contamination Levels Over Time")
|
|
|
|
|
|
|
|
|
223 |
|
224 |
demo.launch()
|
|
|
51 |
except Exception as e:
|
52 |
print(f"Error loading models or scaler: {e}")
|
53 |
|
54 |
+
def predict_contamination_gradients(velocity, temperature, precipitation, humidity):
|
55 |
try:
|
56 |
# Prepare the example data
|
57 |
example_data = pd.DataFrame({
|
|
|
83 |
# Predict contamination levels and gradients for the single example
|
84 |
contamination_levels, gradients = predict_contamination_and_gradients(example_data_scaled)
|
85 |
|
86 |
+
return contamination_levels[0], gradients[0]
|
87 |
+
|
88 |
+
except Exception as e:
|
89 |
+
print(f"Error in Gradio interface: {e}")
|
90 |
+
return ["Error"] * 12
|
91 |
+
|
92 |
+
def plot_contamination_over_time(velocity, temperature, precipitation, humidity):
|
93 |
+
try:
|
94 |
+
# Predict contamination levels first
|
95 |
+
contamination_levels, _ = predict_contamination_gradients(velocity, temperature, precipitation, humidity)
|
96 |
+
|
97 |
# Simulate contamination levels at multiple time intervals
|
98 |
time_intervals = np.arange(0, 601, 60) # Simulating time intervals from 0 to 600 seconds
|
99 |
|
100 |
# Generate simulated contamination levels (linear interpolation between predicted values)
|
101 |
simulated_contamination_levels = np.array([
|
102 |
+
np.linspace(contamination_levels[i], contamination_levels[i] * 2, len(time_intervals))
|
103 |
+
for i in range(len(contamination_levels))
|
104 |
]).T
|
105 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
106 |
# Plot the graph
|
107 |
fig, ax = plt.subplots(figsize=(12, 8))
|
108 |
|
109 |
+
lidar_names = ['F/L', 'F/R', 'Left', 'Right', 'Roof', 'Rear']
|
110 |
for i in range(simulated_contamination_levels.shape[1]):
|
111 |
ax.plot(time_intervals, simulated_contamination_levels[:, i], label=f'{lidar_names[i]}')
|
112 |
ax.axhline(y=0.4, color='r', linestyle='--', label='Contamination Threshold' if i == 0 else "")
|
|
|
|
|
113 |
|
114 |
ax.set_title('Contamination Levels Over Time for Each Lidar')
|
115 |
ax.set_xlabel('Time (seconds)')
|
116 |
ax.set_ylabel('Contamination Level')
|
117 |
ax.legend()
|
118 |
ax.grid(True)
|
|
|
|
|
|
|
|
|
|
|
|
|
119 |
|
120 |
+
return fig
|
121 |
|
122 |
except Exception as e:
|
123 |
+
print(f"Error in plotting: {e}")
|
124 |
+
return plt.figure()
|
125 |
|
126 |
inputs = [
|
127 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
|
170 |
# Submit and Clear Buttons under the inputs
|
171 |
with gr.Row():
|
172 |
gr.Button(value="Submit", variant="primary").click(
|
173 |
+
fn=predict_contamination_gradients,
|
174 |
inputs=inputs,
|
175 |
+
outputs=contamination_outputs + gradients_outputs + cleaning_time_outputs
|
176 |
)
|
177 |
gr.Button(value="Clear").click(fn=lambda: None)
|
178 |
|
|
|
200 |
with gr.Row():
|
201 |
with gr.Column():
|
202 |
gr.Markdown("### Contamination Levels Over Time")
|
203 |
+
gr.Plot(label="Contamination Levels Over Time").click(
|
204 |
+
fn=plot_contamination_over_time,
|
205 |
+
inputs=inputs,
|
206 |
+
outputs="plot"
|
207 |
+
)
|
208 |
|
209 |
demo.launch()
|