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

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +78 -51
app.py CHANGED
@@ -2,77 +2,104 @@ import gradio as gr
2
  import joblib
3
  import pandas as pd
4
 
5
- # Load the trained model and mappings
6
  model = joblib.load('mushroom_classifier.pkl')
7
  mappings = joblib.load('mappings.pkl')
8
 
9
  feature_options = {
10
- 'cap-shape': {'b': 'bell', 'c': 'conical', 'x': 'convex', 'f': 'flat', 'k': 'knobbed', 's': 'sunken'},
11
- 'cap-surface': {'f': 'fibrous', 'g': 'grooves', 'y': 'scaly', 's': 'smooth'},
12
- 'cap-color': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'r': 'green', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'},
13
- 'bruises': {'t': 'bruises', 'f': 'no'},
14
- 'odor': {'a': 'almond', 'l': 'anise', 'c': 'creosote', 'y': 'fishy', 'f': 'foul', 'm': 'musty', 'n': 'none', 'p': 'pungent', 's': 'spicy'},
15
- 'gill-attachment': {'a': 'attached', 'd': 'descending', 'f': 'free', 'n': 'notched'},
16
- 'gill-spacing': {'c': 'close', 'w': 'crowded', 'd': 'distant'},
17
- 'gill-size': {'b': 'broad', 'n': 'narrow'},
18
- 'gill-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'g': 'gray', 'r': 'green', 'o': 'orange', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'},
19
- 'stalk-shape': {'e': 'enlarging', 't': 'tapering'},
20
- 'stalk-root': {'b': 'bulbous', 'c': 'club', 'u': 'cup', 'e': 'equal', 'z': 'rhizomorphs', 'r': 'rooted', '?': 'missing'},
21
- 'veil-type': {'p': 'partial', 'u': 'universal'},
22
- 'veil-color': {'n': 'brown', 'o': 'orange', 'w': 'white', 'y': 'yellow'},
23
- 'ring-number': {'n': 'none', 'o': 'one', 't': 'two'},
24
- 'ring-type': {'c': 'cobwebby', 'e': 'evanescent', 'f': 'flaring', 'l': 'large', 'n': 'none', 'p': 'pendant', 's': 'sheathing', 'z': 'zone'},
25
- 'spore-print-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'r': 'green', 'o': 'orange', 'u': 'purple', 'w': 'white', 'y': 'yellow'},
26
- 'population': {'a': 'abundant', 'c': 'clustered', 'n': 'numerous', 's': 'scattered', 'v': 'several', 'y': 'solitary'},
27
- 'habitat': {'g': 'grasses', 'l': 'leaves', 'm': 'meadows', 'p': 'paths', 'u': 'urban', 'w': 'waste', 'd': 'woods'}
 
 
 
 
28
  }
29
 
30
  def predict_mushroom(features):
31
  numerical_features = {}
32
  for feature, value in features.items():
33
- if feature in mappings and value in mappings[feature]:
34
- numerical_features[feature] = mappings[feature][value]
35
- else:
36
- return "Invalid input. Please check feature values."
37
-
38
  input_df = pd.DataFrame([numerical_features])
39
  prediction = model.predict(input_df)
40
- return "Edible" if prediction[0] == 0 else "Poisonous"
41
 
42
  examples = [
43
- { # Death Cap (Poisonous)
44
- 'cap-shape': 'x', 'cap-surface': 's', 'cap-color': 'r', 'bruises': 'f', 'odor': 'p',
45
- 'gill-attachment': 'f', 'gill-spacing': 'w', 'gill-size': 'n', 'gill-color': 'w',
46
- 'stalk-shape': 'e', 'stalk-root': 'b', 'veil-type': 'p', 'veil-color': 'w',
47
- 'ring-number': 'o', 'ring-type': 'p', 'spore-print-color': 'w', 'population': 'v', 'habitat': 'd'
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
48
  },
49
- { # Common Brown Mushroom (Edible)
50
- 'cap-shape': 'x', 'cap-surface': 's', 'cap-color': 'n', 'bruises': 't', 'odor': 'a',
51
- 'gill-attachment': 'f', 'gill-spacing': 'w', 'gill-size': 'b', 'gill-color': 'h',
52
- 'stalk-shape': 'e', 'stalk-root': 'c', 'veil-type': 'p', 'veil-color': 'w',
53
- 'ring-number': 'o', 'ring-type': 'p', 'spore-print-color': 'n', 'population': 'n', 'habitat': 'm'
54
- },
55
- { # Fly Agaric (Poisonous)
56
- 'cap-shape': 'x', 'cap-surface': 'y', 'cap-color': 'e', 'bruises': 'f', 'odor': 'n',
57
- 'gill-attachment': 'f', 'gill-spacing': 'w', 'gill-size': 'b', 'gill-color': 'w',
58
- 'stalk-shape': 'e', 'stalk-root': 'b', 'veil-type': 'p', 'veil-color': 'w',
59
- 'ring-number': 'o', 'ring-type': 'p', 'spore-print-color': 'w', 'population': 'v', 'habitat': 'd'
60
- },
61
- { # Oyster Mushroom (Edible)
62
- 'cap-shape': 'x', 'cap-surface': 's', 'cap-color': 'w', 'bruises': 'f', 'odor': 'l',
63
- 'gill-attachment': 'd', 'gill-spacing': 'c', 'gill-size': 'b', 'gill-color': 'w',
64
- 'stalk-shape': 't', 'stalk-root': '?', 'veil-type': 'p', 'veil-color': 'w',
65
- 'ring-number': 'n', 'ring-type': 'n', 'spore-print-color': 'u', 'population': 'c', 'habitat': 'd'
 
 
 
 
 
 
66
  }
67
  ]
68
 
69
  demo = gr.Interface(
70
  fn=predict_mushroom,
71
- inputs=[gr.Dropdown(choices=list(options.keys()), label=key) for key, options in feature_options.items()],
72
  outputs="text",
73
- examples=[[v for v in example.values()] for example in examples],
74
- title="MycoNom - Mushroom Classifier",
75
- description="Select mushroom characteristics to predict if it's edible or poisonous. **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."
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()