Update src/streamlit_app.py
Browse files- src/streamlit_app.py +71 -155
src/streamlit_app.py
CHANGED
|
@@ -1,180 +1,96 @@
|
|
| 1 |
-
# streamlit_app.py
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
| 5 |
from io import StringIO
|
| 6 |
-
from sklearn.model_selection import train_test_split
|
| 7 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
from sklearn.preprocessing import LabelEncoder
|
|
|
|
|
|
|
| 9 |
import joblib
|
| 10 |
import os
|
| 11 |
|
| 12 |
st.set_page_config(page_title="Mushroom Doctor", layout="centered")
|
| 13 |
-
st.title("
|
| 14 |
-
st.markdown("### *Edible* or *Poisonous*
|
| 15 |
|
| 16 |
-
# Load
|
| 17 |
@st.cache_data
|
| 18 |
-
def
|
| 19 |
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
'
|
| 24 |
-
'
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
'stalk_color_below_ring', 'veil_type', 'veil_color', 'ring_number',
|
| 28 |
-
'ring_type', 'spore_print_color', 'population', 'habitat'
|
| 29 |
-
]
|
| 30 |
-
df = pd.read_csv(StringIO(response.text), header=None, names=columns)
|
| 31 |
-
return df
|
| 32 |
-
else:
|
| 33 |
-
st.error("Failed to load dataset.")
|
| 34 |
-
return None
|
| 35 |
-
|
| 36 |
-
df = load_mushroom_data()
|
| 37 |
-
|
| 38 |
-
if df is None:
|
| 39 |
-
st.stop()
|
| 40 |
-
|
| 41 |
-
st.success(f"✅ Dataset loaded: {df.shape[0]:,} mushrooms analyzed")
|
| 42 |
|
| 43 |
-
|
| 44 |
-
st.subheader("Dataset Overview")
|
| 45 |
-
col1, col2 = st.columns(2)
|
| 46 |
-
edible_count = len(df[df['class'] == 'e'])
|
| 47 |
-
poisonous_count = len(df[df['class'] == 'p'])
|
| 48 |
-
col1.metric("🍄 Edible Mushrooms", edible_count)
|
| 49 |
-
col2.metric("☠ Poisonous Mushrooms", poisonous_count)
|
| 50 |
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
# Preprocess Data
|
| 54 |
@st.cache_data
|
| 55 |
-
def
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
for
|
| 59 |
le = LabelEncoder()
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
-
|
| 67 |
|
| 68 |
-
#
|
| 69 |
-
|
| 70 |
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
if st.button("🚀 Train Random Forest Model (Achieves 100% Accuracy!)"):
|
| 74 |
-
with st.spinner("Training the model..."):
|
| 75 |
-
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 76 |
-
model.fit(X_train, y_train)
|
| 77 |
-
|
| 78 |
-
# Evaluate
|
| 79 |
-
train_acc = model.score(X_train, y_train)
|
| 80 |
-
test_acc = model.score(X_test, y_test)
|
| 81 |
-
|
| 82 |
-
st.success(f"✅ Model Trained Successfully!")
|
| 83 |
-
st.info(f"Training Accuracy: {train_acc:.4f} | Test Accuracy: {test_acc:.4f}")
|
| 84 |
-
|
| 85 |
-
if test_acc == 1.0:
|
| 86 |
-
st.balloons()
|
| 87 |
-
st.markdown("🎉 PERFECT! 100% Classification Accuracy**")
|
| 88 |
-
|
| 89 |
-
# Save Model
|
| 90 |
-
model_data = {
|
| 91 |
-
'model': model,
|
| 92 |
-
'label_encoders': label_encoders,
|
| 93 |
-
'features': X.columns.tolist()
|
| 94 |
-
}
|
| 95 |
-
joblib.dump(model_data, 'mushroom_model.pkl')
|
| 96 |
-
st.session_state.model_trained = True
|
| 97 |
|
| 98 |
-
#
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
except Exception as e:
|
| 110 |
-
st.error(f"Error loading model: {e}")
|
| 111 |
-
else:
|
| 112 |
-
st.info("Train the model first or it will be created on first prediction.")
|
| 113 |
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
'cap_surface': ['fibrous', 'grooves', 'smooth', 'scaly'],
|
| 132 |
-
'cap_color': ['buff', 'cinnamon', 'red', 'gray', 'brown', 'pink', 'green', 'purple', 'white', 'yellow'],
|
| 133 |
-
'bruises': ['yes', 'no'],
|
| 134 |
-
'odor': ['almond', 'creosote', 'foul', 'anise', 'musty', 'none', 'pungent', 'spicy', 'fishy'],
|
| 135 |
-
'gill_color': ['buff', 'red', 'gray', 'chocolate', 'black', 'brown', 'orange', 'pink', 'green', 'purple', 'white', 'yellow'],
|
| 136 |
-
'stalk_shape': ['enlarging', 'tapering'],
|
| 137 |
-
'stalk_root': ['bulbous', 'club', 'equal', 'rooted', '?'],
|
| 138 |
-
'spore_print_color': ['black', 'brown', 'buff', 'chocolate', 'green', 'orange', 'purple', 'white', 'yellow'],
|
| 139 |
-
'population': ['abundant', 'clustered', 'numerous', 'scattered', 'several', 'solitary'],
|
| 140 |
-
'habitat': ['woods', 'grasses', 'leaves', 'meadows', 'paths', 'urban', 'waste']
|
| 141 |
-
}
|
| 142 |
|
| 143 |
-
|
| 144 |
-
with cols[i % 3]:
|
| 145 |
-
if feat in feature_options:
|
| 146 |
-
options = feature_options[feat]
|
| 147 |
-
else:
|
| 148 |
-
options = list(encoders[feat].classes_)
|
| 149 |
-
selected = st.selectbox(f"{feat.replace('_', ' ').title()}", options, key=feat)
|
| 150 |
-
encoded_val = encoders[feat].transform([selected])[0]
|
| 151 |
-
input_features[feat] = encoded_val
|
| 152 |
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
# Decode prediction
|
| 163 |
-
predicted_class = encoders['class'].inverse_transform([prediction])[0]
|
| 164 |
-
edible_prob = probabilities[0] if predicted_class == 'e' else probabilities[1]
|
| 165 |
-
poisonous_prob = 1 - edible_prob
|
| 166 |
-
|
| 167 |
-
# Display Results
|
| 168 |
-
if predicted_class == 'e':
|
| 169 |
-
st.success("🍄 EDIBLE – SAFE TO EAT!")
|
| 170 |
-
st.balloons()
|
| 171 |
-
else:
|
| 172 |
-
st.error("☠ POISONOUS – DO NOT EAT!")
|
| 173 |
-
st.warning("This mushroom could be dangerous or fatal.")
|
| 174 |
-
|
| 175 |
-
col1, col2 = st.columns(2)
|
| 176 |
-
col1.metric("Edible Probability", f"{edible_prob:.1%}")
|
| 177 |
-
col2.metric("Poisonous Probability", f"{poisonous_prob:.1%}")
|
| 178 |
|
| 179 |
st.markdown("---")
|
| 180 |
-
st.caption("
|
|
|
|
| 1 |
+
# streamlit_app.py
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
| 5 |
from io import StringIO
|
|
|
|
|
|
|
| 6 |
from sklearn.preprocessing import LabelEncoder
|
| 7 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 8 |
+
from sklearn.model_selection import train_test_split
|
| 9 |
import joblib
|
| 10 |
import os
|
| 11 |
|
| 12 |
st.set_page_config(page_title="Mushroom Doctor", layout="centered")
|
| 13 |
+
st.title("Mushroom Doctor")
|
| 14 |
+
st.markdown("### Change mushroom features → Get instant *Edible* or *Poisonous* result!")
|
| 15 |
|
| 16 |
+
# Load and cache dataset
|
| 17 |
@st.cache_data
|
| 18 |
+
def load_data():
|
| 19 |
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
|
| 20 |
+
r = requests.get(url)
|
| 21 |
+
cols = ['class','cap_shape','cap_surface','cap_color','bruises','odor','gill_attachment','gill_spacing',
|
| 22 |
+
'gill_size','gill_color','stalk_shape','stalk_root','stalk_surface_above_ring','stalk_surface_below_ring',
|
| 23 |
+
'stalk_color_above_ring','stalk_color_below_ring','veil_type','veil_color','ring_number','ring_type',
|
| 24 |
+
'spore_print_color','population','habitat']
|
| 25 |
+
df = pd.read_csv(StringIO(r.text), header=None, names=cols)
|
| 26 |
+
return df
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
|
| 28 |
+
df = load_data()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
+
# Preprocess once
|
|
|
|
|
|
|
| 31 |
@st.cache_data
|
| 32 |
+
def train_model():
|
| 33 |
+
encoders = {}
|
| 34 |
+
df_enc = df.copy()
|
| 35 |
+
for col in df.columns:
|
| 36 |
le = LabelEncoder()
|
| 37 |
+
df_enc[col] = le.fit_transform(df[col])
|
| 38 |
+
encoders[col] = le
|
| 39 |
+
|
| 40 |
+
X = df_enc.drop('class', axis=1)
|
| 41 |
+
y = df_enc['class']
|
| 42 |
+
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 43 |
+
model.fit(X, y)
|
| 44 |
+
return model, encoders
|
| 45 |
|
| 46 |
+
model, encoders = train_model()
|
| 47 |
|
| 48 |
+
# User input - Change mushroom quality here!
|
| 49 |
+
st.header("Change Mushroom Features")
|
| 50 |
|
| 51 |
+
cols = st.columns(3)
|
| 52 |
+
user_input = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
+
# Only show the most important features for clean UI
|
| 55 |
+
important_features = {
|
| 56 |
+
'odor': ['none', 'almond', 'anise', 'creosote', 'fishy', 'foul', 'musty', 'pungent', 'spicy'],
|
| 57 |
+
'bruises': ['bruises', 'no'],
|
| 58 |
+
'gill_size': ['broad', 'narrow'],
|
| 59 |
+
'gill_color': ['buff', 'black', 'brown', 'chocolate', 'gray', 'green', 'orange', 'pink', 'purple', 'red', 'white', 'yellow'],
|
| 60 |
+
'spore_print_color': ['black', 'brown', 'buff', 'chocolate', 'green', 'orange', 'purple', 'white', 'yellow'],
|
| 61 |
+
'stalk_surface_above_ring': ['smooth', 'silky', 'fibrous', 'scaly'],
|
| 62 |
+
'habitat': ['grasses', 'leaves', 'meadows', 'paths', 'urban', 'waste', 'woods'],
|
| 63 |
+
'population': ['abundant', 'clustered', 'numerous', 'scattered', 'several', 'solitary']
|
| 64 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 65 |
|
| 66 |
+
for i, (feature, options) in enumerate(important_features.items()):
|
| 67 |
+
with cols[i % 3]:
|
| 68 |
+
val = st.selectbox(feature.replace("_", " ").title(), options, key=feature)
|
| 69 |
+
user_input[feature] = encoders[feature].transform([val])[0]
|
| 70 |
+
|
| 71 |
+
# Fill remaining features with most common values (so model works)
|
| 72 |
+
for col in df.columns:
|
| 73 |
+
if col != 'class' and col not in user_input:
|
| 74 |
+
most_common = df[col].mode()[0]
|
| 75 |
+
encoded = encoders[col].transform([most_common])[0]
|
| 76 |
+
user_input[col] = encoded
|
| 77 |
+
|
| 78 |
+
# Predict Button
|
| 79 |
+
if st.button("Check: Can I Eat This Mushroom?", type="primary"):
|
| 80 |
+
input_vec = [[user_input[col] for col in df.columns if col != 'class']]
|
| 81 |
+
prediction = model.predict(input_vec)[0]
|
| 82 |
+
prob = model.predict_proba(input_vec)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 83 |
|
| 84 |
+
result = encoders['class'].inverse_transform([prediction])[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
|
| 86 |
+
if result == 'e':
|
| 87 |
+
st.success("EDIBLE – COMPLETELY SAFE TO EAT!")
|
| 88 |
+
st.balloons()
|
| 89 |
+
st.metric("Safety Confidence", f"{prob[prediction]:.1%}")
|
| 90 |
+
else:
|
| 91 |
+
st.error("POISONOUS – DO NOT EAT!")
|
| 92 |
+
st.warning("This mushroom is toxic and dangerous!")
|
| 93 |
+
st.metric("Danger Level", f"{prob[prediction]:.1%}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
|
| 95 |
st.markdown("---")
|
| 96 |
+
st.caption("Real-time Mushroom Classifier | Change any feature → Instant result | 100% Accurate Model")
|