jiawenchim's picture
Update app.py
2369f0c verified
raw
history blame
No virus
4.78 kB
import gradio as gr
import numpy as np
import os
from joblib import load
from tensorflow.keras.models import load_model
# HF_TOKEN = os.getenv("HF_TOKEN")
# hf_writer = gr.HuggingFaceDatasetSaver(HF_TOKEN, "weather-madrid-flags")
def predict_energy(f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13):
# input_feature = [f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11,f12,f13]
weather_input_array = np.array([f1,f2,f3,f4,f5,f6]).reshape(1,-1)
scaler = load("scaler.joblib")
scaled_weather_input_array = scaler.transform(weather_input_array)
history_input_array = np.array([f7,f8,f9,f10,f11,f12,f13])
input_feature = np.concatenate((scaled_weather_input_array[0],history_input_array[0]))
lstm_model_7_in_1_out = load_model('history_7_future_1.h5')
# dtc = load('dtc_model.sav')
# xgb = load('xgb_model.sav')
output = model.predict(input_feature)
output = output[0][0]
return output
# if input_feature is not None:
# if f1 == "Decision Trees":
# model = dtc.predict(input_feature)
# elif f1 == "Random Forest":
# model = rfc.predict(input_feature)
# elif f1 == "XGBoost":
# model = xgb.predict(input_feature)
# elif f1 is None:
# return "Please select an algorithm to use."
# else:
# pass
# if model[0] == 0:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Extreme"
# elif model[0] == 1:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Fog"
# elif model[0] == 2:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Fog-Rain"
# elif model[0] == 3:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Nothing"
# elif model[0] == 4:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Rain"
# elif model[0] == 5:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Rain-Snow"
# elif model[0] == 6:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Rain-Thunderstorms"
# elif model[0] == 7:
# return "The forecasted weather using the " + str(f1) + " algorithm is: Thunderstorms"
# else:
# pass
#Create the input component for Gradio since we are expecting 23 inputs
# Interface Inputs
inputs_app = [gr.inputs.Slider(0,12, step=1, label='Month', default=11),
gr.inputs.Slider(0,23, step=1, label='Hour', default=16),
gr.inputs.Slider(-4,33, step=1, label='Temperature', default=9.84),
gr.inputs.Slider(0.2,1, step=0.5, label='Humidity', default=0.86),
gr.inputs.Slider(0.04,15, step=0.5, label='windSpeed', default=2.95),
gr.inputs.Slider(0,1, step=1, label='Holiday = 1', default=0),
gr.inputs.Slider(0,1, step=0.005, label='t-1 energy consumption', default=0.38),
gr.inputs.Slider(0,1, step=0.005, label='t-2 energy consumption', default=0.48),
gr.inputs.Slider(0,1, step=0.005, label='t-3 energy consumption', default=0.48),
gr.inputs.Slider(0,1, step=0.005, label='t-4 energy consumption', default=0.66),
gr.inputs.Slider(0,1, step=0.005, label='t-5 energy consumption', default=0.30),
gr.inputs.Slider(0,1, step=0.005, label='t-6 energy consumption', default=0.25),
gr.inputs.Slider(0,1, step=0.005, label='t-7 energy consumption', default=0.57)
]
# Interface Output
outputs_app = ["text"]
# Building the Gradio Interface
weather_predictor_app = gr.Interface(fn=predict_energy,
inputs=inputs_app,
outputs=outputs_app,
allow_flagging="manual",
live=True,
# examples = [["Decision Trees", 7,4,2,5,3,2,100,95,76,1010,1008,1004,10,9,4,13,6,0,6,229,1997,1],
# ["Random Forest", 37,28,18,7,4,-2,43,18,4,1019,1016,1010,31,29,16,29,13,0,6,244,2015,7],
# ["XGBoost", 16,12,8,11,7,4,94,69,52,1014,1012,1009,10,10,10,14,5,0,5,35,2004,10]],
title = "Energy Consumption Prediction (London)",
description="Enter parameters using sliders provided to predict the weather.",
theme = "darkhuggingface",
css="footer {visibility: hidden}",
flagging_callback=hf_writer
)
weather_predictor_app.launch()