basic information in the app view; unlinked (both model and data)
Browse files- __pycache__/helper_functions.cpython-312.pyc +0 -0
- app.py +138 -58
- helper_functions.py +43 -0
- linear_regression_model.pkl +3 -0
- requirements.txt +5 -1
__pycache__/helper_functions.cpython-312.pyc
ADDED
|
Binary file (2 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,69 +1,149 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
import numpy as np
|
| 4 |
-
from sklearn.linear_model import LinearRegression
|
| 5 |
-
import joblib
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
# App Title
|
| 8 |
-
st.title("Utrecht Pollution
|
| 9 |
-
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
# Try loading a pre-trained model
|
| 15 |
-
model = joblib.load("path_to_your_model/linear_regression_model.pkl")
|
| 16 |
-
except:
|
| 17 |
-
# If the model is not available, train a simple Linear Regression model as a fallback
|
| 18 |
-
st.write("No pre-trained model found. Training a new Linear Regression model...")
|
| 19 |
-
|
| 20 |
-
# Fallback - Generate some random training data for demonstration purposes
|
| 21 |
-
# In reality, replace this with your actual data
|
| 22 |
-
np.random.seed(0)
|
| 23 |
-
X_train = np.random.rand(100, 3) # 100 samples, 3 features
|
| 24 |
-
y_train = 3*X_train[:, 0] + 2*X_train[:, 1] + X_train[:, 2] # Example: linear relationship
|
| 25 |
-
|
| 26 |
-
# Train a linear regression model
|
| 27 |
-
model = LinearRegression()
|
| 28 |
-
model.fit(X_train, y_train)
|
| 29 |
-
|
| 30 |
-
# Optionally, save the trained model to use later
|
| 31 |
-
joblib.dump(model, "linear_regression_model.pkl")
|
| 32 |
-
|
| 33 |
-
return model
|
| 34 |
-
|
| 35 |
-
model = load_model()
|
| 36 |
-
|
| 37 |
-
# Explain the app
|
| 38 |
-
st.write("""
|
| 39 |
-
### Predict Pollution Levels in Utrecht
|
| 40 |
-
This app allows you to input environmental features to predict pollution levels using a simple Linear Regression model.
|
| 41 |
-
""")
|
| 42 |
-
|
| 43 |
-
# Input features needed for your model
|
| 44 |
-
def get_user_input():
|
| 45 |
-
feature_1 = st.number_input('Temperature (°C)', min_value=-10.0, max_value=40.0, value=20.0)
|
| 46 |
-
feature_2 = st.number_input('Wind Speed (km/h)', min_value=0.0, max_value=100.0, value=10.0)
|
| 47 |
-
feature_3 = st.number_input('Humidity (%)', min_value=0.0, max_value=100.0, value=50.0)
|
| 48 |
|
| 49 |
-
#
|
| 50 |
-
input_data = {
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
-
#
|
| 58 |
-
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 66 |
|
| 67 |
-
#
|
| 68 |
-
|
| 69 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import time
|
| 2 |
+
|
| 3 |
+
import altair as alt
|
| 4 |
+
import joblib
|
| 5 |
+
import numpy as np
|
| 6 |
+
import pandas as pd
|
| 7 |
+
import streamlit as st
|
| 8 |
+
from sklearn.linear_model import LinearRegression
|
| 9 |
+
import matplotlib.pyplot as plt
|
| 10 |
+
import plotly.graph_objects as go
|
| 11 |
+
from helper_functions import custom_metric_box, pollution_box
|
| 12 |
+
import plotly.graph_objects as go
|
| 13 |
import streamlit as st
|
| 14 |
import pandas as pd
|
| 15 |
import numpy as np
|
|
|
|
|
|
|
| 16 |
|
| 17 |
+
st.set_page_config(
|
| 18 |
+
page_title="Utrecht Pollution Dashboard",
|
| 19 |
+
page_icon="🏂��🌱",
|
| 20 |
+
layout="wide",
|
| 21 |
+
initial_sidebar_state="expanded")
|
| 22 |
+
|
| 23 |
+
alt.themes.enable("dark")
|
| 24 |
# App Title
|
| 25 |
+
st.title("Utrecht Pollution Dashboard 🌱")
|
| 26 |
+
|
| 27 |
+
@st.cache_resource(ttl=6*300) # Reruns every 6 hours
|
| 28 |
+
def run_model():
|
| 29 |
+
# Load or train your model (pretrained model in this case)
|
| 30 |
+
model = joblib.load("linear_regression_model.pkl")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Static input values
|
| 33 |
+
input_data = pd.DataFrame({
|
| 34 |
+
'Temperature': [20.0],
|
| 35 |
+
'Wind Speed': [10.0],
|
| 36 |
+
'Humidity': [50.0]
|
| 37 |
+
})
|
| 38 |
|
| 39 |
+
# Run the model with static input
|
| 40 |
+
prediction = model.predict(input_data)
|
| 41 |
+
return prediction
|
| 42 |
+
col1, col2 = st.columns((1,1))
|
| 43 |
+
# Create a 3-column layout
|
| 44 |
+
with col1:
|
| 45 |
+
st.subheader('Current Weather')
|
| 46 |
+
col1, col2, col3 = st.columns(3)
|
| 47 |
+
|
| 48 |
+
# First column
|
| 49 |
+
with col1:
|
| 50 |
+
custom_metric_box(label="Temperature", value="2 °C", delta="-3 °C")
|
| 51 |
+
custom_metric_box(label="Humidity", value="60 %", delta="-1 %")
|
| 52 |
+
|
| 53 |
+
# Second column
|
| 54 |
+
with col2:
|
| 55 |
+
custom_metric_box(label="Pressure", value="1010 hPa", delta="+2 hPa")
|
| 56 |
+
custom_metric_box(label="Precipitation", value="5 mm", delta="-1 mm")
|
| 57 |
+
|
| 58 |
+
# Third column
|
| 59 |
+
with col3:
|
| 60 |
+
custom_metric_box(label="Solar Radiation", value="200 W/m²", delta="-20 W/m²")
|
| 61 |
+
custom_metric_box(label="Wind Speed", value="15 km/h", delta="-2 km/h")
|
| 62 |
+
|
| 63 |
+
st.subheader('Current Pollution Levels')
|
| 64 |
+
col1, col2 = st.columns((1,1))
|
| 65 |
+
# Display the prediction
|
| 66 |
+
#st.write(f'Predicted Pollution Level: {prediction[0]:.2f}')
|
| 67 |
+
with col1:
|
| 68 |
+
pollution_box(label="O<sub>3</sub>", value="37 µg/m³", delta="+2 µg/m³")
|
| 69 |
+
with col2:
|
| 70 |
+
pollution_box(label="NO<sub>2</sub>", value="28 µg/m³", delta="+3 µg/m³")
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
prediction = run_model() # Assuming you have a function run_model()
|
| 74 |
+
# Sample data (replace with your actual data)
|
| 75 |
+
dates_past = pd.date_range(end=pd.Timestamp.today(), periods=7).to_list()
|
| 76 |
+
dates_future = pd.date_range(start=pd.Timestamp.today() + pd.Timedelta(days=1), periods=3).to_list()
|
| 77 |
+
|
| 78 |
+
# O3 and NO2 values for the past 7 days
|
| 79 |
+
o3_past_values = [30, 32, 34, 33, 31, 35, 36]
|
| 80 |
+
no2_past_values = [20, 22, 21, 23, 22, 24, 25]
|
| 81 |
+
|
| 82 |
+
# Predicted O3 and NO2 values for the next 3 days
|
| 83 |
+
o3_future_values = [37, 38, 40]
|
| 84 |
+
no2_future_values = [26, 27, 28]
|
| 85 |
|
| 86 |
+
# Combine dates and values
|
| 87 |
+
dates = dates_past + dates_future
|
| 88 |
+
o3_values = o3_past_values + o3_future_values
|
| 89 |
+
no2_values = no2_past_values + no2_future_values
|
| 90 |
|
| 91 |
+
# Create a DataFrame
|
| 92 |
+
df = pd.DataFrame({
|
| 93 |
+
'Date': dates,
|
| 94 |
+
'O3': o3_values,
|
| 95 |
+
'NO2': no2_values
|
| 96 |
+
})
|
| 97 |
|
| 98 |
+
st.subheader('O3 and NO2 Prediction')
|
| 99 |
+
# Create two columns for two separate graphs
|
| 100 |
+
subcol1, subcol2 = st.columns(2)
|
| 101 |
+
# Plot O3 in the first subcolumn
|
| 102 |
+
with subcol1:
|
| 103 |
+
fig_o3 = go.Figure()
|
| 104 |
+
fig_o3.add_trace(go.Scatter(x=df['Date'], y=df['O3'],
|
| 105 |
+
mode='lines+markers',
|
| 106 |
+
name='O3',
|
| 107 |
+
line=dict(color='rgb(0, 191, 255)', width=4))) # Bright blue
|
| 108 |
+
# Add a vertical line for predictions (today's date)
|
| 109 |
+
fig_o3.add_shape(
|
| 110 |
+
dict(
|
| 111 |
+
type="line",
|
| 112 |
+
x0=pd.Timestamp.today(), x1=pd.Timestamp.today(),
|
| 113 |
+
y0=min(o3_values), y1=max(o3_values),
|
| 114 |
+
line=dict(color="White", width=3, dash="dash"),
|
| 115 |
+
)
|
| 116 |
+
)
|
| 117 |
+
fig_o3.update_layout(
|
| 118 |
+
plot_bgcolor='rgba(0, 0, 0, 0)', # Transparent background
|
| 119 |
+
paper_bgcolor='rgba(0, 0, 0, 0)', # Transparent paper background
|
| 120 |
+
yaxis_title="O3 Concentration (µg/m³)",
|
| 121 |
+
font=dict(size=14),
|
| 122 |
+
hovermode="x unified"
|
| 123 |
+
)
|
| 124 |
+
st.plotly_chart(fig_o3)
|
| 125 |
|
| 126 |
+
# Plot NO2 in the second subcolumn
|
| 127 |
+
with subcol2:
|
| 128 |
+
fig_no2 = go.Figure()
|
| 129 |
+
fig_no2.add_trace(go.Scatter(x=df['Date'], y=df['NO2'],
|
| 130 |
+
mode='lines+markers',
|
| 131 |
+
name='NO2',
|
| 132 |
+
line=dict(color='rgb(255, 20, 147)', width=4))) # Bright pink
|
| 133 |
+
# Add a vertical line for predictions (today's date)
|
| 134 |
+
fig_no2.add_shape(
|
| 135 |
+
dict(
|
| 136 |
+
type="line",
|
| 137 |
+
x0=pd.Timestamp.today(), x1=pd.Timestamp.today(),
|
| 138 |
+
y0=min(no2_values), y1=max(no2_values),
|
| 139 |
+
line=dict(color="White", width=3, dash="dash"),
|
| 140 |
+
)
|
| 141 |
+
)
|
| 142 |
+
fig_no2.update_layout(
|
| 143 |
+
plot_bgcolor='rgba(0, 0, 0, 0)', # Transparent background
|
| 144 |
+
paper_bgcolor='rgba(0, 0, 0, 0)', # Transparent paper background
|
| 145 |
+
yaxis_title="NO2 Concentration (µg/m³)",
|
| 146 |
+
font=dict(size=14),
|
| 147 |
+
hovermode="x unified"
|
| 148 |
+
)
|
| 149 |
+
st.plotly_chart(fig_no2)
|
helper_functions.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
|
| 3 |
+
|
| 4 |
+
# Custom function to create styled metric boxes with subscripts, smaller label, and larger metric
|
| 5 |
+
def custom_metric_box(label, value, delta):
|
| 6 |
+
st.markdown(f"""
|
| 7 |
+
<div style="
|
| 8 |
+
background: rgba(255, 255, 255, 0.05);
|
| 9 |
+
border-radius: 16px;
|
| 10 |
+
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
|
| 11 |
+
backdrop-filter: blur(6px);
|
| 12 |
+
-webkit-backdrop-filter: blur(6px);
|
| 13 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
| 14 |
+
padding: 15px;
|
| 15 |
+
margin-bottom: 10px;
|
| 16 |
+
width: 200px; /* Fixed width */
|
| 17 |
+
">
|
| 18 |
+
<h4 style="font-size: 18px; font-weight: normal; margin: 0;">{label}</h4> <!-- Smaller label -->
|
| 19 |
+
<p style="font-size: 36px; font-weight: bold; margin: 0;">{value}</p> <!-- Larger metric -->
|
| 20 |
+
<p style="color: {'green' if '+' in delta else 'orange'}; margin: 0;">{delta}</p>
|
| 21 |
+
</div>
|
| 22 |
+
""", unsafe_allow_html=True)
|
| 23 |
+
|
| 24 |
+
# Custom function to create pollution metric boxes with side-by-side layout for label and value
|
| 25 |
+
# Custom function to create pollution metric boxes with side-by-side layout and fixed width
|
| 26 |
+
def pollution_box(label, value, delta):
|
| 27 |
+
st.markdown(f"""
|
| 28 |
+
<div style="
|
| 29 |
+
background: rgba(255, 255, 255, 0.05);
|
| 30 |
+
border-radius: 16px;
|
| 31 |
+
box-shadow: 0 4px 30px rgba(0, 0, 0, 0.1);
|
| 32 |
+
backdrop-filter: blur(5px);
|
| 33 |
+
-webkit-backdrop-filter: blur(5px);
|
| 34 |
+
border: 1px solid rgba(255, 255, 255, 0.15);
|
| 35 |
+
padding: 15px;
|
| 36 |
+
margin-bottom: 10px;
|
| 37 |
+
width: 300px; /* Fixed width */
|
| 38 |
+
">
|
| 39 |
+
<h4 style="font-size: 18px; font-weight: normal; margin: 0;">{label}</h4> <!-- Smaller label -->
|
| 40 |
+
<p style="font-size: 36px; font-weight: bold; margin: 0;">{value}</p> <!-- Larger metric -->
|
| 41 |
+
<p style="color: {'green' if '+' in delta else 'orange'}; margin: 0;">{delta}</p>
|
| 42 |
+
</div>
|
| 43 |
+
""", unsafe_allow_html=True)
|
linear_regression_model.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dbe290cfbb7bbd4766aba92ca738296536a79b435b9d9d51e0541d88340261dc
|
| 3 |
+
size 593
|
requirements.txt
CHANGED
|
@@ -2,4 +2,8 @@ streamlit
|
|
| 2 |
pandas
|
| 3 |
numpy
|
| 4 |
joblib # or pickle if you're using that to load the model
|
| 5 |
-
scikit-learn # for mock model
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
pandas
|
| 3 |
numpy
|
| 4 |
joblib # or pickle if you're using that to load the model
|
| 5 |
+
scikit-learn # for mock model
|
| 6 |
+
time
|
| 7 |
+
altair
|
| 8 |
+
matplotlib
|
| 9 |
+
plotly
|