Spaces:
Runtime error
Runtime error
import pickle | |
import pandas as pd | |
import numpy as np | |
import xgboost as xgb | |
import gradio as gr | |
import pathlib | |
#plt = platform.system() | |
#if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath | |
model_path = "model.None" | |
model = xgb.Booster() | |
model.load_model(model_path) | |
dv_path = "dv.bin" | |
with open(dv_path, 'rb') as f_out: | |
dv = pickle.load(f_out) | |
scaler_path = "scaler.bin" | |
with open(scaler_path, 'rb') as f_out: | |
scaler = pickle.load(f_out) | |
def preprocess(data): | |
"""Preprocessing of the data""" | |
# turn json input to dataframe | |
data = pd.DataFrame([data]) | |
# define numerical and categorical features | |
numerical = ["X1", "X2", "X3", "X4", "X5", "X7"] | |
categorical = ["X6", "X8"] | |
# preprocess numerical features | |
X_num = scaler.transform(data[numerical]) | |
# preprocess categorical features | |
data[categorical] = data[categorical].astype("string") | |
X_dicts = data[categorical].to_dict(orient="records") | |
X_cat = dv.transform(X_dicts) | |
# concatenate both | |
X = np.concatenate((X_num, X_cat), axis=1) | |
return X | |
def predict(X): | |
"""make predictions""" | |
pred = model.predict(X) | |
print('prediction', pred[0]) | |
return float(pred[0]) | |
def main(X1,X2,X3,X4,X5,X6,X7,X8): | |
"""request input, preprocess it and make prediction""" | |
input_data = { | |
"X1": X1, | |
"X2": X2, | |
"X3": X3, | |
"X4": X4, | |
"X5": X5, | |
"X6": X6, | |
"X7": X7, | |
"X8": X8 | |
} | |
features = preprocess(input_data) | |
features_2 = xgb.DMatrix(features) | |
pred = predict(features_2) | |
result = {'heat load': pred} | |
return pred | |
def classify_image(img): | |
pred,idx,probs = learn.predict(img) | |
return dict(zip(categories,map(float,probs))) | |
#create input and output objects | |
#input | |
input1 = gr.inputs.Number() | |
input2 = gr.inputs.Number() | |
input3 = gr.inputs.Number() | |
input4 = gr.inputs.Number() | |
input5 = gr.inputs.Number() | |
input6 = gr.inputs.Number() | |
input7 = gr.inputs.Number() | |
input8 = gr.inputs.Number() | |
#output object | |
output = gr.outputs.Textbox() | |
intf = gr.Interface(title = "Energy Efficiency", | |
description = "The objective of this project is to predict the Heating Load based on various building features.", | |
fn=main, | |
inputs=[input1,input2,input3,input4,input5,input6,input7,input8], | |
outputs=[output], | |
live=True, | |
enable_queue=True | |
) | |
intf.launch() |