File size: 3,247 Bytes
5f30b16 1c59795 9934aaf 1c59795 9934aaf 1c59795 c6e4b96 1c59795 c6e4b96 1c59795 c6e4b96 1c59795 c6e4b96 1c59795 9934aaf 1c59795 9934aaf 1c59795 c6e4b96 1c59795 c6e4b96 |
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 |
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Text generation huggingface.js Mistral-7B-Instruct-v0.1</title>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="container mx-auto p-3 max-w-xl">
<script type="module">
import { HfInference } from "https://cdn.skypack.dev/@huggingface/[email protected]";
async function* textStreamRes(hf, controller, input) {
let tokens = [];
for await (const output of hf.textGenerationStream(
{
model: "mistralai/Mistral-7B-Instruct-v0.1",
inputs: "<s>[INST]Write an essay about Sartre[/INST]",
parameters: { max_new_tokens: 1000 },
},
{
use_cache: false,
signal: controller.signal,
}
)) {
tokens.push(output);
yield tokens;
}
}
let controller;
async function run() {
controller = new AbortController();
const message = `<s>[INST]{:}[/INST]`;
const textInput = document.querySelector("#input").value;
const input = message.replace("{:}", textInput);
const token = document.querySelector("#token").value;
const hf = new HfInference(token);
const gen = document.querySelector("#generation");
gen.innerHTML = "";
try {
for await (const tokens of textStreamRes(hf, controller, input)) {
const lastToken = tokens[tokens.length - 1];
const span = document.createElement("span");
span.innerText = lastToken.token.text;
gen.appendChild(span);
}
} catch (e) {
console.log("aborted");
}
}
document.addEventListener("DOMContentLoaded", () => {
const token = localStorage.getItem("token");
if (token) {
document.querySelector("#token").value = token;
}
});
document.querySelector("#token").addEventListener("change", (e) => {
localStorage.setItem("token", e.target.value);
});
document.querySelector("#run").addEventListener("click", run);
document.querySelector("#abort").addEventListener("click", () => {
controller.abort();
});
</script>
<div class="grid grid-cols-1 gap-2">
<header>
<h1 class="text-3xl">Mistral-7B-Instruct-v0.1</h1>
<h2 class="text-xl">huggingface.js</h2>
</header>
<input
id="token"
class="border-2 border-gray-500 rounded-md"
placeholder="HF-TOKEN"
type="password"
/>
<textarea
id="input"
class="border-2 border-gray-500 rounded-md"
style="width: 100%; height: 100px"
>
Write an essay about Sartre</textarea
>
<div class="flex gap-2">
<button
id="run"
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
>
GENERATE
</button>
<button
id="abort"
class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
>
ABORT
</button>
</div>
</div>
<div id="generation" class="py-3"></div>
</body>
</html> |