Orangefish commited on
Commit
1f9abb9
1 Parent(s): b2042c8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -129
app.py CHANGED
@@ -1,138 +1,124 @@
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")
134
-
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
 
136
 
137
- if __name__ == "__main__":
138
- demo.launch()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from datetime import datetime
2
+ import requests
3
+ import os
 
 
 
 
 
4
  import joblib
5
  import pandas as pd
 
 
 
6
  import json
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7
 
8
 
9
 
10
 
11
+ def get_weather_csv():
12
+ return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki?unitGroup=metric&include=days&key=FYYH5HKD9558HBXD2D6KWXDGH&contentType=csv').csv()
13
+
14
+ def get_weather_json_quick(date):
15
+ return requests.get(f'https://weather.visualcrossing.com/VisualCrossingWebServices/rest/services/timeline/helsinki/today?include=fcst%2Cobs%2Chistfcst%2Cstats%2Cdays&key=J7TT2WGMUNNHD8JBEDXAJJXB2&contentType=json').json()
16
+
17
+ def get_air_quality_data():
18
+ AIR_QUALITY_API_KEY = os.getenv('AIR_QUALITY_API_KEY')
19
+ json = get_air_json(AIR_QUALITY_API_KEY)
20
+ iaqi = json['iaqi']
21
+ forecast = json['forecast']['daily']
22
+ return [
23
+ json['aqi'], # AQI
24
+ json['time']['s'][:10], # Date
25
+ iaqi['h']['v'],
26
+ iaqi['p']['v'],
27
+ iaqi['pm10']['v'],
28
+ iaqi['t']['v'],
29
+ forecast['o3'][0]['avg'],
30
+ forecast['o3'][0]['max'],
31
+ forecast['o3'][0]['min'],
32
+ forecast['pm10'][0]['avg'],
33
+ forecast['pm10'][0]['max'],
34
+ forecast['pm10'][0]['min'],
35
+ forecast['pm25'][0]['avg'],
36
+ forecast['pm25'][0]['max'],
37
+ forecast['pm25'][0]['min'],
38
+ forecast['uvi'][0]['avg'],
39
+ forecast['uvi'][0]['avg'],
40
+ forecast['uvi'][0]['avg']
41
+ ]
42
+
43
+
44
+ def get_weather_data(json):
45
+ #WEATHER_API_KEY = os.getenv('WEATHER_API_KEY')
46
+
47
+ #csv = get_weather_csv()
48
+ data = json['days'][0]
49
+ print("data parsed sccessfully")
50
+ #return [
51
+ # #json['address'].capitalize(),
52
+ # data['datetime'],
53
+ # data['feelslikemax'],
54
+ # data['feelslikemin'],
55
+ # data['feelslike'],
56
+ # data['dew'],
57
+ # data['humidity'],
58
+ # data['precip'],
59
+ # data['precipprob'],
60
+ # data['precipcover'],
61
+ # data['snow'],
62
+ # data['snowdepth'],
63
+ # data['windgust'],
64
+ # data['windspeed'],
65
+ # data['winddir'],
66
+ # data['pressure'],
67
+ # data['cloudcover'],
68
+ # data['visibility'],
69
+ # data['solarradiation'],
70
+ # data['solarenergy'],
71
+ # data['uvindex'],
72
+ # data['conditions']
73
+ #]
74
+ return data
75
+
76
+
77
+ def get_weather_df(data):
78
+ col_names = [
79
+ 'name',
80
+ 'datetime',
81
+ 'tempmax',
82
+ 'tempmin',
83
+ 'temp',
84
+ 'feelslikemax',
85
+ 'feelslikemin',
86
+ 'feelslike',
87
+ 'dew',
88
+ 'humidity',
89
+ 'precip',
90
+ 'precipprob',
91
+ 'precipcover',
92
+ 'snow',
93
+ 'snowdepth',
94
+ 'windgust',
95
+ 'windspeed',
96
+ 'winddir',
97
+ 'sealevelpressure',
98
+ 'cloudcover',
99
+ 'visibility',
100
+ 'solarradiation',
101
+ 'solarenergy',
102
+ 'uvindex',
103
+ 'conditions'
104
+ ]
105
 
106
 
107
+
108
+ new_data = pd.DataFrame(
109
+ data,
110
+ columns=col_names
111
+ )
112
+ new_data.datetime = new_data.datetime.apply(timestamp_2_time1)
113
+ #new_data.rename(columes={'pressure':'sealevelpressure'})
114
+ return new_data
115
+
116
+ def timestamp_2_time1(x):
117
+ dt_obj = datetime.strptime(str(x), '%Y-%m-%d')
118
+ dt_obj = dt_obj.timestamp() * 1000
119
+ return int(dt_obj)
120
+
121
+ def timestamp_2_time(x):
122
+ dt_obj = datetime.strptime(str(x), '%m/%d/%Y')
123
+ dt_obj = dt_obj.timestamp() * 1000
124
+ return int(dt_obj)