Spaces:
Runtime error
Runtime error
Commit
•
b2042c8
1
Parent(s):
83146ec
Update app.py
Browse files
app.py
CHANGED
@@ -1,59 +1,133 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import gradio as gr
|
2 |
import hopsworks
|
3 |
import joblib
|
4 |
import pandas as pd
|
5 |
import numpy as np
|
6 |
import folium
|
|
|
7 |
import json
|
8 |
import time
|
9 |
from datetime import timedelta, datetime
|
10 |
from branca.element import Figure
|
11 |
|
12 |
-
from functions import
|
13 |
|
14 |
def greet(name):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
|
16 |
-
project = hopsworks.login()
|
17 |
-
mr = project.get_model_registry()
|
18 |
-
#api = project.get_dataset_api()
|
19 |
-
fs = project.get_feature_store()
|
20 |
-
feature_view = fs.get_feature_view(
|
21 |
-
name = 'hel_air_fv1',
|
22 |
-
version = 1
|
23 |
-
)
|
24 |
|
25 |
-
|
26 |
-
# #start_date = datetime.now() - timedelta(days=1)
|
27 |
-
# #start_time = int(start_date.timestamp()) * 1000
|
28 |
|
29 |
|
30 |
-
|
31 |
-
|
32 |
-
# latest_date = time.ctime(int(latest_date_unix))
|
33 |
|
34 |
-
# X = X.drop(columns=["date"]).fillna(0)
|
35 |
|
|
|
36 |
|
37 |
-
model = mr.get_model("gradient_boost_model",version = 4)
|
38 |
-
model_dir = model.download()
|
39 |
|
40 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
-
# # cities = [city_tuple[0] for city_tuple in cities_coords.keys()]
|
43 |
|
44 |
-
#
|
45 |
-
# next_day = next_day_date.strftime ('%d/%m/%Y')
|
46 |
-
# # df = pd.DataFrame(data=preds[0], columns=[f"AQI Predictions for {next_day}"], dtype=int)
|
47 |
-
# str1 = ""
|
48 |
-
# # return int(preds[0])
|
49 |
|
50 |
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
57 |
|
58 |
|
59 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
@@ -61,4 +135,4 @@ demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
|
61 |
|
62 |
|
63 |
if __name__ == "__main__":
|
64 |
-
demo.launch()
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
# In[37]:
|
5 |
+
|
6 |
+
|
7 |
import gradio as gr
|
8 |
import hopsworks
|
9 |
import joblib
|
10 |
import pandas as pd
|
11 |
import numpy as np
|
12 |
import folium
|
13 |
+
import sklearn.preprocessing as proc
|
14 |
import json
|
15 |
import time
|
16 |
from datetime import timedelta, datetime
|
17 |
from branca.element import Figure
|
18 |
|
19 |
+
from functions import get_weather_data, get_weather_df, get_weather_json_quick
|
20 |
|
21 |
def greet(name):
|
22 |
+
X = pd.DataFrame()
|
23 |
+
for i in range(8):
|
24 |
+
# Get, rename column and rescalef
|
25 |
+
next_day_date = datetime.today() + timedelta(days=i)
|
26 |
+
next_day = next_day_date.strftime ('%Y-%m-%d')
|
27 |
+
json = get_weather_json_quick(next_day)
|
28 |
+
temp = get_weather_data(json)
|
29 |
+
X = X.append(temp, ignore_index=True)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
# In[38]:
|
|
|
|
|
33 |
|
34 |
|
35 |
+
X.head()
|
36 |
+
X.columns.values.tolist()
|
|
|
37 |
|
|
|
38 |
|
39 |
+
# In[39]:
|
40 |
|
|
|
|
|
41 |
|
42 |
+
X.drop('preciptype', inplace = True, axis = 1)
|
43 |
+
X.drop('severerisk', inplace = True, axis = 1)
|
44 |
+
X.drop('stations', inplace = True, axis = 1)
|
45 |
+
X.drop('sunrise', inplace = True, axis = 1)
|
46 |
+
X.drop('sunset', inplace = True, axis = 1)
|
47 |
+
X.drop('moonphase', inplace = True, axis = 1)
|
48 |
+
X.drop('description', inplace = True, axis = 1)
|
49 |
+
X.drop('icon', inplace = True, axis = 1)
|
50 |
+
X.drop('datetime', inplace = True, axis = 1)
|
51 |
|
|
|
52 |
|
53 |
+
# In[40]:
|
|
|
|
|
|
|
|
|
54 |
|
55 |
|
56 |
+
X.head()
|
57 |
+
|
58 |
+
|
59 |
+
# In[41]:
|
60 |
+
|
61 |
+
|
62 |
+
X = X.rename(columns={'sunriseEpoch':'pm25'})
|
63 |
+
X = X.rename(columns={'sunsetEpoch':'pm10'})
|
64 |
+
X = X.rename(columns={'source':'o3'})
|
65 |
+
X = X.rename(columns={'normal':'aqi'})
|
66 |
+
X = X.rename(columns={'datetimeEpoch':'city'})
|
67 |
+
|
68 |
+
|
69 |
+
# In[42]:
|
70 |
+
|
71 |
+
|
72 |
+
X.head()
|
73 |
+
|
74 |
+
|
75 |
+
# In[43]:
|
76 |
+
|
77 |
+
|
78 |
+
X = X.drop(columns = ['conditions', "pm25", "pm10", "o3", "aqi"])
|
79 |
+
|
80 |
+
|
81 |
+
|
82 |
+
|
83 |
+
X.insert(0,"pm25",0)
|
84 |
+
X.insert(0,"pm10",0)
|
85 |
+
X.insert(0,"o3",0)
|
86 |
+
X.insert(0,"aqi",0)
|
87 |
+
X.insert(27,"conditions",0)
|
88 |
+
|
89 |
+
|
90 |
+
# In[44]:
|
91 |
+
|
92 |
+
|
93 |
+
X.head()
|
94 |
+
|
95 |
+
|
96 |
+
# In[46]:
|
97 |
+
|
98 |
+
|
99 |
+
project = hopsworks.login()
|
100 |
+
mr = project.get_model_registry()
|
101 |
+
|
102 |
+
|
103 |
+
# In[50]:
|
104 |
+
|
105 |
+
|
106 |
+
model = mr.get_model("gradient_boost_model",version = 4)
|
107 |
+
model_dir = model.download()
|
108 |
+
model = joblib.load(model_dir + "/model.pkl")
|
109 |
+
preds = model.predict(X)
|
110 |
+
|
111 |
+
|
112 |
+
# In[51]:
|
113 |
+
|
114 |
+
|
115 |
+
print(preds)
|
116 |
+
|
117 |
+
|
118 |
+
# In[53]:
|
119 |
+
|
120 |
+
|
121 |
+
str1 = ""
|
122 |
+
for x in range(8):
|
123 |
+
if(x != 0):
|
124 |
+
str1 += (datetime.now() + timedelta(days=x)).strftime('%Y-%m-%d') + " predicted aqi: " + str(int(preds[x]))+"\n"
|
125 |
+
|
126 |
+
print(str1)
|
127 |
+
return str1
|
128 |
+
|
129 |
+
|
130 |
+
# In[ ]:
|
131 |
|
132 |
|
133 |
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
|
|
|
135 |
|
136 |
|
137 |
if __name__ == "__main__":
|
138 |
+
demo.launch()
|