tilos commited on
Commit
a8a34f1
·
1 Parent(s): af5363f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -39
app.py CHANGED
@@ -24,42 +24,45 @@ model = joblib.load("./traffic_model.pkl")
24
  def infer(input_dataframe):
25
  return pd.DataFrame(model.predict(input_dataframe))
26
 
27
- response_tomtom = requests.get(
28
- 'https://api.tomtom.com/traffic/services/4/flowSegmentData/absolute/10/json?key=azGiX8jKKGxCxdsF1OzvbbWGPDuInWez&point=59.39575,17.98343')
29
- json_response_tomtom = json.loads(response_tomtom.text) # get json response
30
-
31
- currentSpeed = json_response_tomtom["flowSegmentData"]["currentSpeed"]
32
- freeFlowSpeed = json_response_tomtom["flowSegmentData"]["freeFlowSpeed"]
33
- congestionLevel = currentSpeed/freeFlowSpeed
34
-
35
- confidence = json_response_tomtom["flowSegmentData"]["confidence"] # Reliability of the traffic data, by percentage
36
-
37
-
38
- # Get weather data from SMHI, updated hourly
39
-
40
- response_smhi = requests.get(
41
- 'https://opendata-download-metanalys.smhi.se/api/category/mesan1g/version/2/geotype/point/lon/17.983/lat/59.3957/data.json')
42
- json_response_smhi = json.loads(response_smhi.text)
43
-
44
- # weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
45
- referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
46
-
47
- t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
48
- ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
49
- prec1h = json_response_smhi["timeSeries"][0]["parameters"][6]["values"][0] # Precipation last hour
50
- fesn1h = json_response_smhi["timeSeries"][0]["parameters"][8]["values"][0] # Snow precipation last hour
51
- vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
52
-
53
-
54
- row ={"referenceTime": referenceTime,
55
- "t": t,
56
- "ws": ws,
57
- "prec1h": prec1h,
58
- "fesn1h": fesn1h,
59
- "vis": vis,
60
- "confidence": confidence}
61
-
62
- row = pd.DataFrame([row], columns=row.keys())
63
- print(row)
64
-
65
- gr.Interface(fn = infer, inputs = inputs, outputs = outputs, title=title, description=description, examples=[row]).launch()
 
 
 
 
24
  def infer(input_dataframe):
25
  return pd.DataFrame(model.predict(input_dataframe))
26
 
27
+ def get_row():
28
+ response_tomtom = requests.get(
29
+ 'https://api.tomtom.com/traffic/services/4/flowSegmentData/absolute/10/json?key=azGiX8jKKGxCxdsF1OzvbbWGPDuInWez&point=59.39575,17.98343')
30
+ json_response_tomtom = json.loads(response_tomtom.text) # get json response
31
+
32
+ currentSpeed = json_response_tomtom["flowSegmentData"]["currentSpeed"]
33
+ freeFlowSpeed = json_response_tomtom["flowSegmentData"]["freeFlowSpeed"]
34
+ congestionLevel = currentSpeed/freeFlowSpeed
35
+
36
+ confidence = json_response_tomtom["flowSegmentData"]["confidence"] # Reliability of the traffic data, by percentage
37
+
38
+
39
+ # Get weather data from SMHI, updated hourly
40
+
41
+ response_smhi = requests.get(
42
+ 'https://opendata-download-metanalys.smhi.se/api/category/mesan1g/version/2/geotype/point/lon/17.983/lat/59.3957/data.json')
43
+ json_response_smhi = json.loads(response_smhi.text)
44
+
45
+ # weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
46
+ referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
47
+
48
+ t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
49
+ ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
50
+ prec1h = json_response_smhi["timeSeries"][0]["parameters"][6]["values"][0] # Precipation last hour
51
+ fesn1h = json_response_smhi["timeSeries"][0]["parameters"][8]["values"][0] # Snow precipation last hour
52
+ vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
53
+
54
+
55
+ row ={"referenceTime": referenceTime,
56
+ "t": t,
57
+ "ws": ws,
58
+ "prec1h": prec1h,
59
+ "fesn1h": fesn1h,
60
+ "vis": vis,
61
+ "confidence": confidence}
62
+
63
+ row = pd.DataFrame([row], columns=row.keys())
64
+ print(row)
65
+
66
+ return row
67
+
68
+ gr.Interface(fn = infer, inputs = inputs, outputs = outputs, title=title, description=description, examples=[get_row()]).launch()