EarthQuakeMap / app.py
Q-bert's picture
Update app.py
8c322e5
raw
history blame
No virus
4.59 kB
import pickle
import gradio as gr
import pandas as pd
import geopandas as gpd
from shapely.geometry import Point
import matplotlib.pyplot as plt
import numpy as np
with open("earthquake_model.pkl", 'rb') as file:
model = pickle.load(file)
def time2num(x):
try:
(h, m, s) = str(x).split(':')
result = int(h) * 3600 + int(m) * 60 + int(s)
return result
except:
return 0
def date2num(x):
try:
(m, d, y) = str(x).split("/")
result = int(y) * 365 + int(m) * 30 + int(d)
return result
except:
return 0
def datetime2num(date_str, time_str):
date_value = date2num(date_str)
time_value = time2num(time_str)
return date_value ,time_value
def test(model, date_str, time_str,all_points):
data_list = []
for lat, lon in all_points:
date, time = datetime2num(date_str, time_str)
data_list.append([date, time, lat, lon])
np_array = np.array(data_list)
res = model.predict_proba(np_array)
return res
def create_geodf(all_points, model, date_str, time_str):
res = test(model, date_str, time_str,all_points)
data_list = []
for lat, lon in all_points:
date, time = datetime2num(date_str, time_str)
data_list.append([date, time, lat, lon])
np_array = np.array(data_list)
df = pd.DataFrame(np_array, columns=['Date', 'Time', 'Latitude', 'Longitude'])
df['Probability_2'] = [i[1] for i in res]
df['geometry'] = [Point(lon, lat) for lat, lon in zip(df['Latitude'], df['Longitude'])]
crs = "EPSG:4326"
gdf = gpd.GeoDataFrame(df, crs=crs, geometry='geometry')
return gdf
def plot_func(date_str, time_str):
min_latitude = -90
max_latitude = 90
latitude_step = 1
min_longitude = -180
max_longitude = 180
longitude_step = 1
latitudes = np.arange(min_latitude, max_latitude + latitude_step, latitude_step)
longitudes = np.arange(min_longitude, max_longitude + longitude_step, longitude_step)
all_points = np.array(np.meshgrid(latitudes, longitudes)).T.reshape(-1, 2)
gdf = create_geodf(all_points, model, date_str, time_str)
top = gdf.nlargest(100, 'Probability_2')
world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(np.ones((180, 360)), cmap='gray', extent=[-180, 180, -90, 90])
world.plot(ax=ax, color='lightgray', edgecolor='black')
top.plot(ax=ax, markersize=50, color='red', legend=True, alpha=0.5)
plt.xlabel('Longitude')
plt.ylabel('Latitude')
plt.title('Possible Earthquake Map')
plt.grid(True)
return plt.gcf()
inputs = [gr.inputs.Textbox(label="Date: (MM/DD/YYYY)"), gr.inputs.Textbox(label="Time: (HH:MM:SS) GMT-4")]
title = "Earthquake Probability Prediction by Q-bert"
description = f"""
# Earthquake Probability Prediction Interface
This interface is based on a Gradient Boosting model trained with a large dataset of significant earthquakes. The model aims to predict earthquake probabilities in different geographical regions worldwide.
## How to Use?
Please enter the date and time information in the text boxes below (date format: MM/DD/YYYY and time format: HH:MM:SS GMT-4). The model will visualize the possible earthquake probabilities on the world map for the specified date and time.
## About the Model
This model has been trained on a substantial dataset and utilizes the Gradient Boosting algorithm. The dataset comprises historical earthquake events along with corresponding geographical information. The model is employed to estimate earthquake probabilities in various regions at the specified date and time.
Please note that these predictions are probabilistic and the precise timing and locations of actual earthquake events are challenging to forecast accurately. Therefore, always consult with authorities for earthquake preparedness and precautionary measures.
## Disclaimer
The results presented in this interface are for entertainment purposes only and should not be used for real-time earthquake event predictions.
In this text, users are informed about how to use the interface and the type of data on which the model was trained. It also emphasizes that the predictions are probabilistic and should not be used as a real-time application for earthquake predictions. As earthquakes can have serious consequences, users are reminded to seek reliable sources for verified information and advice.
"""
gr.Interface(fn=plot_func, inputs=inputs, outputs="plot", title=title, description=description, debugging=True).launch()