Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,77 +2,104 @@ import gradio as gr
|
|
2 |
import joblib
|
3 |
import pandas as pd
|
4 |
|
5 |
-
# Load
|
6 |
model = joblib.load('mushroom_classifier.pkl')
|
7 |
mappings = joblib.load('mappings.pkl')
|
8 |
|
9 |
feature_options = {
|
10 |
-
'cap-shape':
|
11 |
-
'cap-surface':
|
12 |
-
'cap-color':
|
13 |
-
'bruises':
|
14 |
-
'odor':
|
15 |
-
'gill-attachment':
|
16 |
-
'gill-spacing':
|
17 |
-
'gill-size':
|
18 |
-
'gill-color':
|
19 |
-
'stalk-shape':
|
20 |
-
'stalk-root':
|
21 |
-
'
|
22 |
-
'
|
23 |
-
'ring
|
24 |
-
'ring
|
25 |
-
'
|
26 |
-
'
|
27 |
-
'
|
|
|
|
|
|
|
|
|
28 |
}
|
29 |
|
30 |
def predict_mushroom(features):
|
31 |
numerical_features = {}
|
32 |
for feature, value in features.items():
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
input_df = pd.DataFrame([numerical_features])
|
39 |
prediction = model.predict(input_df)
|
40 |
-
return
|
41 |
|
42 |
examples = [
|
43 |
-
{ # Death Cap
|
44 |
-
'cap-shape': '
|
45 |
-
'
|
46 |
-
'
|
47 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
48 |
},
|
49 |
-
{ #
|
50 |
-
'cap-shape': '
|
51 |
-
'
|
52 |
-
'
|
53 |
-
'
|
54 |
-
|
55 |
-
|
56 |
-
'
|
57 |
-
'gill-
|
58 |
-
'
|
59 |
-
'
|
60 |
-
|
61 |
-
|
62 |
-
'
|
63 |
-
'
|
64 |
-
'stalk-
|
65 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
}
|
67 |
]
|
68 |
|
69 |
demo = gr.Interface(
|
70 |
fn=predict_mushroom,
|
71 |
-
inputs=[gr.Dropdown(choices=
|
72 |
outputs="text",
|
73 |
-
examples=[[
|
74 |
-
title="MycoNom - Mushroom Classifier",
|
75 |
-
description="Select mushroom
|
76 |
)
|
77 |
|
78 |
demo.launch()
|
|
|
2 |
import joblib
|
3 |
import pandas as pd
|
4 |
|
5 |
+
# Load model and mappings
|
6 |
model = joblib.load('mushroom_classifier.pkl')
|
7 |
mappings = joblib.load('mappings.pkl')
|
8 |
|
9 |
feature_options = {
|
10 |
+
'cap-shape': ['bell', 'conical', 'convex', 'flat', 'knobbed', 'sunken'],
|
11 |
+
'cap-surface': ['fibrous', 'grooves', 'scaly', 'smooth'],
|
12 |
+
'cap-color': ['brown', 'buff', 'cinnamon', 'gray', 'green', 'pink', 'purple', 'red', 'white', 'yellow'],
|
13 |
+
'bruises': ['bruises', 'no'],
|
14 |
+
'odor': ['almond', 'anise', 'creosote', 'fishy', 'foul', 'musty', 'none', 'pungent', 'spicy'],
|
15 |
+
'gill-attachment': ['attached', 'descending', 'free', 'notched'],
|
16 |
+
'gill-spacing': ['close', 'crowded', 'distant'],
|
17 |
+
'gill-size': ['broad', 'narrow'],
|
18 |
+
'gill-color': ['black', 'brown', 'buff', 'chocolate', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'white', 'yellow'],
|
19 |
+
'stalk-shape': ['enlarging', 'tapering'],
|
20 |
+
'stalk-root': ['bulbous', 'club', 'cup', 'equal', 'rhizomorphs', 'rooted', 'missing'],
|
21 |
+
'stalk-surface-above-ring': ['fibrous', 'scaly', 'silky', 'smooth'],
|
22 |
+
'stalk-surface-below-ring': ['fibrous', 'scaly', 'silky', 'smooth'],
|
23 |
+
'stalk-color-above-ring': ['brown', 'buff', 'cinnamon', 'gray', 'orange', 'pink', 'red', 'white', 'yellow'],
|
24 |
+
'stalk-color-below-ring': ['brown', 'buff', 'cinnamon', 'gray', 'orange', 'pink', 'red', 'white', 'yellow'],
|
25 |
+
'veil-type': ['partial', 'universal'],
|
26 |
+
'veil-color': ['brown', 'orange', 'white', 'yellow'],
|
27 |
+
'ring-number': ['none', 'one', 'two'],
|
28 |
+
'ring-type': ['cobwebby', 'evanescent', 'flaring', 'large', 'none', 'pendant', 'sheathing', 'zone'],
|
29 |
+
'spore-print-color': ['black', 'brown', 'buff', 'chocolate', 'green', 'orange', 'purple', 'white', 'yellow'],
|
30 |
+
'population': ['abundant', 'clustered', 'numerous', 'scattered', 'several', 'solitary'],
|
31 |
+
'habitat': ['grasses', 'leaves', 'meadows', 'paths', 'urban', 'waste', 'woods']
|
32 |
}
|
33 |
|
34 |
def predict_mushroom(features):
|
35 |
numerical_features = {}
|
36 |
for feature, value in features.items():
|
37 |
+
for key, val in mappings[feature].items():
|
38 |
+
if val == value:
|
39 |
+
numerical_features[feature] = key
|
40 |
+
break
|
|
|
41 |
input_df = pd.DataFrame([numerical_features])
|
42 |
prediction = model.predict(input_df)
|
43 |
+
return 'Poisonous' if prediction[0] == 1 else 'Edible'
|
44 |
|
45 |
examples = [
|
46 |
+
{ # Death Cap
|
47 |
+
'cap-shape': 'convex',
|
48 |
+
'cap-surface': 'smooth',
|
49 |
+
'cap-color': 'green',
|
50 |
+
'bruises': 'no',
|
51 |
+
'odor': 'foul',
|
52 |
+
'gill-attachment': 'free',
|
53 |
+
'gill-spacing': 'crowded',
|
54 |
+
'gill-size': 'narrow',
|
55 |
+
'gill-color': 'white',
|
56 |
+
'stalk-shape': 'tapering',
|
57 |
+
'stalk-root': 'bulbous',
|
58 |
+
'stalk-surface-above-ring': 'smooth',
|
59 |
+
'stalk-surface-below-ring': 'smooth',
|
60 |
+
'stalk-color-above-ring': 'white',
|
61 |
+
'stalk-color-below-ring': 'white',
|
62 |
+
'veil-type': 'partial',
|
63 |
+
'veil-color': 'white',
|
64 |
+
'ring-number': 'one',
|
65 |
+
'ring-type': 'pendant',
|
66 |
+
'spore-print-color': 'white',
|
67 |
+
'population': 'several',
|
68 |
+
'habitat': 'woods'
|
69 |
},
|
70 |
+
{ # Edible Brown Mushroom
|
71 |
+
'cap-shape': 'convex',
|
72 |
+
'cap-surface': 'smooth',
|
73 |
+
'cap-color': 'brown',
|
74 |
+
'bruises': 'no',
|
75 |
+
'odor': 'almond',
|
76 |
+
'gill-attachment': 'free',
|
77 |
+
'gill-spacing': 'crowded',
|
78 |
+
'gill-size': 'broad',
|
79 |
+
'gill-color': 'brown',
|
80 |
+
'stalk-shape': 'enlarging',
|
81 |
+
'stalk-root': 'equal',
|
82 |
+
'stalk-surface-above-ring': 'smooth',
|
83 |
+
'stalk-surface-below-ring': 'smooth',
|
84 |
+
'stalk-color-above-ring': 'white',
|
85 |
+
'stalk-color-below-ring': 'white',
|
86 |
+
'veil-type': 'partial',
|
87 |
+
'veil-color': 'white',
|
88 |
+
'ring-number': 'one',
|
89 |
+
'ring-type': 'pendant',
|
90 |
+
'spore-print-color': 'brown',
|
91 |
+
'population': 'numerous',
|
92 |
+
'habitat': 'grasses'
|
93 |
}
|
94 |
]
|
95 |
|
96 |
demo = gr.Interface(
|
97 |
fn=predict_mushroom,
|
98 |
+
inputs=[gr.Dropdown(choices=options, label=feature) for feature, options in feature_options.items()],
|
99 |
outputs="text",
|
100 |
+
examples=[[ex[feature] for feature in feature_options] for ex in examples],
|
101 |
+
title="MycoNom - Mushroom Edibility Classifier",
|
102 |
+
description="Select the mushroom features to determine if it's edible or poisonous.<br><br>**Disclaimer:** This model is for **educational purposes only** and should not be used for real-life mushroom classification or any decision-making processes related to the consumption of mushrooms. While the model performs well on the provided dataset, it has not been thoroughly validated for real-world scenarios and may not accurately detect poisonous mushrooms in all conditions. Always consult an expert or use trusted resources when identifying mushrooms."
|
103 |
)
|
104 |
|
105 |
demo.launch()
|