|
import gradio as gr |
|
import numpy as np |
|
import joblib |
|
import folium |
|
from io import BytesIO |
|
import base64 |
|
|
|
|
|
model_paths = { |
|
'Path': { |
|
'3 hours': 'lr_3H_lat_lon.pkl', |
|
'6 hours': 'lr_6H_lat_lon.pkl', |
|
'9 hours': 'lr_9H_lat_lon.pkl', |
|
'12 hours': 'lr_12H_lat_lon.pkl', |
|
'15 hours': 'lr_15H_lat_lon.pkl', |
|
'18 hours': 'lr_18H_lat_lon.pkl', |
|
'21 hours': 'lr_21H_lat_lon.pkl', |
|
'24 hours': 'lr_24H_lat_lon.pkl', |
|
'27 hours': 'lr_27H_lat_lon.pkl', |
|
'30 hours': 'lr_30H_lat_lon.pkl', |
|
'33 hours': 'lr_33H_lat_lon.pkl', |
|
'36 hours': 'lr_36H_lat_lon.pkl' |
|
} |
|
} |
|
|
|
|
|
scaler_paths = { |
|
'Path': { |
|
'3 hours': 'lr_3H_lat_lon_scaler.pkl', |
|
'6 hours': 'lr_6H_lat_lon_scaler.pkl', |
|
'9 hours': 'lr_9H_lat_lon_scaler.pkl', |
|
'12 hours': 'lr_12H_lat_lon_scaler.pkl', |
|
'15 hours': 'lr_15H_lat_lon_scaler.pkl', |
|
'18 hours': 'lr_18H_lat_lon_scaler.pkl', |
|
'24 hours': 'lr_24H_lat_lon_scaler.pkl', |
|
'27 hours': 'lr_27H_lat_lon_scaler.pkl', |
|
'30 hours': 'lr_30H_lat_lon_scaler.pkl', |
|
'33 hours': 'lr_33H_lat_lon_scaler.pkl', |
|
'36 hours': 'lr_36H_lat_lon_scaler.pkl' |
|
} |
|
} |
|
|
|
def process_input(input_data, scaler): |
|
input_data = np.array(input_data).reshape(-1, 7) |
|
processed_data = input_data[:2].reshape(1, -1) |
|
processed_data = scaler.transform(processed_data) |
|
return processed_data |
|
|
|
def load_model_and_predict(prediction_type, time_interval, input_data): |
|
try: |
|
|
|
model = joblib.load(model_paths[prediction_type][time_interval]) |
|
scaler = joblib.load(scaler_paths[prediction_type][time_interval]) |
|
|
|
|
|
processed_data = process_input(input_data, scaler) |
|
prediction = model.predict(processed_data) |
|
|
|
lat, lon = prediction[0][0], prediction[0][1] |
|
|
|
|
|
map_ = folium.Map(location=[lat, lon], zoom_start=6) |
|
folium.Marker([lat, lon], popup=f"Predicted Location ({lat:.2f}, {lon:.2f})").add_to(map_) |
|
|
|
|
|
map_html = BytesIO() |
|
map_.save(map_html) |
|
map_html.seek(0) |
|
map_base64 = base64.b64encode(map_html.getvalue()).decode("utf-8") |
|
|
|
return f"Predicted Path after {time_interval}: Latitude: {lat}, Longitude: {lon}", f'<iframe src="data:text/html;base64,{map_base64}" width="100%" height="400"></iframe>' |
|
except Exception as e: |
|
return str(e), None |
|
|
|
|
|
with gr.Blocks() as cyclone_predictor: |
|
gr.Markdown("# Cyclone Path Prediction App") |
|
|
|
|
|
prediction_type = gr.Dropdown( |
|
choices=['Path'], |
|
value='Path', |
|
label="Select Prediction Type" |
|
) |
|
|
|
|
|
time_interval = gr.Dropdown( |
|
choices=['3 hours', '6 hours', '9 hours', '12 hours', '15 hours', '18 hours', '21 hours', '24 hours', '27 hours', '30 hours', '33 hours', '36 hours'], |
|
label="Select Time Interval" |
|
) |
|
|
|
|
|
previous_lat_lon = gr.Textbox( |
|
placeholder="Enter previous 3-hour lat/lon (e.g., 15.54,90.64)", |
|
label="Previous 3-hour Latitude/Longitude" |
|
) |
|
previous_speed = gr.Number(label="Previous 3-hour Speed") |
|
previous_timestamp = gr.Textbox( |
|
placeholder="Enter previous 3-hour timestamp (e.g., 2024,10,23,0)", |
|
label="Previous 3-hour Timestamp (year, month, day, hour)" |
|
) |
|
|
|
present_lat_lon = gr.Textbox( |
|
placeholder="Enter present 3-hour lat/lon (e.g., 15.71,90.29)", |
|
label="Present 3-hour Latitude/Longitude" |
|
) |
|
present_speed = gr.Number(label="Present 3-hour Speed") |
|
present_timestamp = gr.Textbox( |
|
placeholder="Enter present 3-hour timestamp (e.g., 2024,10,23,3)", |
|
label="Present 3-hour Timestamp (year, month, day, hour)" |
|
) |
|
|
|
|
|
prediction_output = gr.Textbox(label="Prediction Output") |
|
map_output = gr.HTML(label="Predicted Location Map") |
|
|
|
|
|
def get_input_data(previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp): |
|
try: |
|
|
|
prev_lat, prev_lon = map(float, previous_lat_lon.split(',')) |
|
prev_time = list(map(int, previous_timestamp.split(','))) |
|
previous_data = [prev_lat, prev_lon, previous_speed] + prev_time |
|
|
|
present_lat, present_lon = map(float, present_lat_lon.split(',')) |
|
present_time = list(map(int, present_timestamp.split(','))) |
|
present_data = [present_lat, present_lon, present_speed] + present_time |
|
|
|
return [previous_data, present_data] |
|
except Exception as e: |
|
return str(e) |
|
|
|
predict_button = gr.Button("Predict Path") |
|
|
|
|
|
predict_button.click( |
|
fn=lambda pt, ti, p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time: load_model_and_predict( |
|
pt, ti, get_input_data(p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time) |
|
), |
|
inputs=[prediction_type, time_interval, previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp], |
|
outputs=[prediction_output, map_output] |
|
) |
|
|
|
cyclone_predictor.launch() |