File size: 6,200 Bytes
d42ef34 ef7cd1b d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 e210869 d42ef34 |
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 |
import gradio as gr
import numpy as np
import joblib
# Define model paths
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'
},
'Speed': {
'3 hours': 'lgbm_3H_speed.pkl',
'15 hours': 'lgbm_15H_speed.pkl',
'27 hours': 'lgbm_27H_speed.pkl'
}
}
# Define scaler paths
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',
'21 hours': 'lr_21H_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'
},
'Speed': {
'3 hours': 'lgbm_speed_scale_3H.pkl',
'15 hours': 'lgbm_speed_scale_15H.pkl',
'27 hours': 'lgbm_speed_scaler_27H.pkl'
}
}
# Define time intervals for each prediction type
time_intervals = {
'Path': ['3 hours', '6 hours', '9 hours', '12 hours', '15 hours', '18 hours', '21 hours', '24 hours', '27 hours', '30 hours', '33 hours', '36 hours'],
'Speed': ['3 hours', '15 hours', '27 hours']
}
def process_input(input_data, scaler, prediction_type):
input_data = np.array(input_data).reshape(-1, 7)
if prediction_type == 'Speed':
# For speed prediction, reshape accordingly
input_data = input_data[:2].reshape(1, 2, 7)
processed_data = input_data.reshape(-1, 14)
else: # Path
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:
# Load the model and scaler based on user selection
model = joblib.load(model_paths[prediction_type][time_interval])
scaler = joblib.load(scaler_paths[prediction_type][time_interval])
# Process input and predict
processed_data = process_input(input_data, scaler, prediction_type)
prediction = model.predict(processed_data)
if prediction_type == 'Path':
return f"Predicted Path after {time_interval}: Latitude: {prediction[0][0]}, Longitude: {prediction[0][1]}"
elif prediction_type == 'Speed':
return f"Predicted Speed after {time_interval}: {prediction[0]}"
except Exception as e:
return str(e)
# Gradio interface components
with gr.Blocks() as cyclone_predictor:
gr.Markdown("# Cyclone Path and Speed Prediction App")
# Dropdown for Prediction Type
prediction_type = gr.Dropdown(
choices=['Path', 'Speed'],
value='Path',
label="Select Prediction Type"
)
# Dropdown for Time Interval
time_interval = gr.Dropdown(
choices=time_intervals['Path'],
label="Select Time Interval"
)
# Function to update time intervals based on prediction type
def update_time_intervals(prediction_type_value):
return gr.update(choices=time_intervals[prediction_type_value])
# Update time intervals when prediction type changes
prediction_type.change(
fn=update_time_intervals,
inputs=prediction_type,
outputs=time_interval
)
# Input fields for user data
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)"
)
# Output prediction
prediction_output = gr.Textbox(label="Prediction Output")
# Predict button
def get_input_data(previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp):
try:
# Parse inputs into required format
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")
# Linking function to UI elements
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
)
cyclone_predictor.launch() |