Abrar20 commited on
Commit
bc16aec
·
verified ·
1 Parent(s): cb2634e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -22
app.py CHANGED
@@ -2,8 +2,8 @@ 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 = {
@@ -56,45 +56,67 @@ def load_model_and_predict(prediction_type, time_interval, input_data):
56
  processed_data = process_input(input_data, scaler)
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,6 +131,7 @@ with gr.Blocks() as cyclone_predictor:
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)
 
2
  import numpy as np
3
  import joblib
4
  import folium
 
5
  from io import BytesIO
6
+ import base64
7
 
8
  # Define model paths
9
  model_paths = {
 
56
  processed_data = process_input(input_data, scaler)
57
  prediction = model.predict(processed_data)
58
 
59
+ lat, lon = prediction[0][0], prediction[0][1]
60
+
61
+ # Create Folium map for predicted location
62
+ map_ = folium.Map(location=[lat, lon], zoom_start=6)
63
+ folium.Marker([lat, lon], popup=f"Predicted Location ({lat:.2f}, {lon:.2f})").add_to(map_)
64
+
65
+ # Save map as HTML and convert to base64
66
+ map_html = BytesIO()
67
+ map_.save(map_html, format="html")
68
+ map_base64 = base64.b64encode(map_html.getvalue()).decode("utf-8")
69
+
70
+ return f"Predicted Path after {time_interval}: Latitude: {lat}, Longitude: {lon}", f'<iframe src="data:text/html;base64,{map_base64}" width="100%" height="400"></iframe>'
71
  except Exception as e:
72
+ return str(e), None
 
 
 
 
 
 
 
 
 
 
73
 
74
  # Gradio interface components
75
  with gr.Blocks() as cyclone_predictor:
76
  gr.Markdown("# Cyclone Path Prediction App")
77
 
78
+ # Dropdown for Prediction Type
79
+ prediction_type = gr.Dropdown(
80
+ choices=['Path'],
81
+ value='Path',
82
+ label="Select Prediction Type"
83
+ )
84
+
85
+ # Dropdown for Time Interval
86
  time_interval = gr.Dropdown(
87
  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'],
88
  label="Select Time Interval"
89
  )
90
 
91
+ # Input fields for user data
92
+ previous_lat_lon = gr.Textbox(
93
+ placeholder="Enter previous 3-hour lat/lon (e.g., 15.54,90.64)",
94
+ label="Previous 3-hour Latitude/Longitude"
95
+ )
96
  previous_speed = gr.Number(label="Previous 3-hour Speed")
97
+ previous_timestamp = gr.Textbox(
98
+ placeholder="Enter previous 3-hour timestamp (e.g., 2024,10,23,0)",
99
+ label="Previous 3-hour Timestamp (year, month, day, hour)"
100
+ )
101
+
102
+ present_lat_lon = gr.Textbox(
103
+ placeholder="Enter present 3-hour lat/lon (e.g., 15.71,90.29)",
104
+ label="Present 3-hour Latitude/Longitude"
105
+ )
106
  present_speed = gr.Number(label="Present 3-hour Speed")
107
+ present_timestamp = gr.Textbox(
108
+ placeholder="Enter present 3-hour timestamp (e.g., 2024,10,23,3)",
109
+ label="Present 3-hour Timestamp (year, month, day, hour)"
110
+ )
111
 
112
+ # Output prediction
113
  prediction_output = gr.Textbox(label="Prediction Output")
114
  map_output = gr.HTML(label="Predicted Location Map")
115
+
116
+ # Predict button
117
  def get_input_data(previous_lat_lon, previous_speed, previous_timestamp, present_lat_lon, present_speed, present_timestamp):
118
  try:
119
+ # Parse inputs into required format
120
  prev_lat, prev_lon = map(float, previous_lat_lon.split(','))
121
  prev_time = list(map(int, previous_timestamp.split(',')))
122
  previous_data = [prev_lat, prev_lon, previous_speed] + prev_time
 
131
 
132
  predict_button = gr.Button("Predict Path")
133
 
134
+ # Linking function to UI elements
135
  predict_button.click(
136
  fn=lambda pt, ti, p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time: load_model_and_predict(
137
  pt, ti, get_input_data(p_lat_lon, p_speed, p_time, c_lat_lon, c_speed, c_time)