-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsketch.js
283 lines (228 loc) · 9.8 KB
/
sketch.js
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
let textLines = [];
let userInput = '';
let errorMessage = '';
let isGenerating = false;
let temperatureSlider;
let maxLengthSlider;
let topKSlider;
let lengthPenaltySlider;
let noRepeatNGramSlider;
let scrollOffset = 0;
let horizontalScrollOffset = 0;
let infoDiv;
let infoButton;
const sketch = (p) => {
let canvas;
let input;
let inputDiv;
p.setup = () => {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
inputDiv = document.createElement('div');
inputDiv.id = 'input-container';
document.body.appendChild(inputDiv);
// info div
infoDiv = document.createElement('div');
infoDiv.id = 'info-container';
infoDiv.style.position = 'absolute';
infoDiv.style.top = '10px';
infoDiv.style.left = '10px';
infoDiv.style.color = 'white';
infoDiv.style.fontFamily = 'Arial, sans-serif';
infoDiv.style.display = 'none'
document.body.appendChild(infoDiv);
updateInfo();
infoButton = document.createElement('button');
infoButton.innerHTML = '<i>ℹ️</i>'; // ℹ️ is the information icon
infoButton.style.position = 'absolute';
infoButton.style.top = '10px';
infoButton.style.right = '10px';
infoButton.style.backgroundColor = 'transparent';
infoButton.style.border = 'none';
infoButton.style.color = 'white';
infoButton.style.fontSize = '20px';
infoButton.style.cursor = 'pointer';
infoButton.addEventListener('click', toggleInfoVisibility);
document.body.appendChild(infoButton);
// the input text box
const inputWidth = windowWidth - 100;
const inputHeight = 30;
const posX = (windowWidth - inputWidth) / 2;
const posY = windowHeight - inputHeight - 80;
input = p.createInput();
input.parent('input-container');
input.position(posX, posY);
input.size(inputWidth, inputHeight);
input.changed(handleInput);
const sliderWidth = 140;
const sliderHeight = 20;
const sliderPosY = windowHeight - inputHeight - 40;
// temperature slider
temperatureSlider = p.createSlider(0.01, 1, 0.5, 0.01);
temperatureSlider.position(windowWidth - inputWidth - 30, sliderPosY);
temperatureSlider.size(sliderWidth, sliderHeight);
createLabel('temperature', windowWidth - inputWidth - 30, sliderPosY + sliderHeight + 15);
// max length slider
maxLengthSlider = p.createSlider(50, 500, 100, 10);
maxLengthSlider.position(windowWidth - inputWidth + sliderWidth - 10, sliderPosY);
maxLengthSlider.size(sliderWidth, sliderHeight);
createLabel('max length', windowWidth - inputWidth + sliderWidth - 10, sliderPosY + sliderHeight + 15);
// top-k slider
topKSlider = p.createSlider(1, 100, 50, 1);
topKSlider.position(windowWidth - inputWidth + 2 * sliderWidth + 10, sliderPosY);
topKSlider.size(sliderWidth, sliderHeight);
createLabel('top-k', windowWidth - inputWidth + 2 * sliderWidth + 10, sliderPosY + sliderHeight + 15);
// length penalty slider
lengthPenaltySlider = p.createSlider(0.1, 2, 1, 0.1);
lengthPenaltySlider.position( windowWidth - inputWidth + 3 * sliderWidth + 30, sliderPosY);
lengthPenaltySlider.size(sliderWidth, sliderHeight);
createLabel('length penalty', windowWidth - inputWidth + 3 * sliderWidth + 30, sliderPosY + sliderHeight + 15);
// noRepeatNGramSlider slider
noRepeatNGramSlider = p.createSlider(1, 10, 2, 1);
noRepeatNGramSlider.position(windowWidth - inputWidth + 4 * sliderWidth + 50, sliderPosY);
noRepeatNGramSlider.size(sliderWidth, sliderHeight);
createLabel('no repeat n-grams', windowWidth - inputWidth + 4 * sliderWidth + 50, sliderPosY + sliderHeight + 15);
// canvas
canvas = p.createCanvas(windowWidth - 20, windowHeight - inputHeight - 100); // Adjust canvas height
canvas.parent('sketch-container');
canvas.position(10, 10);
p.textAlign(p.LEFT, p.TOP);
p.textSize(20);
p.fill(255);
};
p.draw = () => {
p.background(0);
for (let i = 0; i < textLines.length; i++) {
let textData = textLines[i];
let lines = textData.text.split('\n');
let y = calculateYPosition(i);
if (textData.isError) {
p.fill(255, 0, 0);
} else if (textData.isInput) {
p.fill(255);
} else {
p.fill(0, 190, 0);
}
for (let j = 0; j < lines.length; j++) {
let x = 20 + scrollOffset;
x += horizontalScrollOffset;
canvas.text(lines[j], x, y);
y += p.textAscent() + p.textDescent();
}
}
};
// [fix] calculate the y position based on the index, scrolling offset, and accumulated height
function calculateYPosition(index) {
let y = canvas.height - 30;
const lineHeight = p.textAscent() + p.textDescent();
for (let i = textLines.length - 1; i > index; i--) {
let lines = textLines[i].text.split('\n');
y -= lines.length * lineHeight;
}
y += p.lerp(scrollOffset * lineHeight, (scrollOffset + 1) * lineHeight, 0.1);
return y;
}
async function handleInput() {
userInput = input.value();
input.value('');
if (userInput.trim() !== '' && !isGenerating) {
// display user input
textLines.push({ text: userInput + '...' + '\n', isInput: true, isError: false });
isGenerating = true;
input.value('Generating...');
input.attribute('disabled', 'true'); // disable user input
await generateText(userInput);
isGenerating = false;
scrollToBottom();
input.value('');
input.removeAttribute('disabled');
}
}
function createLabel(labelText, x, y) {
const label = document.createElement('label');
label.innerText = labelText;
label.style.position = 'absolute';
label.style.left = `${x}px`;
label.style.top = `${y}px`;
label.style.color = 'white';
label.style.fontFamily = 'Arial, sans-serif';
document.getElementById('sketch-container').appendChild(label);
}
function updateInfo() {
infoDiv.innerHTML = `
<p><strong>Temperature:</strong> Controls randomness. Higher values (closer to 1) make output more random, lower values (closer to 0) make it more focused.</p>
<p><strong>Max Length:</strong> Specifies the maximum length of generated text in characters. Stops generation when this length is reached.</p>
<p><strong>Top-K:</strong> Limits vocabulary to the top-K most probable tokens. Controls diversity of generated text.</p>
<p><strong>Length Penalty:</strong> Influences text length. Values > 1 encourage longer outputs, < 1 encourage shorter outputs.</p>
<p><strong>No Repeat N-gram Size:</strong> Applies a penalty for repeating n-grams in the generated text. Prevents the repetition of specific sequences.</p>
`;
}
function toggleInfoVisibility() {
infoDiv.style.display = infoDiv.style.display === 'none' ? 'block' : 'none';
}
function scrollToBottom() {
scrollOffset = 0;
}
p.mouseWheel = (event) => {
scrollOffset -= event.delta / 100;
};
p.mouseDragged = () => {
if (
p.mouseX > 10 &&
p.mouseX < canvas.width - 10 &&
p.mouseY > 10 &&
p.mouseY < canvas.height - 10
) {
horizontalScrollOffset += p.mouseX - p.pmouseX;
}
};
};
new p5(sketch);
async function generateText(promptText) {
errorMessage = 'Server temporarily unavailable :( Please, try again in a few seconds!';
const HF_API_TOKEN = "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";
const model = "blasees/gpt2_chats_proposals";
let result;
const temperature = temperatureSlider.value();
const maxLength = maxLengthSlider.value();
const topK = topKSlider.value();
const lengthPenalty = lengthPenaltySlider.value();
const noRepeatNGram = noRepeatNGramSlider.value();
const data = {
inputs: promptText || " ",
parameters: {
temperature: temperature,
max_length: maxLength,
top_k: topK,
length_penalty: lengthPenalty,
num_beams: 5, // compare 5 beams
no_repeat_ngram_size: noRepeatNGram, // n-gram penalty (no 2-ngrams appear twice)
num_return_sequences: 1, // return just one beam
early_stopping: true,
}
};
try {
const response = await fetch(
`https://api-inference.huggingface.co/models/${model}`,
{
headers: { "Authorization": `Bearer ${HF_API_TOKEN}` },
method: "POST",
body: JSON.stringify(data),
}
);
if (!response.ok) {
throw new Error();
}
result = await response.json();
if (result) {
const generatedText = result[0]["generated_text"];
// Store the generated text without the prompt
textLines.push({ text: generatedText, isInput: false, isError: false }); // Generated text
} else {
throw new Error();
}
} catch (error) {
console.error('POST failed. Possible API still loading. If persists, check inference details.');
textLines.push({ text: errorMessage, isInput: false, isError: true });
}
}