Update src/streamlit_app.py
Browse files- src/streamlit_app.py +144 -74
src/streamlit_app.py
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
# streamlit_app.py
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
|
@@ -10,101 +10,171 @@ 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("###
|
| 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 |
-
df =
|
| 28 |
-
st.success("Dataset loaded โ 8,124 mushrooms!")
|
| 29 |
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
poisonous = len(df[df['class'] == 'p'])
|
| 33 |
-
c1, c2 = st.columns(2)
|
| 34 |
-
c1.metric("Edible (Safe)", edible)
|
| 35 |
-
c2.metric("Poisonous (Deadly)", poisonous)
|
| 36 |
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
@st.cache_data
|
| 39 |
-
def
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
for
|
| 43 |
le = LabelEncoder()
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
X =
|
| 47 |
-
y =
|
| 48 |
-
return X, y,
|
| 49 |
|
| 50 |
-
X, y,
|
|
|
|
|
|
|
| 51 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
|
| 52 |
|
| 53 |
-
# Train
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
model = RandomForestClassifier(n_estimators=100, random_state=42)
|
| 57 |
model.fit(X_train, y_train)
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 61 |
st.balloons()
|
| 62 |
-
st.markdown("
|
| 63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
|
| 65 |
-
# Load
|
| 66 |
-
|
| 67 |
-
if
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 71 |
|
| 72 |
-
# Prediction
|
| 73 |
-
st.header("
|
| 74 |
-
if model
|
| 75 |
-
st.info("
|
| 76 |
else:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 77 |
cols = st.columns(3)
|
| 78 |
-
|
|
|
|
| 79 |
feature_options = {
|
| 80 |
-
'
|
| 81 |
-
'
|
| 82 |
-
'
|
| 83 |
-
'
|
| 84 |
-
'
|
| 85 |
-
'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
}
|
| 87 |
|
| 88 |
-
for i,
|
| 89 |
with cols[i % 3]:
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 102 |
st.balloons()
|
| 103 |
else:
|
| 104 |
-
st.error("POISONOUS โ DO NOT EAT!")
|
| 105 |
-
st.warning("This mushroom
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
|
|
|
| 109 |
|
| 110 |
-
st.
|
|
|
|
|
|
| 1 |
+
# streamlit_app.py - Mushroom Classification App
|
| 2 |
import streamlit as st
|
| 3 |
import pandas as pd
|
| 4 |
import requests
|
|
|
|
| 10 |
import os
|
| 11 |
|
| 12 |
st.set_page_config(page_title="Mushroom Doctor", layout="centered")
|
| 13 |
+
st.title("๐ Mushroom Doctor")
|
| 14 |
+
st.markdown("### *Edible* or *Poisonous*? AI Will Tell You Instantly!")
|
| 15 |
|
| 16 |
+
# Load Dataset Automatically
|
| 17 |
@st.cache_data
|
| 18 |
+
def load_mushroom_data():
|
| 19 |
url = "https://archive.ics.uci.edu/ml/machine-learning-databases/mushroom/agaricus-lepiota.data"
|
| 20 |
+
response = requests.get(url)
|
| 21 |
+
if response.status_code == 200:
|
| 22 |
+
columns = [
|
| 23 |
+
'class', 'cap_shape', 'cap_surface', 'cap_color', 'bruises', 'odor',
|
| 24 |
+
'gill_attachment', 'gill_spacing', 'gill_size', 'gill_color',
|
| 25 |
+
'stalk_shape', 'stalk_root', 'stalk_surface_above_ring',
|
| 26 |
+
'stalk_surface_below_ring', 'stalk_color_above_ring',
|
| 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 |
+
# Display Stats
|
| 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 |
+
st.dataframe(df.head(5), use_container_width=True)
|
| 52 |
+
|
| 53 |
+
# Preprocess Data
|
| 54 |
@st.cache_data
|
| 55 |
+
def preprocess_data(df):
|
| 56 |
+
le_dict = {}
|
| 57 |
+
df_encoded = df.copy()
|
| 58 |
+
for column in df.columns:
|
| 59 |
le = LabelEncoder()
|
| 60 |
+
df_encoded[column] = le.fit_transform(df[column])
|
| 61 |
+
le_dict[column] = le
|
| 62 |
+
X = df_encoded.drop('class', axis=1)
|
| 63 |
+
y = df_encoded['class']
|
| 64 |
+
return X, y, le_dict, df_encoded
|
| 65 |
|
| 66 |
+
X, y, label_encoders, df_encoded = preprocess_data(df)
|
| 67 |
+
|
| 68 |
+
# Train-Test Split
|
| 69 |
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42, stratify=y)
|
| 70 |
|
| 71 |
+
# Train Model
|
| 72 |
+
st.header("Train the Model")
|
| 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 |
+
# Load Trained Model
|
| 99 |
+
st.header("Load Trained Model")
|
| 100 |
+
if 'model_trained' not in st.session_state:
|
| 101 |
+
if os.path.exists('mushroom_model.pkl'):
|
| 102 |
+
try:
|
| 103 |
+
model_data = joblib.load('mushroom_model.pkl')
|
| 104 |
+
st.session_state.model = model_data['model']
|
| 105 |
+
st.session_state.label_encoders = model_data['label_encoders']
|
| 106 |
+
st.session_state.feature_names = model_data['features']
|
| 107 |
+
st.session_state.model_trained = True
|
| 108 |
+
st.success("โ
Model loaded from file!")
|
| 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 |
+
# Prediction Section
|
| 115 |
+
st.header("๐งช Predict: Is This Mushroom Safe?")
|
| 116 |
+
if 'model' not in st.session_state:
|
| 117 |
+
st.info("๐ Train or load the model above to make predictions!")
|
| 118 |
else:
|
| 119 |
+
model = st.session_state.model
|
| 120 |
+
encoders = st.session_state.label_encoders
|
| 121 |
+
features = st.session_state.feature_names
|
| 122 |
+
|
| 123 |
+
# Feature Selection UI
|
| 124 |
+
st.subheader("Select Mushroom Features")
|
| 125 |
+
input_features = {}
|
| 126 |
cols = st.columns(3)
|
| 127 |
+
|
| 128 |
+
# Simplified feature options for UI (key features only)
|
| 129 |
feature_options = {
|
| 130 |
+
'cap_shape': ['bell', 'conical', 'convex', 'flat', 'knobbed', 'sunken'],
|
| 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 |
+
for i, feat in enumerate(features):
|
| 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 |
+
# Make Prediction
|
| 154 |
+
if st.button("๐ฎ Predict Safety", type="secondary"):
|
| 155 |
+
# Prepare input
|
| 156 |
+
input_df = pd.DataFrame([input_features])[features] # Ensure column order
|
| 157 |
+
|
| 158 |
+
# Predict
|
| 159 |
+
prediction = model.predict(input_df)[0]
|
| 160 |
+
probabilities = model.predict_proba(input_df)[0]
|
| 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("๐ Mushroom Doctor | Powered by UCI Dataset & Random Forest | Built with Streamlit")
|