project / app.py
Orangefish's picture
Update app.py
db1ef8c
#!/usr/bin/env python
# coding: utf-8
# In[37]:
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 get_weather_by_date
def greet(name):
X = pd.DataFrame()
for i in range(8):
# Get, rename column and rescalef
target_date = datetime.today() + timedelta(days=i)
target_date = target_date.strftime('%Y-%m-%d')
json = get_weather_by_date(target_date)
X = X.append(json['days'][0], ignore_index=True)
# In[38]:
X.head()
X.columns.values.tolist()
# In[39]:
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.drop('datetime', inplace = True, axis = 1)
# In[40]:
X.head()
# In[41]:
X = X.rename(columns={'sunriseEpoch':'pm25'})
X = X.rename(columns={'sunsetEpoch':'pm10'})
X = X.rename(columns={'source':'o3'})
X = X.rename(columns={'normal':'aqi'})
X = X.rename(columns={'datetimeEpoch':'city'})
# In[42]:
X.head()
# In[43]:
# X = X.drop(columns = ['conditions', "pm25", "pm10", "o3", "aqi"])
X = X.drop(columns = ['conditions', "pm25", "pm10", "o3"])
X.insert(0,"aqi",0)
X.insert(0,"o3",0)
X.insert(0,"pm10",0)
X.insert(0,"pm25",0)
X.insert(27,"conditions",0)
# In[44]:
X.head()
# In[46]:
preds = model.predict(X)
# In[51]:
print(preds)
# In[53]:
str1 = ""
for x in range(8):
if(x != 0):
str1 += (datetime.now() + timedelta(days=x)).strftime('%Y-%m-%d') + " predicted aqi: " + str(int(preds[x]))+"\n"
print(str1)
return str1
# In[ ]:
project = hopsworks.login()
mr = project.get_model_registry()
# In[50]:
model = mr.get_model("gradient_boost_model",version = 4)
model_dir = model.download()
model = joblib.load(model_dir + "/model.pkl")
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
if __name__ == "__main__":
demo.launch()