Campfireman's picture
Update app.py
9d7e90c
raw
history blame
3.95 kB
import gradio as gr
import hopsworks
import joblib
import pandas as pd
import numpy as np
import folium
import sklearn.preprocessing as proc
import json
import time
from datetime import timedelta, datetime
from branca.element import Figure
from functions import decode_features, get_weather_data, get_weather_df, get_weather_json_quick
##################
def greet(total_pred_days):
str1 = ""
if(total_pred_days == ""):
return "Empty input"
count = int(total_pred_days)
if count > 14:
str1 += "Warning: 14 days at most. " + '\n'
count = 14
if count <0:
str1 = "Invalid input."
return str1
count = count + 1
# Get weather data
fs = project.get_feature_store()
print("get the store")
feature_view = fs.get_feature_view(
name = 'weathernew_fv',
version = 1
)
X = pd.DataFrame()
for i in range(count+1):
# Get, rename column and rescale
next_day_date = datetime.today() + timedelta(days=i)
next_day = next_day_date.strftime ('%Y-%m-%d')
json = get_weather_json_quick(next_day)
temp = get_weather_data(json)
X = X.append(temp, ignore_index=True)
# X reshape
X.drop('preciptype', inplace = True, axis = 1)
X.drop('severerisk', inplace = True, axis = 1)
X.drop('stations', inplace = True, axis = 1)
X.drop('sunrise', inplace = True, axis = 1)
X.drop('sunset', inplace = True, axis = 1)
X.drop('moonphase', inplace = True, axis = 1)
X.drop('description', inplace = True, axis = 1)
X.drop('icon', inplace = True, axis = 1)
X = X.drop(columns=["sunriseEpoch", "sunsetEpoch", "source", "datetimeEpoch"]).fillna(0)
X = X.rename(columns={'pressure':'sealevelpressure'})
# Merge X and query
Y = X.append(Q, ignore_index=True)
# Data scaling
Y = Y.drop(columns = ['conditions', 'name', "datetime", "temp", "tempmax", "tempmin"])
category_cols = ['conditions']
cat_std_cols = ['feelslikemax','feelslikemin','feelslike','dew','humidity','precip','precipprob','precipcover','snow','snowdepth','windgust','windspeed','winddir','sealevelpressure','cloudcover','visibility','solarradiation','solarenergy','uvindex']
scaler_std = proc.StandardScaler()
Y.insert(19,"conditions",0)
Y.insert(0,"name",0)
Y[cat_std_cols] = scaler_std.fit_transform(Y[cat_std_cols])
Y[category_cols] = scaler_std.fit_transform(Y[category_cols])
# Predict
preds = model.predict(Y[0:count])
preds1= model1.predict(Y[0:count])
preds2= model2.predict(Y[0:count])
for x in range(count):
if (x != 0):
str1 += (datetime.now() + timedelta(days=x)).strftime('%Y-%m-%d') + " predicted temperature: " +str(float(preds[len(preds) - count + x]))+ "\npredicted max temperature: " +str(float(preds1[len(preds1) - count + x]))+ "\npredicted min temperature: " +str(float(preds2[len(preds2) - count + x]))+"\n"
return str1
#######################################################
# Preparations
project = hopsworks.login()
mr=project.get_model_registry()
model = mr.get_model("temp_model_new", version=1)
model_dir=model.download()
model1 = mr.get_model("tempmax_model_new", version=1)
model_dir1=model1.download()
model2 = mr.get_model("tempmin_model_new", version=1)
model_dir2=model2.download()
model = joblib.load(model_dir + "/model_temp_new.pkl")
model1 = joblib.load(model_dir1 + "/model_tempmax_new.pkl")
model2 = joblib.load(model_dir2+ "/model_tempmin_new.pkl")
fs = project.get_feature_store()
weather_fg = fs.get_or_create_feature_group(
name = 'weather_fg',
version = 1
)
query = weather_fg.select_all()
Q = query.read()
########################################################
# Gradio Interface
demo = gr.Interface(fn=greet, inputs = "text", outputs="text")
if __name__ == "__main__":
demo.launch()