File size: 2,929 Bytes
e89d22b
431b4de
f8b5fef
081a4b3
 
 
 
 
 
 
 
 
f8b5fef
 
 
 
 
 
 
 
 
081a4b3
431b4de
081a4b3
 
 
 
f8b5fef
 
 
 
 
e89d22b
f8b5fef
081a4b3
 
 
 
 
 
 
 
 
e89d22b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8b5fef
081a4b3
 
f8b5fef
081a4b3
 
 
431b4de
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
import { createRepo, commit } from "@huggingface/hub";
const c = console;
const ENDPOINT = "https://huggingface.co";
async function whoami(token) {
    const path = `${ENDPOINT}/api/whoami-v2`;
    const res = await fetch(path, {
        headers: {
            Authorization: `Bearer ${token}`,
        }
    });
    return await res.json();
}
const FILES_TO_UPLOAD = [
    "./mobilenet/model.json",
    "./mobilenet/group1-shard1of2",
    "./mobilenet/group1-shard2of2",
    "./mobilenet/coffee.jpg",
    "./mobilenet/README.md",
];
function filenameFromURL(url) {
    return url.substring(url.lastIndexOf("/") + 1);
}
window.addEventListener("load", function () {
    const tokenEl = document.querySelector("#token");
    const repoNameEl = document.querySelector("#repo_name");
    const button = document.querySelector("#submit");
    const output = document.querySelector("#logs");
    const storedToken = window.localStorage.getItem("hf_token");
    if (storedToken) {
        tokenEl.value = storedToken;
        /// ^to help in dev.
    }
    repoNameEl.value = `tfjs-mobilenet-${Date.now() % 1_000}`;
    /// "random" repo name
    button.addEventListener("click", async function () {
        const token = tokenEl.value;
        const repoName = repoNameEl.value;
        if (!token || !repoName) {
            alert("You need a token and a repo name");
            return;
        }
        button.setAttribute("disabled", "disabled");
        try {
            const { name: username } = await whoami(token);
            const name = `${username}/${repoName}`;
            await createRepo({
                repo: {
                    type: "model",
                    name,
                },
                credentials: {
                    accessToken: token,
                }
            });
            const operations = await Promise.all(FILES_TO_UPLOAD.map(async (file) => {
                return {
                    operation: "addOrUpdate",
                    path: filenameFromURL(file),
                    content: await (await fetch(file)).blob(),
                };
            }));
            const commitOutput = await commit({
                repo: {
                    type: "model",
                    name,
                },
                credentials: {
                    accessToken: token,
                },
                title: "upload model",
                operations,
            });
            c.log(commitOutput);
            const fullUrl = `${ENDPOINT}/${name}`;
            /// ^TODO(get it from the createRepo call)
            button.insertAdjacentHTML("afterend", `<div class="text-green-500 mb-6">🎉 Upload complete! Model page is <a target="_blank" class="text-bold underline" href="${fullUrl}">${fullUrl}</a></div>`);
        }
        catch (err) {
            output.append("\n" + err);
        }
        button.removeAttribute("disabled");
    });
});