Abrar20's picture
Update app.py
cb2634e verified
raw
history blame
5.17 kB
import gradio as gr
import numpy as np
import joblib
import folium
from folium import Map, Marker
from io import BytesIO
# 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':
latitude, longitude = prediction[0][0], prediction[0][1]
return f"Predicted Path after {time_interval}: Latitude: {latitude}, Longitude: {longitude}", display_map(latitude, longitude)
except Exception as e:
return str(e)
def display_map(latitude, longitude):
# Create a map centered around the predicted coordinates
m = folium.Map(location=[latitude, longitude], zoom_start=6)
folium.Marker([latitude, longitude], tooltip="Predicted Location").add_to(m)
# Save map as HTML and load in Gradio
map_data = BytesIO()
m.save(map_data, close_file=False)
return map_data
# Gradio interface components
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()