File size: 2,253 Bytes
fac66ea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const { createApp, ref, onMounted } = Vue;
import { HfInference } from "https://cdn.skypack.dev/@huggingface/[email protected]";

const app = createApp({
    setup() {
        const token = ref(localStorage.getItem("token") || "");
        const userPrompt = ref("Write an essay about Star Wars");
        const generatedText = ref("");
        let controller;

        const createTextGenerationStream = (hfInstance, prompt, abortControllerSignal) => {
            return hfInstance.textGenerationStream(
                {
                    model: "mistralai/Mistral-7B-Instruct-v0.1",
                    inputs: `[INST]${prompt}[/INST]`,
                    parameters: { max_new_tokens: 450 },
                },
                {
                    use_cache: false,
                    signal: abortControllerSignal,
                }
            );
        };

        const generateTextStream = async function* (hfInstance, abortSignal, prompt) {
            let textFragments = [];
            for await (const output of createTextGenerationStream(hfInstance, prompt, abortSignal)) {
                textFragments.push(output.token.text);
                generatedText.value += output.token.text;
                yield;
            }
        };

        const run = async () => {
            controller = new AbortController();
            localStorage.setItem("token", token.value);
            const hfInstance = new HfInference(token.value);

            try {
                for await (const text of generateTextStream(
                    hfInstance,
                    controller.signal,
                    userPrompt.value
                )) {
                    console.log(text);
                }
            } catch (e) {
                console.log(e);
            }
        };

        const stop = () => {
            if (controller) {
                controller.abort();
            }
        };

        onMounted(() => {
            if (localStorage.getItem("token")) {
                token.value = localStorage.getItem("token");
            }
        });

        return {
            token,
            userPrompt,
            generatedText,
            run,
            stop,
        };
    },
});

app.mount("#app");