Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
-
#
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
# Convert full names to letters using feature_options
|
47 |
-
|
48 |
-
for feature,
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
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"
|
58 |
-
|
59 |
-
# Convert
|
60 |
-
|
61 |
-
|
62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
63 |
model = joblib.load('mushroom_classifier.pkl')
|
64 |
-
|
65 |
-
# Predict using the
|
66 |
prediction = model.predict(input_df)
|
67 |
-
|
68 |
-
#
|
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,
|