JohanBeytell commited on
Commit
6044158
·
verified ·
1 Parent(s): 9439f68

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -81
app.py CHANGED
@@ -7,97 +7,40 @@ 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
  )
 
7
  mappings = joblib.load('mappings.pkl')
8
 
9
  feature_options = {
10
+ 'cap-shape': {'bell': 'b', 'conical': 'c', 'convex': 'x', 'flat': 'f', 'knobbed': 'k', 'sunken': 's'},
11
+ 'cap-surface': {'fibrous': 'f', 'grooves': 'g', 'scaly': 'y', 'smooth': 's'},
12
+ 'cap-color': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'green': 'r', 'pink': 'p', 'purple': 'u', 'red': 'e', 'white': 'w', 'yellow': 'y'},
13
+ 'bruises': {'bruises': 't', 'no': 'f'},
14
+ 'odor': {'almond': 'a', 'anise': 'l', 'creosote': 'c', 'fishy': 'y', 'foul': 'f', 'musty': 'm', 'none': 'n', 'pungent': 'p', 'spicy': 's'},
15
+ 'gill-attachment': {'attached': 'a', 'descending': 'd', 'free': 'f', 'notched': 'n'},
16
+ 'gill-spacing': {'close': 'c', 'crowded': 'w', 'distant': 'd'},
17
+ 'gill-size': {'broad': 'b', 'narrow': 'n'},
18
+ 'gill-color': {'black': 'k', 'brown': 'n', 'buff': 'b', 'chocolate': 'h', 'gray': 'g', 'green': 'r', 'orange': 'o', 'pink': 'p', 'purple': 'u', 'red': 'e', 'white': 'w', 'yellow': 'y'},
19
+ 'stalk-shape': {'enlarging': 'e', 'tapering': 't'},
20
+ 'stalk-root': {'bulbous': 'b', 'club': 'c', 'cup': 'u', 'equal': 'e', 'rhizomorphs': 'z', 'rooted': 'r', 'missing': '?'},
21
+ 'stalk-surface-above-ring': {'fibrous': 'f', 'scaly': 'y', 'silky': 'k', 'smooth': 's'},
22
+ 'stalk-surface-below-ring': {'fibrous': 'f', 'scaly': 'y', 'silky': 'k', 'smooth': 's'},
23
+ 'stalk-color-above-ring': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'orange': 'o', 'pink': 'p', 'red': 'e', 'white': 'w', 'yellow': 'y'},
24
+ 'stalk-color-below-ring': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'orange': 'o', 'pink': 'p', 'red': 'e', 'white': 'w', 'yellow': 'y'},
25
+ 'veil-type': {'partial': 'p', 'universal': 'u'},
26
+ 'veil-color': {'brown': 'n', 'orange': 'o', 'white': 'w', 'yellow': 'y'},
27
+ 'ring-number': {'none': 'n', 'one': 'o', 'two': 't'},
28
+ 'ring-type': {'cobwebby': 'c', 'evanescent': 'e', 'flaring': 'f', 'large': 'l', 'none': 'n', 'pendant': 'p', 'sheathing': 's', 'zone': 'z'},
29
+ 'spore-print-color': {'black': 'k', 'brown': 'n', 'buff': 'b', 'chocolate': 'h', 'green': 'r', 'orange': 'o', 'purple': 'u', 'white': 'w', 'yellow': 'y'},
30
+ 'population': {'abundant': 'a', 'clustered': 'c', 'numerous': 'n', 'scattered': 's', 'several': 'v', 'solitary': 'y'},
31
+ 'habitat': {'grasses': 'g', 'leaves': 'l', 'meadows': 'm', 'paths': 'p', 'urban': 'u', 'waste': 'w', 'woods': 'd'}
32
  }
33
 
34
  def predict_mushroom(features):
35
+ numerical_features = {feature: feature_options[feature][value] for feature, value in features.items()}
 
 
 
 
 
36
  input_df = pd.DataFrame([numerical_features])
37
  prediction = model.predict(input_df)
38
  return 'Poisonous' if prediction[0] == 1 else 'Edible'
39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
  demo = gr.Interface(
41
  fn=predict_mushroom,
42
+ inputs=[gr.Dropdown(choices=list(options.keys()), label=feature) for feature, options in feature_options.items()],
43
  outputs="text",
 
44
  title="MycoNom - Mushroom Edibility Classifier",
45
  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."
46
  )