-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshrtn.html
More file actions
241 lines (197 loc) · 7.61 KB
/
shrtn.html
File metadata and controls
241 lines (197 loc) · 7.61 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sentence Embedding Calculator</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
<script src="https://cdn.jsdelivr.net/npm/@mlc-ai/web-llm@0.2.77/lib/index.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/universal-sentence-encoder"></script>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Knewave&display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Sriracha&display=swap" rel="stylesheet">
<style>
body {
font-family: "Sriracha", serif;
background-color: #50c8e6;
}
.container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
justify-items: center;
}
#title {
font-family: 'Knewave';
font-size: 10vh;
}
form {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
justify-items: center;
}
input[type="text"] {
width: 60vw;
font-size: 24px;
border: 3px solid #000000;
border-radius: 5px;
min-height: 40px;
text-align: center;
}
button {
background-color: black;
color: white;
border: none;
padding: 30px 30px;
border-radius: 20px;
cursor: pointer;
font-size: 1rem;
}
button:hover {
background-color: #0056b3;
}
button:disabled {
background-color: #6c757d;
cursor: not-allowed;
}
#result {
margin-top: 36px;
white-space: pre-wrap;
font-size: 24px;
}
.error {
color: #dc3545;
margin-top: 1rem;
}
.loading {
display: none;
margin-top: 1rem;
}
.loading.visible {
display: block;
}
</style>
</head>
<body>
<div class="container">
<h1 id="title">SHRTN</h1>
<h1 id="initial-sentence"> The quick brown fox jumps over the lazy dog</h1>
<form autocomplete="off" autocapitalize="none" autocorrect="on">
<input type="text" id="input" placeholder="Enter your sentence here...">
<!-- <p>Character count:
<p id="character-count">0</p> -->
</p>
<br>
<span style="display: flex; flex-direction: row; width: 50%; justify-content: space-between;">
<button id="calculate">Calculate Embedding</button>
<button id="next">Next Sentence</button>
</span>
</form>
<!-- <textarea id="input" placeholder="Enter your sentence here..."></textarea>
<button id="calculate">Calculate Embedding</button> -->
<div id="loading" class="loading">
Computing embedding...
</div>
<div id="result"></div>
</div>
<script lang="ts">
const input = document.getElementById('input');
const calculate = document.getElementById('calculate');
const result = document.getElementById('result');
const loading = document.getElementById('loading');
const initial_sentence = document.getElementById('initial-sentence');
const form = document.querySelector('form');
const characterCountText = document.getElementById('character-count');
let model;
let indx = Math.floor(Math.random() * 50);
let list;
let initialEmbedding;
let initialSentence;
let characterCount;
let initialCharacterCount;
// Create a type with a character difference, embedding difference, and score.
// const Score = {
// characterDiff: number,
// embeddingDiff: number,
// score: number
// };
const scores = [];
loadModel();
fetch("../shrtn.json")
.then(response => response.json())
.then(data => {
list = data;
initialSentence = list.sentences[indx];
console.log(initialSentence);
// const initialSentence = list.sentences[indx];
initialCharacterCount = initialSentence.length
characterCount = initialCharacterCount;
initial_sentence.textContent = initialSentence;
input.value = initialSentence;
});
function computeScore(characterDiff, embeddingDiff) {
// First 5 character are 5 points
// Next 5 characters are 8 points
// Next 2 characters are 10 points
// All other characters are 15 points
let characterSubScore = 5 * Math.min(characterDiff, 5) +
8 * Math.min(Math.max(characterDiff - 5, 0), 5) +
10 * Math.min(Math.max(characterDiff - 10, 0), 2) +
15 * Math.max(characterDiff - 12, 0);
let embeddingSubScore = 10 * (1 - embeddingDiff);
return Math.floor(embeddingSubScore * characterSubScore);
}
async function loadModel() {
model = await use.load();
console.log('Model loaded');
loading.classList.remove('visible');
// Embed the initial sentence.
initialEmbedding = await model.embed(initialSentence);
characterCountText.textContent = characterCount;
}
function calculateCharacterCountDifference(sentence) {
return initialCharacterCount - sentence.length;
}
function calculateEmbeddingDifference(embedding) {
return tf.losses.cosineDistance(embedding, initialEmbedding).arraySync();
}
calculate.addEventListener('click', async () => {
loading.classList.add('visible');
result.textContent = '';
if (!model) {
await loadModel();
}
// Embed the input sentence.
const embeddings = await model.embed(input.value);
const characterDiff = Math.round(calculateCharacterCountDifference(input.value), 2);
const embeddingDiff = Math.max(0, calculateEmbeddingDifference(embeddings)).toFixed(2);
// Print the embeddings.
scores.push({
characterDiff,
embeddingDiff,
score: computeScore(characterDiff, embeddingDiff)
});
result.textContent = `Character count difference: ${characterDiff}\n` +
`Embedding difference: ${embeddingDiff}` + '\n' + `Score: ${computeScore(characterDiff, embeddingDiff)}`;
loading.classList.remove('visible');
});
next.addEventListener('click', () => {
// Refresh the page
window.location.reload();
});
form.addEventListener('submit', (e) => {
e.preventDefault();
calculate.click();
});
input.addEventListener('keydown', () => {
characterCount = input.value.length;
characterCountText.textContent = characterCount;
});
</script>
</body>
</html>