Emil25 commited on
Commit
41cfb5c
β€’
1 Parent(s): 6528274

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +39 -22
app.py CHANGED
@@ -1,7 +1,10 @@
 
1
  import streamlit as st
2
  import pandas as pd
3
  import pickle
4
 
 
 
5
  st.set_page_config(
6
  page_title=" :mushroom: Mushroom App",
7
  page_icon="πŸ„",
@@ -10,7 +13,9 @@ st.set_page_config(
10
  )
11
 
12
 
 
13
  def user_input_features():
 
14
  cap_diameter = st.sidebar.slider('Cap Diameter',
15
  min_value=0.0,
16
  max_value=2000.0,
@@ -24,7 +29,7 @@ def user_input_features():
24
  'flat',
25
  'sunken',
26
  'spherical',
27
- 'other')
28
  )
29
  gill_attachment = st.sidebar.selectbox('Gill Attachment',
30
  options=('adnate',
@@ -33,7 +38,7 @@ def user_input_features():
33
  'free',
34
  'sinuate',
35
  'pores',
36
- 'none')
37
  )
38
  gill_color = st.sidebar.selectbox('Gill Color',
39
  options=('brown',
@@ -47,7 +52,7 @@ def user_input_features():
47
  'yellow',
48
  'blue',
49
  'orange',
50
- 'black')
51
  )
52
  stem_height = st.sidebar.slider('Stem Height',
53
  min_value=0.0,
@@ -73,16 +78,17 @@ def user_input_features():
73
  'yellow',
74
  'blue',
75
  'orange',
76
- 'black')
77
  )
78
  season = st.sidebar.selectbox('Season',
79
  options=('spring',
80
  'summer',
81
  'autumn',
82
- 'winter')
83
  )
84
-
85
-
 
86
  def get_color(color_name):
87
  color_dict = {
88
  'brown': 0,
@@ -96,11 +102,12 @@ def user_input_features():
96
  'yellow': 8,
97
  'blue': 9,
98
  'orange': 10,
99
- 'black': 11
100
  }
101
  return color_dict.get(color_name.lower(), "not found")
102
-
103
 
 
 
104
  def get_cap_shape(cap_shape):
105
  shape_dict = {
106
  'bell': 0,
@@ -109,11 +116,12 @@ def user_input_features():
109
  'flat': 3,
110
  'sunken': 4,
111
  'spherical': 5,
112
- 'other': 6
113
  }
114
  return shape_dict.get(cap_shape.lower(), "not found")
115
-
116
 
 
 
117
  def get_gill_attachment(gill_attachment):
118
  gill_attachment_dict = {
119
  'adnate': 0,
@@ -122,20 +130,22 @@ def user_input_features():
122
  'free': 3,
123
  'sinuate': 4,
124
  'pores': 5,
125
- 'none': 6
126
  }
127
  return gill_attachment_dict.get(gill_attachment.lower(), "not found")
128
 
129
 
 
130
  def get_season(season):
131
  season_dict = {
132
  'spring': 0,
133
  'summer': 1,
134
  'autumn': 2,
135
- 'winter': 3
136
  }
137
  return season_dict.get(season.lower(), "not found")
138
 
 
139
  data = {'cap-diameter': cap_diameter,
140
  'cap-shape': get_cap_shape(cap_shape),
141
  'gill-attachment': get_gill_attachment(gill_attachment),
@@ -143,40 +153,47 @@ def user_input_features():
143
  'stem-height': stem_height,
144
  'stem-width': stem_width,
145
  'stem-color': get_color(stem_color),
146
- 'season': get_season(season)
147
  }
148
-
 
149
  features = pd.DataFrame(data, index=[0])
150
  return features
151
 
152
 
 
153
  #@st.cache_data()
154
  def get_model():
155
- model = pickle.load(open("models/rfc_model.pkl", "rb"))
156
  return model
157
 
158
 
 
159
  def make_prediction(data):
160
  model = get_model()
161
  return model.predict(data)
162
 
 
 
163
  def main():
164
  st.write("""# :mushroom: Mushroom App""")
165
  st.sidebar.image("img/dataset-cover.jpg")
166
  user_data = user_input_features()
167
 
168
-
169
  if 'btn_predict' not in st.session_state:
170
  st.session_state['btn_predict'] = False
171
 
172
  st.session_state['btn_predict'] = st.button("Predict")
173
-
174
- if st.session_state['btn_predict'] == True:
 
175
  if make_prediction(user_data) == 1:
176
  st.error("# Result: Poisonous :skull_and_crossbones: ")
177
  else:
178
  st.success("# Result: Edible :mushroom: ")
179
-
180
-
181
 
182
- main()
 
 
 
 
1
+ # Importing the necessary libraries
2
  import streamlit as st
3
  import pandas as pd
4
  import pickle
5
 
6
+
7
+ # Setting up the page configuration for Streamlit App
8
  st.set_page_config(
9
  page_title=" :mushroom: Mushroom App",
10
  page_icon="πŸ„",
 
13
  )
14
 
15
 
16
+ # Function for user input features
17
  def user_input_features():
18
+ # Creating sliders and select boxes for user input in the sidebar
19
  cap_diameter = st.sidebar.slider('Cap Diameter',
20
  min_value=0.0,
21
  max_value=2000.0,
 
29
  'flat',
30
  'sunken',
31
  'spherical',
32
+ 'other',)
33
  )
34
  gill_attachment = st.sidebar.selectbox('Gill Attachment',
35
  options=('adnate',
 
38
  'free',
39
  'sinuate',
40
  'pores',
41
+ 'none',)
42
  )
43
  gill_color = st.sidebar.selectbox('Gill Color',
44
  options=('brown',
 
52
  'yellow',
53
  'blue',
54
  'orange',
55
+ 'black',)
56
  )
57
  stem_height = st.sidebar.slider('Stem Height',
58
  min_value=0.0,
 
78
  'yellow',
79
  'blue',
80
  'orange',
81
+ 'black',)
82
  )
83
  season = st.sidebar.selectbox('Season',
84
  options=('spring',
85
  'summer',
86
  'autumn',
87
+ 'winter',)
88
  )
89
+
90
+
91
+ # Function to get the color code
92
  def get_color(color_name):
93
  color_dict = {
94
  'brown': 0,
 
102
  'yellow': 8,
103
  'blue': 9,
104
  'orange': 10,
105
+ 'black': 11,
106
  }
107
  return color_dict.get(color_name.lower(), "not found")
 
108
 
109
+
110
+ # Function to get the cap shape code
111
  def get_cap_shape(cap_shape):
112
  shape_dict = {
113
  'bell': 0,
 
116
  'flat': 3,
117
  'sunken': 4,
118
  'spherical': 5,
119
+ 'other': 6,
120
  }
121
  return shape_dict.get(cap_shape.lower(), "not found")
 
122
 
123
+
124
+ # Function to get gill attachment code
125
  def get_gill_attachment(gill_attachment):
126
  gill_attachment_dict = {
127
  'adnate': 0,
 
130
  'free': 3,
131
  'sinuate': 4,
132
  'pores': 5,
133
+ 'none': 6,
134
  }
135
  return gill_attachment_dict.get(gill_attachment.lower(), "not found")
136
 
137
 
138
+ # Function to get season code
139
  def get_season(season):
140
  season_dict = {
141
  'spring': 0,
142
  'summer': 1,
143
  'autumn': 2,
144
+ 'winter': 3,
145
  }
146
  return season_dict.get(season.lower(), "not found")
147
 
148
+ # Creating a data dictionary to store the user input data
149
  data = {'cap-diameter': cap_diameter,
150
  'cap-shape': get_cap_shape(cap_shape),
151
  'gill-attachment': get_gill_attachment(gill_attachment),
 
153
  'stem-height': stem_height,
154
  'stem-width': stem_width,
155
  'stem-color': get_color(stem_color),
156
+ 'season': get_season(season),
157
  }
158
+
159
+ # Creating a DataFrame from the data dictionary
160
  features = pd.DataFrame(data, index=[0])
161
  return features
162
 
163
 
164
+ # Function to load the prediction model
165
  #@st.cache_data()
166
  def get_model():
167
+ model = pickle.load(open("models/rfc_model.pkl", "rb"))
168
  return model
169
 
170
 
171
+ # Function to make prediction using the model and input data
172
  def make_prediction(data):
173
  model = get_model()
174
  return model.predict(data)
175
 
176
+
177
+ # Main function
178
  def main():
179
  st.write("""# :mushroom: Mushroom App""")
180
  st.sidebar.image("img/dataset-cover.jpg")
181
  user_data = user_input_features()
182
 
183
+ # Creating a session state button for prediction
184
  if 'btn_predict' not in st.session_state:
185
  st.session_state['btn_predict'] = False
186
 
187
  st.session_state['btn_predict'] = st.button("Predict")
188
+
189
+ # Making prediction and showing result
190
+ if st.session_state['btn_predict'] == True:
191
  if make_prediction(user_data) == 1:
192
  st.error("# Result: Poisonous :skull_and_crossbones: ")
193
  else:
194
  st.success("# Result: Edible :mushroom: ")
 
 
195
 
196
+
197
+ # Running the main function
198
+ if __name__ == "__main__":
199
+ main()