Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -1,23 +1,39 @@
|
|
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 |
custom_objects = {
|
10 |
'mse': MeanSquaredError(),
|
11 |
'mean_squared_error': MeanSquaredError(),
|
12 |
}
|
13 |
|
14 |
try:
|
|
|
15 |
model = load_model('bike_model.h5', custom_objects=custom_objects)
|
16 |
scaler_x = joblib.load('scaler_x.pkl')
|
17 |
scaler_y = joblib.load('scaler_y.pkl')
|
18 |
-
# Allow pickle for feature names loading
|
19 |
features = np.load('feature_names.npy', allow_pickle=True)
|
20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
except Exception as e:
|
22 |
st.error(f"Error loading model: {str(e)}")
|
23 |
raise e
|
@@ -26,13 +42,33 @@ st.title('🚲 Bike Sharing Demand Predictor')
|
|
26 |
st.write('Predict hourly bike rental demand based on weather conditions and time features.')
|
27 |
|
28 |
try:
|
29 |
-
model, scaler_x, scaler_y, feature_names = load_resources()
|
30 |
|
31 |
col1, col2 = st.columns(2)
|
32 |
inputs = {}
|
|
|
33 |
for i, feature in enumerate(feature_names):
|
34 |
with col1 if i % 2 == 0 else col2:
|
35 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
36 |
|
37 |
if st.button('Predict Demand', use_container_width=True):
|
38 |
x = scaler_x.transform(np.array(list(inputs.values())).reshape(1, -1))
|
|
|
1 |
import streamlit as st
|
2 |
import numpy as np
|
3 |
+
import pandas as pd
|
4 |
from tensorflow.keras.models import load_model
|
5 |
from tensorflow.keras.losses import MeanSquaredError
|
6 |
import joblib
|
7 |
|
8 |
@st.cache_resource
|
9 |
def load_resources():
|
10 |
+
# Load model and scalers
|
11 |
custom_objects = {
|
12 |
'mse': MeanSquaredError(),
|
13 |
'mean_squared_error': MeanSquaredError(),
|
14 |
}
|
15 |
|
16 |
try:
|
17 |
+
# Load model resources
|
18 |
model = load_model('bike_model.h5', custom_objects=custom_objects)
|
19 |
scaler_x = joblib.load('scaler_x.pkl')
|
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
|
|
|
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, feature_ranges = load_resources()
|
46 |
|
47 |
col1, col2 = st.columns(2)
|
48 |
inputs = {}
|
49 |
+
|
50 |
for i, feature in enumerate(feature_names):
|
51 |
with col1 if i % 2 == 0 else col2:
|
52 |
+
min_val, max_val, description = feature_ranges[feature]
|
53 |
+
|
54 |
+
if feature in ['season', 'holiday', 'workingday', 'weather']:
|
55 |
+
# Integer input for categorical variables
|
56 |
+
inputs[feature] = st.number_input(
|
57 |
+
f'{description}',
|
58 |
+
min_value=int(min_val),
|
59 |
+
max_value=int(max_val),
|
60 |
+
value=int(min_val),
|
61 |
+
step=1
|
62 |
+
)
|
63 |
+
else:
|
64 |
+
# Float input for continuous variables
|
65 |
+
inputs[feature] = st.number_input(
|
66 |
+
f'{description}',
|
67 |
+
min_value=float(min_val),
|
68 |
+
max_value=float(max_val),
|
69 |
+
value=float(min_val),
|
70 |
+
step=0.1
|
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))
|