forked from ariya/nano-jarvis
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.html
330 lines (281 loc) · 9.21 KB
/
index.html
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
<!DOCTYPE html>
<html lang="en" class="full-h">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Nano Jarvis</title>
</head>
<body class="full-h">
<main id="chat">
</main>
<footer>
<div class="input-container">
<input type="text" id="prompt" autocomplete="off" autofocus placeholder="Ask something"></textarea>
</div>
</footer>
<script>
document.addEventListener('DOMContentLoaded', function () {
const $ = (id) => document.getElementById(id);
const $div = (cls) => {
const el = document.createElement('div');
el.setAttribute('class', cls);
return el;
}
function message(type, text) {
const el = $div(`speech-bubble-${type} color-${type}`);
el.innerText = text || '';
const wrapper = $div(`speech speech-${type}`);
wrapper.appendChild(el);
$('chat').appendChild(wrapper);
setTimeout(() => {
el.scrollIntoView();
}, 0);
return el;
}
function stream(type, text) {
const selectors = document.querySelectorAll(`.speech-bubble-${type}`);
const el = selectors[selectors.length - 1] || message(type, text);
el.innerText = text || '';
setTimeout(() => {
el.scrollIntoView();
}, 0);
return el;
}
function unmessage(type) {
const el = document.querySelector(`.speech-${type}`);
el && el.remove();
}
const isTouchDevice = () => 'ontouchstart' in window;
// On a device with touch support (read: mobile), do not autofocus
// since it'll annoyingly trigger the on-screen keyboard
function focusInput() {
if (!isTouchDevice()) {
$('prompt').focus();
}
}
async function ask(question, handler) {
message('human', question);
$('prompt').blur();
const url = '/chat?' + encodeURIComponent(question);
const el = message('loader');
el.innerHTML = '<div class=loader></div>';
setTimeout(get, 100);
async function get() {
const response = await fetch(url);
message('assistant');
let answer = '';
try {
const reader = response.body.getReader();
while (true) {
const { done, value } = await reader.read();
unmessage('loader');
if (done) {
break;
}
const text = new TextDecoder().decode(value, { stream: true });
answer += text;
stream('assistant', answer);
}
} catch (e) {
message('panic', `Something is wrong: ${e.toString()}`);
} finally {
unmessage('loader');
handler && handler(answer);
setTimeout(focusInput, 0);
}
}
}
$('prompt').addEventListener('keydown', function handleKeyInput(event) {
if (event.key === 'Enter') {
const el = $('prompt');
const question = el.value.trim();
if (question.length > 0) {
ask(question);
el.value = '';
}
}
});
setTimeout(() => {
message('assistant', 'Hi, this is Nano Jarvis!');
}, 100);
const tests = [
'Who is the CEO of Google?',
'What is a dwarf planet?',
'Give an example!',
'List all terrestrial planets',
'Which is the closest to the Sun?',
'Which planet known as the red one?',
'When was solar system formed?',
'What materials compose the gas giants?',
'What about the ice giants',
'Explain the heliopause',
'When did Voyager 2 enter the interstellar space?',
'How about Voyager 1?'
];
async function verify() {
const question = tests.shift();
if (question && question.length > 0) {
await ask(question, () => setTimeout(verify, 300));
}
}
// setTimeout(verify, 1000);
});
</script>
<style>
html {
line-height: 1.5;
background-color: white;
font-family: ui-sans-serif, system-ui, -apple-system,
BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue",
Arial, "Noto Sans", sans-serif, "Apple Color Emoji",
"Segoe UI Emoji", "Segoe UI Symbol", "Noto Color Emoji";
}
.full-h {
margin: 0;
padding: 0;
height: 100%;
}
body {
display: flex;
flex-direction: column;
color: white;
color-scheme: light;
}
*,
::after,
::before {
box-sizing: border-box;
border-width: 0;
border-style: solid;
border-color: currentColor;
}
footer {
flex: none;
padding: 0;
}
main#chat {
padding: 1rem;
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
flex: auto;
}
.input-container {
display: flex;
justify-content: center;
align-items: center;
flex-direction: row;
}
input,
textarea {
font-family: inherit;
margin: 0;
color: white;
width: 100%;
height: 3rem;
padding-left: 1rem;
padding-right: 1rem;
font-size: 1rem;
line-height: 1.5rem;
border: 2px solid white;
background-color: black;
border-radius: .5rem;
}
input:focus,
textarea:focus {
outline: none;
}
input:disabled,
input[disabled] {
cursor: not-allowed;
}
.speech {
display: grid;
column-gap: .75rem;
padding-top: .25rem;
padding-bottom: .25rem;
}
.speech-loader,
.speech-assistant {
place-items: start;
}
.speech-human,
.speech-panic {
place-items: end;
}
.speech-bubble-loader,
.speech-bubble-assistant,
.speech-bubble-human,
.speech-bubble-panic {
display: block;
white-space: pre-line;
position: relative;
width: fit-content;
padding: .5rem 1rem;
min-height: 2.5rem;
min-width: 2.5rem;
max-width: 90%;
border-radius: 0.5rem;
border: none;
}
.speech-bubble-assistant {
border-color: #FBFFFE;
box-shadow: .1rem .1rem .2rem #C8D0CB;
}
.speech-bubble-human {
border-color: #127475;
}
.speech-bubble-panic {
border-color: #9B239D;
}
.color-assistant {
background-color: #FBFFFE;
color: #131614;
border: 1px solid #D3D9D5;
}
.color-human {
background-color: #127475;
color: #FBFFFE;
}
.color-panic {
background-color: #9B239D;
color: #ddd;
}
div#input {
position: fixed;
width: 100%;
left: 0;
bottom: 2px;
padding: 0px;
}
.loader {
width: 2em;
aspect-ratio: 2;
background:
no-repeat linear-gradient(#000 0 0),
no-repeat linear-gradient(#000 0 0),
no-repeat linear-gradient(#000 0 0);
background-size: 20% 50%;
animation: load 1s infinite linear;
}
@keyframes load {
0% {
background-position: 0% 100%, 50% 100%, 100% 100%
}
20% {
background-position: 0% 50%, 50% 100%, 100% 100%
}
40% {
background-position: 0% 0%, 50% 50%, 100% 100%
}
60% {
background-position: 0% 100%, 50% 0%, 100% 50%
}
80% {
background-position: 0% 100%, 50% 100%, 100% 0%
}
100% {
background-position: 0% 100%, 50% 100%, 100% 100%
}
}
</style>
</body>
</html>