File size: 4,963 Bytes
ef7cd1b 950c329 ef7cd1b 950c329 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 0c49580 f017c69 |
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 |
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'
}
}
# 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',
'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:
# 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 = model.predict(processed_data)
if prediction_type == 'Path':
return f"Predicted Path after {time_interval}: Latitude: {prediction[0][0]}, Longitude: {prediction[0][1]}"
except Exception as e:
return str(e)
# Gradio interface components
with gr.Blocks() as cyclone_predictor:
gr.Markdown("# Cyclone Path Prediction App")
# Dropdown for Prediction Type
prediction_type = gr.Dropdown(
choices=['Path'],
value='Path',
label="Select Prediction Type"
)
# Dropdown for Time Interval
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"
)
# 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") # Removed placeholder
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") # Removed placeholder
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 Path")
# 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() |