Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,10 +1,41 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
def predict_mushroom(cap_diameter, gill_spacing, stem_root, veil_color, season):
|
2 |
artifacts = load_artifacts()
|
3 |
model = artifacts['model']
|
4 |
target_encoder = artifacts['target_encoder']
|
5 |
feature_encoders = artifacts['feature_encoders']
|
6 |
|
7 |
-
# Tüm özellikleri içeren sözlük
|
8 |
input_data = {
|
9 |
'cap-diameter': [cap_diameter],
|
10 |
'cap-shape': ['x'], # varsayılan değer
|
@@ -30,7 +61,6 @@ def predict_mushroom(cap_diameter, gill_spacing, stem_root, veil_color, season):
|
|
30 |
|
31 |
df = pd.DataFrame(input_data)
|
32 |
|
33 |
-
# Kategorik özellikleri dönüştür
|
34 |
categorical_features = [col for col in df.columns if col not in ['cap-diameter', 'stem-height', 'stem-width']]
|
35 |
for col in categorical_features:
|
36 |
if col in feature_encoders:
|
@@ -39,4 +69,51 @@ def predict_mushroom(cap_diameter, gill_spacing, stem_root, veil_color, season):
|
|
39 |
prediction = model.predict(df)[0]
|
40 |
class_prediction = target_encoder.inverse_transform([prediction])[0]
|
41 |
|
42 |
-
return "Edible" if class_prediction == 'e' else "Poisonous"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import pandas as pd
|
3 |
+
import numpy as np
|
4 |
+
import pickle
|
5 |
+
from huggingface_hub import hf_hub_download
|
6 |
+
|
7 |
+
def load_artifacts():
|
8 |
+
model_path = hf_hub_download(repo_id="alperugurcan/poisonous-mushrooms", filename="model.pkl")
|
9 |
+
target_encoder_path = hf_hub_download(repo_id="alperugurcan/poisonous-mushrooms", filename="target_encoder.pkl")
|
10 |
+
label_encoders_path = hf_hub_download(repo_id="alperugurcan/poisonous-mushrooms", filename="label_encoders.pkl")
|
11 |
+
numeric_columns_path = hf_hub_download(repo_id="alperugurcan/poisonous-mushrooms", filename="numeric_columns.pkl")
|
12 |
+
categorical_columns_path = hf_hub_download(repo_id="alperugurcan/poisonous-mushrooms", filename="categorical_columns.pkl")
|
13 |
+
|
14 |
+
with open(model_path, 'rb') as f:
|
15 |
+
model = pickle.load(f)
|
16 |
+
with open(target_encoder_path, 'rb') as f:
|
17 |
+
target_encoder = pickle.load(f)
|
18 |
+
with open(label_encoders_path, 'rb') as f:
|
19 |
+
label_encoders = pickle.load(f)
|
20 |
+
with open(numeric_columns_path, 'rb') as f:
|
21 |
+
numeric_columns = pickle.load(f)
|
22 |
+
with open(categorical_columns_path, 'rb') as f:
|
23 |
+
categorical_columns = pickle.load(f)
|
24 |
+
|
25 |
+
return {
|
26 |
+
'model': model,
|
27 |
+
'target_encoder': target_encoder,
|
28 |
+
'feature_encoders': label_encoders,
|
29 |
+
'numeric_columns': numeric_columns,
|
30 |
+
'categorical_columns': categorical_columns
|
31 |
+
}
|
32 |
+
|
33 |
def predict_mushroom(cap_diameter, gill_spacing, stem_root, veil_color, season):
|
34 |
artifacts = load_artifacts()
|
35 |
model = artifacts['model']
|
36 |
target_encoder = artifacts['target_encoder']
|
37 |
feature_encoders = artifacts['feature_encoders']
|
38 |
|
|
|
39 |
input_data = {
|
40 |
'cap-diameter': [cap_diameter],
|
41 |
'cap-shape': ['x'], # varsayılan değer
|
|
|
61 |
|
62 |
df = pd.DataFrame(input_data)
|
63 |
|
|
|
64 |
categorical_features = [col for col in df.columns if col not in ['cap-diameter', 'stem-height', 'stem-width']]
|
65 |
for col in categorical_features:
|
66 |
if col in feature_encoders:
|
|
|
69 |
prediction = model.predict(df)[0]
|
70 |
class_prediction = target_encoder.inverse_transform([prediction])[0]
|
71 |
|
72 |
+
return "Edible" if class_prediction == 'e' else "Poisonous"
|
73 |
+
|
74 |
+
iface = gr.Interface(
|
75 |
+
fn=predict_mushroom,
|
76 |
+
inputs=[
|
77 |
+
gr.Slider(
|
78 |
+
minimum=2.0,
|
79 |
+
maximum=20.0,
|
80 |
+
value=10.0,
|
81 |
+
step=0.5,
|
82 |
+
label="Cap Diameter (cm)",
|
83 |
+
info="Slide to select mushroom cap width"
|
84 |
+
),
|
85 |
+
gr.Dropdown(
|
86 |
+
choices=['c', 'w'],
|
87 |
+
value='c',
|
88 |
+
label="Gill Spacing",
|
89 |
+
info="c: close, w: wide"
|
90 |
+
),
|
91 |
+
gr.Dropdown(
|
92 |
+
choices=['b', 'e', 'c', 'r'],
|
93 |
+
value='b',
|
94 |
+
label="Stem Root",
|
95 |
+
info="b: bulbous, e: equal, c: club, r: rooted"
|
96 |
+
),
|
97 |
+
gr.Dropdown(
|
98 |
+
choices=['w', 'n', 'o', 'y'],
|
99 |
+
value='w',
|
100 |
+
label="Veil Color",
|
101 |
+
info="w: white, n: brown, o: orange, y: yellow"
|
102 |
+
),
|
103 |
+
gr.Dropdown(
|
104 |
+
choices=['s', 'u', 'a', 'w'],
|
105 |
+
value='s',
|
106 |
+
label="Season",
|
107 |
+
info="s: spring, u: summer, a: autumn, w: winter"
|
108 |
+
)
|
109 |
+
],
|
110 |
+
outputs=gr.Label(label="Prediction"),
|
111 |
+
title="Mushroom Edibility Classifier",
|
112 |
+
description="""
|
113 |
+
Predict if a mushroom is edible or poisonous using its 5 most important characteristics.
|
114 |
+
WARNING: This is a demonstration only. Never eat wild mushrooms based on this prediction!
|
115 |
+
"""
|
116 |
+
)
|
117 |
+
|
118 |
+
if __name__ == "__main__":
|
119 |
+
iface.launch()
|