Chenzhou commited on
Commit
c4ec6df
1 Parent(s): af6c04e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +96 -30
app.py CHANGED
@@ -1,3 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import sklearn
2
  import gradio as gr
3
  import joblib
@@ -9,38 +45,67 @@ import dateutil.parser as dp
9
  import pandas as pd
10
  from huggingface_hub import hf_hub_url, cached_download
11
  import time
12
- import datetime
13
-
14
- title = "Stockholm Highway E4 Real Time Traffic Prediction"
15
- description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction, updated in every hour"
16
-
17
- inputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"), label="Input Data", interactive=1)]
18
-
19
- outputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])]
20
-
21
- model = joblib.load("./traffic_model.pkl")
22
 
23
- response_smhi = requests.get(
 
 
 
 
 
 
 
 
 
 
 
 
 
 
24
  'https://opendata-download-metanalys.smhi.se/api/category/mesan1g/version/2/geotype/point/lon/17.983/lat/59.3957/data.json')
25
- json_response_smhi = json.loads(response_smhi.text)
 
 
 
 
 
 
 
 
 
26
 
27
- def infer(input_dataframe):
28
- return pd.DataFrame(model.predict(input_dataframe))
 
 
 
 
 
 
 
 
29
 
30
- referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
 
 
 
31
 
 
 
 
32
 
33
- def get_time():
34
- return datetime.datetime.now()
 
 
 
35
 
36
- #with gr.Blocks() as demo:
37
- # with gr.Row():
38
- # with gr.Column():
39
- # c_time2 = gr.Textbox(label="Current Time refreshed every second")
40
 
41
- # demo.load(lambda: datetime.datetime.now(), None, c_time2, every=1)
42
 
43
-
44
  with gr.Blocks() as demo:
45
  with gr.Row():
46
  with gr.Column():
@@ -48,18 +113,19 @@ with gr.Blocks() as demo:
48
  headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
49
  # datatype=["timestamp", "float", "float", "float", "float", "float"],
50
  label="Input Data", interactive=1)
51
- with gr.Column():
52
- c_time2 = gr.Textbox(label="Current Time refreshed every second")
53
-
54
-
55
  with gr.Column():
56
  gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])
57
-
58
- demo.load(lambda: datetime.datetime.now(), None, c_time2, every=1)
59
 
60
  with gr.Row():
61
  btn_sub = gr.Button(value="Submit")
62
 
63
  btn_sub.click(infer, inputs = inputs, outputs = outputs)
 
 
 
 
 
64
 
65
- demo.queue().launch()
 
 
1
+ Hugging Face's logo
2
+ Hugging Face
3
+ Search models, datasets, users...
4
+ Models
5
+ Datasets
6
+ Spaces
7
+ Docs
8
+ Solutions
9
+ Pricing
10
+
11
+
12
+
13
+ Spaces:
14
+
15
+ tilos
16
+ /
17
+ Real_Time_Traffic_Prediction Copied
18
+ like
19
+ 0
20
+ App
21
+ Files and versions
22
+ Community
23
+ Real_Time_Traffic_Prediction
24
+ /
25
+ app.py
26
+ tilos's picture
27
+ tilos
28
+ Update app.py
29
+ e6f6142
30
+ about 14 hours ago
31
+ raw
32
+ history
33
+ blame
34
+ contribute
35
+ delete
36
+ 4.17 kB
37
  import sklearn
38
  import gradio as gr
39
  import joblib
 
45
  import pandas as pd
46
  from huggingface_hub import hf_hub_url, cached_download
47
  import time
 
 
 
 
 
 
 
 
 
 
48
 
49
+ def get_row():
50
+ response_tomtom = requests.get(
51
+ 'https://api.tomtom.com/traffic/services/4/flowSegmentData/absolute/10/json?key=azGiX8jKKGxCxdsF1OzvbbWGPDuInWez&point=59.39575,17.98343')
52
+ json_response_tomtom = json.loads(response_tomtom.text) # get json response
53
+
54
+ currentSpeed = json_response_tomtom["flowSegmentData"]["currentSpeed"]
55
+ freeFlowSpeed = json_response_tomtom["flowSegmentData"]["freeFlowSpeed"]
56
+ congestionLevel = currentSpeed/freeFlowSpeed
57
+
58
+ confidence = json_response_tomtom["flowSegmentData"]["confidence"] # Reliability of the traffic data, by percentage
59
+
60
+
61
+ # Get weather data from SMHI, updated hourly
62
+
63
+ response_smhi = requests.get(
64
  'https://opendata-download-metanalys.smhi.se/api/category/mesan1g/version/2/geotype/point/lon/17.983/lat/59.3957/data.json')
65
+ json_response_smhi = json.loads(response_smhi.text)
66
+
67
+ # weather data manual https://opendata.smhi.se/apidocs/metanalys/parameters.html#parameter-wsymb
68
+ referenceTime = dp.parse(json_response_smhi["referenceTime"]).timestamp()
69
+
70
+ t = json_response_smhi["timeSeries"][0]["parameters"][0]["values"][0] # Temperature
71
+ ws = json_response_smhi["timeSeries"][0]["parameters"][4]["values"][0] # Wind Speed
72
+ prec1h = json_response_smhi["timeSeries"][0]["parameters"][6]["values"][0] # Precipation last hour
73
+ fesn1h = json_response_smhi["timeSeries"][0]["parameters"][8]["values"][0] # Snow precipation last hour
74
+ vis = json_response_smhi["timeSeries"][0]["parameters"][9]["values"][0] # Visibility
75
 
76
+ # Use current time
77
+ referenceTime = time.time()
78
+
79
+ row ={"referenceTime": referenceTime,
80
+ "temperature": t,
81
+ "wind speed": ws,
82
+ "precipation last hour": prec1h,
83
+ "snow precipation last hour": fesn1h,
84
+ "visibility": vis,
85
+ "confidence of data": confidence}
86
 
87
+ row = pd.DataFrame([row], columns=row.keys())
88
+ print(row)
89
+ row.dropna(axis=0, inplace=True)
90
+ return row
91
 
92
+ model = joblib.load(cached_download(
93
+ hf_hub_url("tilos/Traffic_Prediction", "traffic_model.pkl")
94
+ ))
95
 
96
+ def infer(input_dataframe):
97
+ return pd.DataFrame(model.predict(input_dataframe)).clip(0, 1)
98
+
99
+ title = "Stoclholm Highway E4 Real Time Traffic Prediction"
100
+ description = "Stockholm E4 (59°23'44.7"" N 17°59'00.4""E) highway real time traffic prediction"
101
 
102
+ inputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(7,"fixed"),
103
+ headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
104
+ # datatype=["timestamp", "float", "float", "float", "float", "float"],
105
+ label="Input Data", interactive=1)]
106
 
107
+ outputs = [gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])]
108
 
 
109
  with gr.Blocks() as demo:
110
  with gr.Row():
111
  with gr.Column():
 
113
  headers=["referenceTime", "t", "ws", "prec1h", "fesn1h", "vis", "confidence"],
114
  # datatype=["timestamp", "float", "float", "float", "float", "float"],
115
  label="Input Data", interactive=1)
 
 
 
 
116
  with gr.Column():
117
  gr.Dataframe(row_count = (1, "fixed"), col_count=(1, "fixed"), label="Predictions", headers=["Congestion Level"])
118
+ demo.load(get_row, every=10)
 
119
 
120
  with gr.Row():
121
  btn_sub = gr.Button(value="Submit")
122
 
123
  btn_sub.click(infer, inputs = inputs, outputs = outputs)
124
+
125
+
126
+
127
+ # interface = gr.Interface(fn = infer, inputs = inputs, outputs = outputs, title=title, description=description, examples=[get_row()], cache_examples=False)
128
+ # interface.launch()
129
 
130
+ if __name__ == "__main__":
131
+ demo.queue().launch()