-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
312 lines (247 loc) · 10.8 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
"""
################
# main.py #
################
This FastAPI application integrates predictive and generative AI models to enhance road safety.
It provides REST API endpoints to:
- Predict accident severity based on live data (e.g., traffic, weather, location).
- Generate contextual safety messages using generative AI.
- Visualize accident-prone areas through dynamic heatmaps.
- Convert safety messages into audio responses using Google Text-to-Speech.
Main features:
- Accident severity prediction using live and pre-existing data.
- Generating and delivering AI-powered safety messages in text and audio formats.
- Route-based accident heatmaps and visualization using folium.
"""
from fastapi import FastAPI, HTTPException
from fastapi.responses import FileResponse, HTMLResponse
from contextlib import asynccontextmanager
import numpy as np
import pandas as pd
import utils
from pydantic import BaseModel
from generative_ai import generate_safety_message
from gtts import gTTS
import folium
import csv
from fastapi import Depends
#import logging
class AccidentData(BaseModel):
Date: str
Time: str
Day_of_Week: str
Junction_Control: str
Junction_Detail: str
Light_Conditions: str
Carriageway_Hazards: str
Road_Surface_Conditions: str
Road_Type: str
Urban_or_Rural_Area: str
Weather_Conditions: str
Police_Force: str
Vehicle_Type: str
Local_Authority: str
Latitude: float
Longitude: float
Number_of_Casualties: int
Number_of_Vehicles: int
Speed_limit: int
class GenerativeInput(BaseModel):
latitude: float
longitude: float
class RouteInput(BaseModel):
start_location: str
end_location: str
@asynccontextmanager
async def lifespan(app: FastAPI):
utils.load_saved_artifacts() # Load models and necessary artifacts
yield
# Create the FastAPI app and metadata
app = FastAPI(
title="Road Safety Enhancement API",
description="""
This API integrates predictive and generative AI models to enhance road safety.
It uses historical accident data, Google Maps APIs, and generative AI models to predict accident severity,
generate safety alerts, and visualize accident-prone areas along specified routes.
""",
version="1.0.0",
contact={
"name": "Raz Yousufi",
"url": "https://www.hope.ac.uk/mathematicsandcomputerscience/",
"email": "[email protected]",
},
lifespan=lifespan
)
# Utility function to process date and time
def process_date_time(features):
day, month, year = utils.process_date(features['Date'])
time_minutes = utils.process_time(features['Time'])
return day, month, year, time_minutes
# Handle numerical and categorical input processing
def prepare_input_data(features):
# Get columns from utils
columns = utils.get_columns()
# Initialize the input array with zeros
input_data = np.zeros(len(columns))
# Process date and time
day, month, year, time_minutes = process_date_time(features)
# Create a dictionary to map column names to their indices
column_indices = {col: idx for idx, col in enumerate(columns)}
# Assign date and time values to the appropriate columns
input_data[column_indices['Day']] = day
input_data[column_indices['Month']] = month
input_data[column_indices['Year']] = year
input_data[column_indices['Time_Minutes']] = time_minutes
# Handle categorical input processing using the utils function
utils.handle_categorical_input(input_data, features)
# Handle numerical input processing using the utils function
utils.handle_numerical_input(input_data, features)
# Wrap input_data in a pandas DataFrame with the correct column names
input_data_df = pd.DataFrame([input_data], columns=columns)
# Log the final input data for debugging
#input_data_df.to_csv('static/input_data_log.csv', index=False)
#print(f"Final Input: {input_data_df}")
return input_data_df
@app.post('/Test_prediction')
async def test_prediction(data: AccidentData):
try:
# Convert the input data (received as JSON) into a dictionary for processing
features = data.model_dump()
# Prepare input data
input_data_df = prepare_input_data(features)
# Now pass the DataFrame to the model for prediction
#prediction = utils.get_model().predict(input_data_df.to_numpy())
prediction = utils.get_model().predict(input_data_df)
result = int(prediction[0])
return {'Accident_Severity': result}
except KeyError as e:
raise HTTPException(status_code=400, detail=f"Missing key in request data: {str(e)}")
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post('/predict-from-coordinates')
async def predict_from_coordinates(latitude: float, longitude: float):
try:
live_data = utils.fetch_live_data(latitude, longitude) # Fetch live data
features = utils.preprocess_live_data(live_data) # Process live data
# Prepare input data
input_data_df = prepare_input_data(features)
# Now pass the DataFrame to the model for prediction
prediction = utils.get_model().predict(input_data_df)
result = int(prediction[0])
return {'Accident_Severity': result}
except KeyError as e:
raise HTTPException(status_code=400, detail=f"Missing key in request data: {str(e)}")
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post('/generative-response')
async def generative_response(latitude: float, longitude: float, severity: dict = Depends(predict_from_coordinates)):
try:
# Fetch live data and preprocess it
live_data = utils.fetch_live_data(latitude, longitude)
features = utils.preprocess_live_data(live_data)
# Fetch traffic data and update features
traffic_data = utils.fetch_here_traffic_data(latitude, longitude)
features.update(traffic_data)
features['current_traffic'] = traffic_data.get('Traffic_Condition', 'Unknown')
# Use predicted severity
features['predicted_severity'] = severity['Accident_Severity']
# Generate safety message
response_message = generate_safety_message(features)
return {"generative_response": response_message}
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post('/generative-response-audio')
async def generative_response_audio(latitude: float, longitude: float, severity: dict = Depends(predict_from_coordinates)):
try:
# Get generative response
generative_response_result = await generative_response(latitude, longitude, severity)
response_message = generative_response_result["generative_response"]
# Convert message to audio
tts = gTTS(response_message, lang='en')
audio_file_path = "safety_message.mp3"
tts.save(audio_file_path)
# Return the audio file as a response
return FileResponse(audio_file_path, media_type='audio/mpeg', filename=audio_file_path)
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.post('/route-accident-heatmap', response_class=HTMLResponse)
async def route_accident_heatmap(route_input: RouteInput):
try:
# Extract input data for route
start_location = route_input.start_location
end_location = route_input.end_location
# Fetch route points and create a map
route_points, polyline = utils.get_route(start_location, end_location)
map_center = route_points[0]
base_map = folium.Map(location=map_center, zoom_start=13)
severity_colors = {0: 'red',
1: 'orange',
2: 'blue'}
folium.PolyLine(route_points, color='green', weight=5).add_to(base_map)
for lat, lon in route_points:
live_data = utils.fetch_live_data(lat, lon)
features = utils.preprocess_live_data(live_data)
# Prepare input data
input_data_df = prepare_input_data(features)
# Make predictions
prediction = utils.get_model().predict(input_data_df)
severity = int(prediction[0])
# Add marker for predicted severity
folium.CircleMarker(
location=(lat, lon),
radius=0.3,
color=severity_colors.get(severity, 'black'),
fill=True,
fill_color=severity_colors.get(severity, 'black'),
fill_opacity=0.99
).add_to(base_map)
heatmap_path = "static/route_heatmap.html"
base_map.save(heatmap_path)
with open(heatmap_path, 'r') as f:
return HTMLResponse(content=f.read())
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
@app.get("/accident-heatmap", response_class=HTMLResponse)
async def accident_heatmap():
try:
coordinates = load_coordinates_from_file() # Load previously saved coordinates
map_center = [51.5074, -0.1278] # London center
base_map = folium.Map(location=map_center, zoom_start=13)
severity_colors = {0: 'red',
1: 'orange',
2: 'blue'}
# Add accident markers to the map
for lat, lon, severity in coordinates:
folium.CircleMarker(
location=(lat, lon),
radius=0.2,
color=severity_colors.get(severity, 'green'),
fill=True,
fill_color=severity_colors.get(severity, 'green'),
fill_opacity=0.99
).add_to(base_map)
heatmap_path = "static/heatmap.html"
base_map.save(heatmap_path)
with open(heatmap_path, 'r') as f:
return HTMLResponse(content=f.read())
except Exception as e:
raise HTTPException(status_code=500, detail=f"Internal server error: {str(e)}")
#def save_coordinates_to_file(latitude, longitude, severity):
# with open('static/coordinates.csv', 'a', newline='') as csvfile:
# writer = csv.writer(csvfile)
# writer.writerow([latitude, longitude, severity])
def load_coordinates_from_file():
coordinates = []
with open('static/coordinates.csv', 'r') as csvfile:
reader = csv.reader(csvfile)
next(reader) # Skip header
for row in reader:
lat = float(row[0])
lon = float(row[1])
severity_code = int(row[2])
coordinates.append([lat, lon, severity_code])
return coordinates