Upload 6 files
Browse files- ai-analytics/__init__.py +0 -0
- ai-analytics/endpoint.py +100 -0
- ai-analytics/prophet_model.py +78 -0
- data-preprocessing/__init__.py +0 -0
- data-preprocessing/endpoint.py +98 -0
- data-preprocessing/generate_traffic_data.py +32 -0
ai-analytics/__init__.py
ADDED
File without changes
|
ai-analytics/endpoint.py
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import sys
|
6 |
+
from datetime import datetime
|
7 |
+
from typing import cast
|
8 |
+
import threading
|
9 |
+
|
10 |
+
import tornado.web
|
11 |
+
import tornado.websocket
|
12 |
+
from tornado import ioloop
|
13 |
+
|
14 |
+
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
15 |
+
from prophet_model import ProphetModel
|
16 |
+
|
17 |
+
ISO_PATTERN = r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'
|
18 |
+
DATASET = os.getenv("DATASET", "data/traffic_data.csv")
|
19 |
+
|
20 |
+
class MainHandler(tornado.web.RequestHandler):
|
21 |
+
"""
|
22 |
+
Handler to render the index page
|
23 |
+
"""
|
24 |
+
def get(self):
|
25 |
+
self.render("index.html")
|
26 |
+
|
27 |
+
class PredictionHandler(tornado.web.RequestHandler):
|
28 |
+
"""
|
29 |
+
Handler to process the request for predicting traffic data
|
30 |
+
"""
|
31 |
+
def initialize(self):
|
32 |
+
"""
|
33 |
+
Initializes and trains a model
|
34 |
+
"""
|
35 |
+
self.model = ProphetModel()
|
36 |
+
self.model.train_model(DATASET)
|
37 |
+
|
38 |
+
def post(self):
|
39 |
+
"""
|
40 |
+
POST method to process the request for traffic data generation
|
41 |
+
"""
|
42 |
+
try:
|
43 |
+
print(self.request.body)
|
44 |
+
run_parameters = json.loads(self.request.body)
|
45 |
+
|
46 |
+
if run_parameters is None:
|
47 |
+
self.write("Invalid parameters: 400")
|
48 |
+
self.set_status(400)
|
49 |
+
return
|
50 |
+
|
51 |
+
predict_time = cast(str, run_parameters.get("predict_time"))
|
52 |
+
|
53 |
+
if re.match(ISO_PATTERN, predict_time):
|
54 |
+
#convert string of format 2024-10-01 08:00:00 to datetime object
|
55 |
+
predict_time = datetime.strptime(predict_time, "%Y-%m-%d %H:%M:%S")
|
56 |
+
else:
|
57 |
+
self.write("Please provide date in the format YYYY-MM-DD HH:mm:ss")
|
58 |
+
self.set_status(400)
|
59 |
+
return
|
60 |
+
|
61 |
+
dataset = self.model.predict(predict_time)
|
62 |
+
self.write(json.dumps(dataset))
|
63 |
+
|
64 |
+
except json.JSONDecodeError:
|
65 |
+
self.write("Invalid JSON payload: 400")
|
66 |
+
self.set_status(400)
|
67 |
+
return
|
68 |
+
except Exception as error_message:
|
69 |
+
self.set_status(500)
|
70 |
+
self.write(f"Error in data generation: {str(error_message)}")
|
71 |
+
return
|
72 |
+
|
73 |
+
def make_app():
|
74 |
+
"""
|
75 |
+
Create the tornado application
|
76 |
+
"""
|
77 |
+
return tornado.web.Application([
|
78 |
+
(r"/", MainHandler),
|
79 |
+
(r"/predict", PredictionHandler),
|
80 |
+
])
|
81 |
+
|
82 |
+
def start():
|
83 |
+
"""
|
84 |
+
Start the server
|
85 |
+
"""
|
86 |
+
def run_server():
|
87 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
88 |
+
try:
|
89 |
+
app = make_app()
|
90 |
+
app.listen(8889)
|
91 |
+
print("Server is running on port 8889")
|
92 |
+
ioloop.IOLoop.current().start()
|
93 |
+
except Exception as error_message:
|
94 |
+
print("An error occurred while starting the server:", error_message)
|
95 |
+
|
96 |
+
server_thread = threading.Thread(target=run_server)
|
97 |
+
server_thread.start()
|
98 |
+
|
99 |
+
if __name__ == "__main__":
|
100 |
+
start()
|
ai-analytics/prophet_model.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from datetime import datetime
|
2 |
+
import pickle
|
3 |
+
import pandas as pd
|
4 |
+
from prophet import Prophet
|
5 |
+
|
6 |
+
class ProphetModel:
|
7 |
+
def __init__(self):
|
8 |
+
self.model = None
|
9 |
+
|
10 |
+
def train_model(self, csv_file):
|
11 |
+
"""
|
12 |
+
Train the Prophet model using the data from the given CSV file.
|
13 |
+
|
14 |
+
:param csv_file: Path to the CSV file containing the dataset.
|
15 |
+
"""
|
16 |
+
# Load the dataset
|
17 |
+
df = pd.read_csv(csv_file)
|
18 |
+
|
19 |
+
# Prepare the data
|
20 |
+
df.rename(columns={'time': 'ds', 'volume': 'y'}, inplace=True)
|
21 |
+
|
22 |
+
# Initialize and fit the Prophet model
|
23 |
+
self.model = Prophet()
|
24 |
+
self.model.fit(df)
|
25 |
+
|
26 |
+
def predict(self, datetime):
|
27 |
+
"""
|
28 |
+
Query the forecast for a given date.
|
29 |
+
|
30 |
+
:param date: The date for which to query the forecast (format: 'YYYY-MM-DD').
|
31 |
+
:return: The forecasted value for the given date.
|
32 |
+
"""
|
33 |
+
if self.model is None:
|
34 |
+
raise Exception("Model has not been trained. Call train_model() first.")
|
35 |
+
|
36 |
+
# Create a DataFrame with the given date
|
37 |
+
future = pd.DataFrame({'ds': [datetime]})
|
38 |
+
|
39 |
+
# Make predictions
|
40 |
+
forecast = self.model.predict(future)
|
41 |
+
|
42 |
+
# Return the forecasted value to json format
|
43 |
+
return forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].to_json(orient='records')
|
44 |
+
|
45 |
+
def save_model(self, model_file):
|
46 |
+
"""
|
47 |
+
Save the trained model to a file.
|
48 |
+
|
49 |
+
:param model_file: Path to the file where the model will be saved.
|
50 |
+
"""
|
51 |
+
#check if trained model exists
|
52 |
+
if self.model is None:
|
53 |
+
raise Exception("Model has not been trained. Call train_model() first.")
|
54 |
+
|
55 |
+
with open(model_file, 'wb') as fout:
|
56 |
+
pickle.dump(self.model, fout)
|
57 |
+
|
58 |
+
def load_model(self, model_file):
|
59 |
+
"""
|
60 |
+
Load a trained model from a file.
|
61 |
+
|
62 |
+
:param model_file: Path to the file containing the saved model.
|
63 |
+
"""
|
64 |
+
with open(model_file, 'rb') as fin:
|
65 |
+
self.model = pickle.load(fin)
|
66 |
+
return self
|
67 |
+
|
68 |
+
# prophet_model = ProphetModel()
|
69 |
+
# prophet_model.train_model('data/traffic_data.csv')
|
70 |
+
# print(type(prophet_model))
|
71 |
+
# predict_time = datetime.strptime('2023-10-01 09:00:00', "%Y-%m-%d %H:%M:%S")
|
72 |
+
# forecast = prophet_model.predict(predict_time)
|
73 |
+
# prophet_model.save_model('saved_models/forecast_model.pkl')
|
74 |
+
# print(forecast)
|
75 |
+
# prophet_model = prophet_model.load_model('saved-models/forecast_model.pkl')
|
76 |
+
# print(type(prophet_model))
|
77 |
+
# forecast = prophet_model.predict(predict_time)
|
78 |
+
# print(forecast)
|
data-preprocessing/__init__.py
ADDED
File without changes
|
data-preprocessing/endpoint.py
ADDED
@@ -0,0 +1,98 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import asyncio
|
2 |
+
import json
|
3 |
+
import os
|
4 |
+
import re
|
5 |
+
import sys
|
6 |
+
from datetime import datetime
|
7 |
+
from typing import cast
|
8 |
+
import threading
|
9 |
+
|
10 |
+
import tornado.web
|
11 |
+
import tornado.websocket
|
12 |
+
from tornado import ioloop
|
13 |
+
|
14 |
+
sys.path.append(os.path.dirname(os.path.realpath(__file__)))
|
15 |
+
from generate_traffic_data import generate_data
|
16 |
+
|
17 |
+
subscriptions = {}
|
18 |
+
PATTERN = r'^\d{4}-\d{2}-\d{2}'
|
19 |
+
ISO_PATTERN = r'^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$'
|
20 |
+
|
21 |
+
class MainHandler(tornado.web.RequestHandler):
|
22 |
+
"""
|
23 |
+
Handler to render the index page
|
24 |
+
"""
|
25 |
+
def get(self):
|
26 |
+
self.render("index.html")
|
27 |
+
|
28 |
+
class DataGeneratorHandler(tornado.web.RequestHandler):
|
29 |
+
"""
|
30 |
+
Handler to process the request for generating traffic data
|
31 |
+
"""
|
32 |
+
def post(self):
|
33 |
+
"""
|
34 |
+
POST method to process the request for traffic data generation
|
35 |
+
"""
|
36 |
+
try:
|
37 |
+
print(self.request.body)
|
38 |
+
run_parameters = json.loads(self.request.body)
|
39 |
+
|
40 |
+
if run_parameters is None:
|
41 |
+
self.write("Invalid parameters: 400")
|
42 |
+
self.set_status(400)
|
43 |
+
return
|
44 |
+
|
45 |
+
start_time = cast(str, run_parameters.get("start_time"))
|
46 |
+
end_time = cast(str, run_parameters.get("end_time"))
|
47 |
+
window_size = cast(str, run_parameters.get("window_size"))
|
48 |
+
|
49 |
+
if re.match(ISO_PATTERN, start_time) and re.match(ISO_PATTERN, end_time):
|
50 |
+
#convert string of format 2024-10-01 08:00:00 to datetime object
|
51 |
+
start_time = datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
|
52 |
+
end_time = datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
|
53 |
+
window_size = int(window_size)
|
54 |
+
else:
|
55 |
+
self.write("Please provide date in the format " + ISO_PATTERN)
|
56 |
+
self.set_status(400)
|
57 |
+
return
|
58 |
+
|
59 |
+
dataset = generate_data(start_time, end_time, window_size)
|
60 |
+
self.write(json.dumps(dataset))
|
61 |
+
|
62 |
+
except json.JSONDecodeError:
|
63 |
+
self.write("Invalid JSON payload: 400")
|
64 |
+
self.set_status(400)
|
65 |
+
return
|
66 |
+
except Exception as error_message:
|
67 |
+
self.set_status(500)
|
68 |
+
self.write(f"Error in data generation: {str(error_message)}")
|
69 |
+
return
|
70 |
+
|
71 |
+
def make_app():
|
72 |
+
"""
|
73 |
+
Create the tornado application
|
74 |
+
"""
|
75 |
+
return tornado.web.Application([
|
76 |
+
(r"/", MainHandler),
|
77 |
+
(r"/traffic", DataGeneratorHandler),
|
78 |
+
])
|
79 |
+
|
80 |
+
def start():
|
81 |
+
"""
|
82 |
+
Start the server
|
83 |
+
"""
|
84 |
+
def run_server():
|
85 |
+
asyncio.set_event_loop(asyncio.new_event_loop())
|
86 |
+
try:
|
87 |
+
app = make_app()
|
88 |
+
app.listen(8889)
|
89 |
+
print("Server is running on port 8889")
|
90 |
+
ioloop.IOLoop.current().start()
|
91 |
+
except Exception as error_message:
|
92 |
+
print("An error occurred while starting the server:", error_message)
|
93 |
+
|
94 |
+
server_thread = threading.Thread(target=run_server)
|
95 |
+
server_thread.start()
|
96 |
+
|
97 |
+
if __name__ == "__main__":
|
98 |
+
start()
|
data-preprocessing/generate_traffic_data.py
ADDED
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import random
|
2 |
+
from datetime import datetime, timedelta
|
3 |
+
|
4 |
+
def generate_data(start_time: datetime, end_time: datetime, window_size: int):
|
5 |
+
"""
|
6 |
+
Generate random traffic volume data for a given time range and window size.
|
7 |
+
"""
|
8 |
+
traffic_data = []
|
9 |
+
current_time = start_time
|
10 |
+
|
11 |
+
while current_time < end_time:
|
12 |
+
# Generate random traffic volume data for the current window
|
13 |
+
traffic_volume = random.randint(0, 100)
|
14 |
+
traffic_data.append({
|
15 |
+
'time': current_time.strftime("%Y-%m-%d %H:%M:%S"),
|
16 |
+
'volume': traffic_volume
|
17 |
+
})
|
18 |
+
current_time += timedelta(minutes=window_size)
|
19 |
+
|
20 |
+
return traffic_data
|
21 |
+
|
22 |
+
if __name__ == "__main__":
|
23 |
+
start_time = datetime(2023, 10, 1, 8, 0, 0) # October 1, 2023, 08:00:00
|
24 |
+
end_time = datetime(2023, 10, 1, 10, 0, 0) # October 1, 2023, 10:00:00
|
25 |
+
window_size = 15 # 15-minute window
|
26 |
+
|
27 |
+
traffic_data = generate_data(start_time, end_time, window_size)
|
28 |
+
|
29 |
+
with open('data/traffic_data.csv', 'w') as f:
|
30 |
+
f.write("time,volume\n")
|
31 |
+
for data in traffic_data:
|
32 |
+
f.write(f"{data['time']},{data['volume']}\n")
|