added app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import joblib
|
| 3 |
+
import pandas as pd
|
| 4 |
+
|
| 5 |
+
# Load pipeline components
|
| 6 |
+
scaler, pca, clf = joblib.load("tennis_model.pkl")
|
| 7 |
+
|
| 8 |
+
# All possible categorical values (must match training one-hot encoding)
|
| 9 |
+
outlook_options = ["Sunny", "Overcast", "Rain"]
|
| 10 |
+
temp_options = ["Hot", "Mild", "Cool"]
|
| 11 |
+
humidity_options = ["High", "Normal"]
|
| 12 |
+
wind_options = ["Weak", "Strong"]
|
| 13 |
+
|
| 14 |
+
def predict_play(outlook, temp, humidity, wind):
|
| 15 |
+
# Build input row
|
| 16 |
+
data = pd.DataFrame([[outlook, temp, humidity, wind]],
|
| 17 |
+
columns=["outlook", "temp", "humidity", "wind"])
|
| 18 |
+
|
| 19 |
+
# One-hot encode to match training
|
| 20 |
+
data_enc = pd.get_dummies(data)
|
| 21 |
+
|
| 22 |
+
# Ensure all training columns exist
|
| 23 |
+
for col in scaler.feature_names_in_:
|
| 24 |
+
if col not in data_enc:
|
| 25 |
+
data_enc[col] = 0
|
| 26 |
+
|
| 27 |
+
data_enc = data_enc[scaler.feature_names_in_] # reorder
|
| 28 |
+
|
| 29 |
+
# Scale + PCA + Predict
|
| 30 |
+
X_scaled = scaler.transform(data_enc)
|
| 31 |
+
X_pca = pca.transform(X_scaled)
|
| 32 |
+
pred = clf.predict(X_pca)[0]
|
| 33 |
+
|
| 34 |
+
return f"Prediction: {pred}"
|
| 35 |
+
|
| 36 |
+
# Gradio UI
|
| 37 |
+
with gr.Blocks() as demo:
|
| 38 |
+
gr.Markdown("# 🎾 Play Tennis Predictor")
|
| 39 |
+
outlook = gr.Dropdown(outlook_options, label="Outlook")
|
| 40 |
+
temp = gr.Dropdown(temp_options, label="Temperature")
|
| 41 |
+
humidity = gr.Dropdown(humidity_options, label="Humidity")
|
| 42 |
+
wind = gr.Dropdown(wind_options, label="Wind")
|
| 43 |
+
|
| 44 |
+
btn = gr.Button("Predict")
|
| 45 |
+
output = gr.Textbox(label="Result")
|
| 46 |
+
|
| 47 |
+
btn.click(fn=predict_play, inputs=[outlook, temp, humidity, wind], outputs=output)
|
| 48 |
+
|
| 49 |
+
demo.launch(server_host="0.0.0.0",server_port=7860,debug=True)
|