Spaces:
Runtime error
Runtime error
File size: 2,472 Bytes
5fe24db ef93412 5fe24db |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
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() |