Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -9,6 +9,7 @@ import dateutil.parser as dp
|
|
9 |
import pandas as pd
|
10 |
from huggingface_hub import hf_hub_url, cached_download
|
11 |
import time
|
|
|
12 |
|
13 |
def get_row():
|
14 |
response_tomtom = requests.get(
|
@@ -29,7 +30,7 @@ def get_row():
|
|
29 |
json_response_smhi = json.loads(response_smhi.text)
|
30 |
|
31 |
# weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
|
32 |
-
referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
|
33 |
|
34 |
t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
|
35 |
ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
|
@@ -38,7 +39,7 @@ def get_row():
|
|
38 |
vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
|
39 |
|
40 |
# Use current time
|
41 |
-
referenceTime = time.time()
|
42 |
|
43 |
row ={"referenceTime": referenceTime,
|
44 |
"temperature": t,
|
@@ -54,41 +55,58 @@ def get_row():
|
|
54 |
return row
|
55 |
|
56 |
model = joblib.load(cached_download(
|
57 |
-
hf_hub_url("
|
58 |
))
|
59 |
|
60 |
def infer(input_dataframe):
|
61 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
62 |
|
63 |
title = "Stoclholm Highway E4 Real Time Traffic Prediction"
|
64 |
description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction"
|
65 |
|
66 |
-
inputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"),
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
|
71 |
-
outputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])]
|
72 |
|
73 |
with gr.Blocks() as demo:
|
74 |
with gr.Row():
|
75 |
with gr.Column():
|
76 |
-
gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"),
|
77 |
headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
|
78 |
# datatype=["timestamp", "float", "float", "float", "float", "float"],
|
79 |
label="Input Data", interactive=1)
|
80 |
with gr.Column():
|
81 |
-
gr.Dataframe(row_count = (1, "fixed"), col_count=(
|
82 |
-
|
83 |
-
demo.load(get_row, every=10)
|
84 |
|
85 |
with gr.Row():
|
86 |
btn_sub = gr.Button(value="Submit")
|
|
|
|
|
|
|
|
|
87 |
|
88 |
btn_sub.click(infer, inputs = inputs, outputs = outputs)
|
|
|
89 |
|
90 |
#examples = gr.Examples(fn = infer, examples=[get_row()],inputs=inputs,outputs=outputs ,cache_examples=True)
|
91 |
-
examples = gr.Examples(examples=[get_row()] ,inputs=inputs
|
|
|
|
|
|
|
|
|
92 |
|
93 |
|
94 |
|
@@ -96,4 +114,4 @@ with gr.Blocks() as demo:
|
|
96 |
# interface.launch()
|
97 |
|
98 |
if __name__ == "__main__":
|
99 |
-
demo.queue().launch()
|
|
|
9 |
import pandas as pd
|
10 |
from huggingface_hub import hf_hub_url, cached_download
|
11 |
import time
|
12 |
+
from datetime import datetime
|
13 |
|
14 |
def get_row():
|
15 |
response_tomtom = requests.get(
|
|
|
30 |
json_response_smhi = json.loads(response_smhi.text)
|
31 |
|
32 |
# weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
|
33 |
+
# referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
|
34 |
|
35 |
t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
|
36 |
ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
|
|
|
39 |
vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
|
40 |
|
41 |
# Use current time
|
42 |
+
referenceTime = datetime.fromtimestamp(time.time())
|
43 |
|
44 |
row ={"referenceTime": referenceTime,
|
45 |
"temperature": t,
|
|
|
55 |
return row
|
56 |
|
57 |
model = joblib.load(cached_download(
|
58 |
+
hf_hub_url("Chenzhou/Traffic_Prediction", "traffic_model_adam.pkl")
|
59 |
))
|
60 |
|
61 |
def infer(input_dataframe):
|
62 |
+
serie = input_dataframe["referenceTime"]
|
63 |
+
ts = dp.parse(serie.iloc[0]).timestamp()
|
64 |
+
input_dataframe["referenceTime"] = ts
|
65 |
+
res = pd.DataFrame(model.predict(input_dataframe)).clip(0, 1).iloc[0, 0]
|
66 |
+
if res > 0.8:
|
67 |
+
status = "Smooth Traffic on E4"
|
68 |
+
elif res > 0.5:
|
69 |
+
status = "Slight congestion on E4"
|
70 |
+
else:
|
71 |
+
status = "Total congestion on E4"
|
72 |
+
return pd.DataFrame({'Freeflow Level':[res], 'Status': [status]})
|
73 |
|
74 |
title = "Stoclholm Highway E4 Real Time Traffic Prediction"
|
75 |
description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction"
|
76 |
|
77 |
+
# inputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"),
|
78 |
+
# headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
|
79 |
+
# # datatype=["timestamp", "float", "float", "float", "float", "float"],
|
80 |
+
# label="Input Data", interactive=1)]
|
81 |
|
82 |
+
# outputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])]
|
83 |
|
84 |
with gr.Blocks() as demo:
|
85 |
with gr.Row():
|
86 |
with gr.Column():
|
87 |
+
inputs = gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"),
|
88 |
headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
|
89 |
# datatype=["timestamp", "float", "float", "float", "float", "float"],
|
90 |
label="Input Data", interactive=1)
|
91 |
with gr.Column():
|
92 |
+
outputs = gr.Dataframe(row_count = (1, "fixed"), col_count=(2, "fixed"), label="Predictions", headers=["Freeflow Level", "Status"])
|
|
|
|
|
93 |
|
94 |
with gr.Row():
|
95 |
btn_sub = gr.Button(value="Submit")
|
96 |
+
with gr.Row():
|
97 |
+
btn_ref = gr.Button(value="Get real-time data")
|
98 |
+
|
99 |
+
|
100 |
|
101 |
btn_sub.click(infer, inputs = inputs, outputs = outputs)
|
102 |
+
btn_ref.click(get_row, inputs = None, outputs = inputs)
|
103 |
|
104 |
#examples = gr.Examples(fn = infer, examples=[get_row()],inputs=inputs,outputs=outputs ,cache_examples=True)
|
105 |
+
examples = gr.Examples(fn = infer, examples=[get_row()] ,inputs=inputs, outputs=outputs, cache_examples=False)
|
106 |
+
|
107 |
+
# demo.load(get_row, inputs = None, outputs = [inputs], every=10)
|
108 |
+
demo.load(get_row, inputs = None, outputs = [inputs])
|
109 |
+
|
110 |
|
111 |
|
112 |
|
|
|
114 |
# interface.launch()
|
115 |
|
116 |
if __name__ == "__main__":
|
117 |
+
demo.queue().launch()
|