JohanBeytell commited on
Commit
cfc14d7
·
verified ·
1 Parent(s): 35814cf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +46 -41
app.py CHANGED
@@ -7,28 +7,28 @@ model = joblib.load('mushroom_classifier.pkl')
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):
@@ -37,29 +37,34 @@ feature_options = {
37
  # prediction = model.predict(input_df)
38
  # return 'Poisonous' if prediction[0] == 1 else 'Edible'
39
 
40
- def predict_mushroom(*features):
41
- # Convert the feature names to a dictionary based on the order
42
- feature_names = list(mappings.keys())
43
- numerical_features = {}
 
44
 
45
- # Convert the string values to numerical values using the mappings
46
- for i, feature in enumerate(features):
47
- if feature in mappings[feature_names[i]]:
48
- numerical_features[feature_names[i]] = mappings[feature_names[i]][feature]
 
 
 
 
49
  else:
50
- raise ValueError(f"Invalid value for feature '{feature_names[i]}': {feature}")
51
 
52
- # Convert the numerical features into a DataFrame for the model
53
  input_df = pd.DataFrame([numerical_features])
54
-
55
- # Ensure the data is in the right format
56
- try:
57
- prediction = model.predict(input_df)
58
- except Exception as e:
59
- raise ValueError(f"Error during prediction: {e}")
60
-
61
- # Return the classification result based on the model output
62
- return 'Poisonous' if prediction[0] == 1 else 'Edible'
63
 
64
  demo = gr.Interface(
65
  fn=predict_mushroom,
 
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
+ 'stalk-surface-above-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
22
+ 'stalk-surface-below-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
23
+ 'stalk-color-above-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
24
+ 'stalk-color-below-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
25
+ 'veil-type': {'p': 'partial', 'u': 'universal'},
26
+ 'veil-color': {'n': 'brown', 'o': 'orange', 'w': 'white', 'y': 'yellow'},
27
+ 'ring-number': {'n': 'none', 'o': 'one', 't': 'two'},
28
+ 'ring-type': {'c': 'cobwebby', 'e': 'evanescent', 'f': 'flaring', 'l': 'large', 'n': 'none', 'p': 'pendant', 's': 'sheathing', 'z': 'zone'},
29
+ 'spore-print-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'r': 'green', 'o': 'orange', 'u': 'purple', 'w': 'white', 'y': 'yellow'},
30
+ 'population': {'a': 'abundant', 'c': 'clustered', 'n': 'numerous', 's': 'scattered', 'v': 'several', 'y': 'solitary'},
31
+ 'habitat': {'g': 'grasses', 'l': 'leaves', 'm': 'meadows', 'p': 'paths', 'u': 'urban', 'w': 'waste', 'd': 'woods'}
32
  }
33
 
34
  # def predict_mushroom(features):
 
37
  # prediction = model.predict(input_df)
38
  # return 'Poisonous' if prediction[0] == 1 else 'Edible'
39
 
40
+ # Prediction function for Gradio
41
+ def predict_mushroom(*inputs):
42
+ # Map the inputs to feature names
43
+ features = list(feature_options.keys())
44
+ user_input = dict(zip(features, inputs))
45
 
46
+ # Convert string inputs to numerical values using mappings
47
+ numerical_features = {}
48
+ for feature, value in user_input.items():
49
+ if feature in mappings:
50
+ if value in mappings[feature]:
51
+ numerical_features[feature] = mappings[feature][value] # Map string to numerical value
52
+ else:
53
+ raise ValueError(f"Invalid value '{value}' for feature '{feature}'.")
54
  else:
55
+ raise ValueError(f"Feature '{feature}' is not recognized.")
56
 
57
+ # Convert the numerical features into a DataFrame
58
  input_df = pd.DataFrame([numerical_features])
59
+
60
+ # Predict using the trained model
61
+ prediction = model.predict(input_df)
62
+
63
+ # Interpret the prediction
64
+ if prediction[0] == 0:
65
+ return 'Edible'
66
+ else:
67
+ return 'Poisonous'
68
 
69
  demo = gr.Interface(
70
  fn=predict_mushroom,