Update app.py
Browse files
app.py
CHANGED
|
@@ -1,32 +1,51 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
|
|
|
|
|
|
| 4 |
model = joblib.load('rf_model.pkl')
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
if 0 in model_input:
|
| 11 |
-
return_message = "Error: Invalid Input \n Please Input: \n"
|
| 12 |
-
model_input_name = ['fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar', 'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density', 'pH', 'sulphates', 'alcohol']
|
| 13 |
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
return_message = return_message + " " + model_input_name[i]
|
| 17 |
-
return return_message
|
| 18 |
-
else:
|
| 19 |
-
data = pd.DataFrame([[ fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides, free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol
|
| 20 |
-
]], columns=model_input_name)
|
| 21 |
-
prediction = model.predict(data)[0]
|
| 22 |
-
return_message = "Predicted Quality: " + str(prediction['prediction_label'][0]) + "\nConfidence: " + str(prediction['prediction_score'][0])
|
| 23 |
-
return return_message
|
| 24 |
|
|
|
|
|
|
|
|
|
|
| 25 |
demo = gr.Interface(
|
| 26 |
-
fn
|
| 27 |
-
inputs
|
| 28 |
-
outputs
|
| 29 |
-
title
|
| 30 |
-
description="
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pandas as pd
|
| 3 |
import joblib
|
| 4 |
+
|
| 5 |
+
# Load the model
|
| 6 |
model = joblib.load('rf_model.pkl')
|
| 7 |
|
| 8 |
+
# Feature names
|
| 9 |
+
model_input_name = [
|
| 10 |
+
'fixed_acidity', 'volatile_acidity', 'citric_acid', 'residual_sugar',
|
| 11 |
+
'chlorides', 'free_sulfur_dioxide', 'total_sulfur_dioxide', 'density',
|
| 12 |
+
'pH', 'sulphates', 'alcohol'
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
def predict_quality(
|
| 16 |
+
fixed_acidity=None, volatile_acidity=None, citric_acid=None, residual_sugar=None,
|
| 17 |
+
chlorides=None, free_sulfur_dioxide=None, total_sulfur_dioxide=None, density=None,
|
| 18 |
+
pH=None, sulphates=None, alcohol=None
|
| 19 |
+
):
|
| 20 |
+
|
| 21 |
+
# Collect inputs
|
| 22 |
+
model_input = [
|
| 23 |
+
fixed_acidity, volatile_acidity, citric_acid, residual_sugar, chlorides,
|
| 24 |
+
free_sulfur_dioxide, total_sulfur_dioxide, density, pH, sulphates, alcohol
|
| 25 |
+
]
|
| 26 |
|
| 27 |
+
# Check for missing inputs (None)
|
| 28 |
+
if any(v is None for v in model_input):
|
| 29 |
+
missing = [
|
| 30 |
+
name for v, name in zip(model_input, model_input_name) if v is None
|
| 31 |
+
]
|
| 32 |
+
return "❌ Missing Input(s):\n" + "\n".join(missing)
|
| 33 |
|
| 34 |
+
# Create DataFrame
|
| 35 |
+
df = pd.DataFrame([model_input], columns=model_input_name)
|
|
|
|
|
|
|
|
|
|
| 36 |
|
| 37 |
+
# Predict (RandomForest returns a number like 5 or 6)
|
| 38 |
+
prediction = model.predict(df)[0]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
+
return f"⭐ Predicted Wine Quality: {prediction}"
|
| 41 |
+
|
| 42 |
+
# Gradio UI
|
| 43 |
demo = gr.Interface(
|
| 44 |
+
fn=predict_quality,
|
| 45 |
+
inputs=[gr.Number(label=name) for name in model_input_name],
|
| 46 |
+
outputs=gr.Textbox(label="Result", lines=4),
|
| 47 |
+
title="🍾 White Wine Quality Predictor 🍾",
|
| 48 |
+
description="🍷 Predict the quality of white wine using a trained Random Forest model."
|
| 49 |
+
)
|
| 50 |
+
|
| 51 |
+
demo.launch()
|