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,24 +83,13 @@ def predict_contamination_gradients(velocity, temperature, precipitation, humidi
|
|
| 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(
|
| 104 |
]).T
|
| 105 |
|
| 106 |
# Plot the graph
|
|
@@ -117,11 +106,17 @@ def plot_contamination_over_time(velocity, temperature, precipitation, humidity)
|
|
| 117 |
ax.legend()
|
| 118 |
ax.grid(True)
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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,9 +165,9 @@ with gr.Blocks() as demo:
|
|
| 170 |
# Submit and Clear Buttons under the inputs
|
| 171 |
with gr.Row():
|
| 172 |
gr.Button(value="Submit", variant="primary").click(
|
| 173 |
-
fn=
|
| 174 |
inputs=inputs,
|
| 175 |
-
outputs=contamination_outputs + gradients_outputs + cleaning_time_outputs
|
| 176 |
)
|
| 177 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 178 |
|
|
@@ -196,14 +191,4 @@ with gr.Blocks() as demo:
|
|
| 196 |
for out in cleaning_time_outputs:
|
| 197 |
out.render()
|
| 198 |
|
| 199 |
-
# Bottom Section: Graph at the very end
|
| 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()
|
|
|
|
| 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({
|
|
|
|
| 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[0][i], contamination_levels[0][i] * 2, len(time_intervals))
|
| 92 |
+
for i in range(contamination_levels.shape[1])
|
| 93 |
]).T
|
| 94 |
|
| 95 |
# Plot the graph
|
|
|
|
| 106 |
ax.legend()
|
| 107 |
ax.grid(True)
|
| 108 |
|
| 109 |
+
plot_output = fig
|
| 110 |
+
|
| 111 |
+
contamination_output = [f"{val * 100:.2f}%" for val in contamination_levels[0]]
|
| 112 |
+
gradients_output = [f"{val:.4f}" for val in gradients[0]]
|
| 113 |
+
cleaning_time_output = [f"{600:.2f}" for _ in range(6)] # Placeholder cleaning times
|
| 114 |
+
|
| 115 |
+
return [plot_output] + contamination_output + gradients_output + cleaning_time_output
|
| 116 |
|
| 117 |
except Exception as e:
|
| 118 |
+
print(f"Error in prediction and plotting: {e}")
|
| 119 |
+
return [plt.figure()] + ["Error"] * 18
|
| 120 |
|
| 121 |
inputs = [
|
| 122 |
gr.Slider(minimum=0, maximum=100, value=50, step=0.05, label="Velocity (mph)"),
|
|
|
|
| 165 |
# Submit and Clear Buttons under the inputs
|
| 166 |
with gr.Row():
|
| 167 |
gr.Button(value="Submit", variant="primary").click(
|
| 168 |
+
fn=predict_and_plot,
|
| 169 |
inputs=inputs,
|
| 170 |
+
outputs=[gr.Plot(label="Contamination Levels Over Time")] + contamination_outputs + gradients_outputs + cleaning_time_outputs
|
| 171 |
)
|
| 172 |
gr.Button(value="Clear").click(fn=lambda: None)
|
| 173 |
|
|
|
|
| 191 |
for out in cleaning_time_outputs:
|
| 192 |
out.render()
|
| 193 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
demo.launch()
|