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 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 X = X.drop(columns = ['conditions', "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() X.insert(19,"conditions",0) X.insert(0,"name",0) X[cat_std_cols] = scaler_std.fit_transform(X[cat_std_cols]) X[category_cols] = scaler_std.fit_transform(X[category_cols]) # Predict preds = model.predict(X[0:count]) preds1= model1.predict(X[0:count]) preds2= model2.predict(X[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() # 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() 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") ######################################################## # Gradio Interface #demo = gr.Interface(fn=greet, inputs = "text", outputs="text") with gr.Blocks() as demo: with gr.Row(): with gr.Column(): days = gr.Slider( label="How many days do you want to predict the temperature of? ", value=1, minimum=1, maximum=15, step=1 ) with gr.Column(): output = gr.Textbox( label="Predicted results: " ) days.change(greet, days, output) if __name__ == "__main__": demo.launch()