Man0707 commited on
Commit
69a9950
·
verified ·
1 Parent(s): 2bad453

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +108 -38
src/streamlit_app.py CHANGED
@@ -1,40 +1,110 @@
1
- import altair as alt
2
- import numpy as np
3
- import pandas as pd
4
  import streamlit as st
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- """
7
- # Welcome to Streamlit!
8
-
9
- Edit `/streamlit_app.py` to customize this app to your heart's desire :heart:.
10
- If you have any questions, checkout our [documentation](https://docs.streamlit.io) and [community
11
- forums](https://discuss.streamlit.io).
12
-
13
- In the meantime, below is an example of what you can do with just a few lines of code:
14
- """
15
-
16
- num_points = st.slider("Number of points in spiral", 1, 10000, 1100)
17
- num_turns = st.slider("Number of turns in spiral", 1, 300, 31)
18
-
19
- indices = np.linspace(0, 1, num_points)
20
- theta = 2 * np.pi * num_turns * indices
21
- radius = indices
22
-
23
- x = radius * np.cos(theta)
24
- y = radius * np.sin(theta)
25
-
26
- df = pd.DataFrame({
27
- "x": x,
28
- "y": y,
29
- "idx": indices,
30
- "rand": np.random.randn(num_points),
31
- })
32
-
33
- st.altair_chart(alt.Chart(df, height=700, width=700)
34
- .mark_point(filled=True)
35
- .encode(
36
- x=alt.X("x", axis=None),
37
- y=alt.Y("y", axis=None),
38
- color=alt.Color("idx", legend=None, scale=alt.Scale()),
39
- size=alt.Size("rand", legend=None, scale=alt.Scale(range=[1, 150])),
40
- ))
 
1
+ # streamlit_app.py ← THIS NAME WORKS PERFECTLY ON HUGGING FACE
 
 
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("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")