JohanBeytell commited on
Commit
711f0eb
·
verified ·
1 Parent(s): 8c695a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +30 -29
app.py CHANGED
@@ -37,39 +37,40 @@ feature_options = {
37
  # prediction = model.predict(input_df)
38
  # return 'Poisonous' if prediction[0] == 1 else 'Edible'
39
 
40
- # Prediction function
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 full names to letters using feature_options
47
- numerical_features = {}
48
- for feature, value in user_input.items():
49
- if feature in feature_options:
50
- # Reverse the feature_options dictionary to map full names to letter codes
51
- inverse_mapping = {v: k for k, v in feature_options[feature].items()}
52
- if value in inverse_mapping:
53
- numerical_features[feature] = inverse_mapping[value] # Map full name to letter
54
- else:
55
- raise ValueError(f"Invalid value '{value}' for feature '{feature}'.")
56
  else:
57
- raise ValueError(f"Feature '{feature}' is not recognized.")
58
-
59
- # Convert the numerical features into a DataFrame
60
- input_df = pd.DataFrame([numerical_features])
61
-
62
- # Load the trained model
 
 
 
 
 
 
 
 
63
  model = joblib.load('mushroom_classifier.pkl')
64
-
65
- # Predict using the trained model
66
  prediction = model.predict(input_df)
67
-
68
- # Interpret the prediction
69
- if prediction[0] == 0:
70
- return 'Edible'
71
- else:
72
- return 'Poisonous'
73
 
74
  demo = gr.Interface(
75
  fn=predict_mushroom,
 
37
  # prediction = model.predict(input_df)
38
  # return 'Poisonous' if prediction[0] == 1 else 'Edible'
39
 
 
40
  def predict_mushroom(*inputs):
41
+ # Build a dictionary of feature: full name selections.
42
+ # The keys are the feature names (e.g., 'cap-shape') and the values are the full names (e.g., 'bell')
43
+ user_full = dict(zip(feature_options.keys(), inputs))
44
+
45
+ # Convert the full names to letters using feature_options (reverse mapping)
46
+ user_letters = {}
47
+ for feature, full_value in user_full.items():
48
+ # Create reverse mapping: full name -> letter for this feature
49
+ inverse = {v: k for k, v in feature_options[feature].items()}
50
+ if full_value in inverse:
51
+ user_letters[feature] = inverse[full_value]
 
 
 
52
  else:
53
+ raise ValueError(f"Invalid selection for {feature}: {full_value}")
54
+
55
+ # Convert letters to numeric values using the mappings from mappings.pkl
56
+ numeric_features = {}
57
+ for feature, letter in user_letters.items():
58
+ if feature in mappings and letter in mappings[feature]:
59
+ numeric_features[feature] = mappings[feature][letter]
60
+ else:
61
+ raise ValueError(f"Mapping not found for feature {feature} with letter {letter}")
62
+
63
+ # Create a DataFrame for model input
64
+ input_df = pd.DataFrame([numeric_features])
65
+
66
+ # Load the trained model (assumes the file is in the working directory)
67
  model = joblib.load('mushroom_classifier.pkl')
68
+
69
+ # Predict using the model
70
  prediction = model.predict(input_df)
71
+
72
+ # Return the human-readable result
73
+ return 'Edible' if prediction[0] == 0 else 'Poisonous'
 
 
 
74
 
75
  demo = gr.Interface(
76
  fn=predict_mushroom,