Abrar20 commited on
Commit
950c329
·
verified ·
1 Parent(s): e6c2e40

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +67 -43
app.py CHANGED
@@ -1,44 +1,39 @@
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
 
@@ -59,46 +54,75 @@ def load_model_and_predict(prediction_type, time_interval, input_data):
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
 
 
1
  import gradio as gr
2
  import numpy as np
3
  import joblib
 
 
4
 
5
  # Define model paths
6
  model_paths = {
7
  'Path': {
8
+ '3 hours': 'lr_3H_lat_lon.pkl',
9
+ '6 hours': 'lr_6H_lat_lon.pkl',
10
+ '9 hours': 'lr_9H_lat_lon.pkl',
11
+ '12 hours': 'lr_12H_lat_lon.pkl',
12
+ '15 hours': 'lr_15H_lat_lon.pkl',
13
+ '18 hours': 'lr_18H_lat_lon.pkl',
14
+ '21 hours': 'lr_21H_lat_lon.pkl',
15
+ '24 hours': 'lr_24H_lat_lon.pkl',
16
+ '27 hours': 'lr_27H_lat_lon.pkl',
17
+ '30 hours': 'lr_30H_lat_lon.pkl',
18
+ '33 hours': 'lr_33H_lat_lon.pkl',
19
+ '36 hours': 'lr_36H_lat_lon.pkl'
 
20
  }
21
  }
22
 
23
  # Define scaler paths
24
  scaler_paths = {
25
  'Path': {
26
+ '3 hours': 'lr_3H_lat_lon_scaler.pkl',
27
+ '6 hours': 'lr_6H_lat_lon_scaler.pkl',
28
+ '9 hours': 'lr_9H_lat_lon_scaler.pkl',
29
+ '12 hours': 'lr_12H_lat_lon_scaler.pkl',
30
+ '15 hours': 'lr_15H_lat_lon_scaler.pkl',
31
+ '18 hours': 'lr_18H_lat_lon_scaler.pkl',
32
+ '24 hours': 'lr_24H_lat_lon_scaler.pkl',
33
+ '27 hours': 'lr_27H_lat_lon_scaler.pkl',
34
+ '30 hours': 'lr_30H_lat_lon_scaler.pkl',
35
+ '33 hours': 'lr_33H_lat_lon_scaler.pkl',
36
+ '36 hours': 'lr_36H_lat_lon_scaler.pkl'
 
 
37
  }
38
  }
39
 
 
54
  prediction = model.predict(processed_data)
55
 
56
  if prediction_type == 'Path':
57
+ return f"Predicted Path after {time_interval}: Latitude: {prediction[0][0]}, Longitude: {prediction[0][1]}"
 
 
 
 
58
  except Exception as e:
59
  return str(e)
60
 
61
  # Gradio interface components
62
  with gr.Blocks() as cyclone_predictor:
63
+ gr.Markdown("# Cyclone Path Prediction App")
64
 
65
  # Dropdown for Prediction Type
66
  prediction_type = gr.Dropdown(
67
+ choices=['Path'],
68
+ value='Path',
69
  label="Select Prediction Type"
70
  )
71
 
72
  # Dropdown for Time Interval
73
  time_interval = gr.Dropdown(
74
+ 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'],
75
  label="Select Time Interval"
76
  )
77
 
78
  # Input fields for user data
79
+ previous_lat_lon = gr.Textbox(
80
+ placeholder="Enter previous 3-hour lat/lon (e.g., 15.54,90.64)",
81
+ label="Previous 3-hour Latitude/Longitude"
82
  )
83
+ previous_speed = gr.Number(label="Previous 3-hour Speed", placeholder="e.g., 31")
84
+ previous_timestamp = gr.Textbox(
85
+ placeholder="Enter previous 3-hour timestamp (e.g., 2024,10,23,0)",
86
+ label="Previous 3-hour Timestamp (year, month, day, hour)"
87
+ )
88
+
89
+ present_lat_lon = gr.Textbox(
90
+ placeholder="Enter present 3-hour lat/lon (e.g., 15.71,90.29)",
91
+ label="Present 3-hour Latitude/Longitude"
92
+ )
93
+ present_speed = gr.Number(label="Present 3-hour Speed", placeholder="e.g., 32")
94
+ present_timestamp = gr.Textbox(
95
+ placeholder="Enter present 3-hour timestamp (e.g., 2024,10,23,3)",
96
+ label="Present 3-hour Timestamp (year, month, day, hour)"
97
+ )
98
+
99
  # Output prediction
100
  prediction_output = gr.Textbox(label="Prediction Output")
101
 
102
  # Predict button
103
+ def get_input_data(previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp):
104
+ try:
105
+ # Parse inputs into required format
106
+ prev_lat, prev_lon = map(float, previous_lat_lon.split(','))
107
+ prev_time = list(map(int, previous_timestamp.split(',')))
108
+ previous_data = [prev_lat, prev_lon, previous_speed] + prev_time
109
+
110
+ present_lat, present_lon = map(float, present_lat_lon.split(','))
111
+ present_time = list(map(int, present_timestamp.split(',')))
112
+ present_data = [present_lat, present_lon, present_speed] + present_time
113
+
114
+ return [previous_data, present_data]
115
+ except Exception as e:
116
+ return str(e)
117
+
118
+ predict_button = gr.Button("Predict Path")
119
 
120
  # Linking function to UI elements
121
  predict_button.click(
122
+ fn=lambda pt, ti, p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time: load_model_and_predict(
123
+ pt, ti, get_input_data(p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time)
124
+ ),
125
+ inputs=[prediction_type, time_interval, previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp],
126
  outputs=prediction_output
127
  )
128