import pickle import gradio as gr 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")] gr.Interface(fn=plot_func, inputs=inputs, outputs="plot",debugging=True).launch()