Update src/streamlit_app.py
Browse files- src/streamlit_app.py +52 -27
src/streamlit_app.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
# app.py
|
| 2 |
-
# Wine Quality Predictor - 100% Original &
|
| 3 |
-
#
|
|
|
|
| 4 |
|
| 5 |
import streamlit as st
|
| 6 |
import pandas as pd
|
|
@@ -13,7 +14,7 @@ from sklearn.metrics import accuracy_score
|
|
| 13 |
# ------------------ Page Setup ------------------
|
| 14 |
st.set_page_config(
|
| 15 |
page_title="Wine Judge",
|
| 16 |
-
page_icon="
|
| 17 |
layout="centered",
|
| 18 |
initial_sidebar_state="expanded"
|
| 19 |
)
|
|
@@ -50,28 +51,46 @@ st.markdown("""
|
|
| 50 |
</style>
|
| 51 |
""", unsafe_allow_html=True)
|
| 52 |
|
| 53 |
-
# ------------------
|
| 54 |
-
st.markdown("<h1>Wine Judge</h1>", unsafe_allow_html=True)
|
| 55 |
-
st.markdown("<p style='text-align:center; font-size:1.5rem; color:#b0a3d4;'>Will your wine be legendary... or forgotten?</p>", unsafe_allow_html=True)
|
| 56 |
-
st.markdown("---")
|
| 57 |
-
|
| 58 |
-
# ------------------ Load Data ------------------
|
| 59 |
@st.cache_data
|
| 60 |
-
def
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
|
| 67 |
-
|
| 68 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
| 73 |
|
| 74 |
-
df =
|
| 75 |
|
| 76 |
# ------------------ Show Stats ------------------
|
| 77 |
col1, col2, col3 = st.columns(3)
|
|
@@ -82,26 +101,30 @@ with col2:
|
|
| 82 |
with col3:
|
| 83 |
st.metric("White Wines", len(df[df["wine_type"] == "White"]))
|
| 84 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
# ------------------ Train Model ------------------
|
| 86 |
features = df.drop(columns=["quality", "good_wine"])
|
| 87 |
target = df["good_wine"]
|
| 88 |
|
| 89 |
-
|
| 90 |
|
| 91 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 92 |
-
|
| 93 |
)
|
| 94 |
|
| 95 |
scaler = StandardScaler()
|
| 96 |
-
num_cols =
|
| 97 |
X_train[num_cols] = scaler.fit_transform(X_train[num_cols])
|
| 98 |
X_test[num_cols] = scaler.transform(X_test[num_cols])
|
| 99 |
|
| 100 |
@st.cache_resource
|
| 101 |
def train_model():
|
| 102 |
model = RandomForestClassifier(
|
| 103 |
-
n_estimators=
|
| 104 |
-
max_depth=
|
| 105 |
random_state=42,
|
| 106 |
class_weight="balanced",
|
| 107 |
n_jobs=-1
|
|
@@ -125,7 +148,9 @@ inputs["wine_type_Red"] = 1 if wine == "Red" else 0
|
|
| 125 |
inputs["wine_type_White"] = 1 if wine == "White" else 0
|
| 126 |
|
| 127 |
cols = st.columns(2)
|
| 128 |
-
|
|
|
|
|
|
|
| 129 |
|
| 130 |
for i, feat in enumerate(feature_list):
|
| 131 |
with cols[i % 2]:
|
|
@@ -159,4 +184,4 @@ st.markdown("</div>", unsafe_allow_html=True)
|
|
| 159 |
|
| 160 |
# ------------------ Footer ------------------
|
| 161 |
st.markdown("---")
|
| 162 |
-
st.caption("100% original code •
|
|
|
|
| 1 |
# app.py
|
| 2 |
+
# Wine Quality Predictor - 100% Original, Fixed & Self-Contained
|
| 3 |
+
# Synthetic data generated fresh - No external loads, no copyright issues
|
| 4 |
+
# Created & tested November 29, 2025
|
| 5 |
|
| 6 |
import streamlit as st
|
| 7 |
import pandas as pd
|
|
|
|
| 14 |
# ------------------ Page Setup ------------------
|
| 15 |
st.set_page_config(
|
| 16 |
page_title="Wine Judge",
|
| 17 |
+
page_icon="🍷", # Fixed: Use emoji instead of text for icon
|
| 18 |
layout="centered",
|
| 19 |
initial_sidebar_state="expanded"
|
| 20 |
)
|
|
|
|
| 51 |
</style>
|
| 52 |
""", unsafe_allow_html=True)
|
| 53 |
|
| 54 |
+
# ------------------ Generate Synthetic Data (Self-Contained) ------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
@st.cache_data
|
| 56 |
+
def generate_wine_data(n_samples=500):
|
| 57 |
+
np.random.seed(42) # For reproducibility
|
| 58 |
+
|
| 59 |
+
# Features inspired by real wine chemistry (original generation)
|
| 60 |
+
features = {}
|
| 61 |
+
features['fixed_acidity'] = np.random.uniform(4.0, 15.0, n_samples)
|
| 62 |
+
features['volatile_acidity'] = np.random.uniform(0.1, 1.5, n_samples)
|
| 63 |
+
features['citric_acid'] = np.random.uniform(0.0, 1.0, n_samples)
|
| 64 |
+
features['residual_sugar'] = np.random.uniform(0.5, 15.0, n_samples)
|
| 65 |
+
features['chlorides'] = np.random.uniform(0.01, 0.6, n_samples)
|
| 66 |
+
features['free_sulfur_dioxide'] = np.random.uniform(1.0, 72.0, n_samples)
|
| 67 |
+
features['total_sulfur_dioxide'] = np.random.uniform(6.0, 290.0, n_samples)
|
| 68 |
+
features['density'] = np.random.uniform(0.99, 1.01, n_samples)
|
| 69 |
+
features['pH'] = np.random.uniform(2.7, 4.3, n_samples)
|
| 70 |
+
features['sulphates'] = np.random.uniform(0.3, 2.0, n_samples)
|
| 71 |
+
features['alcohol'] = np.random.uniform(8.0, 14.0, n_samples)
|
| 72 |
|
| 73 |
+
# Wine type: 40% Red, 60% White (mimics real distribution)
|
| 74 |
+
wine_types = np.random.choice(['Red', 'White'], n_samples, p=[0.4, 0.6])
|
| 75 |
|
| 76 |
+
# Quality: Derived from features (higher alcohol/sulphates = better, plus noise)
|
| 77 |
+
quality = np.clip(
|
| 78 |
+
3 +
|
| 79 |
+
0.5 * features['alcohol'] -
|
| 80 |
+
2 * features['volatile_acidity'] +
|
| 81 |
+
features['sulphates'] +
|
| 82 |
+
np.random.normal(0, 1.5, n_samples),
|
| 83 |
+
3, 9
|
| 84 |
+
).astype(int)
|
| 85 |
|
| 86 |
+
df = pd.DataFrame(features)
|
| 87 |
+
df['wine_type'] = wine_types
|
| 88 |
+
df['quality'] = quality
|
| 89 |
+
df['good_wine'] = (quality >= 6).astype(int)
|
| 90 |
+
|
| 91 |
+
return df
|
| 92 |
|
| 93 |
+
df = generate_wine_data()
|
| 94 |
|
| 95 |
# ------------------ Show Stats ------------------
|
| 96 |
col1, col2, col3 = st.columns(3)
|
|
|
|
| 101 |
with col3:
|
| 102 |
st.metric("White Wines", len(df[df["wine_type"] == "White"]))
|
| 103 |
|
| 104 |
+
# Preview data
|
| 105 |
+
st.markdown("### Sample Data")
|
| 106 |
+
st.dataframe(df.head(), use_container_width=True)
|
| 107 |
+
|
| 108 |
# ------------------ Train Model ------------------
|
| 109 |
features = df.drop(columns=["quality", "good_wine"])
|
| 110 |
target = df["good_wine"]
|
| 111 |
|
| 112 |
+
features_encoded = pd.get_dummies(features, columns=["wine_type"])
|
| 113 |
|
| 114 |
X_train, X_test, y_train, y_test = train_test_split(
|
| 115 |
+
features_encoded, target, test_size=0.2, random_state=42, stratify=target
|
| 116 |
)
|
| 117 |
|
| 118 |
scaler = StandardScaler()
|
| 119 |
+
num_cols = features_encoded.select_dtypes(include=np.number).columns.tolist()
|
| 120 |
X_train[num_cols] = scaler.fit_transform(X_train[num_cols])
|
| 121 |
X_test[num_cols] = scaler.transform(X_test[num_cols])
|
| 122 |
|
| 123 |
@st.cache_resource
|
| 124 |
def train_model():
|
| 125 |
model = RandomForestClassifier(
|
| 126 |
+
n_estimators=200, # Reduced for faster training on synthetic data
|
| 127 |
+
max_depth=10,
|
| 128 |
random_state=42,
|
| 129 |
class_weight="balanced",
|
| 130 |
n_jobs=-1
|
|
|
|
| 148 |
inputs["wine_type_White"] = 1 if wine == "White" else 0
|
| 149 |
|
| 150 |
cols = st.columns(2)
|
| 151 |
+
|
| 152 |
+
# Fixed: Proper list comprehension
|
| 153 |
+
feature_list = [c for c in features_encoded.columns if "wine_type" not in str(c)]
|
| 154 |
|
| 155 |
for i, feat in enumerate(feature_list):
|
| 156 |
with cols[i % 2]:
|
|
|
|
| 184 |
|
| 185 |
# ------------------ Footer ------------------
|
| 186 |
st.markdown("---")
|
| 187 |
+
st.caption("100% original code • Synthetic Wine Data • Made with care on November 29, 2025")
|