File size: 7,140 Bytes
6e8c2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee9b2e8
6e8c2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# Importing the necessary libraries
import streamlit as st
import pandas as pd
import pickle

# Setting up the page configuration for Streamlit App
st.set_page_config(
    page_title=" :mushroom: Mushroom App",
    page_icon="🍄",
    layout="wide",
    initial_sidebar_state="expanded"
)

# Function for user input features
def user_input_features():
    # Creating sliders and select boxes for user input in the sidebar
    cap_diameter = st.sidebar.slider('Cap Diameter',
                            min_value=0.0,
                            max_value=2000.0,
                            value = 1000.0,
                            step=1.0,
    )
    cap_shape = st.sidebar.selectbox('Cap Shape',
                            options=('bell',
                                    'conical',
                                    'convex',
                                    'flat',
                                    'sunken',
                                    'spherical',
                                    'other',)
    )
    gill_attachment = st.sidebar.selectbox('Gill Attachment',
                            options=('adnate',
                                    'adnexed',
                                    'decurrent',
                                    'free',
                                    'sinuate',
                                    'pores',
                                    'none',)
    )
    gill_color = st.sidebar.selectbox('Gill Color',
                            options=('brown',
                                    'buff',
                                    'gray',
                                    'green',
                                    'pink',
                                    'purple',
                                    'red',
                                    'white',
                                    'yellow',
                                    'blue',
                                    'orange',
                                    'black',)
    )
    stem_height = st.sidebar.slider('Stem Height',
                            min_value=0.0,
                            max_value=4.0,
                            value=2.0,
                            step=0.1,
    )
    stem_width = st.sidebar.slider('Stem Width',
                            min_value=0.0,
                            max_value=4000.0,
                            value=2000.0,
                            step=1.0,
    )
    stem_color = st.sidebar.selectbox('Stem Color',
                            options=('brown',
                                    'buff',
                                    'gray',
                                    'green',
                                    'pink',
                                    'purple',
                                    'red',
                                    'white',
                                    'yellow',
                                    'blue',
                                    'orange',
                                    'black',)
    )
    season = st.sidebar.selectbox('Season',
                            options=('spring',
                                    'summer',
                                    'autumn',
                                    'winter',)
    )

    # Function to get the color code
    def get_color(color_name):
        color_dict = {
            'brown': 0,
            'buff': 1,
            'gray': 2,
            'green': 3,
            'pink': 4,
            'purple': 5,
            'red': 6,
            'white': 7,
            'yellow': 8,
            'blue': 9,
            'orange': 10,
            'black': 11,
        }
        return color_dict.get(color_name.lower(), "not found")

    # Function to get the cap shape code
    def get_cap_shape(cap_shape):
        shape_dict = {
            'bell': 0,
            'conical': 1,
            'convex': 2,
            'flat': 3,
            'sunken': 4,
            'spherical': 5,
            'other': 6,
        }
        return shape_dict.get(cap_shape.lower(), "not found")

    # Function to get gill attachment code
    def get_gill_attachment(gill_attachment):
        gill_attachment_dict = {
            'adnate': 0,
            'adnexed': 1,
            'decurrent': 2,
            'free': 3,
            'sinuate': 4,
            'pores': 5,
            'none': 6,
        }
        return gill_attachment_dict.get(gill_attachment.lower(), "not found")

    # Function to get season code
    def get_season(season):
        season_dict = {
            'spring': 0,
            'summer': 1,
            'autumn': 2,
            'winter': 3,
        }
        return season_dict.get(season.lower(), "not found")

    # Creating a data dictionary to store the user input data
    data = {'cap-diameter': cap_diameter,
            'cap-shape': get_cap_shape(cap_shape),
            'gill-attachment': get_gill_attachment(gill_attachment),
            'gill-color': get_color(gill_color),
            'stem-height': stem_height,
            'stem-width': stem_width,
            'stem-color': get_color(stem_color),
            'season': get_season(season),
    }

    # Creating a DataFrame from the data dictionary
    features = pd.DataFrame(data, index=[0])
    return features

# Function to load the prediction model
#@st.cache_data()
def get_model():
    model = pickle.load(open("models/rfc_model.pkl", "rb"))
    return model

# Function to make prediction using the model and input data
def make_prediction(data):
    model = get_model()
    return model.predict(data)

# Function to process uploaded CSV file and make predictions
def process_file(file):
    data = pd.read_csv(file)
    model = get_model()
    predictions = model.predict(data)
    data['prediction'] = predictions
    return data

# Main function
def main():
    st.write("""# :mushroom: Mushroom App""")
    st.sidebar.image("img/dataset-cover.jpg")
    user_data = user_input_features()

    # Creating a session state button for prediction
    if 'btn_predict' not in st.session_state:
        st.session_state['btn_predict'] = False

    st.session_state['btn_predict'] = st.button("Predict")

    # Making prediction and showing result
    if st.session_state['btn_predict'] == True:
        if make_prediction(user_data) == 1:
            st.error("# Result: Poisonous :skull_and_crossbones: ")
        else:
            st.success("# Result: Edible :mushroom: ")

    # File upload for batch prediction
    st.write("## Загрузка CSV-файла с данными о грибах для массового предсказания")
    uploaded_file = st.file_uploader("Choose a CSV file", type="csv")
    if uploaded_file is not None:
        result_df = process_file(uploaded_file)
        st.write(result_df)
        csv = result_df.to_csv(index=False).encode('utf-8')
        st.download_button(
            label="Download predictions as CSV",
            data=csv,
            file_name='predictions.csv',
            mime='text/csv',
        )

# Running the main function
if __name__ == "__main__":
    main()