Spaces:
Runtime error
Runtime error
alperugurcan
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,75 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
-
import pandas as pd
|
4 |
from tensorflow.keras.models import load_model
|
5 |
-
|
6 |
import joblib
|
7 |
|
|
|
|
|
8 |
@st.cache_resource
|
9 |
def load_resources():
|
10 |
-
# Load model and scalers
|
11 |
custom_objects = {
|
12 |
-
'mse':
|
13 |
-
'mean_squared_error': MeanSquaredError(),
|
14 |
}
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
scaler_y = joblib.load('scaler_y.pkl')
|
21 |
-
features = np.load('feature_names.npy', allow_pickle=True)
|
22 |
-
|
23 |
-
# Load original data to get min-max values
|
24 |
-
df = pd.read_csv('train.csv')
|
25 |
-
feature_ranges = {
|
26 |
-
'season': (1, 4, 'Season (1:Spring, 2:Summer, 3:Fall, 4:Winter)'),
|
27 |
-
'holiday': (0, 1, 'Holiday (0:No, 1:Yes)'),
|
28 |
-
'workingday': (0, 1, 'Working Day (0:No, 1:Yes)'),
|
29 |
-
'weather': (1, 4, 'Weather (1:Clear, 2:Mist, 3:Light Rain/Snow, 4:Heavy Rain/Snow)'),
|
30 |
-
'temp': (df['temp'].min(), df['temp'].max(), 'Temperature (Celsius)'),
|
31 |
-
'atemp': (df['atemp'].min(), df['atemp'].max(), 'Feels Like Temperature (Celsius)'),
|
32 |
-
'humidity': (df['humidity'].min(), df['humidity'].max(), 'Humidity (%)'),
|
33 |
-
'windspeed': (df['windspeed'].min(), df['windspeed'].max(), 'Wind Speed')
|
34 |
-
}
|
35 |
-
|
36 |
-
return model, scaler_x, scaler_y, features, feature_ranges
|
37 |
-
except Exception as e:
|
38 |
-
st.error(f"Error loading model: {str(e)}")
|
39 |
-
raise e
|
40 |
|
41 |
st.title('🚲 Bike Sharing Demand Predictor')
|
42 |
st.write('Predict hourly bike rental demand based on weather conditions and time features.')
|
43 |
|
44 |
try:
|
45 |
-
model, scaler_x, scaler_y, feature_names
|
46 |
|
47 |
col1, col2 = st.columns(2)
|
48 |
-
inputs = {}
|
49 |
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
if st.button('Predict Demand', use_container_width=True):
|
74 |
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
|
75 |
x = x.reshape(1, 1, x.shape[1])
|
76 |
|
77 |
-
pred_scaled = model.predict(x)
|
78 |
pred = scaler_y.inverse_transform(pred_scaled)[0][0]
|
79 |
|
80 |
st.success(f'Predicted demand: {max(0, int(pred))} bikes')
|
@@ -83,4 +78,15 @@ except Exception as e:
|
|
83 |
st.error(f'Error: {str(e)}')
|
84 |
|
85 |
st.markdown('---')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
86 |
st.markdown('Made with ❤️ using Streamlit and TensorFlow')
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
|
|
3 |
from tensorflow.keras.models import load_model
|
4 |
+
import tensorflow.keras.losses
|
5 |
import joblib
|
6 |
|
7 |
+
tensorflow.keras.losses.mse = tensorflow.keras.losses.mean_squared_error
|
8 |
+
|
9 |
@st.cache_resource
|
10 |
def load_resources():
|
|
|
11 |
custom_objects = {
|
12 |
+
'mse': tensorflow.keras.losses.mean_squared_error
|
|
|
13 |
}
|
14 |
+
model = load_model('bike_model.h5', custom_objects=custom_objects)
|
15 |
+
scaler_x = joblib.load('scaler_x.pkl')
|
16 |
+
scaler_y = joblib.load('scaler_y.pkl')
|
17 |
+
features = np.load('feature_names.npy', allow_pickle=True)
|
18 |
+
return model, scaler_x, scaler_y, features
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
20 |
st.title('🚲 Bike Sharing Demand Predictor')
|
21 |
st.write('Predict hourly bike rental demand based on weather conditions and time features.')
|
22 |
|
23 |
try:
|
24 |
+
model, scaler_x, scaler_y, feature_names = load_resources()
|
25 |
|
26 |
col1, col2 = st.columns(2)
|
|
|
27 |
|
28 |
+
with col1:
|
29 |
+
# Categorical inputs
|
30 |
+
season = st.selectbox('season', options=[1, 2, 3, 4],
|
31 |
+
help='1:Spring, 2:Summer, 3:Fall, 4:Winter')
|
32 |
+
|
33 |
+
holiday = st.selectbox('holiday', options=[0, 1],
|
34 |
+
help='0:No, 1:Yes')
|
35 |
+
|
36 |
+
workingday = st.selectbox('workingday', options=[0, 1],
|
37 |
+
help='0:No, 1:Yes')
|
38 |
+
|
39 |
+
weather = st.selectbox('weather', options=[1, 2, 3, 4],
|
40 |
+
help='1:Clear, 2:Mist, 3:Light Rain/Snow, 4:Heavy Rain')
|
41 |
+
|
42 |
+
with col2:
|
43 |
+
# Continuous inputs with sliders
|
44 |
+
temp = st.slider('temp (°C)', min_value=0.82, max_value=41.0,
|
45 |
+
value=20.0, step=0.1)
|
46 |
+
|
47 |
+
atemp = st.slider('feels like temp', min_value=0.76, max_value=45.5,
|
48 |
+
value=23.7, step=0.1)
|
49 |
+
|
50 |
+
humidity = st.slider('humidity (%)', min_value=0, max_value=100,
|
51 |
+
value=62)
|
52 |
+
|
53 |
+
windspeed = st.slider('windspeed', min_value=0.0, max_value=57.0,
|
54 |
+
value=13.0, step=0.1)
|
55 |
+
|
56 |
+
# Create input dictionary
|
57 |
+
inputs = {
|
58 |
+
'season': season,
|
59 |
+
'holiday': holiday,
|
60 |
+
'workingday': workingday,
|
61 |
+
'weather': weather,
|
62 |
+
'temp': temp,
|
63 |
+
'atemp': atemp,
|
64 |
+
'humidity': humidity,
|
65 |
+
'windspeed': windspeed
|
66 |
+
}
|
67 |
|
68 |
if st.button('Predict Demand', use_container_width=True):
|
69 |
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
|
70 |
x = x.reshape(1, 1, x.shape[1])
|
71 |
|
72 |
+
pred_scaled = model.predict(x, verbose=0)
|
73 |
pred = scaler_y.inverse_transform(pred_scaled)[0][0]
|
74 |
|
75 |
st.success(f'Predicted demand: {max(0, int(pred))} bikes')
|
|
|
78 |
st.error(f'Error: {str(e)}')
|
79 |
|
80 |
st.markdown('---')
|
81 |
+
st.markdown("""
|
82 |
+
### Feature Information:
|
83 |
+
- **season**: 1=Spring, 2=Summer, 3=Fall, 4=Winter
|
84 |
+
- **holiday**: 0=No, 1=Yes
|
85 |
+
- **workingday**: 0=No, 1=Yes
|
86 |
+
- **weather**: 1=Clear, 2=Mist, 3=Light Rain/Snow, 4=Heavy Rain
|
87 |
+
- **temp**: Temperature in Celsius (0.82°C to 41°C)
|
88 |
+
- **atemp**: "Feels like" temperature (0.76°C to 45.5°C)
|
89 |
+
- **humidity**: Relative humidity (0% to 100%)
|
90 |
+
- **windspeed**: Wind speed (0 to 57)
|
91 |
+
""")
|
92 |
st.markdown('Made with ❤️ using Streamlit and TensorFlow')
|