radames commited on
Commit
1c59795
1 Parent(s): 5f30b16

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +92 -17
index.html CHANGED
@@ -1,19 +1,94 @@
1
  <!DOCTYPE html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
  <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8" />
5
+ <title>Home</title>
6
+ <script src="https://cdn.tailwindcss.com"></script>
7
+ </head>
8
+ <body class="container mx-auto p-3">
9
+ <script type="module">
10
+ import { HfInference } from "https://cdn.skypack.dev/@huggingface/inference@2.6.3";
11
+ async function* textStreamRes(hf, controller, input) {
12
+ let tokens = [];
13
+ for await (const output of hf.textGenerationStream(
14
+ {
15
+ model: "mistralai/Mistral-7B-Instruct-v0.1",
16
+ inputs: input,
17
+ parameters: { max_new_tokens: 1000 },
18
+ },
19
+ {
20
+ use_cache: false,
21
+ fetch: (url, options) =>
22
+ fetch(url, { ...options, signal: controller.signal }),
23
+ }
24
+ )) {
25
+ tokens.push(output);
26
+ yield tokens;
27
+ }
28
+ }
29
+
30
+ let controller;
31
+ async function run() {
32
+ controller = new AbortController();
33
+ const message = `<s>[INST]{:}[/INST]`;
34
+ const textInput = document.querySelector("#input").value;
35
+ const input = message.replace("{:}", textInput);
36
+ const token = document.querySelector("#token").value;
37
+ const hf = new HfInference(token);
38
+
39
+ const gen = document.querySelector("#generation");
40
+ gen.innerHTML = "";
41
+ const textStream = textStreamRes(hf, controller, input);
42
+ for await (const tokens of textStream) {
43
+ const lastToken = tokens[tokens.length - 1];
44
+ const span = document.createElement("span");
45
+ span.innerText = lastToken.token.text;
46
+ gen.appendChild(span);
47
+ }
48
+ }
49
+ document.addEventListener("DOMContentLoaded", () => {
50
+ const token = localStorage.getItem("token");
51
+ if (token) {
52
+ document.querySelector("#token").value = token;
53
+ }
54
+ });
55
+ document.querySelector("#token").addEventListener("change", (e)=> {
56
+ localStorage.setItem("token", e.target.value);
57
+ });
58
+ document.querySelector("#run").addEventListener("click", run);
59
+ document.querySelector("#abort").addEventListener("click", () => {
60
+ controller.abort();
61
+ });
62
+ </script>
63
+ <div>
64
+ <input
65
+ id="token"
66
+ class="border-2 border-gray-500 rounded-md"
67
+ placeholder="HF-TOKEN"
68
+ type="password"
69
+ />
70
+ <textarea
71
+ id="input"
72
+ class="border-2 border-gray-500 rounded-md"
73
+ style="width: 100%; height: 100px"
74
+ >
75
+ Write an essay about Sartre</textarea
76
+ >
77
+ <div class="flex">
78
+ <button
79
+ id="run"
80
+ class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"
81
+ >
82
+ GENERATE
83
+ </button>
84
+ <button
85
+ id="abort"
86
+ class="bg-red-500 hover:bg-red-700 text-white font-bold py-2 px-4 rounded"
87
+ >
88
+ ABORT
89
+ </button>
90
+ </div>
91
+ </div>
92
+ <div id="generation" class="py-3"></div>
93
+ </body>
94
  </html>