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': {'b': 'bell', 'c': 'conical', 'x': 'convex', 'f': 'flat', 'k': 'knobbed', 's': 'sunken'}, | |
'cap-surface': {'f': 'fibrous', 'g': 'grooves', 'y': 'scaly', 's': 'smooth'}, | |
'cap-color': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'r': 'green', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'}, | |
'bruises': {'t': 'bruises', 'f': 'no'}, | |
'odor': {'a': 'almond', 'l': 'anise', 'c': 'creosote', 'y': 'fishy', 'f': 'foul', 'm': 'musty', 'n': 'none', 'p': 'pungent', 's': 'spicy'}, | |
'gill-attachment': {'a': 'attached', 'd': 'descending', 'f': 'free', 'n': 'notched'}, | |
'gill-spacing': {'c': 'close', 'w': 'crowded', 'd': 'distant'}, | |
'gill-size': {'b': 'broad', 'n': 'narrow'}, | |
'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'}, | |
'stalk-shape': {'e': 'enlarging', 't': 'tapering'}, | |
'stalk-root': {'b': 'bulbous', 'c': 'club', 'u': 'cup', 'e': 'equal', 'z': 'rhizomorphs', 'r': 'rooted', '?': 'missing'}, | |
'stalk-surface-above-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'}, | |
'stalk-surface-below-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'}, | |
'stalk-color-above-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'}, | |
'stalk-color-below-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'}, | |
'veil-type': {'p': 'partial', 'u': 'universal'}, | |
'veil-color': {'n': 'brown', 'o': 'orange', 'w': 'white', 'y': 'yellow'}, | |
'ring-number': {'n': 'none', 'o': 'one', 't': 'two'}, | |
'ring-type': {'c': 'cobwebby', 'e': 'evanescent', 'f': 'flaring', 'l': 'large', 'n': 'none', 'p': 'pendant', 's': 'sheathing', 'z': 'zone'}, | |
'spore-print-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'r': 'green', 'o': 'orange', 'u': 'purple', 'w': 'white', 'y': 'yellow'}, | |
'population': {'a': 'abundant', 'c': 'clustered', 'n': 'numerous', 's': 'scattered', 'v': 'several', 'y': 'solitary'}, | |
'habitat': {'g': 'grasses', 'l': 'leaves', 'm': 'meadows', 'p': 'paths', 'u': 'urban', 'w': 'waste', 'd': 'woods'} | |
} | |
# 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' | |
# Prediction function | |
def predict_mushroom(*inputs): | |
# Map the inputs to feature names | |
features = list(feature_options.keys()) | |
user_input = dict(zip(features, inputs)) | |
# Convert full names to letters using feature_options | |
numerical_features = {} | |
for feature, value in user_input.items(): | |
if feature in feature_options: | |
# Reverse the feature_options dictionary to map full names to letter codes | |
inverse_mapping = {v: k for k, v in feature_options[feature].items()} | |
if value in inverse_mapping: | |
numerical_features[feature] = inverse_mapping[value] # Map full name to letter | |
else: | |
raise ValueError(f"Invalid value '{value}' for feature '{feature}'.") | |
else: | |
raise ValueError(f"Feature '{feature}' is not recognized.") | |
# Convert the numerical features into a DataFrame | |
input_df = pd.DataFrame([numerical_features]) | |
# Load the trained model | |
model = joblib.load('mushroom_classifier.pkl') | |
# Predict using the trained model | |
prediction = model.predict(input_df) | |
# Interpret the prediction | |
if prediction[0] == 0: | |
return 'Edible' | |
else: | |
return 'Poisonous' | |
demo = gr.Interface( | |
fn=predict_mushroom, | |
inputs=[gr.Dropdown(choices=list(options.values()), 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>You can train your own version of this model by heading to OPEN-ARC: https://github.com/Infinitode/OPEN-ARC.<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() |