import streamlit as st
import pickle
import numpy as np
import os
# Define Model File Path
model_path = "random_forest_pipeline.pkl"
# Streamlit UI Setup
st.set_page_config(page_title="Wine Quality Prediction 🍷🔬", layout="centered")
# UI Styling
st.markdown(
"""
""",
unsafe_allow_html=True,
)
st.markdown('
Wine Quality Prediction 🍷🔬
', unsafe_allow_html=True)
st.write("Predicting Wine Quality based on wine parameters.")
# Check if model exists
if os.path.exists(model_path):
with open(model_path, "rb") as f:
model = pickle.load(f)
st.success("✅ Model loaded successfully!")
model_loaded = True
else:
st.error(f"❌ Model file '{model_path}' not found! Please upload or train the model first.")
model_loaded = False
# User Inputs
fixed_acidity = st.number_input("Fixed Acidity", min_value=4.6, max_value=15.9, value=7.6)
volatile_acidity = st.number_input("Volatile Acidity", min_value=0.12, max_value=1.58, value=1.2)
citric_acid = st.number_input("Citric Acid", min_value=0.0, max_value=1.66, value=0.5)
residual_sugar = st.number_input("Residual Sugar", min_value=0.9, max_value=15.5, value=5.8)
chlorides = st.number_input("Chlorides", min_value=0.012, max_value=0.611, value=0.044)
free_sulfur_dioxide = st.slider("Free Sulfur Dioxide", 1, 72, 15)
total_sulfur_dioxide = st.slider("Total Sulfur Dioxide", 6, 289, 100)
density = st.number_input("Density", min_value=0.99007, max_value=1.00369, value=1.00111)
pH = st.number_input("pH", min_value=2.74, max_value=4.01, value=3.12)
sulphates = st.number_input("Sulphates", min_value=0.33, max_value=2.00, value=0.9)
alcohol = st.number_input("Alcohol", min_value=8.4, max_value=14.9, value=10.2)
# Prepare Input for Prediction
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 Wine Quality"):
if model_loaded:
prediction = model.predict(input_data)
st.markdown(f'Predicted Wine Quality: {prediction[0]}
', unsafe_allow_html=True)
else:
st.error(f"❌ Model file '{model_path}' not found. Please train the model and try again.")