This repository has been archived by the owner on Dec 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
173 lines (127 loc) · 4.25 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
import datetime
import enum
import math
import pandas as pd
import requests
import streamlit as st
class DayType(enum.Enum):
WORKING_DAY = 1
THURSEDAY = 3
FRIDAY = 4
def haversine_distance(lat_1: float, lon_1: float, lat_2: float, lon_2: float) -> float:
# Earth radius in kilometers
earth_radius = 6371
# Convert latitude and longitude coordinates from degrees to radians
lat1, lon1, lat2, lon2 = map(math.radians, [lat_1, lon_1, lat_2, lon_2])
# Calculate the differences between the latitudes and longitudes
delta_lat = lat2 - lat1
delta_lon = lon2 - lon1
# Calculate the square of half the chord length between the points
a = (
math.sin(delta_lat / 2) ** 2
+ math.cos(lat1) * math.cos(lat2) * math.sin(delta_lon / 2) ** 2
)
# Calculate the angular distance in radians
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
# Calculate the distance in kilometers
distance = earth_radius * c
return distance
st.title("Nostradamus")
cols = st.columns(2)
with cols[0]:
origin_lat: float | None = None
origin_lng: float | None = None
origin: str = ""
if "origin" in st.query_params:
origin_lat, origin_lng = (
float(item) if item != "" else None
for item in st.query_params.get("origin", ["", ""])
)
origin = (
f"{origin_lat},{origin_lng}"
if origin_lat is not None and origin_lng is not None
else ""
)
origin = st.text_input("Origin", placeholder="37.271408, 49.504597", value=origin)
if not origin or "," not in origin:
st.error("no origin entered")
st.stop()
origin_lat, origin_lng = map(float, origin.split(","))
st.query_params["origin"] = [str(origin_lat), str(origin_lng)]
with cols[1]:
dest_lat: float | None = None
dest_lng: float | None = None
dest: str = ""
if "dest" in st.query_params:
dest_lat, dest_lng = (
float(item) if item != "" else None
for item in st.query_params.get("dest", ["", ""])
)
dest = (
f"{dest_lat},{dest_lng}"
if dest_lat is not None and dest_lng is not None
else ""
)
dest = st.text_input("Destination", placeholder="37.27284, 49.54639", value=dest)
if not dest or "," not in dest:
st.error("no destination entered")
st.stop()
dest_lat, dest_lng = map(float, dest.split(","))
st.query_params["dest"] = [str(dest_lat), str(dest_lng)]
points = pd.DataFrame(
{
"lat": [origin_lat, dest_lat],
"lon": [origin_lng, dest_lng],
},
)
st.map(points)
hd = haversine_distance(origin_lat, origin_lng, dest_lat, dest_lng)
st.text(f"Haversine distance: {hd}")
cols = st.columns(2)
with cols[0]:
day_type_index: int | None = None
if "day_type" in st.query_params:
day_type_index = next(
index
for index, dt in enumerate(DayType)
if dt.name == st.query_params.get("day_type", ["-"])[0]
)
day_type = st.selectbox(
"Day Type",
[dt.name for dt in DayType],
index=day_type_index,
)
if day_type is None:
st.error("no day_type entered")
st.stop()
assert day_type is not None
st.query_params["day_type"] = [day_type]
with cols[1]:
dep_time: datetime.time | None = None
if "dep_time" in st.query_params:
try:
dep_time = datetime.time.fromisoformat(
st.query_params.get("dep_time", [""])[0]
)
except ValueError:
dep_time = None
dep_time = st.time_input("Departure time", value=dep_time)
if dep_time is None:
st.error("day/time information required")
st.stop()
assert dep_time is not None
st.query_params["dep_time"] = [dep_time.isoformat()]
with st.spinner("Wait for ETA..."):
resp = requests.post(
"http://172.21.88.100:1378/predict",
json={
"origin_lat": origin_lat,
"origin_lng": origin_lng,
"destination_lat": dest_lat,
"destination_lng": dest_lng,
"hour": dep_time.hour,
"haversine_distance": hd,
"day_type": DayType[day_type].value,
},
)
st.text(f"{resp.json()}")