Update app.py
Browse files
app.py
CHANGED
|
@@ -4,38 +4,33 @@ import pickle
|
|
| 4 |
import numpy as np
|
| 5 |
from sklearn.preprocessing import StandardScaler
|
| 6 |
|
| 7 |
-
|
|
|
|
| 8 |
def load_model():
|
| 9 |
-
with open(
|
| 10 |
return pickle.load(f)
|
| 11 |
|
| 12 |
-
# 2) Prediction function: takes five numeric inputs, scales them, returns class
|
| 13 |
def predict(feature1, feature2, feature3, feature4, feature5):
|
| 14 |
model = load_model()
|
| 15 |
-
|
| 16 |
-
#
|
| 17 |
-
|
| 18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 19 |
|
| 20 |
-
|
| 21 |
-
demo = gr.Interface(
|
| 22 |
-
fn=predict,
|
| 23 |
-
inputs=[
|
| 24 |
-
gr.Slider(0, 10, value=5, label="Feature 1"),
|
| 25 |
-
gr.Slider(0, 10, value=3, label="Feature 2"),
|
| 26 |
-
gr.Slider(0, 10, value=7, label="Feature 3"),
|
| 27 |
-
gr.Slider(0, 10, value=6, label="Feature 4"),
|
| 28 |
-
gr.Slider(0, 10, value=4, label="Feature 5"),
|
| 29 |
-
],
|
| 30 |
-
outputs=gr.Textbox(label="Predicted Class"),
|
| 31 |
-
title="TaskMaster Job Scheduler",
|
| 32 |
-
description="Enter five feature values to get a RandomForest prediction.",
|
| 33 |
-
)
|
| 34 |
|
| 35 |
-
# 4) Launch with SSR turned off for Spaces
|
| 36 |
if __name__ == "__main__":
|
| 37 |
-
demo.launch(
|
| 38 |
-
server_name="0.0.0.0",
|
| 39 |
-
server_port=7860,
|
| 40 |
-
ssr_mode=False # disable server-side rendering on the Space
|
| 41 |
-
)
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from sklearn.preprocessing import StandardScaler
|
| 6 |
|
| 7 |
+
MODEL_PATH = "rf_model.pkl"
|
| 8 |
+
|
| 9 |
def load_model():
|
| 10 |
+
with open(MODEL_PATH, "rb") as f:
|
| 11 |
return pickle.load(f)
|
| 12 |
|
|
|
|
| 13 |
def predict(feature1, feature2, feature3, feature4, feature5):
|
| 14 |
model = load_model()
|
| 15 |
+
raw = np.array([[feature1, feature2, feature3, feature4, feature5]])
|
| 16 |
+
# Standardize features (must match training preprocessing)
|
| 17 |
+
scaler = StandardScaler()
|
| 18 |
+
scaled = scaler.fit_transform(raw)
|
| 19 |
+
return int(model.predict(scaled)[0])
|
| 20 |
+
|
| 21 |
+
with gr.Blocks() as demo:
|
| 22 |
+
gr.Markdown("## 🐨 Random Forest Prediction App\nMove the sliders and click **Predict**")
|
| 23 |
+
with gr.Row():
|
| 24 |
+
f1 = gr.Slider(0, 10, value=5, label="Feature 1")
|
| 25 |
+
f2 = gr.Slider(0, 10, value=3, label="Feature 2")
|
| 26 |
+
with gr.Row():
|
| 27 |
+
f3 = gr.Slider(0, 10, value=7, label="Feature 3")
|
| 28 |
+
f4 = gr.Slider(0, 10, value=6, label="Feature 4")
|
| 29 |
+
f5 = gr.Slider(0, 10, value=4, label="Feature 5")
|
| 30 |
+
btn = gr.Button("Predict")
|
| 31 |
+
out = gr.Textbox(label="Prediction")
|
| 32 |
|
| 33 |
+
btn.click(fn=predict, inputs=[f1, f2, f3, f4, f5], outputs=out)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
|
|
|
| 35 |
if __name__ == "__main__":
|
| 36 |
+
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|