alperugurcan's picture
Update app.py
787860e verified
raw
history blame
3.13 kB
import streamlit as st
import numpy as np
from tensorflow.keras.models import load_model
from tensorflow.keras.losses import MeanSquaredError
import joblib
@st.cache_resource
def load_resources():
custom_objects = {
'mse': MeanSquaredError()
}
model = load_model('bike_model.h5', custom_objects=custom_objects)
scaler_x = joblib.load('scaler_x.pkl')
scaler_y = joblib.load('scaler_y.pkl')
features = np.load('feature_names.npy', allow_pickle=True)
return model, scaler_x, scaler_y, features
st.title('🚲 Bike Sharing Demand Predictor')
st.write('Predict hourly bike rental demand based on weather conditions and time features.')
try:
model, scaler_x, scaler_y, feature_names = load_resources()
col1, col2 = st.columns(2)
with col1:
# Categorical inputs
season = st.selectbox('season', options=[1, 2, 3, 4],
help='1:Spring, 2:Summer, 3:Fall, 4:Winter')
holiday = st.selectbox('holiday', options=[0, 1],
help='0:No, 1:Yes')
workingday = st.selectbox('workingday', options=[0, 1],
help='0:No, 1:Yes')
weather = st.selectbox('weather', options=[1, 2, 3, 4],
help='1:Clear, 2:Mist, 3:Light Rain/Snow, 4:Heavy Rain')
with col2:
# Continuous inputs with sliders
temp = st.slider('temp (°C)', min_value=0.82, max_value=41.0,
value=20.0, step=0.1)
atemp = st.slider('feels like temp', min_value=0.76, max_value=45.5,
value=23.7, step=0.1)
humidity = st.slider('humidity (%)', min_value=0, max_value=100,
value=62)
windspeed = st.slider('windspeed', min_value=0.0, max_value=57.0,
value=13.0, step=0.1)
# Create input dictionary
inputs = {
'season': season,
'holiday': holiday,
'workingday': workingday,
'weather': weather,
'temp': temp,
'atemp': atemp,
'humidity': humidity,
'windspeed': windspeed
}
if st.button('Predict Demand', use_container_width=True):
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
x = x.reshape(1, 1, x.shape[1])
pred_scaled = model.predict(x, verbose=0)
pred = scaler_y.inverse_transform(pred_scaled)[0][0]
st.success(f'Predicted demand: {max(0, int(pred))} bikes')
except Exception as e:
st.error(f'Error: {str(e)}')
st.markdown('---')
st.markdown("""
### Feature Information:
- **season**: 1=Spring, 2=Summer, 3=Fall, 4=Winter
- **holiday**: 0=No, 1=Yes
- **workingday**: 0=No, 1=Yes
- **weather**: 1=Clear, 2=Mist, 3=Light Rain/Snow, 4=Heavy Rain
- **temp**: Temperature in Celsius (0.82°C to 41°C)
- **atemp**: "Feels like" temperature (0.76°C to 45.5°C)
- **humidity**: Relative humidity (0% to 100%)
- **windspeed**: Wind speed (0 to 57)
""")
st.markdown('Made with ❤️ using Streamlit and TensorFlow')