Q-bert commited on
Commit
1c25f86
1 Parent(s): 1fb31ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +80 -7
app.py CHANGED
@@ -1,11 +1,84 @@
1
  import pickle
2
- with open("earthquake_model.pkl", 'rb') as file:
3
- loaded_model = pickle.load(file)
4
  import gradio as gr
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- def greet(name):
7
- return "Hello " + name + "!"
8
 
9
- demo = gr.Interface(fn=greet, inputs="text", outputs="text")
10
-
11
- demo.launch()
 
1
  import pickle
 
 
2
  import gradio as gr
3
+ import geopandas as gpd
4
+ from shapely.geometry import Point
5
+ import matplotlib.pyplot as plt
6
+ import numpy as np
7
+ with open("earthquake_model.pkl", 'rb') as file:
8
+ model = pickle.load(file)
9
+ def time2num(x):
10
+ try:
11
+ (h, m, s) = str(x).split(':')
12
+ result = int(h) * 3600 + int(m) * 60 + int(s)
13
+ return result
14
+ except:
15
+ return 0
16
+
17
+ def date2num(x):
18
+ try:
19
+ (m, d, y) = str(x).split("/")
20
+ result = int(y) * 365 + int(m) * 30 + int(d)
21
+ return result
22
+ except:
23
+ return 0
24
+
25
+ def datetime2num(date_str, time_str):
26
+ date_value = date2num(date_str)
27
+ time_value = time2num(time_str)
28
+ return date_value ,time_value
29
+ def test(model, date_str, time_str,all_points):
30
+ data_list = []
31
+ for lat, lon in all_points:
32
+ date, time = datetime2num(date_str, time_str)
33
+ data_list.append([date, time, lat, lon])
34
+ np_array = np.array(data_list)
35
+ res = model.predict_proba(np_array)
36
+ return res
37
+ def create_geodf(all_points, model, date_str, time_str):
38
+ res = test(model, date_str, time_str,all_points)
39
+
40
+ data_list = []
41
+ for lat, lon in all_points:
42
+ date, time = datetime2num(date_str, time_str)
43
+ data_list.append([date, time, lat, lon])
44
+ np_array = np.array(data_list)
45
+
46
+ df = pd.DataFrame(np_array, columns=['Date', 'Time', 'Latitude', 'Longitude'])
47
+ df['Probability_2'] = [i[1] for i in res]
48
+ df['geometry'] = [Point(lon, lat) for lat, lon in zip(df['Latitude'], df['Longitude'])]
49
+
50
+ crs = "EPSG:4326"
51
+ gdf = gpd.GeoDataFrame(df, crs=crs, geometry='geometry')
52
+
53
+ return gdf
54
+
55
+ def plot_func(date_str, time_str):
56
+ min_latitude = -90
57
+ max_latitude = 90
58
+ latitude_step = 1
59
+ min_longitude = -180
60
+ max_longitude = 180
61
+ longitude_step = 1
62
+ latitudes = np.arange(min_latitude, max_latitude + latitude_step, latitude_step)
63
+ longitudes = np.arange(min_longitude, max_longitude + longitude_step, longitude_step)
64
+ all_points = np.array(np.meshgrid(latitudes, longitudes)).T.reshape(-1, 2)
65
+ gdf = create_geodf(all_points, model, date_str, time_str)
66
+
67
+ top = gdf.nlargest(100, 'Probability_2')
68
+ world = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
69
+ fig, ax = plt.subplots(figsize=(12, 12))
70
+ ax.imshow(np.ones((180, 360)), cmap='gray', extent=[-180, 180, -90, 90])
71
+ world.plot(ax=ax, color='lightgray', edgecolor='black')
72
+ top.plot(ax=ax, markersize=50, color='red', legend=True, alpha=0.5)
73
+
74
+ plt.xlabel('Longitude')
75
+ plt.ylabel('Latitude')
76
+ plt.title('Possible Earthquake Map')
77
+ plt.grid(True)
78
+
79
+ return plt.gcf()
80
+
81
+ inputs = [gr.inputs.Textbox(label="Date: (MM/DD/YYYY)"), gr.inputs.Textbox(label="Time: (HH:MM:SS) GMT-4")]
82
 
 
 
83
 
84
+ gr.Interface(fn=plot_func, inputs=inputs, outputs="plot",debugging=True).launch()