Emil25/mlops_final_project
1
1# Importing the necessary libraries2import streamlit as st3import pandas as pd4import pickle5 6 7# Setting up the page configuration for Streamlit App8st.set_page_config(9 page_title=" :mushroom: Mushroom App",10 page_icon="๐",11 layout="wide",12 initial_sidebar_state="expanded"13)14 15 16# Function for user input features17def user_input_features():18 # Creating sliders and select boxes for user input in the sidebar19 cap_diameter = st.sidebar.slider('Cap Diameter',20 min_value=0.0,21 max_value=2000.0,22 value = 1000.0,23 step=1.0,24 )25 cap_shape = st.sidebar.selectbox('Cap Shape',26 options=('bell',27 'conical',28 'convex',29 'flat',30 'sunken',31 'spherical',32 'other',)33 )34 gill_attachment = st.sidebar.selectbox('Gill Attachment',35 options=('adnate',36 'adnexed',37 'decurrent',38 'free',39 'sinuate',40 'pores',41 'none',)42 )43 gill_color = st.sidebar.selectbox('Gill Color',44 options=('brown',45 'buff',46 'gray',47 'green',48 'pink',49 'purple',50 'red',51 'white',52 'yellow',53 'blue',54 'orange',55 'black',)56 )57 stem_height = st.sidebar.slider('Stem Height',58 min_value=0.0,59 max_value=4.0,60 value=2.0,61 step=0.1,62 )63 stem_width = st.sidebar.slider('Stem Width',64 min_value=0.0,65 max_value=4000.0,66 value=2000.0,67 step=1.0,68 )69 stem_color = st.sidebar.selectbox('Stem Color',70 options=('brown',71 'buff',72 'gray',73 'green',74 'pink',75 'purple',76 'red',77 'white',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 code92 def get_color(color_name):93 color_dict = {94 'brown': 0,95 'buff': 1,96 'gray': 2,97 'green': 3,98 'pink': 4,99 'purple': 5,100 'red': 6,101 'white': 7,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 code111 def get_cap_shape(cap_shape):112 shape_dict = {113 'bell': 0,114 'conical': 1,115 'convex': 2,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 code125 def get_gill_attachment(gill_attachment):126 gill_attachment_dict = {127 'adnate': 0,128 'adnexed': 1,129 'decurrent': 2,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 code139 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 data149 data = {'cap-diameter': cap_diameter,150 'cap-shape': get_cap_shape(cap_shape),151 'gill-attachment': get_gill_attachment(gill_attachment),152 'gill-color': get_color(gill_color),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 dictionary160 features = pd.DataFrame(data, index=[0])161 return features162 163 164# Function to load the prediction model165#@st.cache_data()166def get_model():167 model = pickle.load(open("models/rfc_model.pkl", "rb"))168 return model169 170 171# Function to make prediction using the model and input data172def make_prediction(data):173 model = get_model()174 return model.predict(data)175 176 177# Main function178def 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 prediction184 if 'btn_predict' not in st.session_state:185 st.session_state['btn_predict'] = False186 187 st.session_state['btn_predict'] = st.button("Predict")188 189 # Making prediction and showing result190 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 function198if __name__ == "__main__":199 main()