Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,37 +1,46 @@
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.models import load_model
|
4 |
-
from tensorflow.keras.losses import MeanSquaredError
|
5 |
import joblib
|
6 |
|
7 |
@st.cache_resource
|
8 |
def load_resources():
|
9 |
-
|
10 |
-
custom_objects = {'loss': MeanSquaredError()}
|
11 |
-
model = load_model('bike_model.h5', custom_objects=custom_objects)
|
12 |
scaler_x = joblib.load('scaler_x.pkl')
|
13 |
scaler_y = joblib.load('scaler_y.pkl')
|
14 |
features = np.load('feature_names.npy')
|
15 |
return model, scaler_x, scaler_y, features
|
16 |
|
17 |
st.title('🚲 Bike Sharing Demand Predictor')
|
|
|
18 |
|
19 |
-
|
|
|
20 |
|
21 |
-
|
22 |
-
|
23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
-
if st.button('Predict'):
|
26 |
-
try:
|
27 |
-
# Scale and reshape input
|
28 |
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
|
29 |
x = x.reshape(1, 1, x.shape[1])
|
30 |
|
31 |
-
# Predict and inverse transform
|
32 |
pred_scaled = model.predict(x)
|
33 |
pred = scaler_y.inverse_transform(pred_scaled)[0][0]
|
34 |
|
35 |
st.success(f'Predicted demand: {max(0, int(pred))} bikes')
|
36 |
-
|
37 |
-
|
|
|
|
|
|
|
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
from tensorflow.keras.models import load_model
|
|
|
4 |
import joblib
|
5 |
|
6 |
@st.cache_resource
|
7 |
def load_resources():
|
8 |
+
model = load_model('bike_model.h5')
|
|
|
|
|
9 |
scaler_x = joblib.load('scaler_x.pkl')
|
10 |
scaler_y = joblib.load('scaler_y.pkl')
|
11 |
features = np.load('feature_names.npy')
|
12 |
return model, scaler_x, scaler_y, features
|
13 |
|
14 |
st.title('🚲 Bike Sharing Demand Predictor')
|
15 |
+
st.write('Predict hourly bike rental demand based on weather conditions and time features.')
|
16 |
|
17 |
+
try:
|
18 |
+
model, scaler_x, scaler_y, feature_names = load_resources()
|
19 |
|
20 |
+
# Create columns for better layout
|
21 |
+
col1, col2 = st.columns(2)
|
22 |
+
|
23 |
+
inputs = {}
|
24 |
+
for i, feature in enumerate(feature_names):
|
25 |
+
# Alternate between columns
|
26 |
+
with col1 if i % 2 == 0 else col2:
|
27 |
+
inputs[feature] = st.number_input(
|
28 |
+
f'{feature}',
|
29 |
+
value=0.0,
|
30 |
+
help=f'Enter value for {feature}'
|
31 |
+
)
|
32 |
|
33 |
+
if st.button('Predict Demand', use_container_width=True):
|
|
|
|
|
34 |
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
|
35 |
x = x.reshape(1, 1, x.shape[1])
|
36 |
|
|
|
37 |
pred_scaled = model.predict(x)
|
38 |
pred = scaler_y.inverse_transform(pred_scaled)[0][0]
|
39 |
|
40 |
st.success(f'Predicted demand: {max(0, int(pred))} bikes')
|
41 |
+
|
42 |
+
except Exception as e:
|
43 |
+
st.error(f'Error: {str(e)}')
|
44 |
+
|
45 |
+
st.markdown('---')
|
46 |
+
st.markdown('Made with ❤️ using Streamlit and TensorFlow')
|