Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
|
|
|
|
|
|
|
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()
|