Man0707 commited on
Commit
4c8fd33
ยท
verified ยท
1 Parent(s): 69a9950

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +144 -74
src/streamlit_app.py CHANGED
@@ -1,4 +1,4 @@
1
- # streamlit_app.py โ† THIS NAME WORKS PERFECTLY ON HUGGING FACE
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("### Is it *Edible* or *Poisonous*?")
15
 
16
- # Load 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
- return pd.read_csv(StringIO(r.text), header=None, names=cols)
 
 
 
 
 
 
 
 
 
26
 
27
- df = load_data()
28
- st.success("Dataset loaded โ€“ 8,124 mushrooms!")
29
 
30
- # Show stats
31
- edible = len(df[df['class'] == 'e'])
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
- # Preprocess
 
 
 
 
 
 
 
 
 
 
 
 
38
  @st.cache_data
39
- def preprocess():
40
- encoders = {}
41
- df_enc = df.copy()
42
- for col in df.columns:
43
  le = LabelEncoder()
44
- df_enc[col] = le.fit_transform(df[col])
45
- encoders[col] = le
46
- X = df_enc.drop('class', axis=1)
47
- y = df_enc['class']
48
- return X, y, encoders
49
 
50
- X, y, encoders = preprocess()
 
 
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 button
54
- if st.button("Train Model โ€“ 100% Accuracy!", type="primary"):
55
- with st.spinner("Training Random Forest..."):
 
56
  model = RandomForestClassifier(n_estimators=100, random_state=42)
57
  model.fit(X_train, y_train)
58
- acc = model.score(X_test, y_test)
59
- st.success(f"Trained! Accuracy: {acc:.1%}")
60
- if acc == 1.0:
 
 
 
 
 
 
61
  st.balloons()
62
- st.markdown("*PERFECT CLASSIFICATION!*")
63
- joblib.dump({"model": model, "encoders": encoders}, "model.pkl")
 
 
 
 
 
 
 
 
64
 
65
- # Load model
66
- model = None
67
- if os.path.exists("model.pkl"):
68
- loaded = joblib.load("model.pkl")
69
- model = loaded["model"]
70
- encoders = loaded["encoders"]
 
 
 
 
 
 
 
 
 
71
 
72
- # Prediction
73
- st.header("Check Your Mushroom")
74
- if model is None:
75
- st.info("Click 'Train Model' first!")
76
  else:
 
 
 
 
 
 
 
77
  cols = st.columns(3)
78
- inputs = {}
 
79
  feature_options = {
80
- 'odor': ['none','almond','anise','creosote','fishy','foul','musty','pungent','spicy'],
81
- 'bruises': ['bruises','no'],
82
- 'gill_size': ['broad','narrow'],
83
- 'spore_print_color': ['black','brown','buff','chocolate','green','orange','purple','white','yellow'],
84
- 'population': ['abundant','clustered','numerous','scattered','several','solitary'],
85
- 'habitat': ['grasses','leaves','meadows','paths','urban','waste','woods']
 
 
 
 
 
86
  }
87
 
88
- for i, col in enumerate(X.columns):
89
  with cols[i % 3]:
90
- options = feature_options.get(col, list(encoders[col].classes_))
91
- val = st.selectbox(col.replace("_", " ").title(), options, key=col)
92
- inputs[col] = encoders[col].transform([val])[0]
93
-
94
- if st.button("Predict โ€“ Safe or Deadly?", type="secondary"):
95
- input_vec = [[inputs[c] for c in X.columns]]
96
- pred = model.predict(input_vec)[0]
97
- prob = model.predict_proba(input_vec)[0]
98
- result = encoders['class'].inverse_transform([pred])[0]
99
-
100
- if result == 'e':
101
- st.success("EDIBLE โ€“ SAFE TO EAT!")
 
 
 
 
 
 
 
 
 
 
 
 
 
102
  st.balloons()
103
  else:
104
- st.error("POISONOUS โ€“ DO NOT EAT!")
105
- st.warning("This mushroom is deadly!")
106
-
107
- st.metric("Edible Probability", f"{prob[0]:.1%}")
108
- st.metric("Poisonous Probability", f"{prob[1]:.1%}")
 
109
 
110
- st.caption("Mushroom Doctor โ€ข 100% Deployable โ€ข File: streamlit_app.py")
 
 
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")