Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import joblib
|
4 |
+
from sklearn.preprocessing import StandardScaler
|
5 |
+
import os
|
6 |
+
|
7 |
+
# Define model paths
|
8 |
+
model_paths = {
|
9 |
+
'Path': {
|
10 |
+
'3 hours': 'path_to_3H_model.pkl',
|
11 |
+
'6 hours': 'path_to_6H_model.pkl',
|
12 |
+
'9 hours': 'path_to_9H_model.pkl'
|
13 |
+
},
|
14 |
+
'Speed': {
|
15 |
+
'3 hours': 'path_to_3H_speed_model.pkl',
|
16 |
+
'6 hours': 'path_to_6H_speed_model.pkl',
|
17 |
+
'9 hours': 'path_to_9H_speed_model.pkl'
|
18 |
+
},
|
19 |
+
'Pressure': {
|
20 |
+
'3 hours': 'path_to_3H_pressure_model.pkl',
|
21 |
+
'6 hours': 'path_to_6H_pressure_model.pkl',
|
22 |
+
'9 hours': 'path_to_9H_pressure_model.pkl'
|
23 |
+
}
|
24 |
+
}
|
25 |
+
|
26 |
+
# Define scaler paths
|
27 |
+
scaler_paths = {
|
28 |
+
'Path': {
|
29 |
+
'3 hours': 'path_to_3H_scaler.pkl',
|
30 |
+
'6 hours': 'path_to_6H_scaler.pkl',
|
31 |
+
'9 hours': 'path_to_9H_scaler.pkl'
|
32 |
+
},
|
33 |
+
'Speed': {
|
34 |
+
'3 hours': 'path_to_3H_speed_scaler.pkl',
|
35 |
+
'6 hours': 'path_to_6H_speed_scaler.pkl',
|
36 |
+
'9 hours': 'path_to_9H_speed_scaler.pkl'
|
37 |
+
},
|
38 |
+
'Pressure': {
|
39 |
+
'3 hours': 'path_to_3H_pressure_scaler.pkl',
|
40 |
+
'6 hours': 'path_to_6H_pressure_scaler.pkl',
|
41 |
+
'9 hours': 'path_to_9H_pressure_scaler.pkl'
|
42 |
+
}
|
43 |
+
}
|
44 |
+
|
45 |
+
def process_input(input_data, scaler):
|
46 |
+
input_data = np.array(input_data).reshape(-1, 7)
|
47 |
+
processed_data = input_data[:2].reshape(1, -1)
|
48 |
+
processed_data = scaler.transform(processed_data)
|
49 |
+
return processed_data
|
50 |
+
|
51 |
+
def load_model_and_predict(prediction_type, time_interval, input_data):
|
52 |
+
try:
|
53 |
+
# Load the model and scaler based on user selection
|
54 |
+
model = joblib.load(model_paths[prediction_type][time_interval])
|
55 |
+
scaler = joblib.load(scaler_paths[prediction_type][time_interval])
|
56 |
+
|
57 |
+
# Process input and predict
|
58 |
+
processed_data = process_input(input_data, scaler)
|
59 |
+
prediction = model.predict(processed_data)
|
60 |
+
|
61 |
+
if prediction_type == 'Path':
|
62 |
+
return f"Predicted Latitude: {prediction[0][0]}, Predicted Longitude: {prediction[0][1]}"
|
63 |
+
elif prediction_type == 'Speed':
|
64 |
+
return f"Predicted Speed: {prediction[0]}"
|
65 |
+
elif prediction_type == 'Pressure':
|
66 |
+
return f"Predicted Pressure: {prediction[0]}"
|
67 |
+
except Exception as e:
|
68 |
+
return str(e)
|
69 |
+
|
70 |
+
# Gradio interface components
|
71 |
+
with gr.Blocks() as cyclone_predictor:
|
72 |
+
gr.Markdown("# Cyclone Prediction App")
|
73 |
+
|
74 |
+
# Dropdown for Prediction Type
|
75 |
+
prediction_type = gr.Dropdown(
|
76 |
+
choices=['Path', 'Speed', 'Pressure'],
|
77 |
+
label="Select Prediction Type"
|
78 |
+
)
|
79 |
+
|
80 |
+
# Dropdown for Time Interval
|
81 |
+
time_interval = gr.Dropdown(
|
82 |
+
choices=['3 hours', '6 hours', '9 hours'],
|
83 |
+
label="Select Time Interval"
|
84 |
+
)
|
85 |
+
|
86 |
+
# Input fields for user data
|
87 |
+
input_data = gr.Textbox(
|
88 |
+
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]]",
|
89 |
+
label="Input Cyclone Data (2 rows required)"
|
90 |
+
)
|
91 |
+
|
92 |
+
# Output prediction
|
93 |
+
prediction_output = gr.Textbox(label="Prediction Output")
|
94 |
+
|
95 |
+
# Predict button
|
96 |
+
predict_button = gr.Button("Predict")
|
97 |
+
|
98 |
+
# Linking function to UI elements
|
99 |
+
predict_button.click(
|
100 |
+
load_model_and_predict,
|
101 |
+
inputs=[prediction_type, time_interval, input_data],
|
102 |
+
outputs=prediction_output
|
103 |
+
)
|
104 |
+
|
105 |
+
cyclone_predictor.launch()
|