File size: 11,794 Bytes
719d0db
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import openai
import time
import base64
import copy
import streamlit as st
import folium
from folium.plugins import FloatImage
from streamlit_folium import folium_static
from langchain.schema import AIMessage

#----------------
# util functions
#----------------
def apply_html(html: str, **kwargs) -> None:
    st.markdown(html, unsafe_allow_html=True, **kwargs)

def stream_words(words: str,
                 prefix: str = "",
                 suffix: str = "",
                 sleep_time: float = 0.02) -> None:
    elem = st.empty()
    _words = ""
    for word in list(words):
        _words += word
        elem.markdown(f"{prefix} {_words} {suffix}", unsafe_allow_html=True)
        time.sleep(sleep_time)

def add_time_unit(time: float) -> str:
    if time < 60:
        return f"{time} sec"
    elif time >= 60 and time < 3599:
        return f"{time/60:.2f} mins"
    else:
        return f"{time/3600:.2f} hours" 

def find_node_id_by_name(data_list, name) -> int:
    for index, item in enumerate(data_list):
        if item.get("name") == name:
            return index
    return -1  # Return -1 if the name is not found

def mark_destination(tour_list, m) -> None:
    for destination in tour_list:
        image_name = f"static/{destination['name'].replace(' ', '-')}.png"
        if destination["name"] == "Ryoanji Temple":
            icon_width = 50; icon_height = 35
        elif destination["name"] == "Ryoanji Temple":
            icon_width = 50; icon_height = 40
        elif destination["name"] == "Kyoto Geishinkan":
            icon_width = 40; icon_height = 40
        elif destination["name"] == "Nijo-jo Castle":
            icon_width = 45; icon_height = 35
        else:
            icon_width = 50; icon_height = 50
        
        folium.Marker(
            location=[destination["latlng"][0], destination["latlng"][1]],
            tooltip=destination["name"],
            icon=folium.features.CustomIcon(icon_image = image_name,
                                            icon_size = (icon_width, icon_height),
                                            icon_anchor = (30, 30),
                                            popup_anchor = (3, 3))
        ).add_to(m)

    # add an indicator of the start/end point to Kyoto Station
    # z_indedx_offset: https://github.com/python-visualization/folium/issues/1281
    destination = tour_list[0]
    folium.Marker(
        location=[destination["latlng"][0]+0.003, destination["latlng"][1]-0.003],
        tooltip="start/end point",
        icon=folium.features.CustomIcon(icon_image = "static/star_emoji.png",
                                        icon_size = (30, 30),
                                        icon_anchor = (30, 30),
                                        popup_anchor = (3, 3)),
        z_index_offset=10000
    ).add_to(m)

    # add legend
    # Ref: https://python-visualization.github.io/folium/latest/user_guide/plugins/float_image.html
    with open("static/legend.png", "rb") as lf:
        # open in binary mode, read bytes, encode, decode obtained bytes as utf-8 string
        b64_content = base64.b64encode(lf.read()).decode("utf-8")
    FloatImage("data:image/png;base64,{}".format(b64_content), bottom=1, left=1).add_to(m)

def initialize_map() -> folium.Map:
    m = folium.Map(location=[st.session_state.lat_mean, st.session_state.lng_mean], tiles="Cartodb Positron")
    m.fit_bounds([st.session_state.sw, st.session_state.ne])
    mark_destination(st.session_state.tour_list, m)
    return m

def vis_route(routes, labels, m, ex_step, route_type, ant_path=True) -> None:
    if ("tour_list" in st.session_state) and (routes in st.session_state):
        tour_list = st.session_state.tour_list
        for j, route in enumerate(st.session_state[routes]): # vehicle loop
            for i in range(len(route)): # edge loop
                if i < len(route) - 1:
                    if i == ex_step:
                        if route_type == "actual":
                            color = "red"
                            popup = "Actual edge"  
                        else:
                            color = "blue"
                            popup = "CF edge"
                    else:
                        if labels[j][i] == 0:
                            color = "#2ca02c" 
                            popup = "Route length priority"
                        else:
                            color = "#9467bd"
                            popup = "Time window priority"
                    origin_id = route[i]
                    dest_id = route[i+1]
                    origin_latlng = tour_list[origin_id]["latlng"]
                    dest_latlng = tour_list[dest_id]["latlng"]
                    line = folium.PolyLine([origin_latlng, dest_latlng], color=color).add_to(m)
                    if ant_path:
                        folium.plugins.AntPath([origin_latlng, dest_latlng], tooltip=popup, color=color).add_to(m)
                    else:
                        folium.plugins.PolyLineTextPath(line, ">  ", offset=11, repeat=True, attributes={"fill": color,
                                                                                                         "font-size": 30}).add_to(m)

def visualize_actual_route(m: folium.Map) -> None:
    with st.columns((0.2, 1, 0.1))[1]:
        st.subheader(f"{st.session_state.curr_route}", divider="red")
        folium_static(m)

def visualize_cf_route(m: folium.Map) -> None:
    with st.columns((0.1, 1, 0.2))[1]:
        st.subheader("CF route", divider="blue")
        folium_static(m)

def select_actual_route():
    route_name =  "the actual route" if st.session_state.curr_route == "Actual Route" else "your current route"
    msg = f"You chose to stay {route_name}. Feel free to ask another why and why-not question for your current route!"
    st.session_state.chat_history.append(AIMessage(content=msg))
    m = initialize_map()
    m_ = initialize_map()
    if "labels" in st.session_state:
        cf_step = st.session_state.cf_step-1 if st.session_state.generated_cf_route else -1
        vis_route("routes", st.session_state.labels, m, cf_step, "actual")
        vis_route("routes", st.session_state.labels, m_, cf_step, "actual", ant_path=False)
    st.session_state.chat_history.append((m, None, m_, None))
    st.session_state.close_chat = False

def select_cf_route():
    msg = "You chose to replace your current route with the CF route. Feel free to ask a why and why-not question for the CF route!"
    st.session_state.chat_history.append(AIMessage(content=msg))
    # replace the actual route & labels with the CF ones
    st.session_state.routes = copy.deepcopy(st.session_state.cf_routes)
    st.session_state.labels = copy.deepcopy(st.session_state.cf_labels)
    st.session_state.curr_route = "Current Route"
    m = initialize_map()
    m_ = initialize_map()
    if "cf_labels" in st.session_state:
        cf_step = st.session_state.cf_step-1 if st.session_state.generated_cf_route else -1
        vis_route("routes", st.session_state.labels, m, cf_step, "actual")
        vis_route("routes", st.session_state.labels, m_, cf_step, "actual", ant_path=False)
    st.session_state.chat_history.append((m, None, m_, None))
    st.session_state.close_chat = False

# ref: https://stackoverflow.com/questions/76522693/how-to-check-the-validity-of-the-openai-key-from-python
def validate_openai_api_key(api_key):
    client = openai.OpenAI(api_key=api_key)
    try:
        client.models.list()
    except openai.AuthenticationError:
        return False
    else:
        return True

#-----
# CSS
#-----
RESPONSIBLE_MAP = """\
<style>
    [title~="st.iframe"]:not([height="0"]) { 
        width: 100%; 
        aspect-ratio: 1; 
        height: auto;
    }
</style>
"""
def apply_responsible_map_css() -> None:
    apply_html(RESPONSIBLE_MAP)

CENTERIZE_INCON = """
<style>
    [data-testid="stHorizontalBlock"] {
        align-items: center;
    }
    .stButton {
        text-align:center
    }
</style>
"""
def apply_centerize_icon_css() -> None:
    apply_html(CENTERIZE_INCON)

RED_CODE = """\
<style>
    code {
        color: #cc3333;
    }
</style>
"""
def apply_red_code_css() -> None:
    apply_html(RED_CODE)

REMOVE_SIDEBAR_TOPSPACE = """\
<style>
    .st-emotion-cache-6qob1r.eczjsme3 {
        margin-top: -75px;
    }
    .st-emotion-cache-1b9x38r.eczjsme2 {
        margin-top: 75px
    }
</style>
"""
def apply_remove_sidebar_topspace() -> None:
    apply_html(REMOVE_SIDEBAR_TOPSPACE)

#----
# JS
#----
# SET_WORDS_TO_CHATBOX = """\
# <script>
#     function insertText() {{
#         var chatInput = parent.document.querySelector('textarea[data-testid="stChatInput"]');
#         var nativeInputValueSetter = Object.getOwnPropertyDescriptor(window.HTMLTextAreaElement.prototype, "value").set;
#         nativeInputValueSetter.call(chatInput, "{words}");
#         var event = new Event('input', {{bubbles: true}});
#         chatInput.dispatchEvent(event);
#     }}
#     insertText();
# </script>
# """
# def set_chat_input(words:str) -> None:
#     st.components.v1.html(SET_WORDS_TO_CHATBOX.format(words=words), height=0)

# Ref. https://discuss.streamlit.io/t/issues-with-background-colour-for-buttons/38723/8
# Ref. https://github.com/streamlit/streamlit/issues/6605
CHANGE_HOVER_COLOR = """\
<script>
    var hide_me_list = window.parent.document.querySelectorAll('iframe');
    for (let i = 0; i < hide_me_list.length; i++) {{
        if (hide_me_list[i].height == 0) {{
            hide_me_list[i].parentNode.style.height = 0;
            hide_me_list[i].parentNode.style.marginBottom = '-1rem';
        }};
    }};

    if (window.matchMedia('(prefers-color-scheme: dark)').matches) {{
        var border = 'rgb(250,250,250,.2)';
    }} else {{
        var border = 'rgb(49,51,63,.2)';
    }}
    
    var elements = window.parent.document.querySelectorAll('{widget_type}');
    var fontColor = window.getComputedStyle(elements[0]).color;
    for (var i = 0; i < elements.length; ++i) {{ 
        if (elements[i].innerText == '{widget_label}') {{ 
            elements[i].style.color = fontColor;
            elements[i].style.background = '{background_color}';
            elements[i].onmouseover = function() {{ 
                this.style.color = '{hover_color}';
                this.style.borderColor = '{hover_color}';
            }};
            elements[i].onmouseout = function() {{ 
                this.style.color = fontColor;
                this.style.borderColor = border;
                this.style.background = '{background_color}';
            }};
            elements[i].onclick = function() {{ 
                this.style.color = 'white';
                this.style.borderColor = '{hover_color}';
                this.style.background = '{hover_color}';
            }};
            elements[i].onfocus = function() {{
                this.style.boxShadow = '{hover_color} 0px 0px 0px 0.2rem';
                this.style.borderColor = '{hover_color}';
                this.style.color = '{hover_color}';
            }};
            elements[i].onblur = function() {{
                this.style.boxShadow = 'none';
                this.style.color = fontColor;
                this.style.borderColor = border;
            }};
        }}
    }}
</script>
"""
def change_hover_color(widget_type: str,
                       widget_label: str, 
                       hover_color: str,
                       background_color: str = ""):
    st.components.v1.html(CHANGE_HOVER_COLOR.format(widget_type=widget_type,
                                         widget_label=widget_label,
                                         hover_color=hover_color,
                                         background_color=background_color), height=0)