-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
509 lines (456 loc) · 13.9 KB
/
index.html
File metadata and controls
509 lines (456 loc) · 13.9 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Suntrace LLM Function Demo</title>
<!-- Leaflet CSS and JS -->
<link
rel="stylesheet"
href="https://unpkg.com/leaflet@1.9.4/dist/leaflet.css"
/>
<script src="https://unpkg.com/leaflet@1.9.4/dist/leaflet.js"></script>
<!-- Leaflet Draw plugin -->
<link
rel="stylesheet"
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css"
/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js"></script>
<!-- Bootstrap CSS -->
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css"
rel="stylesheet"
/>
<!-- Add Marked.js for Markdown parsing -->
<script src="https://cdn.jsdelivr.net/npm/marked/marked.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/dompurify@3.0.6/dist/purify.min.js"></script>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 0;
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden; /* Prevent page-level scrolling */
}
.container-fluid {
flex: 1;
padding: 20px;
display: flex;
flex-direction: column;
overflow: hidden; /* Prevent container scrolling */
}
.row {
flex: 1;
margin-bottom: 20px;
display: flex;
overflow: hidden; /* Prevent row scrolling */
}
.col-md-8,
.col-md-4 {
display: flex;
flex-direction: column;
height: 100%; /* Full height of parent */
}
#map {
width: 100%;
height: 100%;
min-height: 400px;
max-height: 100%; /* Prevent map from growing beyond container */
border-radius: 5px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.chat-container {
display: flex;
flex-direction: column;
height: 100%; /* Take full height of parent */
max-height: 100%; /* Constrain height */
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
border-radius: 5px;
background-color: #f8f9fa;
overflow: hidden; /* Hide overflow */
}
.chat-messages {
flex: 1;
overflow-y: auto; /* Allow vertical scrolling */
padding: 15px;
background-color: white;
border-top-left-radius: 5px;
border-top-right-radius: 5px;
}
.chat-form {
display: flex;
padding: 10px;
background-color: #f8f9fa;
border-bottom-left-radius: 5px;
border-bottom-right-radius: 5px;
}
#chat-input {
flex: 1;
padding: 8px;
border: 1px solid #ddd;
border-radius: 4px;
margin-right: 10px;
}
.message {
margin-bottom: 15px;
padding: 10px;
border-radius: 5px;
max-width: 80%;
}
.user-message {
background-color: #e3f2fd;
color: #0d47a1;
align-self: flex-end;
margin-left: auto;
}
.ai-message {
background-color: #f5f5f5;
color: #333;
}
.loading {
text-align: center;
padding: 10px;
}
.send-btn {
background-color: #4caf50;
color: white;
border: none;
}
.header {
text-align: center;
margin-bottom: 20px;
}
.badge {
font-size: 0.8em;
padding: 5px;
margin-right: 5px;
background-color: #666;
}
.legend {
padding: 10px;
background: white;
box-shadow: 0 0 15px rgba(0, 0, 0, 0.2);
border-radius: 5px;
position: absolute;
bottom: 10px;
right: 10px;
z-index: 1000;
}
.legend div {
margin: 5px 0;
}
.legend-item {
display: flex;
align-items: center;
margin-bottom: 5px;
}
.legend-color {
width: 15px;
height: 15px;
margin-right: 8px;
border-radius: 3px;
}
/* Add styles for Markdown content */
.ai-message h1,
.ai-message h2,
.ai-message h3 {
margin-top: 10px;
margin-bottom: 8px;
}
.ai-message h1 {
font-size: 1.4em;
}
.ai-message h2 {
font-size: 1.2em;
}
.ai-message h3 {
font-size: 1.1em;
}
.ai-message ul,
.ai-message ol {
padding-left: 20px;
margin: 8px 0;
}
.ai-message p {
margin: 8px 0;
}
.ai-message code {
background-color: #f4f4f4;
padding: 2px 4px;
border-radius: 3px;
}
.ai-message pre {
background-color: #f4f4f4;
padding: 10px;
border-radius: 5px;
overflow-x: auto;
}
.ai-message table {
border-collapse: collapse;
margin: 10px 0;
}
.ai-message th,
.ai-message td {
border: 1px solid #ddd;
padding: 6px;
text-align: left;
}
</style>
</head>
<body>
<div class="container-fluid">
<div class="header">
<h1>Suntrace LLM Function Demo</h1>
</div>
<div class="row">
<div class="col-md-8">
<div id="map"></div>
<div class="legend">
<h6>Legend</h6>
<div class="legend-item">
<div class="legend-color" style="background-color: #3388ff"></div>
<span>Drawn Region</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #00aaff"></div>
<span>CandidateMini-grids</span>
</div>
<div class="legend-item">
<div class="legend-color" style="background-color: #444444"></div>
<span>Buildings</span>
</div>
</div>
</div>
<div class="col-md-4">
<div class="chat-container">
<div class="chat-messages" id="chat-messages">
<div class="message ai-message">
Hello! I can help you analyze geospatial data. Draw a region on
the map and ask me questions about it.
</div>
</div>
<div class="chat-form">
<input
type="text"
id="chat-input"
placeholder="Ask a question..."
/>
<button class="btn send-btn" id="send-btn">Send</button>
</div>
</div>
</div>
</div>
</div>
<script>
let map;
let drawnItems = new L.FeatureGroup();
let candidateMinigridsLayer = L.featureGroup();
let buildingsLayer = L.featureGroup();
let currentDrawnPolygon = null;
// Initialize the map
async function initMap() {
map = L.map("map").setView([3.16, 32.8], 10); // Initial center on Lamwo district, Uganda
// Add tile layer
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: "© OpenStreetMap contributors",
}).addTo(map);
// Add draw control
map.addLayer(drawnItems);
const drawControl = new L.Control.Draw({
draw: {
polygon: {
allowIntersection: false,
drawError: {
color: "#e1e100",
message: "<strong>You can't draw that!</strong>",
},
shapeOptions: {
color: "#3388ff",
},
},
polyline: false,
circle: false,
rectangle: true,
marker: false,
circlemarker: false,
},
edit: {
featureGroup: drawnItems,
remove: true,
},
});
map.addControl(drawControl);
// Event handler for when a shape is drawn
map.on(L.Draw.Event.CREATED, function (e) {
// Clear previously drawn items
drawnItems.clearLayers();
const layer = e.layer;
drawnItems.addLayer(layer);
// Save the coordinates for API calls
if (layer instanceof L.Polygon || layer instanceof L.Rectangle) {
const coordinates = layer
.getLatLngs()[0]
.map((latlng) => [latlng.lng, latlng.lat]);
currentDrawnPolygon = coordinates;
// Add a message to the chat
addMessage(
"Region selected. Ask a question about this area.",
"ai"
);
}
});
// Load map data from the API
try {
const response = await fetch("/get_map_layers");
const data = await response.json();
// Set map view to the center of the data
if (data.center && data.bounds) {
map.fitBounds(data.bounds);
}
// Add minigrids layer
if (data.candidate_minigrids) {
L.geoJSON(data.candidate_minigrids, {
style: {
color: "#ff0000",
weight: 2,
opacity: 0.8,
fillOpacity: 0.35,
},
onEachFeature: function (feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindPopup(
`Candidate Minigrid: ${feature.properties.name}`
);
}
},
}).addTo(candidateMinigridsLayer);
}
// Add buildings layer
if (data.buildings) {
L.geoJSON(data.buildings, {
style: {
color: "#444444",
weight: 1,
opacity: 0.8,
fillOpacity: 0.5,
},
}).addTo(buildingsLayer);
}
// Add layers to map
candidateMinigridsLayer.addTo(map);
buildingsLayer.addTo(map);
// Add layer controls
const overlayMaps = {
"Candidate Minigrids": candidateMinigridsLayer,
Buildings: buildingsLayer,
};
L.control.layers(null, overlayMaps).addTo(map);
} catch (error) {
console.error("Error loading map data:", error);
addMessage("Error loading map data. Please refresh the page.", "ai");
}
}
// Initialize chat handlers
function initChat() {
const chatInput = document.getElementById("chat-input");
const sendBtn = document.getElementById("send-btn");
// Send message on button click
sendBtn.addEventListener("click", sendMessage);
// Send message on Enter key
chatInput.addEventListener("keypress", function (e) {
if (e.key === "Enter") {
sendMessage();
}
});
}
// Send message function
async function sendMessage() {
const chatInput = document.getElementById("chat-input");
const userQuery = chatInput.value.trim();
if (userQuery === "") return;
// Add user message to chat
addMessage(userQuery, "user");
// Clear input
chatInput.value = "";
// Show loading message
const loadingId = addMessage("Thinking...", "ai", true);
try {
// Prepare request data
const requestData = {
query: userQuery,
};
// Add polygon coordinates if available
if (currentDrawnPolygon) {
requestData.polygon = currentDrawnPolygon;
}
// Send request to API
const response = await fetch("/query", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(requestData),
});
const data = await response.json();
// Remove loading message
removeMessage(loadingId);
// Display response
if (data.error) {
addMessage(`Error: ${data.error}`, "ai");
} else {
addMessage(data.response, "ai");
}
} catch (error) {
// Remove loading message
removeMessage(loadingId);
// Display error
console.error("Error sending query:", error);
addMessage(
"Sorry, there was an error processing your request. Please try again.",
"ai"
);
}
}
// Add message to the chat
function addMessage(text, sender, isLoading = false) {
const messagesContainer = document.getElementById("chat-messages");
const messageDiv = document.createElement("div");
messageDiv.className = `message ${sender}-message`;
if (isLoading) {
messageDiv.className += " loading";
messageDiv.innerHTML = `<div class="spinner-border spinner-border-sm" role="status"></div> ${text}`;
} else {
if (sender === "ai") {
// Safely render markdown for AI responses
const cleanHtml = DOMPurify.sanitize(marked.parse(text));
messageDiv.innerHTML = cleanHtml;
} else {
// For user messages, keep using textContent for security
messageDiv.textContent = text;
}
}
// Generate a unique ID for the message
const messageId = `msg-${Date.now()}`;
messageDiv.id = messageId;
messagesContainer.appendChild(messageDiv);
// Scroll to bottom
messagesContainer.scrollTop = messagesContainer.scrollHeight;
return messageId;
}
// Remove message from chat
function removeMessage(messageId) {
const message = document.getElementById(messageId);
if (message) {
message.remove();
}
}
// Initialize the page
document.addEventListener("DOMContentLoaded", function () {
initMap();
initChat();
});
</script>
</body>
</html>