Sathwikchowdary's picture
Update home.py
783bc55 verified
import streamlit as st
import pickle
import numpy as np
import os
# Load all trained models
MODEL_FILES = {
"KNN": "knn_model.pkl",
"Random Forest": "random_forest_model.pkl",
"Decision Tree": "decision_tree_model.pkl",
"Bagging": "bagging_model.pkl",
"Voting": "voting_model.pkl",
}
# Streamlit UI Config
st.set_page_config(page_title="🍷 Wine Quality Predictor", layout="centered")
# Custom Styling for Background & UI
st.markdown(
"""
<style>
.stApp {
background: linear-gradient(to right, #4B0101, #800020);
color: white;
}
.title {
font-size: 36px !important;
font-weight: bold;
color: #FFD700; /* Gold */
text-align: center;
}
.subtitle {
font-size: 24px !important;
font-weight: bold;
color: #FFA500; /* Orange */
}
.stSelectbox label, .stSlider label, .stNumberInput label {
font-size: 18px !important;
font-weight: bold;
color: white;
}
.stButton>button {
background-color: #FFD700; /* Gold */
color: #4B0101; /* Wine Red */
font-size: 18px;
font-weight: bold;
border-radius: 10px;
}
.stButton>button:hover {
background-color: #FFA500; /* Orange */
color: white;
}
.prediction {
font-size: 26px;
font-weight: bold;
color: #32CD32; /* Bright Green */
text-align: center;
}
</style>
""",
unsafe_allow_html=True,
)
# Title and Description
st.markdown('<h1 class="title">🍷 Wine Quality Prediction</h1>', unsafe_allow_html=True)
st.write("Predict the quality of wine based on its chemical properties.")
# Select Model
st.markdown('<h2 class="subtitle">πŸ” Select Prediction Model</h2>', unsafe_allow_html=True)
selected_model = st.selectbox("Choose a Model", list(MODEL_FILES.keys()))
# Load Selected Model
model_path = MODEL_FILES[selected_model]
if os.path.exists(model_path):
with open(model_path, "rb") as f:
model = pickle.load(f)
model_loaded = True
else:
model_loaded = False
st.error(f"Model file '{model_path}' not found. Please upload the correct model file.")
# User Inputs for Wine Features
st.markdown('<h2 class="subtitle">🍷 Enter Wine Properties</h2>', unsafe_allow_html=True)
fixed_acidity = st.number_input("Fixed Acidity", min_value=3.0, max_value=15.0, value=7.0)
volatile_acidity = st.number_input("Volatile Acidity", min_value=0.0, max_value=2.0, value=0.5)
citric_acid = st.number_input("Citric Acid", min_value=0.0, max_value=1.5, value=0.2)
residual_sugar = st.number_input("Residual Sugar", min_value=0.1, max_value=15.0, value=2.0)
chlorides = st.number_input("Chlorides", min_value=0.01, max_value=0.2, value=0.05)
free_sulfur_dioxide = st.number_input("Free Sulfur Dioxide", min_value=1, max_value=100, value=30)
total_sulfur_dioxide = st.number_input("Total Sulfur Dioxide", min_value=5, max_value=300, value=120)
density = st.number_input("Density", min_value=0.98, max_value=1.1, value=0.995)
pH = st.number_input("pH", min_value=2.5, max_value=4.5, value=3.2)
sulphates = st.number_input("Sulphates", min_value=0.3, max_value=2.0, value=0.8)
alcohol = st.number_input("Alcohol Content", min_value=5.0, max_value=20.0, value=10.0)
# Prepare input for model
input_data = np.array([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar,
chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density,
pH, sulphates, alcohol]])
# Prediction Button
if st.button("Predict Quality"):
if model_loaded:
prediction = model.predict(input_data)
st.markdown(f'<p class="prediction">Predicted Wine Quality: {int(prediction[0])}/10</p>', unsafe_allow_html=True)
else:
st.error(f"Model file '{model_path}' not found. Please upload the correct model file.")
st.write("*Powered by Machine Learning & AI* πŸš€")