File size: 18,657 Bytes
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f397ead
f083205
dcff9af
f397ead
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
a0ebfd5
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f083205
 
 
 
 
 
be5548b
 
f083205
be5548b
 
 
 
 
 
 
f083205
 
 
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
077ff6c
be5548b
 
 
 
 
 
 
 
 
 
f397ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be5548b
 
 
 
ece8924
 
 
 
 
 
 
 
 
 
 
 
 
077ff6c
 
be5548b
 
 
 
ece8924
 
 
be5548b
 
 
 
f397ead
be5548b
 
 
 
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
c4fbd6e
be5548b
 
 
 
 
077ff6c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f083205
077ff6c
f083205
 
 
 
 
 
 
 
 
 
 
 
 
077ff6c
 
 
 
 
 
 
 
 
 
 
 
f397ead
 
 
 
 
 
 
077ff6c
f397ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be5548b
 
 
 
 
 
 
 
 
9cf63fa
be5548b
 
 
 
 
 
 
077ff6c
 
 
 
 
 
 
 
 
 
 
be5548b
 
 
 
f397ead
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
be5548b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
077ff6c
be5548b
 
 
 
077ff6c
be5548b
 
 
 
 
 
 
a0ebfd5
be5548b
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
<head>
    <title>SocialAI School Demo</title>
    <style>
        form {
            display: inline-block;
            display: inline-block;
            margin-left: 2px;
            margin-right: 2px;
            margin-bottom: 2px;
            margin-top: 2px;
        }

        .controls-container {
            display: flex;
            flex-direction: column;
            align-items: flex-start; /* aligns children to the start */
        }

        .form-container {
            display: flex;
            align-items: center;
            margin-bottom: 10px; /* provides spacing between controls */
        }

        .form-label {
            width: 200px; /* adjust this value as needed */
            text-align: right;
            margin-right: 10px;
        }


        body {
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: flex-start; /* Align to the top/start */
            height: 100vh;
            margin: 0; /* Remove default margin */
            padding: 10px; /* Optional: Add some padding at the top for spacing */
        }

        .btn-container {
            display: flex;
            flex-direction: column;
            margin-bottom: 20px;
        }

        .action-row {
            display: flex;
            flex-direction: row;
            gap: 10px;
            margin-bottom: 10px; /* Adds a space between the two rows */
        }

        .tree-container {
            width: 60%;
            min-width: 1000px;
            max-height: 600px;
            margin-left: 5px;
            margin-right: 5px;
            margin-bottom: 5px;
            margin-top: 5px;
            padding-top: 10px;
            padding-left: 30px;
            padding-right: 30px;
            border: 5px solid #ccc; /* Optional: just to visualize the container's boundaries */
            border-radius: 10px; /* uniform roundness */
            box-shadow: 3px 3px 5px #888888; /* horizontal offset | vertical offset | blur radius | color */
        }

        .env-container {
            width: 60%;  /* Adjust to desired width */
            min-width: 1000px;  /* Adjust to desired width */
            margin-left: 5px;
            margin-right: 5px;
            margin-bottom: 5px;
            margin-top: 5px;
            padding-top: 2px;
            padding-left: 30px;
            padding-right: 30px;
            padding-bottom: 30px;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            border: 3px solid #ccc; /* Optional: just to visualize the container's boundaries */
            border-radius: 10px; /* uniform roundness */
            box-shadow: 3px 3px 5px #888888; /* horizontal offset | vertical offset | blur radius | color */
        }

        .env-flex {
            display: flex;
            align-items: flex-start;  /* to align items to the top */
        }

        img {
            margin-left: 5px;
            margin-right: 5px;
            margin-bottom: 5px;
            margin-top: 5px;
        }

        #envImage {
            width: 100%; /* Make image responsive */
            min-width: 500px;
            height: auto; /* Maintain aspect ratio */
            display: block; /* Remove default image inline behaviors */
            margin-left: auto; /* Center the image horizontally if needed */
            margin-right: auto; /* Center the image horizontally if needed */
        }


        .bubble {
            margin-left: 20px;  /* space between the image and the bubble */
            position: relative;
            background-color: #f7f8f9;
            border-radius: 10px;
            padding: 10px;
            width: max-content;
            width: 100%; /* Make image responsive */
            height: auto; /* Maintain aspect ratio */
            min-width: 500px;
            margin-bottom: 20px;
        }

        /* The switch container */
        .switch {
          position: relative;
          display: inline-block;
          width: 60px;
          height: 34px;
        }

        /* Hide default HTML checkbox */
        .switch input {
          opacity: 0;
          width: 0;
          height: 0;
        }

        /* The slider (the on/off part) */
        .slider {
          position: absolute;
          cursor: pointer;
          top: 0;
          left: 0;
          right: 0;
          bottom: 0;
          background-color: #ccc;
          transition: 0.4s;
        }

        .slider:before {
          position: absolute;
          content: "";
          height: 26px;
          width: 26px;
          left: 4px;
          bottom: 4px;
          background-color: white;
          transition: 0.4s;
        }

        /* When input is checked, slide to the right */
        input:checked + .slider {
          background-color: #2196F3;
        }

        input:checked + .slider:before {
          transform: translateX(26px);
        }

        /* Round sliders */
        .slider.round {
          border-radius: 34px;
        }

        .slider.round:before {
          border-radius: 50%;
        }

    </style>
    <script>
        function performAction(actionName) {
            let bodyData = `action=${actionName}`;

            // If the action is "speak", fetch the selected template and word
            if (actionName === "speak") {
                const template = document.getElementById('actionTemplate').value;
                const word = document.getElementById('actionWord').value;
                bodyData += `&template=${template}&word=${word}`;
            }

            fetch('/perform_action', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: bodyData
            })
            .then(response => response.json())
            .then(data => {
                document.getElementById('envImage').src = `data:image/jpeg;base64,${data.image_data}`;
                if (actionName === "done") {

                    // Save the scroll position in sessionStorage before reloading
                    sessionStorage.setItem('scrollPosition', window.scrollY);
                    console.log(window.scrollY);

                    // Function to reload the page with updated URL parameter
                    function reloadPage() {
                        let currentUrl = new URL(window.location.href);
                        currentUrl.searchParams.set('_', new Date().getTime());
                        window.location.href = currentUrl.href;
                    }

                    // Call the function to reload the page
                    reloadPage();

<!--                    const svgTree = document.getElementById('svgTree');-->
<!--                    svgTree.data = `./static/current_tree.svg?_=${data.timestamp}`;-->
<!--                    // todo: update the dropdown lists for parameters-->
                }

                // Add this to handle the caretaker's utterance
                let bubble = document.getElementById('caretakerBubble');

                // Check for success
                if (data.done) {
                    if (data.success) {
                        bubble.innerHTML = '<br><div style="text-align: center; color: green;">SUCCESS!</div><br>' +
                           '<div style="text-align: center; color: gray; font-style: italic; font-size: smaller;">(press Enter to continue)</div><br>';
                        bubble.style.display = 'block';
                    } else {
                        bubble.innerHTML = '<br><div style="text-align: center; color: red;">FAILURE!</div><br>' +
                           '<div style="text-align: center; color: gray; font-style: italic; font-size: smaller;">(press Enter to continue)</div><br>';
                        bubble.style.display = 'block';
                    }
                } else if (data.bubble_text) {
                    let formattedText = data.bubble_text.replace(/\n/g, '<br>');
                    bubble.innerHTML = formattedText;
                    bubble.style.display = 'block';
                } else {
                    bubble.style.display = 'none';
                }



            })
            .catch(error => {
                console.error('Error:', error);
            });

        }

        document.addEventListener("DOMContentLoaded", function() {
            document.body.addEventListener("keydown", function(event) {
                if (event.key === "ArrowLeft") {
                    event.preventDefault();
                    performAction('left');
                } else if (event.key === "ArrowUp") {
                    event.preventDefault();
                    performAction('forward');
                } else if (event.key === "ArrowRight") {
                    event.preventDefault();
                    performAction('right');
                } else if (event.key === "s") {
                    event.preventDefault();
                    performAction('speak');
                } else if (event.key === "Spacebar" || event.key === " ") {
                    event.preventDefault();
                    performAction('toggle');
                } else if (event.key === "Shift") { // You had two conditions for Shift, so I've merged them.
                    event.preventDefault();
                    performAction('noop');
                } else if (event.key === "Enter") {
                    event.preventDefault();
                    performAction('done');
                }
            });
        });


        // handle the change event of the mask unobserved slider change
        function updateMaskUnobserved() {
            const maskUnobservedValue = document.querySelector('input[name="mask_unobserved"]').checked;

            fetch('/set_mask_unobserved', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `mask_unobserved=${maskUnobservedValue}`
            })
            .then(response => response.json())
            .then(data => {
                // Update the image src with the new image data
                document.getElementById('envImage').src = `data:image/jpeg;base64,${data.image_data}`;
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        // handle the change event of the textual observations slider change
        function updateTextualObservations() {
            const textualObservationsValue = document.querySelector('input[name="textual_observations"]').checked;

            fetch('/set_textual_observations', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/x-www-form-urlencoded',
                },
                body: `textual_observations=${textualObservationsValue}`
            })
            .then(response => response.json())
            .then(data => {
                // Add this to handle the caretaker's utterance
                let bubble = document.getElementById('caretakerBubble');

                // Check for success
                if (data.done) {
                    if (data.success) {
                        bubble.innerHTML = '<br><div style="text-align: center; color: green;">SUCCESS!</div><br>' +
                           '<div style="text-align: center; color: gray; font-style: italic; font-size: smaller;">(press Enter to continue)</div><br>';
                        bubble.style.display = 'block';
                    } else {
                        bubble.innerHTML = '<br><div style="text-align: center; color: red;">FAILURE!</div><br>' +
                           '<div style="text-align: center; color: gray; font-style: italic; font-size: smaller;">(press Enter to continue)</div><br>';
                        bubble.style.display = 'block';
                    }
                } else if (data.bubble_text) {
                    let formattedText = data.bubble_text.replace(/\n/g, '<br>');
                    bubble.innerHTML = formattedText;
                    bubble.style.display = 'block';
                } else {
                    bubble.style.display = 'none';
                }
            })
            .catch(error => {
                console.error('Error:', error);
            });
        }

        function setEnvParams() {
            // Collect data from dropdowns
            var data = {};

            {% for key in parameter_options.keys() %}
            data['{{ key.id }}'] = document.getElementById('{{ key.id }}').value;
            {% endfor %}

            // Send data to Flask backend
            fetch('/set_env_params', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify(data),
            })
            .then(response => {
                if(response.ok) {
                    // If the request was successful, reload the page without cache
                    let currentUrl = new URL(window.location.href);
                    currentUrl.searchParams.set('_', new Date().getTime());
                    window.location.href = currentUrl.href;
                } else {
                    // Handle errors
                    console.error('Error in setEnvParams:', response);
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });

        }

    function restoreScrollPosition() {
        let savedScrollPosition = parseInt(sessionStorage.getItem('scrollPosition'));
        if (!isNaN(savedScrollPosition)) {
            window.scrollTo(0, savedScrollPosition);
            sessionStorage.removeItem('scrollPosition');
        }
    }

    // Attach the restoreScrollPosition function to the appropriate event
    window.addEventListener('load', restoreScrollPosition);
    </script>
</head>
<body>
    <div class="controls-container">
        <div class="form-container">
            <span class="form-label">Select Environment:</span>
            <form method="post" action="/set_env" id="envForm">
                <select name="env_label" onchange="document.getElementById('envForm').submit();">
                {% for env in available_env_labels %}
                    <option value="{{ env }}" {% if env == current_env_label %}selected{% endif %} {% if env.startswith("-")%}disabled{% endif %}>{{ env }}</option>
                {% endfor %}
                </select>
            </form>
        </div>

        <div class="form-container">
            <span class="form-label">Mask unobserved cells:</span>
            <label class="switch">
                <input type="checkbox" name="mask_unobserved" value="true" onchange="updateMaskUnobserved()" {% if mask_unobserved %}checked{% endif %}>
                <span class="slider round"></span>
            </label>
        </div>
        <div class="form-container">
            <span class="form-label">Textual observation:</span>
            <label class="switch">
                <input type="checkbox" name="textual_observations" value="true" onchange="updateTextualObservations()" {% if textual_observations %}checked{% endif %}>
                <span class="slider round"></span>
            </label>
        </div>
    </div>

    <p>This is the sampling tree. The current sampled parameters are highlighted blue</p>
    <div class="tree-container" >
        <div class="form-container">
            <span class="form-label">Select Parameters:</span>
            <form method="post" id="paramsEnvForm">
                {% set ns = namespace(lvl=1) %}
                {% for key, options in parameter_options.items() %}
                    {% if key.level != ns.lvl %}
                        {% if ns.lvl >= 0 %}<hr>{% endif %}
                    {% set ns.lvl = key.level %}
                    {% endif %}

                    <label>{{ key.label }}</label>
                    <select name="{{ key }}" id="{{ key.id }}" onchange="setEnvParams();">
                        {% for option in options %}
                        <option value="{{ option.id }}" {% if current_parameters[key].id == option.id %}selected{% endif %} >
                            {{ option.label }}
                        </option>
                        {% endfor %}
                    </select>
                {% endfor %}
            </form>
        </div>
        <object id="svgTree" type="image/svg+xml" data="{{ url_for('static', filename='current_tree.svg', _=timestamp) }}" width="95%" height="70%">Your browser does not support SVG</object>
    </div>

    <p>This is the environment.</p>
    <div class="env-container">
        <p>You can control the agent using the following actions:</p>

        <div class="btn-container">
            <div class="action-row">Primitive actions:
            <button onclick="performAction('left')">Left [&larr;]</button>
            <button onclick="performAction('forward')">Forward [&uarr;]</button>
            <button onclick="performAction('right')">Right [&rarr;]</button>
            <button onclick="performAction('toggle')">Toggle [Spacebar]</button>
            <button onclick="performAction('noop')">No-op [Shift]</button>
            <button onclick="performAction('done')">Done [Enter]</button>
            </div>

            <div class="action-row">
                Speaking actions:
                <select id="actionTemplate">
                    {% for templ in grammar_templates %}
                        <option value="{{ templ }}" {{ 'selected' if templ == "Help" else '' }}>{{ templ }}</option>
                    {% endfor %}
                </select>
                <select id="actionWord">
                    {% for word in grammar_words %}
                        <option value="{{ word }}" {{ 'selected' if word == "please" else '' }}>{{ word }}</option>
                    {% endfor %}
                </select>
                <button onclick="performAction('speak')">Speak [s]</button>
            </div>

        </div>

        <div class="env-flex" >
            <img id="envImage" src="data:image/jpeg;base64,{{ image_data }}" alt="State of the environment">

            <div id="caretakerBubble" class="bubble">
                {{ bubble_text }}
            </div>
        </div>

    </div>
</body>
</html>