Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -3,6 +3,9 @@ import gradio as gr
|
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
import datasets
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
title = "Stoclholm Highway E4 Real Time Traffic Prediction"
|
| 8 |
description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction, updated in every hour"
|
|
@@ -17,4 +20,33 @@ model = joblib.load("./traffic_model.pkl")
|
|
| 17 |
def infer(input_dataframe):
|
| 18 |
return pd.DataFrame(model.predict(input_dataframe))
|
| 19 |
|
| 20 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
import joblib
|
| 4 |
import pandas as pd
|
| 5 |
import datasets
|
| 6 |
+
import requests
|
| 7 |
+
import json
|
| 8 |
+
import dateutil.parser as dp
|
| 9 |
|
| 10 |
title = "Stoclholm Highway E4 Real Time Traffic Prediction"
|
| 11 |
description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction, updated in every hour"
|
|
|
|
| 20 |
def infer(input_dataframe):
|
| 21 |
return pd.DataFrame(model.predict(input_dataframe))
|
| 22 |
|
| 23 |
+
response_tomtom = requests.get(
|
| 24 |
+
'https://api.tomtom.com/traffic/services/4/flowSegmentData/absolute/10/json?key=azGiX8jKKGxCxdsF1OzvbbWGPDuInWez&point=59.39575,17.98343')
|
| 25 |
+
json_response_tomtom = json.loads(response_tomtom.text) # get json response
|
| 26 |
+
|
| 27 |
+
currentSpeed = json_response_tomtom["flowSegmentData"]["currentSpeed"]
|
| 28 |
+
freeFlowSpeed = json_response_tomtom["flowSegmentData"]["freeFlowSpeed"]
|
| 29 |
+
congestionLevel = currentSpeed/freeFlowSpeed
|
| 30 |
+
|
| 31 |
+
confidence = json_response_tomtom["flowSegmentData"]["confidence"] # Reliability of the traffic data, by percentage
|
| 32 |
+
|
| 33 |
+
|
| 34 |
+
# Get weather data from SMHI, updated hourly
|
| 35 |
+
|
| 36 |
+
response_smhi = requests.get(
|
| 37 |
+
'https://opendata-download-metanalys.smhi.se/api/category/mesan1g/version/2/geotype/point/lon/17.983/lat/59.3957/data.json')
|
| 38 |
+
json_response_smhi = json.loads(response_smhi.text)
|
| 39 |
+
|
| 40 |
+
# weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
|
| 41 |
+
referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
|
| 42 |
+
|
| 43 |
+
t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
|
| 44 |
+
ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
|
| 45 |
+
prec1h = json_response_smhi["timeSeries"][0]["parameters"][6]["values"][0] # Precipation last hour
|
| 46 |
+
fesn1h = json_response_smhi["timeSeries"][0]["parameters"][8]["values"][0] # Snow precipation last hour
|
| 47 |
+
vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
row = [referenceTime, t, ws, prec1h, fesn1h, vis, confidence, congestionLevel]
|
| 51 |
+
|
| 52 |
+
gr.Interface(fn = infer, inputs = inputs, outputs = outputs, title=title, description=description, examples=[row]).launch()
|