Abrar20 commited on
Commit
8ce8068
·
verified ·
1 Parent(s): da21cd4

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +102 -0
app.py ADDED
@@ -0,0 +1,102 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import joblib
2
+ import numpy as np
3
+ import pandas as pd
4
+ import folium
5
+ import gradio as gr
6
+ from folium import Map
7
+
8
+ # Function to load the saved model
9
+ def load_model():
10
+ return joblib.load('cyclone_model.pkl')
11
+
12
+ # Function to predict the next 6 rows based on input
13
+ def predict_next_6_rows(lat_present, lon_present, dist2land_present, storm_speed_present,
14
+ year_present, month_present, day_present, hour_present,
15
+ lat_prev, lon_prev, dist2land_prev, storm_speed_prev):
16
+
17
+ # Construct the current and previous data as feature arrays
18
+ current_data = [lat_present, lon_present, dist2land_present, storm_speed_present, year_present, month_present, day_present, hour_present]
19
+ previous_data = [lat_prev, lon_prev, dist2land_prev, storm_speed_prev, year_present, month_present, day_present, hour_present - 3]
20
+
21
+ # Adjust for negative hours
22
+ if previous_data[-1] < 0:
23
+ previous_data[-1] += 24
24
+ previous_data[6] -= 1 # Adjust day
25
+
26
+ # Preprocess the input into the required shape (2 rows, 8 columns)
27
+ input_data = [previous_data, current_data]
28
+ input_data = np.array(input_data).reshape(1, 2, 8)
29
+
30
+ # Flatten the input to match the model's input shape
31
+ input_data_flat = input_data.reshape(1, -1)
32
+
33
+ # Load the model and make predictions
34
+ loaded_model = load_model()
35
+ predictions = loaded_model.predict(input_data_flat)
36
+
37
+ # Reshape the predictions back to (6, 4) format
38
+ predictions_reshaped = predictions.reshape(6, 4)
39
+
40
+ # Create a DataFrame for the predictions
41
+ columns = ['LAT', 'LON', 'DIST2LAND', 'STORM_SPEED']
42
+ df_predictions = pd.DataFrame(predictions_reshaped, columns=columns)
43
+
44
+ # Add the 'Hour' column, incrementing by 3 hours from the present time
45
+ df_predictions['Hour'] = [(hour_present + (i + 1) * 3) % 24 for i in range(6)] # Ensure the hour wraps around 24
46
+
47
+ # Display the DataFrame
48
+ return df_predictions
49
+
50
+ # Function to plot predictions on a folium map and return the HTML representation
51
+ def plot_predictions_on_map(df_predictions):
52
+ # Extract LAT and LON from the predictions
53
+ latitudes = df_predictions['LAT'].tolist()
54
+ longitudes = df_predictions['LON'].tolist()
55
+
56
+ # Create a folium map centered at the first predicted point
57
+ m = folium.Map(location=[latitudes[0], longitudes[0]], zoom_start=6)
58
+
59
+ # Add the predicted points to the map and connect them with a polyline
60
+ locations = list(zip(latitudes, longitudes))
61
+
62
+ # Add the points to the map
63
+ for lat, lon in locations:
64
+ folium.Marker([lat, lon]).add_to(m)
65
+
66
+ # Add a polyline to connect the points
67
+ folium.PolyLine(locations, color='blue', weight=2.5, opacity=0.7).add_to(m)
68
+
69
+ # Return the map HTML representation
70
+ return m._repr_html_()
71
+
72
+ # Function for the Gradio interface
73
+ def gradio_interface(lat_present, lon_present, dist2land_present, storm_speed_present,
74
+ year_present, month_present, day_present, hour_present,
75
+ lat_prev, lon_prev, dist2land_prev, storm_speed_prev):
76
+
77
+ # Get the DataFrame prediction
78
+ df_predictions = predict_next_6_rows(lat_present, lon_present, dist2land_present, storm_speed_present,
79
+ year_present, month_present, day_present, hour_present,
80
+ lat_prev, lon_prev, dist2land_prev, storm_speed_prev)
81
+
82
+ # Generate the map as an HTML string
83
+ map_html = plot_predictions_on_map(df_predictions)
84
+
85
+ # Return the DataFrame as HTML and the map as an HTML string
86
+ return df_predictions.to_html(), map_html
87
+
88
+ # Gradio Interface Setup
89
+ inputs = [
90
+ gr.Number(label="Current Latitude"), gr.Number(label="Current Longitude"),
91
+ gr.Number(label="Current DIST2LAND"), gr.Number(label="Current STORM_SPEED"),
92
+ gr.Number(label="Current Year"), gr.Number(label="Current Month"),
93
+ gr.Number(label="Current Day"), gr.Number(label="Current Hour"),
94
+ gr.Number(label="Previous Latitude"), gr.Number(label="Previous Longitude"),
95
+ gr.Number(label="Previous DIST2LAND"), gr.Number(label="Previous STORM_SPEED")
96
+ ]
97
+
98
+ outputs = [gr.HTML(label="Predicted DataFrame"), gr.HTML(label="Generated Map")]
99
+
100
+ # Launch the Gradio App
101
+ gr.Interface(fn=gradio_interface, inputs=inputs, outputs=outputs, title="Cyclone Path Prediction",
102
+ description="Input current and previous cyclone data to predict the next 6 rows and visualize the path on a map").launch()