File size: 3,350 Bytes
ef7cd1b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
import numpy as np
import joblib
from sklearn.preprocessing import StandardScaler
import os

# Define model paths
model_paths = {
    'Path': {
        '3 hours': 'path_to_3H_model.pkl',
        '6 hours': 'path_to_6H_model.pkl',
        '9 hours': 'path_to_9H_model.pkl'
    },
    'Speed': {
        '3 hours': 'path_to_3H_speed_model.pkl',
        '6 hours': 'path_to_6H_speed_model.pkl',
        '9 hours': 'path_to_9H_speed_model.pkl'
    },
    'Pressure': {
        '3 hours': 'path_to_3H_pressure_model.pkl',
        '6 hours': 'path_to_6H_pressure_model.pkl',
        '9 hours': 'path_to_9H_pressure_model.pkl'
    }
}

# Define scaler paths
scaler_paths = {
    'Path': {
        '3 hours': 'path_to_3H_scaler.pkl',
        '6 hours': 'path_to_6H_scaler.pkl',
        '9 hours': 'path_to_9H_scaler.pkl'
    },
    'Speed': {
        '3 hours': 'path_to_3H_speed_scaler.pkl',
        '6 hours': 'path_to_6H_speed_scaler.pkl',
        '9 hours': 'path_to_9H_speed_scaler.pkl'
    },
    'Pressure': {
        '3 hours': 'path_to_3H_pressure_scaler.pkl',
        '6 hours': 'path_to_6H_pressure_scaler.pkl',
        '9 hours': 'path_to_9H_pressure_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 Latitude: {prediction[0][0]}, Predicted Longitude: {prediction[0][1]}"
        elif prediction_type == 'Speed':
            return f"Predicted Speed: {prediction[0]}"
        elif prediction_type == 'Pressure':
            return f"Predicted Pressure: {prediction[0]}"
    except Exception as e:
        return str(e)

# Gradio interface components
with gr.Blocks() as cyclone_predictor:
    gr.Markdown("# Cyclone Prediction App")
    
    # Dropdown for Prediction Type
    prediction_type = gr.Dropdown(
        choices=['Path', 'Speed', 'Pressure'],
        label="Select Prediction Type"
    )
    
    # Dropdown for Time Interval
    time_interval = gr.Dropdown(
        choices=['3 hours', '6 hours', '9 hours'],
        label="Select Time Interval"
    )
    
    # Input fields for user data
    input_data = gr.Textbox(
        placeholder="Enter cyclone data as list of lists, e.g., [[15.54,90.64,31,2024,10,23,0], [15.71,90.29,32,2024,10,23,3]]",
        label="Input Cyclone Data (2 rows required)"
    )
    
    # Output prediction
    prediction_output = gr.Textbox(label="Prediction Output")
    
    # Predict button
    predict_button = gr.Button("Predict")
    
    # Linking function to UI elements
    predict_button.click(
        load_model_and_predict,
        inputs=[prediction_type, time_interval, input_data],
        outputs=prediction_output
    )

cyclone_predictor.launch()