Spaces:
Sleeping
Sleeping
from sklearn.preprocessing import LabelEncoder | |
import pandas as pd | |
import pickle | |
import gradio as gr | |
import gradio as gr | |
svc=pickle.load(open('svc.pickle','rb')) | |
def predict_class(cap_shape, cap_surface, cap_color, bruises, odor, gill_attachment, | |
gill_spacing, gill_size, gill_color, stalk_shape, stalk_root, | |
stalk_surface_above_ring, stalk_surface_below_ring, stalk_color_above_ring, | |
stalk_color_below_ring, veil_color, ring_number, ring_type, spore_print_color, | |
population, habitat): | |
input_data=[cap_shape, cap_surface, cap_color, bruises, odor, gill_attachment, | |
gill_spacing, gill_size, gill_color, stalk_shape, stalk_root, | |
stalk_surface_above_ring, stalk_surface_below_ring, stalk_color_above_ring, | |
stalk_color_below_ring, veil_color, ring_number, ring_type, spore_print_color, | |
population, habitat] | |
features = [ 'cap-shape', 'cap-surface', 'cap-color', 'bruises', 'odor', | |
'gill-attachment', 'gill-spacing', 'gill-size', 'gill-color', | |
'stalk-shape', 'stalk-root', 'stalk-surface-above-ring', | |
'stalk-surface-below-ring', 'stalk-color-above-ring', | |
'stalk-color-below-ring', 'veil-color', 'ring-number', | |
'ring-type', 'spore-print-color', 'population', 'habitat'] | |
mushroom_data = { | |
'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'} | |
} | |
encoder=LabelEncoder() | |
real_df=pd.read_csv('mushrooms.csv') | |
real_df.drop(['class','veil-type'],axis=1,inplace=True) | |
encoded_value=[] | |
valueforprediction=[] | |
for i in range(21): | |
valueforprediction.append(mushroom_data[features[i]][input_data[i]]) | |
random=pd.DataFrame([valueforprediction],columns=features) | |
for i in real_df.columns: | |
print(i,real_df[i].unique()) | |
encoder.fit_transform(real_df[i]) | |
encoded_value.append(encoder.transform(random[i])[0]) | |
prediction=svc.predict([encoded_value]) | |
class_label = 'poisonous' if prediction == 1 else 'edible' | |
return class_label | |
input_features = {'cap-shape': ['bell', 'conical', 'convex', 'flat', 'knobbed', 'sunken'], | |
'cap-surface': ['fibrous', 'grooves', 'scaly', 'smooth'], | |
'cap-color': ['brown', 'buff', 'cinnamon', 'gray', 'green', 'pink', 'purple', 'red', 'white', 'yellow'], | |
'bruises': ['bruises', 'no'], | |
'odor': ['almond', 'anise', 'creosote', 'fishy', 'foul', 'musty', 'none', 'pungent', 'spicy'], | |
'gill-attachment': ['attached', 'free'], | |
'gill-spacing': ['close', 'crowded'], | |
'gill-size': ['broad', 'narrow'], | |
'gill-color': ['black', 'brown', 'buff', 'chocolate', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'white', 'yellow'], | |
'stalk-shape': ['enlarging', 'tapering'], | |
'stalk-root': ['bulbous', 'club', 'equal', 'rooted', 'missing'], | |
'stalk-surface-above-ring': ['fibrous', 'scaly', 'silky', 'smooth'], | |
'stalk-surface-below-ring': ['fibrous', 'scaly', 'silky', 'smooth'], | |
'stalk-color-above-ring': ['brown', 'buff', 'cinnamon', 'gray', 'orange', 'pink', 'red', 'white', 'yellow'], | |
'stalk-color-below-ring': ['brown', 'buff', 'cinnamon', 'gray', 'orange', 'pink', 'red', 'white', 'yellow'], | |
'veil-color': ['brown', 'orange', 'white', 'yellow'], | |
'ring-number': ['none', 'one', 'two'], | |
'ring-type': [ 'evanescent', 'flaring', 'large', 'none', 'pendant'], | |
'spore-print-color': ['black', 'brown', 'buff', 'chocolate', 'green', 'orange', 'purple', 'white', 'yellow'], | |
'population': ['abundant', 'clustered', 'numerous', 'scattered', 'several', 'solitary'], | |
'habitat': ['grasses', 'leaves', 'meadows', 'paths', 'urban', 'waste', 'woods']} | |
# Define the output classes | |
output_classes = ['p', 'e'] | |
input_components = [gr.Dropdown(choices=values, label=feature) for feature, values in input_features.items()] | |
# Create Gradio interface | |
iface = gr.Interface( | |
fn=predict_class, | |
inputs=input_components, | |
outputs="label", | |
title="Mushroom Classifier", | |
description="Predict whether a mushroom is poisonous or edible based on its features." | |
) | |
iface.launch(inline=False,share=True) | |