Update app.py
Browse files
app.py
CHANGED
|
@@ -1,29 +1,24 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
| 3 |
-
import pandas as pd
|
| 4 |
import numpy as np
|
| 5 |
import os
|
|
|
|
|
|
|
| 6 |
from sklearn.preprocessing import StandardScaler
|
| 7 |
|
| 8 |
-
#
|
| 9 |
-
|
| 10 |
-
"Random Forest": "random_forest_pipeline.pkl", # Ensure correct paths
|
| 11 |
-
"Decision Tree": "decision_tree_pipeline.pkl",
|
| 12 |
-
"KNN": "knn_pipeline.pkl",
|
| 13 |
-
"Bagging": "bagging_pipeline.pkl",
|
| 14 |
-
"Voting": "voting_pipeline.pkl",
|
| 15 |
-
}
|
| 16 |
|
| 17 |
# Streamlit UI Setup
|
| 18 |
st.set_page_config(page_title="Wine Quality Prediction π·π¬", layout="centered")
|
| 19 |
|
| 20 |
-
#
|
| 21 |
st.markdown(
|
| 22 |
"""
|
| 23 |
<style>
|
| 24 |
.stApp { background-color: #003366; color: white; }
|
| 25 |
-
.title { font-size: 36px
|
| 26 |
-
.subtitle { font-size: 24px
|
| 27 |
.stSelectbox label, .stNumberInput label, .stSlider label { font-size: 18px; font-weight: bold; color: white; }
|
| 28 |
.stButton>button { background-color: #ffcc00; color: #003366; font-size: 18px; font-weight: bold; border-radius: 10px; }
|
| 29 |
.stButton>button:hover { background-color: #ff9900; color: white; }
|
|
@@ -36,19 +31,45 @@ st.markdown(
|
|
| 36 |
st.markdown('<h1 class="title">Wine Quality Prediction π·π¬</h1>', unsafe_allow_html=True)
|
| 37 |
st.write("Predicting Wine Quality based on wine parameters.")
|
| 38 |
|
| 39 |
-
# Model
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
# Load Model
|
| 44 |
-
model_path = MODEL_FILES[selected_model]
|
| 45 |
-
if os.path.exists(model_path):
|
| 46 |
-
with open(model_path, "rb") as f:
|
| 47 |
model = pickle.load(f)
|
| 48 |
model_loaded = True
|
| 49 |
else:
|
| 50 |
model_loaded = False
|
| 51 |
-
st.error(f"Model file '{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
# User Inputs
|
| 54 |
fixed_acidity = st.number_input("Fixed Acidity", min_value=4.6, max_value=15.9, value=7.6)
|
|
@@ -63,7 +84,7 @@ pH = st.number_input("pH", min_value=2.74, max_value=4.01, value=3.12)
|
|
| 63 |
sulphates = st.number_input("Sulphates", min_value=0.33, max_value=2.00, value=0.9)
|
| 64 |
alcohol = st.number_input("Alcohol", min_value=8.4, max_value=14.9, value=10.2)
|
| 65 |
|
| 66 |
-
# Prepare
|
| 67 |
input_data = np.array([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
|
| 68 |
free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]])
|
| 69 |
|
|
@@ -73,4 +94,6 @@ if st.button("Predict Wine Quality"):
|
|
| 73 |
prediction = model.predict(input_data)
|
| 74 |
st.markdown(f'<p class="prediction">Predicted Wine Quality: {prediction[0]}</p>', unsafe_allow_html=True)
|
| 75 |
else:
|
| 76 |
-
st.error(f"Model file '{
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pickle
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 6 |
+
from sklearn.pipeline import Pipeline
|
| 7 |
from sklearn.preprocessing import StandardScaler
|
| 8 |
|
| 9 |
+
# Define Model File Paths
|
| 10 |
+
MODEL_PATH = "random_forest_pipeline.pkl"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
|
| 12 |
# Streamlit UI Setup
|
| 13 |
st.set_page_config(page_title="Wine Quality Prediction π·π¬", layout="centered")
|
| 14 |
|
| 15 |
+
# UI Styling
|
| 16 |
st.markdown(
|
| 17 |
"""
|
| 18 |
<style>
|
| 19 |
.stApp { background-color: #003366; color: white; }
|
| 20 |
+
.title { font-size: 36px; font-weight: bold; color: white; text-align: center; }
|
| 21 |
+
.subtitle { font-size: 24px; font-weight: bold; color: #ffcc00; }
|
| 22 |
.stSelectbox label, .stNumberInput label, .stSlider label { font-size: 18px; font-weight: bold; color: white; }
|
| 23 |
.stButton>button { background-color: #ffcc00; color: #003366; font-size: 18px; font-weight: bold; border-radius: 10px; }
|
| 24 |
.stButton>button:hover { background-color: #ff9900; color: white; }
|
|
|
|
| 31 |
st.markdown('<h1 class="title">Wine Quality Prediction π·π¬</h1>', unsafe_allow_html=True)
|
| 32 |
st.write("Predicting Wine Quality based on wine parameters.")
|
| 33 |
|
| 34 |
+
# Check if Model Exists
|
| 35 |
+
if os.path.exists(MODEL_PATH):
|
| 36 |
+
with open(MODEL_PATH, "rb") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
model = pickle.load(f)
|
| 38 |
model_loaded = True
|
| 39 |
else:
|
| 40 |
model_loaded = False
|
| 41 |
+
st.error(f"β Model file '{MODEL_PATH}' not found! Please train and save the model first.")
|
| 42 |
+
|
| 43 |
+
# Instructions to Save the Model
|
| 44 |
+
st.write("π **To save the model, run this in your Python script:**")
|
| 45 |
+
st.code(f"""
|
| 46 |
+
import pickle
|
| 47 |
+
from sklearn.ensemble import RandomForestClassifier
|
| 48 |
+
from sklearn.pipeline import Pipeline
|
| 49 |
+
from sklearn.preprocessing import StandardScaler
|
| 50 |
+
from sklearn.model_selection import train_test_split
|
| 51 |
+
from sklearn.datasets import load_wine
|
| 52 |
+
|
| 53 |
+
# Load sample dataset
|
| 54 |
+
data = load_wine()
|
| 55 |
+
X, y = data.data, data.target
|
| 56 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 57 |
+
|
| 58 |
+
# Create pipeline
|
| 59 |
+
model = Pipeline([
|
| 60 |
+
("scaler", StandardScaler()),
|
| 61 |
+
("classifier", RandomForestClassifier(n_estimators=100, random_state=42))
|
| 62 |
+
])
|
| 63 |
+
|
| 64 |
+
# Train model
|
| 65 |
+
model.fit(X_train, y_train)
|
| 66 |
+
|
| 67 |
+
# Save the pipeline
|
| 68 |
+
with open("{MODEL_PATH}", "wb") as f:
|
| 69 |
+
pickle.dump(model, f)
|
| 70 |
+
|
| 71 |
+
print("β
Model saved successfully!")
|
| 72 |
+
""", language="python")
|
| 73 |
|
| 74 |
# User Inputs
|
| 75 |
fixed_acidity = st.number_input("Fixed Acidity", min_value=4.6, max_value=15.9, value=7.6)
|
|
|
|
| 84 |
sulphates = st.number_input("Sulphates", min_value=0.33, max_value=2.00, value=0.9)
|
| 85 |
alcohol = st.number_input("Alcohol", min_value=8.4, max_value=14.9, value=10.2)
|
| 86 |
|
| 87 |
+
# Prepare Input for Prediction
|
| 88 |
input_data = np.array([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
|
| 89 |
free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]])
|
| 90 |
|
|
|
|
| 94 |
prediction = model.predict(input_data)
|
| 95 |
st.markdown(f'<p class="prediction">Predicted Wine Quality: {prediction[0]}</p>', unsafe_allow_html=True)
|
| 96 |
else:
|
| 97 |
+
st.error(f"β Model file '{MODEL_PATH}' not found. Please train the model and try again.")
|
| 98 |
+
|
| 99 |
+
|