Update app.py
Browse files
app.py
CHANGED
|
@@ -2,18 +2,9 @@ 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
|
| 10 |
-
|
| 11 |
-
"Random Forest": "random_forest_pipeline.pkl",
|
| 12 |
-
"Decision Tree": "decision_tree_pipeline.pkl",
|
| 13 |
-
"KNN": "knn_pipeline.pkl",
|
| 14 |
-
"Bagging": "bagging_pipeline.pkl",
|
| 15 |
-
"Voting": "voting_pipeline.pkl",
|
| 16 |
-
}
|
| 17 |
|
| 18 |
# Streamlit UI Setup
|
| 19 |
st.set_page_config(page_title="Wine Quality Prediction π·π¬", layout="centered")
|
|
@@ -37,42 +28,14 @@ st.markdown(
|
|
| 37 |
st.markdown('<h1 class="title">Wine Quality Prediction π·π¬</h1>', unsafe_allow_html=True)
|
| 38 |
st.write("Predicting Wine Quality based on wine parameters.")
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
# Check if Model Exists
|
| 45 |
-
model_path = MODEL_FILES[selected_model]
|
| 46 |
-
if os.path.exists(model_path):
|
| 47 |
-
with open(model_path, "rb") as f:
|
| 48 |
model = pickle.load(f)
|
| 49 |
model_loaded = True
|
| 50 |
else:
|
| 51 |
model_loaded = False
|
| 52 |
-
st.error(f"β Model file '{
|
| 53 |
-
|
| 54 |
-
# Instructions to Save the Model
|
| 55 |
-
st.write("π **To save the model, run this in your Python script:**")
|
| 56 |
-
st.code(f"""
|
| 57 |
-
import pickle
|
| 58 |
-
from sklearn.ensemble import RandomForestClassifier
|
| 59 |
-
from sklearn.pipeline import Pipeline
|
| 60 |
-
from sklearn.preprocessing import StandardScaler
|
| 61 |
-
|
| 62 |
-
# Assuming X_train, y_train are defined
|
| 63 |
-
model = Pipeline([
|
| 64 |
-
("scaler", StandardScaler()),
|
| 65 |
-
("classifier", RandomForestClassifier(n_estimators=100, random_state=42))
|
| 66 |
-
])
|
| 67 |
-
|
| 68 |
-
model.fit(X_train, y_train)
|
| 69 |
-
|
| 70 |
-
# Save the pipeline
|
| 71 |
-
with open("{model_path}", "wb") as f:
|
| 72 |
-
pickle.dump(model, f)
|
| 73 |
-
|
| 74 |
-
print("β
Model saved successfully!")
|
| 75 |
-
""", language="python")
|
| 76 |
|
| 77 |
# User Inputs
|
| 78 |
fixed_acidity = st.number_input("Fixed Acidity", min_value=4.6, max_value=15.9, value=7.6)
|
|
@@ -91,13 +54,12 @@ alcohol = st.number_input("Alcohol", min_value=8.4, max_value=14.9, value=10.2)
|
|
| 91 |
input_data = np.array([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
|
| 92 |
free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]])
|
| 93 |
|
| 94 |
-
# Prediction
|
| 95 |
if st.button("Predict Wine Quality"):
|
| 96 |
if model_loaded:
|
| 97 |
prediction = model.predict(input_data)
|
| 98 |
st.markdown(f'<p class="prediction">Predicted Wine Quality: {prediction[0]}</p>', unsafe_allow_html=True)
|
| 99 |
else:
|
| 100 |
-
st.error(f"β Model file '{
|
| 101 |
-
|
| 102 |
|
| 103 |
|
|
|
|
| 2 |
import pickle
|
| 3 |
import numpy as np
|
| 4 |
import os
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Define Model File Path
|
| 7 |
+
MODEL_PATH = "random_forest_pipeline.pkl"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
# Streamlit UI Setup
|
| 10 |
st.set_page_config(page_title="Wine Quality Prediction π·π¬", layout="centered")
|
|
|
|
| 28 |
st.markdown('<h1 class="title">Wine Quality Prediction π·π¬</h1>', unsafe_allow_html=True)
|
| 29 |
st.write("Predicting Wine Quality based on wine parameters.")
|
| 30 |
|
| 31 |
+
# Load Model if Available
|
| 32 |
+
if os.path.exists(MODEL_PATH):
|
| 33 |
+
with open(MODEL_PATH, "rb") as f:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
model = pickle.load(f)
|
| 35 |
model_loaded = True
|
| 36 |
else:
|
| 37 |
model_loaded = False
|
| 38 |
+
st.error(f"β Model file '{MODEL_PATH}' not found! Please upload the trained model.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
# User Inputs
|
| 41 |
fixed_acidity = st.number_input("Fixed Acidity", min_value=4.6, max_value=15.9, value=7.6)
|
|
|
|
| 54 |
input_data = np.array([[fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
|
| 55 |
free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol]])
|
| 56 |
|
| 57 |
+
# Prediction Button
|
| 58 |
if st.button("Predict Wine Quality"):
|
| 59 |
if model_loaded:
|
| 60 |
prediction = model.predict(input_data)
|
| 61 |
st.markdown(f'<p class="prediction">Predicted Wine Quality: {prediction[0]}</p>', unsafe_allow_html=True)
|
| 62 |
else:
|
| 63 |
+
st.error(f"β Model file '{MODEL_PATH}' not found. Please train the model and try again.")
|
|
|
|
| 64 |
|
| 65 |
|