alperugurcan commited on
Commit
cd44e6c
·
verified ·
1 Parent(s): 1363fb9

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +61 -55
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
- 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
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, 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))
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')