Update app.py
Browse files
app.py
CHANGED
@@ -1,6 +1,9 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import joblib
|
|
|
|
|
|
|
4 |
|
5 |
# Define model paths
|
6 |
model_paths = {
|
@@ -54,55 +57,44 @@ def load_model_and_predict(prediction_type, time_interval, input_data):
|
|
54 |
prediction = model.predict(processed_data)
|
55 |
|
56 |
if prediction_type == 'Path':
|
57 |
-
|
|
|
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 |
-
|
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 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
)
|
83 |
-
|
84 |
-
|
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") # Removed placeholder
|
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 |
-
|
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
|
@@ -117,13 +109,12 @@ with gr.Blocks() as cyclone_predictor:
|
|
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 |
|
129 |
-
cyclone_predictor.launch()
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
import joblib
|
4 |
+
import folium
|
5 |
+
from folium import Map, Marker
|
6 |
+
from io import BytesIO
|
7 |
|
8 |
# Define model paths
|
9 |
model_paths = {
|
|
|
57 |
prediction = model.predict(processed_data)
|
58 |
|
59 |
if prediction_type == 'Path':
|
60 |
+
latitude, longitude = prediction[0][0], prediction[0][1]
|
61 |
+
return f"Predicted Path after {time_interval}: Latitude: {latitude}, Longitude: {longitude}", display_map(latitude, longitude)
|
62 |
except Exception as e:
|
63 |
return str(e)
|
64 |
|
65 |
+
def display_map(latitude, longitude):
|
66 |
+
# Create a map centered around the predicted coordinates
|
67 |
+
m = folium.Map(location=[latitude, longitude], zoom_start=6)
|
68 |
+
folium.Marker([latitude, longitude], tooltip="Predicted Location").add_to(m)
|
69 |
+
|
70 |
+
# Save map as HTML and load in Gradio
|
71 |
+
map_data = BytesIO()
|
72 |
+
m.save(map_data, close_file=False)
|
73 |
+
return map_data
|
74 |
+
|
75 |
# Gradio interface components
|
76 |
with gr.Blocks() as cyclone_predictor:
|
77 |
gr.Markdown("# Cyclone Path Prediction App")
|
78 |
|
79 |
+
prediction_type = gr.Dropdown(choices=['Path'], value='Path', label="Select Prediction Type")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
80 |
time_interval = gr.Dropdown(
|
81 |
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'],
|
82 |
label="Select Time Interval"
|
83 |
)
|
84 |
|
85 |
+
previous_lat_lon = gr.Textbox(placeholder="Enter previous 3-hour lat/lon (e.g., 15.54,90.64)", label="Previous 3-hour Latitude/Longitude")
|
86 |
+
previous_speed = gr.Number(label="Previous 3-hour Speed")
|
87 |
+
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)")
|
88 |
+
|
89 |
+
present_lat_lon = gr.Textbox(placeholder="Enter present 3-hour lat/lon (e.g., 15.71,90.29)", label="Present 3-hour Latitude/Longitude")
|
90 |
+
present_speed = gr.Number(label="Present 3-hour Speed")
|
91 |
+
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)")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
92 |
|
|
|
93 |
prediction_output = gr.Textbox(label="Prediction Output")
|
94 |
+
map_output = gr.HTML(label="Predicted Location Map")
|
95 |
+
|
96 |
def get_input_data(previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp):
|
97 |
try:
|
|
|
98 |
prev_lat, prev_lon = map(float, previous_lat_lon.split(','))
|
99 |
prev_time = list(map(int, previous_timestamp.split(',')))
|
100 |
previous_data = [prev_lat, prev_lon, previous_speed] + prev_time
|
|
|
109 |
|
110 |
predict_button = gr.Button("Predict Path")
|
111 |
|
|
|
112 |
predict_button.click(
|
113 |
fn=lambda pt, ti, p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time: load_model_and_predict(
|
114 |
pt, ti, get_input_data(p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time)
|
115 |
),
|
116 |
inputs=[prediction_type, time_interval, previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp],
|
117 |
+
outputs=[prediction_output, map_output]
|
118 |
)
|
119 |
|
120 |
+
cyclone_predictor.launch()
|