forked from spyysalo/bert-span-classifier
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo.html
More file actions
226 lines (215 loc) · 4.82 KB
/
demo.html
File metadata and controls
226 lines (215 loc) · 4.82 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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Span classifier demo</title>
<style type="text/css">
html, body {
margin: 0;
font-family: 'Open Sans', sans-serif;
width: 100%;
height: 100%;
}
article {
flex-direction: column;
align-items: stretch;
}
main {
flex-grow: 1;
}
header, main, footer {
flex-shrink: 0;
}
header {
width: 100%;
padding: 10px;
background-color: blue;
color:white;
}
footer {
width: 100%;
padding: 10px;
background-color: blue;
color:white;
position: absolute;
bottom: 0;
}
footer a {
color: white;
}
div.main-row {
display: flex;
flex-direction: row;
margin: 1em;
}
div.column {
display: flex;
flex-direction: column;
}
.left {
flex-grow: 1;
text-align: right;
}
.center {
flex-grow: 0;
text-align: center;
}
.right {
flex-grow: 1;
text-align: left;
}
#tokenization {
flex-grow: 1;
}
</style>
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<script>
// https://stackoverflow.com/a/6234804
function escapeHtml(unsafe) {
return unsafe
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'");
}
function isAbsolute(url) {
var re = new RegExp('^[a-z]+://', 'i');
return re.test(url);
}
function makeUrl(url, params) {
// Adapted from https://fetch.spec.whatwg.org/#fetch-api
if (isAbsolute(url)) {
url = new URL(url);
} else {
url = new URL(url, window.location.origin);
}
Object.keys(params).forEach(
key => url.searchParams.append(key, params[key])
);
return url;
}
var resultChart;
function initChart() {
var ctx = document.getElementById('chart').getContext('2d');
resultChart = new Chart(ctx, {
type: 'horizontalBar',
data: {
labels: [
'Chemical',
'Disease',
'Gene/prot',
'Organism'
],
datasets: [{
backgroundColor: [
'#7fa2ff',
'red',
'#8fcfff',
'#ffccaa'
],
borderColor: 'black',
data: [0, 0, 0, 0]
}]
},
options: {
'responsive': false,
'legend': {
'display': false,
}
}
});
}
function updateChart(prediction) {
let data = [
prediction['che'],
prediction['dis'],
prediction['ggp'],
prediction['org']
];
console.log(data);
resultChart.data.datasets[0].data = data;
resultChart.update();
}
var inputTimeout; // Don't predict on every keypress
var predictTimeout; // Don't predict on every keypress
var predicting = false; // Don't queue up multiples
async function predictSpan() {
let params = {};
let inputs = document.getElementsByClassName("listen-to");
for (let i=0; i<inputs.length; i++) {
params[inputs[i].id] = inputs[i].value;
}
var url = makeUrl("http://127.0.0.1:9000", params)
console.log(url, params);
try {
predicting = true;
var response = await fetch(url);
var data = await response.json();
let tokenization = document.getElementById("tokenization");
let tokenized = data['left'].concat(['[MASK]'], data['right'])
tokenization.value = tokenized.join(" ");
updateChart(data);
console.log(tokenization);
console.log(data);
predicting = false;
} catch(e) {
console.log(e);
}
}
function pollPredict() {
clearTimeout(predictTimeout);
if (!predicting) {
predictSpan()
} else {
predictTimeout = setTimeout(pollPredict, 100);
}
}
function inputChanged() {
clearTimeout(inputTimeout);
inputTimeout = setTimeout(pollPredict, 10);
}
function load() {
var inputs = document.getElementsByClassName("listen-to");
for (let i=0; i<inputs.length; i++) {
let input = inputs[i];
input.addEventListener('input', inputChanged);
}
initChart();
}
window.onload = load;
</script>
</head>
<body>
<article>
<header>
<div>Span classifier demo</div>
</header>
<main>
<div class="main-row">
<div class="left column">
<span>Left context</span>
<input id="left" class="left listen-to" type="text"/>
</div>
<div class="center column">
<span>Span</span>
<input id="span" class="center listen-to" type="text"/>
</div>
<div class="right column">
<span>Right context</span>
<input id="right" class="right listen-to" type="text"/>
</div>
</div>
<div class="main-row">
<input id="tokenization" type="text" value=""></input>
</div>
<div class="main-row">
<canvas id="chart"></canvas>
</div>
</main>
<footer>
<span><a href="https://github.com/spyysalo/bert-span-classifier">https://github.com/spyysalo/bert-span-classifier</a></span>
</footer>
</article>
</body>
</html>