kppham commited on
Commit
b88ade0
·
verified ·
1 Parent(s): a7d5eca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -1
app.py CHANGED
@@ -38,4 +38,46 @@ def train_model():
38
  model.fit(X_train, y_train)
39
 
40
  joblib.dump(model, MODEL_PATH)
41
- print("Model sav
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
  model.fit(X_train, y_train)
39
 
40
  joblib.dump(model, MODEL_PATH)
41
+ print("Model saved as rf_model.pkl")
42
+ return model
43
+
44
+
45
+ # Load or train model
46
+ if os.path.exists(MODEL_PATH):
47
+ print("Loading existing model...")
48
+ model = joblib.load(MODEL_PATH)
49
+ else:
50
+ model = train_model()
51
+
52
+
53
+ # ---------------------------
54
+ # PREDICTION FUNCTION
55
+ # ---------------------------
56
+
57
+ feature_names = [
58
+ 'fixed acidity', 'volatile acidity', 'citric acid', 'residual sugar',
59
+ 'chlorides', 'free sulfur dioxide', 'total sulfur dioxide', 'density',
60
+ 'pH', 'sulphates', 'alcohol'
61
+ ]
62
+
63
+ def predict_quality(*inputs):
64
+ df = pd.DataFrame([inputs], columns=feature_names)
65
+ prediction = model.predict(df)[0]
66
+ return f"Predicted Wine Quality: {prediction}"
67
+
68
+
69
+ # ---------------------------
70
+ # GRADIO UI
71
+ # ---------------------------
72
+
73
+ inputs_ui = [gr.Number(label=name) for name in feature_names]
74
+
75
+ demo = gr.Interface(
76
+ fn=predict_quality,
77
+ inputs=inputs_ui,
78
+ outputs=gr.Textbox(label="Prediction"),
79
+ title="🍾 White Wine Quality Predictor (Trains on HF Space)",
80
+ description="Random Forest model trained on the UCI White Wine Quality dataset."
81
+ )
82
+
83
+ demo.launch()