Arsala Grey
commited on
Commit
·
fac66ea
1
Parent(s):
0d92137
refactored code
Browse files- index.html +6 -78
- main.js +74 -0
index.html
CHANGED
|
@@ -6,97 +6,25 @@
|
|
| 6 |
<title>My static Space</title>
|
| 7 |
<link rel="stylesheet" href="./style.css" />
|
| 8 |
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
| 9 |
-
<script type="module">
|
| 10 |
-
const { createApp, ref, onMounted } = Vue;
|
| 11 |
-
import { HfInference } from "https://cdn.skypack.dev/@huggingface/[email protected]";
|
| 12 |
-
|
| 13 |
-
const app = createApp({
|
| 14 |
-
setup() {
|
| 15 |
-
const title = ref("Mistral-7B-Instruct-v0.1");
|
| 16 |
-
const subtitle = ref("huggingface.js");
|
| 17 |
-
const token = ref(localStorage.getItem("token") || "");
|
| 18 |
-
const inputText = ref("Write an essay about Star Wars");
|
| 19 |
-
const generatedText = ref("");
|
| 20 |
-
let controller;
|
| 21 |
-
|
| 22 |
-
const textStreamRes = async function* (hf, controller, input) {
|
| 23 |
-
let tokens = [];
|
| 24 |
-
for await (const output of hf.textGenerationStream(
|
| 25 |
-
{
|
| 26 |
-
model: "mistralai/Mistral-7B-Instruct-v0.1",
|
| 27 |
-
inputs: `[INST]${input}[/INST]`,
|
| 28 |
-
parameters: { max_new_tokens: 450 },
|
| 29 |
-
},
|
| 30 |
-
{
|
| 31 |
-
use_cache: false,
|
| 32 |
-
signal: controller.signal,
|
| 33 |
-
}
|
| 34 |
-
)) {
|
| 35 |
-
tokens.push(output.token.text);
|
| 36 |
-
generatedText.value += output.token.text;
|
| 37 |
-
yield;
|
| 38 |
-
}
|
| 39 |
-
};
|
| 40 |
-
|
| 41 |
-
const run = async () => {
|
| 42 |
-
controller = new AbortController();
|
| 43 |
-
localStorage.setItem("token", token.value);
|
| 44 |
-
const hf = new HfInference(token.value);
|
| 45 |
-
|
| 46 |
-
try {
|
| 47 |
-
for await (const text of textStreamRes(
|
| 48 |
-
hf,
|
| 49 |
-
controller,
|
| 50 |
-
inputText.value
|
| 51 |
-
)) {
|
| 52 |
-
console.log(text);
|
| 53 |
-
}
|
| 54 |
-
} catch (e) {
|
| 55 |
-
console.log(e);
|
| 56 |
-
}
|
| 57 |
-
};
|
| 58 |
-
|
| 59 |
-
const abort = () => {
|
| 60 |
-
if (controller) {
|
| 61 |
-
controller.abort();
|
| 62 |
-
}
|
| 63 |
-
};
|
| 64 |
-
|
| 65 |
-
onMounted(() => {
|
| 66 |
-
if (localStorage.getItem("token")) {
|
| 67 |
-
token.value = localStorage.getItem("token");
|
| 68 |
-
}
|
| 69 |
-
});
|
| 70 |
-
|
| 71 |
-
return {
|
| 72 |
-
title,
|
| 73 |
-
subtitle,
|
| 74 |
-
token,
|
| 75 |
-
inputText,
|
| 76 |
-
generatedText,
|
| 77 |
-
run,
|
| 78 |
-
abort,
|
| 79 |
-
};
|
| 80 |
-
},
|
| 81 |
-
}).mount("#app");
|
| 82 |
-
</script>
|
| 83 |
</head>
|
| 84 |
<body>
|
| 85 |
<div id="app">
|
| 86 |
<header>
|
| 87 |
-
<h1>
|
| 88 |
-
<h2>{{subtitle}}</h2>
|
| 89 |
</header>
|
|
|
|
| 90 |
<input v-model="token" placeholder="HF-TOKEN" type="password" />
|
|
|
|
| 91 |
<textarea
|
| 92 |
-
v-model="
|
| 93 |
style="width: 100%; height: 100px"
|
| 94 |
></textarea>
|
| 95 |
<div>
|
| 96 |
<button @click="run">GENERATE</button>
|
| 97 |
-
<button @click="
|
| 98 |
</div>
|
| 99 |
<div>{{generatedText}}</div>
|
| 100 |
</div>
|
|
|
|
| 101 |
</body>
|
| 102 |
</html>
|
|
|
|
| 6 |
<title>My static Space</title>
|
| 7 |
<link rel="stylesheet" href="./style.css" />
|
| 8 |
<script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
</head>
|
| 10 |
<body>
|
| 11 |
<div id="app">
|
| 12 |
<header>
|
| 13 |
+
<h1>Generate Text With Mistral-7B-Instruct-v0.1</h1>
|
|
|
|
| 14 |
</header>
|
| 15 |
+
<h3>Hugging Face Token</h3>
|
| 16 |
<input v-model="token" placeholder="HF-TOKEN" type="password" />
|
| 17 |
+
<h3>Prompt</h3>
|
| 18 |
<textarea
|
| 19 |
+
v-model="userPrompt"
|
| 20 |
style="width: 100%; height: 100px"
|
| 21 |
></textarea>
|
| 22 |
<div>
|
| 23 |
<button @click="run">GENERATE</button>
|
| 24 |
+
<button @click="stop">STOP</button>
|
| 25 |
</div>
|
| 26 |
<div>{{generatedText}}</div>
|
| 27 |
</div>
|
| 28 |
+
<script type="module" src="./main.js"></script>
|
| 29 |
</body>
|
| 30 |
</html>
|
main.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
const { createApp, ref, onMounted } = Vue;
|
| 2 |
+
import { HfInference } from "https://cdn.skypack.dev/@huggingface/[email protected]";
|
| 3 |
+
|
| 4 |
+
const app = createApp({
|
| 5 |
+
setup() {
|
| 6 |
+
const token = ref(localStorage.getItem("token") || "");
|
| 7 |
+
const userPrompt = ref("Write an essay about Star Wars");
|
| 8 |
+
const generatedText = ref("");
|
| 9 |
+
let controller;
|
| 10 |
+
|
| 11 |
+
const createTextGenerationStream = (hfInstance, prompt, abortControllerSignal) => {
|
| 12 |
+
return hfInstance.textGenerationStream(
|
| 13 |
+
{
|
| 14 |
+
model: "mistralai/Mistral-7B-Instruct-v0.1",
|
| 15 |
+
inputs: `[INST]${prompt}[/INST]`,
|
| 16 |
+
parameters: { max_new_tokens: 450 },
|
| 17 |
+
},
|
| 18 |
+
{
|
| 19 |
+
use_cache: false,
|
| 20 |
+
signal: abortControllerSignal,
|
| 21 |
+
}
|
| 22 |
+
);
|
| 23 |
+
};
|
| 24 |
+
|
| 25 |
+
const generateTextStream = async function* (hfInstance, abortSignal, prompt) {
|
| 26 |
+
let textFragments = [];
|
| 27 |
+
for await (const output of createTextGenerationStream(hfInstance, prompt, abortSignal)) {
|
| 28 |
+
textFragments.push(output.token.text);
|
| 29 |
+
generatedText.value += output.token.text;
|
| 30 |
+
yield;
|
| 31 |
+
}
|
| 32 |
+
};
|
| 33 |
+
|
| 34 |
+
const run = async () => {
|
| 35 |
+
controller = new AbortController();
|
| 36 |
+
localStorage.setItem("token", token.value);
|
| 37 |
+
const hfInstance = new HfInference(token.value);
|
| 38 |
+
|
| 39 |
+
try {
|
| 40 |
+
for await (const text of generateTextStream(
|
| 41 |
+
hfInstance,
|
| 42 |
+
controller.signal,
|
| 43 |
+
userPrompt.value
|
| 44 |
+
)) {
|
| 45 |
+
console.log(text);
|
| 46 |
+
}
|
| 47 |
+
} catch (e) {
|
| 48 |
+
console.log(e);
|
| 49 |
+
}
|
| 50 |
+
};
|
| 51 |
+
|
| 52 |
+
const stop = () => {
|
| 53 |
+
if (controller) {
|
| 54 |
+
controller.abort();
|
| 55 |
+
}
|
| 56 |
+
};
|
| 57 |
+
|
| 58 |
+
onMounted(() => {
|
| 59 |
+
if (localStorage.getItem("token")) {
|
| 60 |
+
token.value = localStorage.getItem("token");
|
| 61 |
+
}
|
| 62 |
+
});
|
| 63 |
+
|
| 64 |
+
return {
|
| 65 |
+
token,
|
| 66 |
+
userPrompt,
|
| 67 |
+
generatedText,
|
| 68 |
+
run,
|
| 69 |
+
stop,
|
| 70 |
+
};
|
| 71 |
+
},
|
| 72 |
+
});
|
| 73 |
+
|
| 74 |
+
app.mount("#app");
|