Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,33 +1,55 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
import pandas as pd
|
|
|
|
| 4 |
from huggingface_hub import hf_hub_download
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
repo_id = "
|
| 8 |
-
model_path = hf_hub_download(repo_id=repo_id, filename="random_forest_model.pkl")
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
with open(model_path, 'rb') as f:
|
| 11 |
model = pickle.load(f)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
-
#
|
| 14 |
-
|
| 15 |
-
df = pd.DataFrame([[feature1, feature2, feature3]],
|
| 16 |
-
columns=['feature1', 'feature2', 'feature3'])
|
| 17 |
-
pred = model.predict(df)[0]
|
| 18 |
-
prob = model.predict_proba(df)[0].max()
|
| 19 |
-
return f"Prediction: {pred} (Confidence: {prob:.2f})"
|
| 20 |
|
| 21 |
-
# Create the API endpoint
|
| 22 |
demo = gr.Interface(
|
| 23 |
fn=predict,
|
| 24 |
-
inputs=
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
gr.Number(label="Feature 3")
|
| 28 |
-
],
|
| 29 |
-
outputs=gr.Textbox(label="Result"),
|
| 30 |
-
title="Random Forest API"
|
| 31 |
)
|
| 32 |
|
| 33 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
import pickle
|
| 3 |
import pandas as pd
|
| 4 |
+
import numpy as np
|
| 5 |
from huggingface_hub import hf_hub_download
|
| 6 |
|
| 7 |
+
# Download model from MODEL repository (not stored in Space)
|
| 8 |
+
repo_id = "DevNumb/random-forest-model-reduced" # Your MODEL repo
|
|
|
|
| 9 |
|
| 10 |
+
print("Downloading model (1.64 GB)...")
|
| 11 |
+
model_path = hf_hub_download(
|
| 12 |
+
repo_id=repo_id,
|
| 13 |
+
filename="random_forest_model_reduced.pkl",
|
| 14 |
+
local_dir="/tmp/model" # Downloads to Space's 50 GB disk
|
| 15 |
+
)
|
| 16 |
+
|
| 17 |
+
print("Loading model...")
|
| 18 |
with open(model_path, 'rb') as f:
|
| 19 |
model = pickle.load(f)
|
| 20 |
+
print("✅ Model ready!")
|
| 21 |
+
|
| 22 |
+
# Feature names from your JSON
|
| 23 |
+
FEATURE_NAMES = [
|
| 24 |
+
'OA_TEMP', 'OA_TEMP_WB', 'Hour', 'Weekday', 'Month',
|
| 25 |
+
'CHL_STA_1', 'CHL_STA_2', 'CHL_STA_3',
|
| 26 |
+
'CHL_COMP_SPD_CTRL_1', 'CHL_COMP_SPD_CTRL_2', 'CHL_COMP_SPD_CTRL_3',
|
| 27 |
+
'CT_FAN_SPD_CTRL_1', 'CT_FAN_SPD_CTRL_2', 'CT_FAN_SPD_CTRL_3',
|
| 28 |
+
'CHL_CD_FLOW_1', 'CHL_CD_FLOW_2', 'CHL_CD_FLOW_3',
|
| 29 |
+
'CWL_SEC_LOAD'
|
| 30 |
+
]
|
| 31 |
+
|
| 32 |
+
def predict(*args):
|
| 33 |
+
input_data = pd.DataFrame([list(args)], columns=FEATURE_NAMES)
|
| 34 |
+
prediction = model.predict(input_data)[0]
|
| 35 |
+
|
| 36 |
+
try:
|
| 37 |
+
probabilities = model.predict_proba(input_data)[0]
|
| 38 |
+
confidence = np.max(probabilities)
|
| 39 |
+
result = f"**Prediction:** {prediction:.2f}\n\n**Confidence:** {confidence:.2%}"
|
| 40 |
+
except:
|
| 41 |
+
result = f"**Prediction:** {prediction}"
|
| 42 |
+
|
| 43 |
+
return result
|
| 44 |
|
| 45 |
+
# Create input fields for all 18 features
|
| 46 |
+
inputs = [gr.Number(label=name) for name in FEATURE_NAMES]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
|
|
|
| 48 |
demo = gr.Interface(
|
| 49 |
fn=predict,
|
| 50 |
+
inputs=inputs,
|
| 51 |
+
outputs=gr.Markdown(label="Result"),
|
| 52 |
+
title="HVAC Chiller System Prediction API"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
)
|
| 54 |
|
| 55 |
demo.launch()
|