Spaces:
Sleeping
Sleeping
Srivastava
commited on
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from sklearn.preprocessing import LabelEncoder
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import pickle
|
| 4 |
+
import gradio as gr
|
| 5 |
+
svc=pickle.load(open('svc.pickle','rb'))
|
| 6 |
+
def predict_class(cap_shape, cap_surface, cap_color, bruises, odor, gill_attachment,
|
| 7 |
+
gill_spacing, gill_size, gill_color, stalk_shape, stalk_root,
|
| 8 |
+
stalk_surface_above_ring, stalk_surface_below_ring, stalk_color_above_ring,
|
| 9 |
+
stalk_color_below_ring, veil_color, ring_number, ring_type, spore_print_color,
|
| 10 |
+
population, habitat):
|
| 11 |
+
input_data=[cap_shape, cap_surface, cap_color, bruises, odor, gill_attachment,
|
| 12 |
+
gill_spacing, gill_size, gill_color, stalk_shape, stalk_root,
|
| 13 |
+
stalk_surface_above_ring, stalk_surface_below_ring, stalk_color_above_ring,
|
| 14 |
+
stalk_color_below_ring, veil_color, ring_number, ring_type, spore_print_color,
|
| 15 |
+
population, habitat]
|
| 16 |
+
encoder=LabelEncoder()
|
| 17 |
+
real_df=pd.read_csv('mushrooms.csv')
|
| 18 |
+
real_df.drop(['class','veil-type'],axis=1,inplace=True)
|
| 19 |
+
encoded_value=[]
|
| 20 |
+
features = [ 'cap-shape', 'cap-surface', 'cap-color', 'bruises', 'odor',
|
| 21 |
+
'gill-attachment', 'gill-spacing', 'gill-size', 'gill-color',
|
| 22 |
+
'stalk-shape', 'stalk-root', 'stalk-surface-above-ring',
|
| 23 |
+
'stalk-surface-below-ring', 'stalk-color-above-ring',
|
| 24 |
+
'stalk-color-below-ring', 'veil-color', 'ring-number',
|
| 25 |
+
'ring-type', 'spore-print-color', 'population', 'habitat']
|
| 26 |
+
randomly_selected_values = ['s', 'y', 'g', 'f', 'c', 'a', 'w', 'n', 'b', 't', 'e', 's', 'k', 'o', 'y', 'w', 'o', 'f', 'r', 'y', 'p']
|
| 27 |
+
random=pd.DataFrame([input_data],columns=features)
|
| 28 |
+
for i in real_df.columns:
|
| 29 |
+
encoder.fit_transform(real_df[i])
|
| 30 |
+
encoded_value.append(encoder.transform(random[i])[0])
|
| 31 |
+
|
| 32 |
+
prediction=svc.predict([encoded_value])
|
| 33 |
+
class_label = 'poisonous' if prediction == 1 else 'edible'
|
| 34 |
+
return class_label
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
import gradio as gr
|
| 43 |
+
input_features = {
|
| 44 |
+
'cap-shape': ['x', 'b', 's', 'f', 'k', 'c'],
|
| 45 |
+
'cap-surface': ['s', 'y', 'f', 'g'],
|
| 46 |
+
'cap-color': ['n', 'y', 'w', 'g', 'e', 'p', 'b', 'u', 'c', 'r'],
|
| 47 |
+
'bruises': ['t', 'f'],
|
| 48 |
+
'odor': ['p', 'a', 'l', 'n', 'f', 'c', 'y', 's', 'm'],
|
| 49 |
+
'gill-attachment': ['f', 'a'],
|
| 50 |
+
'gill-spacing': ['c', 'w'],
|
| 51 |
+
'gill-size': ['n', 'b'],
|
| 52 |
+
'gill-color': ['k', 'n', 'g', 'p', 'w', 'h', 'u', 'e', 'b', 'r', 'y', 'o'],
|
| 53 |
+
'stalk-shape': ['e', 't'],
|
| 54 |
+
'stalk-root': ['e', 'c', 'b', 'r', '?'],
|
| 55 |
+
'stalk-surface-above-ring': ['s', 'f', 'k', 'y'],
|
| 56 |
+
'stalk-surface-below-ring': ['s', 'f', 'y', 'k'],
|
| 57 |
+
'stalk-color-above-ring': ['w', 'g', 'p', 'n', 'b', 'e', 'o', 'c', 'y'],
|
| 58 |
+
'stalk-color-below-ring': ['w', 'p', 'g', 'b', 'n', 'e', 'y', 'o', 'c'],
|
| 59 |
+
'veil-color': ['w', 'n', 'o', 'y'],
|
| 60 |
+
'ring-number': ['o', 't', 'n'],
|
| 61 |
+
'ring-type': ['p', 'e', 'l', 'f', 'n'],
|
| 62 |
+
'spore-print-color': ['k', 'n', 'u', 'h', 'w', 'r', 'o', 'y', 'b'],
|
| 63 |
+
'population': ['s', 'n', 'a', 'v', 'y', 'c'],
|
| 64 |
+
'habitat': ['u', 'g', 'm', 'd', 'p', 'w', 'l']
|
| 65 |
+
}
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
# Convert input features dictionary to a list of dictionaries
|
| 69 |
+
print(len(input_features))
|
| 70 |
+
# Define the output classes
|
| 71 |
+
output_classes = ['p', 'e']
|
| 72 |
+
|
| 73 |
+
input_components = [gr.Dropdown(choices=values, label=feature) for feature, values in input_features.items()]
|
| 74 |
+
|
| 75 |
+
# Create Gradio interface
|
| 76 |
+
iface = gr.Interface(
|
| 77 |
+
fn=predict_class,
|
| 78 |
+
inputs=input_components,
|
| 79 |
+
outputs="label",
|
| 80 |
+
title="Mushroom Classifier",
|
| 81 |
+
description="Predict whether a mushroom is poisonous or edible based on its features."
|
| 82 |
+
)
|
| 83 |
+
iface.launch(inline=False,share=True)
|