kppham commited on
Commit
7ffd0d5
·
verified ·
1 Parent(s): 4f2f3c2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +31 -41
app.py CHANGED
@@ -1,51 +1,41 @@
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()
 
 
1
  import gradio as gr
2
  import pandas as pd
3
  import joblib
4
+ import os
5
+ from sklearn.ensemble import RandomForestClassifier
6
+ from sklearn.model_selection import train_test_split
7
 
8
+ MODEL_PATH = "rf_model.pkl"
9
+ DATA_URL = "https://archive.ics.uci.edu/ml/machine-learning-databases/wine-quality/winequality-white.csv"
10
+
11
+ # ---------------------------
12
+ # TRAIN MODEL (only if needed)
13
+ # ---------------------------
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
+ def train_model():
16
+ print("Downloading white wine dataset...")
17
+ df = pd.read_csv(DATA_URL, sep=';')
 
 
 
18
 
19
+ feature_names = [
20
+ 'fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar',
21
+ 'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density',
22
+ 'pH', 'sulphates', 'alcohol'
23
+ ]
24
 
25
+ X = df[feature_names]
26
+ y = df['quality']
27
 
28
+ X_train, X_test, y_train, y_test = train_test_split(
29
+ X, y, test_size=0.2, random_state=42
30
+ )
31
 
32
+ print("Training Random Forest model...")
33
+ model = RandomForestClassifier(
34
+ n_estimators=300,
35
+ max_depth=12,
36
+ random_state=42
37
+ )
38
+ model.fit(X_train, y_train)
 
39
 
40
+ joblib.dump(model, MODEL_PATH)
41
+ print("Model sav