|
| 1 | +<doctype html> |
| 2 | + |
| 3 | + <head> |
| 4 | + <title>Document Classification Console</title> |
| 5 | + |
| 6 | + <style> |
| 7 | + .button { |
| 8 | + margin-top: 20px; |
| 9 | + height: 30px; |
| 10 | + width: 100px; |
| 11 | + font-size: 12pt; |
| 12 | + } |
| 13 | + |
| 14 | + #query { |
| 15 | + height: 50px; |
| 16 | + width: 700px; |
| 17 | + font-size: 14pt; |
| 18 | + padding-left: 10px; |
| 19 | + } |
| 20 | + |
| 21 | + #submit_button { |
| 22 | + height: 50px; |
| 23 | + width: 70px; |
| 24 | + font-size: 14pt; |
| 25 | + } |
| 26 | + |
| 27 | + .selection_area { |
| 28 | + padding: 20px; |
| 29 | + } |
| 30 | + |
| 31 | + input[type=radio]:checked+label { |
| 32 | + color: red; |
| 33 | + } |
| 34 | + </style> |
| 35 | + |
| 36 | + <script> |
| 37 | + var PYTHON_SERVER_ADDRESS = "{{ console_address }}"; |
| 38 | + window.onload = function () { |
| 39 | + function makeScoreString(raw_scores) { |
| 40 | + return raw_scores[0] + ": " + (raw_scores[1] * 100).toFixed(2) + "%"; |
| 41 | + } |
| 42 | + function makeUL(query, array) { |
| 43 | + // Create the list element: |
| 44 | + var prediction_container = document.createElement('div'); |
| 45 | + var list = document.createElement('ul'); |
| 46 | + |
| 47 | + var header = document.createElement("h4"); |
| 48 | + header.appendChild(document.createTextNode("Help us gather more data!")); |
| 49 | + prediction_container.appendChild(header); |
| 50 | + prediction_container.appendChild(document.createTextNode("Select the correct prediction from below and submit to provide us more data to learn from!")) |
| 51 | + |
| 52 | + // prediction_container.appendChild(list); |
| 53 | + |
| 54 | + var selection_area = document.createElement("div"); |
| 55 | + selection_area.setAttribute("class", "selection_area"); |
| 56 | + |
| 57 | + |
| 58 | + for (var i = 0; i < array.length; i++) { |
| 59 | + // Create the list item: |
| 60 | + var item = document.createElement('li'); |
| 61 | + var intent_name = array[i][0]; |
| 62 | + var intent_score = array[i][1]; |
| 63 | + |
| 64 | + var choiceSelection = document.createElement('input') |
| 65 | + choiceSelection.setAttribute('type', 'radio'); |
| 66 | + choiceSelection.setAttribute('name', 'choice'); |
| 67 | + choiceSelection.setAttribute('value', query + "," + intent_name); |
| 68 | + choiceSelection.setAttribute('id', "choice_" + intent_name); |
| 69 | + |
| 70 | + var choiceLabel = document.createElement('label') |
| 71 | + choiceLabel.setAttribute('for', "choice_" + intent_name); |
| 72 | + choiceLabel.appendChild(document.createTextNode(makeScoreString(array[i]))); |
| 73 | + |
| 74 | + selection_area.appendChild(choiceSelection); |
| 75 | + selection_area.appendChild(choiceLabel); |
| 76 | + selection_area.appendChild(document.createElement("br")); |
| 77 | + } |
| 78 | + |
| 79 | + // add submit button |
| 80 | + |
| 81 | + var submit_data_button = document.createElement("button"); |
| 82 | + submit_data_button.setAttribute("type", "submit"); |
| 83 | + submit_data_button.setAttribute("class", "button"); |
| 84 | + submit_data_button.onclick = function () { |
| 85 | + var data_point = document.querySelector('input[name="choice"]:checked').value; |
| 86 | + var xhr = new XMLHttpRequest(); |
| 87 | + xhr.onreadystatechange = function () { |
| 88 | + if (this.readyState == 4) { |
| 89 | + if (this.status == 200) { |
| 90 | + var data = JSON.parse(xhr.responseText); |
| 91 | + // Display the returned data in browser |
| 92 | + var success_message = "The data point (query=" + data.query + ", label=" + data.label + ") has been added, thank you for your help!"; |
| 93 | + document.getElementById('raw_scores').innerHTML = success_message; |
| 94 | + } else { |
| 95 | + console.error('Error: ' + this.status); |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + xhr.open('GET', PYTHON_SERVER_ADDRESS + 'api/add_data/?data_point=' + encodeURI(data_point)); |
| 101 | + xhr.send(); |
| 102 | + }; |
| 103 | + |
| 104 | + submit_data_button.appendChild(document.createTextNode("Submit")); |
| 105 | + |
| 106 | + selection_area.appendChild(submit_data_button); |
| 107 | + prediction_container.appendChild(selection_area); |
| 108 | + return prediction_container; |
| 109 | + } |
| 110 | + |
| 111 | + // Add the contents of options[0] to #foo: |
| 112 | + document.getElementById('submit_button').onclick = function () { |
| 113 | + var query_box_val = document.getElementById('query').value; |
| 114 | + var xhr = new XMLHttpRequest(); |
| 115 | + xhr.onreadystatechange = function () { |
| 116 | + if (this.readyState == 4) { |
| 117 | + if (this.status == 200) { |
| 118 | + var data = JSON.parse(xhr.responseText); |
| 119 | + // Display the returned data in browser |
| 120 | + document.getElementById("best_prediction").innerHTML = makeScoreString(data.prediction); |
| 121 | + document.getElementById('raw_scores').innerHTML = ""; |
| 122 | + document.getElementById('raw_scores').appendChild(makeUL(data.query, data.raw_scores)); |
| 123 | + } else { |
| 124 | + console.error('Error: ' + this.status); |
| 125 | + } |
| 126 | + } |
| 127 | + }; |
| 128 | + |
| 129 | + xhr.open('GET', PYTHON_SERVER_ADDRESS + "api/model/?query=" + encodeURI(query_box_val)); |
| 130 | + xhr.send(); |
| 131 | + }; |
| 132 | + |
| 133 | + // Query on enter |
| 134 | + var query_box = document.getElementById("query"); |
| 135 | + query_box.addEventListener("keyup", function (event) { |
| 136 | + // Number 13 is the "Enter" key on the keyboard |
| 137 | + if (event.keyCode === 13) { |
| 138 | + event.preventDefault(); |
| 139 | + document.getElementById("submit_button").click(); |
| 140 | + } |
| 141 | + }); |
| 142 | + }; |
| 143 | + </script> |
| 144 | + </head> |
| 145 | + |
| 146 | + <body> |
| 147 | + |
| 148 | + <h2 id="title">Document Classification Console</h2> |
| 149 | + <input type="text" name="doc" id="query" value="Sample query"> |
| 150 | + <button type="submit" id="submit_button">Enter</button> |
| 151 | + |
| 152 | + <br /> |
| 153 | + <h3>Top Model Prediction</h3> |
| 154 | + <div id="best_prediction"></div> |
| 155 | + |
| 156 | + <br /> |
| 157 | + <div id="raw_scores"></div> |
| 158 | + </body> |
| 159 | +</doctype> |
0 commit comments