Spaces:
Sleeping
Sleeping
import gradio as gr | |
import joblib | |
import pandas as pd | |
# Load model and mappings | |
model = joblib.load('mushroom_classifier.pkl') | |
mappings = joblib.load('mappings.pkl') | |
feature_options = { | |
'cap-shape': {'bell': 'b', 'conical': 'c', 'convex': 'x', 'flat': 'f', 'knobbed': 'k', 'sunken': 's'}, | |
'cap-surface': {'fibrous': 'f', 'grooves': 'g', 'scaly': 'y', 'smooth': 's'}, | |
'cap-color': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'green': 'r', 'pink': 'p', 'purple': 'u', 'red': 'e', 'white': 'w', 'yellow': 'y'}, | |
'bruises': {'bruises': 't', 'no': 'f'}, | |
'odor': {'almond': 'a', 'anise': 'l', 'creosote': 'c', 'fishy': 'y', 'foul': 'f', 'musty': 'm', 'none': 'n', 'pungent': 'p', 'spicy': 's'}, | |
'gill-attachment': {'attached': 'a', 'descending': 'd', 'free': 'f', 'notched': 'n'}, | |
'gill-spacing': {'close': 'c', 'crowded': 'w', 'distant': 'd'}, | |
'gill-size': {'broad': 'b', 'narrow': 'n'}, | |
'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'}, | |
'stalk-shape': {'enlarging': 'e', 'tapering': 't'}, | |
'stalk-root': {'bulbous': 'b', 'club': 'c', 'cup': 'u', 'equal': 'e', 'rhizomorphs': 'z', 'rooted': 'r', 'missing': '?'}, | |
'stalk-surface-above-ring': {'fibrous': 'f', 'scaly': 'y', 'silky': 'k', 'smooth': 's'}, | |
'stalk-surface-below-ring': {'fibrous': 'f', 'scaly': 'y', 'silky': 'k', 'smooth': 's'}, | |
'stalk-color-above-ring': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'orange': 'o', 'pink': 'p', 'red': 'e', 'white': 'w', 'yellow': 'y'}, | |
'stalk-color-below-ring': {'brown': 'n', 'buff': 'b', 'cinnamon': 'c', 'gray': 'g', 'orange': 'o', 'pink': 'p', 'red': 'e', 'white': 'w', 'yellow': 'y'}, | |
'veil-type': {'partial': 'p', 'universal': 'u'}, | |
'veil-color': {'brown': 'n', 'orange': 'o', 'white': 'w', 'yellow': 'y'}, | |
'ring-number': {'none': 'n', 'one': 'o', 'two': 't'}, | |
'ring-type': {'cobwebby': 'c', 'evanescent': 'e', 'flaring': 'f', 'large': 'l', 'none': 'n', 'pendant': 'p', 'sheathing': 's', 'zone': 'z'}, | |
'spore-print-color': {'black': 'k', 'brown': 'n', 'buff': 'b', 'chocolate': 'h', 'green': 'r', 'orange': 'o', 'purple': 'u', 'white': 'w', 'yellow': 'y'}, | |
'population': {'abundant': 'a', 'clustered': 'c', 'numerous': 'n', 'scattered': 's', 'several': 'v', 'solitary': 'y'}, | |
'habitat': {'grasses': 'g', 'leaves': 'l', 'meadows': 'm', 'paths': 'p', 'urban': 'u', 'waste': 'w', 'woods': 'd'} | |
} | |
# def predict_mushroom(features): | |
# numerical_features = {feature: feature_options[feature][value] for feature, value in features.items()} | |
# input_df = pd.DataFrame([numerical_features]) | |
# prediction = model.predict(input_df) | |
# return 'Poisonous' if prediction[0] == 1 else 'Edible' | |
def predict_mushroom(*features): | |
# Convert the feature names to a dictionary based on the order | |
feature_names = list(mappings.keys()) | |
numerical_features = {} | |
# Convert the string values to numerical values using the mappings | |
for i, feature in enumerate(features): | |
if feature in mappings[feature_names[i]]: | |
numerical_features[feature_names[i]] = mappings[feature_names[i]][feature] | |
else: | |
raise ValueError(f"Invalid value for feature '{feature_names[i]}': {feature}") | |
# Convert the numerical features into a DataFrame for the model | |
input_df = pd.DataFrame([numerical_features]) | |
# Ensure the data is in the right format | |
try: | |
prediction = model.predict(input_df) | |
except Exception as e: | |
raise ValueError(f"Error during prediction: {e}") | |
# Return the classification result based on the model output | |
return 'Poisonous' if prediction[0] == 1 else 'Edible' | |
demo = gr.Interface( | |
fn=predict_mushroom, | |
inputs=[gr.Dropdown(choices=list(options.keys()), label=feature) for feature, options in feature_options.items()], | |
outputs="text", | |
title="MycoNom - Mushroom Edibility Classifier", | |
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." | |
) | |
demo.launch() |