-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
84 lines (77 loc) · 2.51 KB
/
index.html
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
<!doctype html>
<html>
<head>
<script src="https://cdn.jsdelivr.net/pyodide/v0.24.1/full/pyodide.js"></script>
</head>
<body>
<p>
You can run llama2-py in Pyodide
</p>
<ul>
<li>https://github.com/takano32/pyodide-llama2-py</li>
<li>https://github.com/pyodide/pyodide</li>
<li>https://github.com/tairov/llama2.py</li>
</ul>
<input id="code" style="width: 100%;" value="Dream comes true this day" />
<button onclick="evaluatePython()">Run</button>
<br />
<br />
<div>Output:</div>
<textarea id="output" style="width: 100%;" rows="23" disabled></textarea>
<script>
const output = document.getElementById("output");
const code = document.getElementById("code");
function addToOutput(s) {
output.value += "Prompt: " + code.value + "\n" + s + "\n";
}
output.value = "Initializing...\n";
// init Pyodide
async function main() {
let package_list = [
"llama2-py",
];
let pyodide = await loadPyodide();
await pyodide.loadPackage("micropip");
const micropip = pyodide.pyimport("micropip");
await micropip.install(package_list);
await pyodide.runPythonAsync([
`from pyodide.http import pyfetch`,
`stories_filename = "stories15M.bin"`,
`stories_url = "./" + stories_filename`,
`tokenizer_filename = "tokenizer.bin"`,
`tokenizer_url = "./" + tokenizer_filename`,
`async def download(url, filename):`,
` with open(filename, mode='wb') as f:`,
` res = await pyfetch(url)`,
` data = await res.bytes()`,
` f.write(data)`,
`await download(stories_url, stories_filename)`,
`await download(tokenizer_url, tokenizer_filename)`,
].join("\n"));
output.value += "Ready!\n";
return pyodide;
}
let pyodideReadyPromise = main();
async function evaluatePython() {
let pyodide = await pyodideReadyPromise;
try {
let output = await pyodide.runPython([
`import llama2_py`,
`import io`,
`from contextlib import redirect_stdout`,
``,
`prompt = "${code.value}"`,
`buf = io.StringIO()`,
`with redirect_stdout(buf):`,
` res = llama2_py.run({"checkpoint": "stories15M.bin", "temperature": 0.0, "steps": 256, "prompt": prompt})`,
`reply = buf.getvalue()`,
`reply`
].join("\n"));
addToOutput(output);
} catch (err) {
addToOutput(err);
}
}
</script>
</body>
</html>