Chat3 try?
Browse files- chat-copil2-web.html +0 -1
- chat3.html +178 -0
chat-copil2-web.html
CHANGED
|
@@ -64,7 +64,6 @@ const statusEl = document.getElementById('status');
|
|
| 64 |
const toastEl = document.getElementById('toast');
|
| 65 |
const tokenModal = document.getElementById('token-modal');
|
| 66 |
const tokenInput = document.getElementById('token-input');
|
| 67 |
-
hideTokenModal();
|
| 68 |
|
| 69 |
let state = { pipe:null, modelId:null, task:'text-generation' };
|
| 70 |
const savedToken = localStorage.getItem('hf_token'); if(savedToken){env.HF_TOKEN=savedToken;}
|
|
|
|
| 64 |
const toastEl = document.getElementById('toast');
|
| 65 |
const tokenModal = document.getElementById('token-modal');
|
| 66 |
const tokenInput = document.getElementById('token-input');
|
|
|
|
| 67 |
|
| 68 |
let state = { pipe:null, modelId:null, task:'text-generation' };
|
| 69 |
const savedToken = localStorage.getItem('hf_token'); if(savedToken){env.HF_TOKEN=savedToken;}
|
chat3.html
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="en">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8" />
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
|
| 6 |
+
<title>AI Chat App</title>
|
| 7 |
+
<style>
|
| 8 |
+
body {
|
| 9 |
+
margin: 0;
|
| 10 |
+
font-family: sans-serif;
|
| 11 |
+
display: flex;
|
| 12 |
+
flex-direction: column;
|
| 13 |
+
height: 100vh;
|
| 14 |
+
background: #f5f5f5;
|
| 15 |
+
}
|
| 16 |
+
header {
|
| 17 |
+
padding: 10px;
|
| 18 |
+
background: #333;
|
| 19 |
+
color: white;
|
| 20 |
+
text-align: center;
|
| 21 |
+
}
|
| 22 |
+
#model-select {
|
| 23 |
+
width: 100%;
|
| 24 |
+
padding: 10px;
|
| 25 |
+
font-size: 1em;
|
| 26 |
+
}
|
| 27 |
+
#chat {
|
| 28 |
+
flex: 1;
|
| 29 |
+
overflow-y: auto;
|
| 30 |
+
padding: 10px;
|
| 31 |
+
background: #fff;
|
| 32 |
+
}
|
| 33 |
+
.message {
|
| 34 |
+
margin: 10px 0;
|
| 35 |
+
padding: 10px;
|
| 36 |
+
border-radius: 5px;
|
| 37 |
+
max-width: 80%;
|
| 38 |
+
}
|
| 39 |
+
.user {
|
| 40 |
+
background: #d1e7dd;
|
| 41 |
+
align-self: flex-end;
|
| 42 |
+
}
|
| 43 |
+
.bot {
|
| 44 |
+
background: #e2e3e5;
|
| 45 |
+
align-self: flex-start;
|
| 46 |
+
}
|
| 47 |
+
#input-area {
|
| 48 |
+
display: flex;
|
| 49 |
+
padding: 10px;
|
| 50 |
+
background: #eee;
|
| 51 |
+
}
|
| 52 |
+
#user-input {
|
| 53 |
+
flex: 1;
|
| 54 |
+
padding: 10px;
|
| 55 |
+
font-size: 1em;
|
| 56 |
+
}
|
| 57 |
+
#send-btn {
|
| 58 |
+
padding: 10px;
|
| 59 |
+
font-size: 1em;
|
| 60 |
+
}
|
| 61 |
+
#status {
|
| 62 |
+
text-align: center;
|
| 63 |
+
padding: 5px;
|
| 64 |
+
font-size: 0.9em;
|
| 65 |
+
color: #555;
|
| 66 |
+
}
|
| 67 |
+
</style>
|
| 68 |
+
</head>
|
| 69 |
+
<body>
|
| 70 |
+
<header>
|
| 71 |
+
<h1>AI Chat App</h1>
|
| 72 |
+
</header>
|
| 73 |
+
<select id="model-select">
|
| 74 |
+
<option value="Xenova/distilgpt2">Xenova/distilgpt2</option>
|
| 75 |
+
<option value="Xenova/phi-3-mini-4k-instruct">Xenova/phi-3-mini-4k-instruct</option>
|
| 76 |
+
<option value="Xenova/t5-small">Xenova/t5-small</option>
|
| 77 |
+
<option value="Xenova/gemma-2b-it">Xenova/gemma-2b-it</option>
|
| 78 |
+
<option value="Xenova/llama-3-8b-instruct">Xenova/llama-3-8b-instruct</option>
|
| 79 |
+
<option value="Xenova/Mistral-7B-Instruct-v0.2">Xenova/Mistral-7B-Instruct-v0.2</option>
|
| 80 |
+
</select>
|
| 81 |
+
<div id="status">Select a model to begin</div>
|
| 82 |
+
<div id="chat"></div>
|
| 83 |
+
<div id="input-area">
|
| 84 |
+
<input type="text" id="user-input" placeholder="Type your message..." />
|
| 85 |
+
<button id="send-btn">Send</button>
|
| 86 |
+
</div>
|
| 87 |
+
|
| 88 |
+
<script type="module">
|
| 89 |
+
const chat = document.getElementById('chat');
|
| 90 |
+
const input = document.getElementById('user-input');
|
| 91 |
+
const sendBtn = document.getElementById('send-btn');
|
| 92 |
+
const modelSelect = document.getElementById('model-select');
|
| 93 |
+
const status = document.getElementById('status');
|
| 94 |
+
|
| 95 |
+
let worker;
|
| 96 |
+
let busy = false;
|
| 97 |
+
|
| 98 |
+
function appendMessage(text, sender) {
|
| 99 |
+
const msg = document.createElement('div');
|
| 100 |
+
msg.className = `message ${sender}`;
|
| 101 |
+
msg.textContent = text;
|
| 102 |
+
chat.appendChild(msg);
|
| 103 |
+
chat.scrollTop = chat.scrollHeight;
|
| 104 |
+
}
|
| 105 |
+
|
| 106 |
+
function setStatus(text) {
|
| 107 |
+
status.textContent = text;
|
| 108 |
+
}
|
| 109 |
+
|
| 110 |
+
function initWorker(model) {
|
| 111 |
+
if (worker) worker.terminate();
|
| 112 |
+
setStatus('Loading model...');
|
| 113 |
+
busy = true;
|
| 114 |
+
worker = new Worker(URL.createObjectURL(new Blob([`
|
| 115 |
+
importScripts('https://cdn.jsdelivr.net/npm/@xenova/transformers@2.6.0/dist/transformers.min.js');
|
| 116 |
+
|
| 117 |
+
let pipeline;
|
| 118 |
+
self.onmessage = async (e) => {
|
| 119 |
+
const { type, data } = e.data;
|
| 120 |
+
try {
|
| 121 |
+
if (type === 'load') {
|
| 122 |
+
pipeline = await window.transformers.pipeline('text-generation', data.model, { progress_callback: (x) => self.postMessage({ type: 'progress', data: x }) });
|
| 123 |
+
self.postMessage({ type: 'ready' });
|
| 124 |
+
} else if (type === 'generate') {
|
| 125 |
+
const output = await pipeline(data.text, { max_new_tokens: 100 });
|
| 126 |
+
self.postMessage({ type: 'response', data: output[0].generated_text });
|
| 127 |
+
}
|
| 128 |
+
} catch (err) {
|
| 129 |
+
self.postMessage({ type: 'error', data: err.stack });
|
| 130 |
+
}
|
| 131 |
+
};
|
| 132 |
+
`], { type: 'application/javascript' })));
|
| 133 |
+
|
| 134 |
+
worker.onmessage = (e) => {
|
| 135 |
+
const { type, data } = e.data;
|
| 136 |
+
if (type === 'ready') {
|
| 137 |
+
setStatus('Model ready');
|
| 138 |
+
busy = false;
|
| 139 |
+
} else if (type === 'progress') {
|
| 140 |
+
setStatus('Loading... ' + Math.round(data * 100) + '%');
|
| 141 |
+
} else if (type === 'response') {
|
| 142 |
+
appendMessage(data, 'bot');
|
| 143 |
+
setStatus('Model ready');
|
| 144 |
+
busy = false;
|
| 145 |
+
} else if (type === 'error') {
|
| 146 |
+
appendMessage('Error: ' + data, 'bot');
|
| 147 |
+
setStatus('Error occurred');
|
| 148 |
+
busy = false;
|
| 149 |
+
}
|
| 150 |
+
};
|
| 151 |
+
|
| 152 |
+
worker.postMessage({ type: 'load', data: { model } });
|
| 153 |
+
}
|
| 154 |
+
|
| 155 |
+
modelSelect.addEventListener('change', () => {
|
| 156 |
+
initWorker(modelSelect.value);
|
| 157 |
+
});
|
| 158 |
+
|
| 159 |
+
sendBtn.addEventListener('click', () => {
|
| 160 |
+
if (busy) return;
|
| 161 |
+
const text = input.value.trim();
|
| 162 |
+
if (!text) return;
|
| 163 |
+
appendMessage(text, 'user');
|
| 164 |
+
input.value = '';
|
| 165 |
+
setStatus('Generating response...');
|
| 166 |
+
busy = true;
|
| 167 |
+
worker.postMessage({ type: 'generate', data: { text } });
|
| 168 |
+
});
|
| 169 |
+
|
| 170 |
+
input.addEventListener('keydown', (e) => {
|
| 171 |
+
if (e.key === 'Enter') sendBtn.click();
|
| 172 |
+
});
|
| 173 |
+
|
| 174 |
+
// Load default model
|
| 175 |
+
initWorker(modelSelect.value);
|
| 176 |
+
</script>
|
| 177 |
+
</body>
|
| 178 |
+
</html>
|