Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import joblib
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
# Load the trained model and mappings
|
6 |
+
model = joblib.load('mushroom_classifier.pkl')
|
7 |
+
mappings = joblib.load('mappings.pkl')
|
8 |
+
|
9 |
+
feature_options = {
|
10 |
+
'cap-shape': {'b': 'bell', 'c': 'conical', 'x': 'convex', 'f': 'flat', 'k': 'knobbed', 's': 'sunken'},
|
11 |
+
'cap-surface': {'f': 'fibrous', 'g': 'grooves', 'y': 'scaly', 's': 'smooth'},
|
12 |
+
'cap-color': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'r': 'green', 'p': 'pink', 'u': 'purple', 'e': 'red', 'w': 'white', 'y': 'yellow'},
|
13 |
+
'bruises': {'t': 'bruises', 'f': 'no'},
|
14 |
+
'odor': {'a': 'almond', 'l': 'anise', 'c': 'creosote', 'y': 'fishy', 'f': 'foul', 'm': 'musty', 'n': 'none', 'p': 'pungent', 's': 'spicy'},
|
15 |
+
'gill-attachment': {'a': 'attached', 'd': 'descending', 'f': 'free', 'n': 'notched'},
|
16 |
+
'gill-spacing': {'c': 'close', 'w': 'crowded', 'd': 'distant'},
|
17 |
+
'gill-size': {'b': 'broad', 'n': 'narrow'},
|
18 |
+
'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'},
|
19 |
+
'stalk-shape': {'e': 'enlarging', 't': 'tapering'},
|
20 |
+
'stalk-root': {'b': 'bulbous', 'c': 'club', 'u': 'cup', 'e': 'equal', 'z': 'rhizomorphs', 'r': 'rooted', '?': 'missing'},
|
21 |
+
'stalk-surface-above-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
|
22 |
+
'stalk-surface-below-ring': {'f': 'fibrous', 'y': 'scaly', 'k': 'silky', 's': 'smooth'},
|
23 |
+
'stalk-color-above-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
|
24 |
+
'stalk-color-below-ring': {'n': 'brown', 'b': 'buff', 'c': 'cinnamon', 'g': 'gray', 'o': 'orange', 'p': 'pink', 'e': 'red', 'w': 'white', 'y': 'yellow'},
|
25 |
+
'veil-type': {'p': 'partial', 'u': 'universal'},
|
26 |
+
'veil-color': {'n': 'brown', 'o': 'orange', 'w': 'white', 'y': 'yellow'},
|
27 |
+
'ring-number': {'n': 'none', 'o': 'one', 't': 'two'},
|
28 |
+
'ring-type': {'c': 'cobwebby', 'e': 'evanescent', 'f': 'flaring', 'l': 'large', 'n': 'none', 'p': 'pendant', 's': 'sheathing', 'z': 'zone'},
|
29 |
+
'spore-print-color': {'k': 'black', 'n': 'brown', 'b': 'buff', 'h': 'chocolate', 'r': 'green', 'o': 'orange', 'u': 'purple', 'w': 'white', 'y': 'yellow'},
|
30 |
+
'population': {'a': 'abundant', 'c': 'clustered', 'n': 'numerous', 's': 'scattered', 'v': 'several', 'y': 'solitary'},
|
31 |
+
'habitat': {'g': 'grasses', 'l': 'leaves', 'm': 'meadows', 'p': 'paths', 'u': 'urban', 'w': 'waste', 'd': 'woods'}
|
32 |
+
}
|
33 |
+
|
34 |
+
def predict_mushroom(**features):
|
35 |
+
numerical_features = {}
|
36 |
+
for feature, value in features.items():
|
37 |
+
if feature in mappings and value in mappings[feature]:
|
38 |
+
numerical_features[feature] = mappings[feature][value]
|
39 |
+
else:
|
40 |
+
return "Invalid input detected."
|
41 |
+
|
42 |
+
input_df = pd.DataFrame([numerical_features])
|
43 |
+
prediction = model.predict(input_df)
|
44 |
+
return "Edible" if prediction[0] == 0 else "Poisonous"
|
45 |
+
|
46 |
+
dropdowns = [gr.Dropdown(choices=list(options.values()), label=feature.replace('-', ' ').capitalize()) for feature, options in feature_options.items()]
|
47 |
+
|
48 |
+
demo = gr.Interface(fn=predict_mushroom, inputs=dropdowns, outputs="text", title="Mushroom Classification", description="Select the characteristics of the mushroom to determine if it's edible or poisonous.")
|
49 |
+
|
50 |
+
demo.launch()
|