Spaces:
Runtime error
Runtime error
import joblib | |
import numpy as np | |
import pandas as pd | |
import folium | |
import streamlit as st | |
from streamlit_folium import folium_static | |
import warnings | |
warnings.filterwarnings("ignore") | |
# Define model paths | |
model_paths = { | |
'Path': { | |
'3 hours': 'lr_3H_lat_lon.pkl', | |
'6 hours': 'lr_6H_lat_lon.pkl', | |
'9 hours': 'lr_9H_lat_lon.pkl', | |
'12 hours': 'lr_12H_lat_lon.pkl', | |
'15 hours': 'lr_15H_lat_lon.pkl', | |
'18 hours': 'lr_18H_lat_lon.pkl', | |
'21 hours': 'lr_21H_lat_lon.pkl', | |
'24 hours': 'lr_24H_lat_lon.pkl', | |
'27 hours': 'lr_27H_lat_lon.pkl', | |
'30 hours': 'lr_30H_lat_lon.pkl', | |
'33 hours': 'lr_33H_lat_lon.pkl', | |
'36 hours': 'lr_36H_lat_lon.pkl' | |
} | |
} | |
# Define scaler paths | |
scaler_paths = { | |
'Path': { | |
'3 hours': 'lr_3H_lat_lon_scaler.pkl', | |
'6 hours': 'lr_6H_lat_lon_scaler.pkl', | |
'9 hours': 'lr_9H_lat_lon_scaler.pkl', | |
'12 hours': 'lr_12H_lat_lon_scaler.pkl', | |
'15 hours': 'lr_15H_lat_lon_scaler.pkl', | |
'18 hours': 'lr_18H_lat_lon_scaler.pkl', | |
'24 hours': 'lr_24H_lat_lon_scaler.pkl', | |
'27 hours': 'lr_27H_lat_lon_scaler.pkl', | |
'30 hours': 'lr_30H_lat_lon_scaler.pkl', | |
'33 hours': 'lr_33H_lat_lon_scaler.pkl', | |
'36 hours': 'lr_36H_lat_lon_scaler.pkl' | |
} | |
} | |
# Load model and scaler based on time interval | |
def load_model(time_interval): | |
model = joblib.load(model_paths['Path'][time_interval]) | |
scaler = joblib.load(scaler_paths['Path'][time_interval]) | |
return model, scaler | |
def process_input(input_data, scaler): | |
input_data = np.array(input_data).reshape(-1, 7) | |
processed_data = input_data[:2].reshape(1, -1) | |
processed_data = scaler.transform(processed_data) | |
return processed_data | |
def predict_path(time_interval, input_data): | |
model, scaler = load_model(time_interval) | |
processed_data = process_input(input_data, scaler) | |
prediction = model.predict(processed_data) | |
# Create DataFrame for predictions | |
df_predictions = pd.DataFrame(prediction, columns=['LAT', 'LON']) | |
df_predictions['Time'] = [time_interval] | |
return df_predictions | |
# Function to plot predictions on a folium map and return the HTML representation | |
def plot_predictions_on_map(df_predictions): | |
latitudes = df_predictions['LAT'].tolist() | |
longitudes = df_predictions['LON'].tolist() | |
m = folium.Map(location=[latitudes[0], longitudes[0]], zoom_start=6) | |
locations = list(zip(latitudes, longitudes)) | |
for lat, lon in locations: | |
folium.Marker([lat, lon]).add_to(m) | |
folium.PolyLine(locations, color='blue', weight=2.5, opacity=0.7).add_to(m) | |
return m | |
# Streamlit App | |
def main(): | |
st.title("Cyclone Path Prediction") | |
st.write("Input current and previous cyclone data to predict the path and visualize it on a map.") | |
# User inputs | |
time_interval = st.selectbox("Select Prediction Time Interval", [ | |
'3 hours', '6 hours', '9 hours', '12 hours', '15 hours', '18 hours', | |
'21 hours', '24 hours', '27 hours', '30 hours', '33 hours', '36 hours' | |
]) | |
previous_lat = st.number_input("Previous Latitude", format="%f") | |
previous_lon = st.number_input("Previous Longitude", format="%f") | |
previous_speed = st.number_input("Previous Speed", format="%f") | |
previous_year = st.number_input("Previous Year", format="%d") | |
previous_month = st.number_input("Previous Month", format="%d") | |
previous_day = st.number_input("Previous Day", format="%d") | |
previous_hour = st.number_input("Previous Hour", format="%d") | |
present_lat = st.number_input("Present Latitude", format="%f") | |
present_lon = st.number_input("Present Longitude", format="%f") | |
present_speed = st.number_input("Present Speed", format="%f") | |
present_year = st.number_input("Present Year", format="%d") | |
present_month = st.number_input("Present Month", format="%d") | |
present_day = st.number_input("Present Day", format="%d") | |
present_hour = st.number_input("Present Hour", format="%d") | |
if st.button("Predict"): | |
# Process input into array format | |
previous_data = [previous_lat, previous_lon, previous_speed, previous_year, previous_month, previous_day, previous_hour] | |
present_data = [present_lat, present_lon, present_speed, present_year, present_month, present_day, present_hour] | |
input_data = [previous_data, present_data] | |
# Predict path | |
df_predictions = predict_path(time_interval, input_data) | |
# Display predicted path DataFrame | |
st.write("Predicted Path DataFrame:") | |
st.write(df_predictions) | |
# Plot map with predictions | |
st.write("Cyclone Path Map:") | |
map_ = plot_predictions_on_map(df_predictions) | |
folium_static(map_) | |
if __name__ == "__main__": | |
main() | |