Spaces:
Running
Running
Delete app.ts
Browse files
app.ts
DELETED
@@ -1,91 +0,0 @@
|
|
1 |
-
import { createRepo, commit, CommitFile, whoAmI } from "@huggingface/hub";
|
2 |
-
|
3 |
-
const c = console;
|
4 |
-
|
5 |
-
const FILES_TO_UPLOAD = [
|
6 |
-
`${window.location.origin}/mobilenet/model.json`,
|
7 |
-
`${window.location.origin}/mobilenet/group1-shard1of2`,
|
8 |
-
`${window.location.origin}/mobilenet/group1-shard2of2`,
|
9 |
-
`${window.location.origin}/mobilenet/coffee.jpg`,
|
10 |
-
`${window.location.origin}/mobilenet/README.md`,
|
11 |
-
];
|
12 |
-
|
13 |
-
function filenameFromURL(url: string): string {
|
14 |
-
return url.substring(url.lastIndexOf("/") + 1);
|
15 |
-
}
|
16 |
-
|
17 |
-
window.document.addEventListener("DOMContentLoaded", () => {
|
18 |
-
const tokenEl = document.querySelector<HTMLInputElement>("#token")!;
|
19 |
-
const repoNameEl = document.querySelector<HTMLInputElement>("#repo_name")!;
|
20 |
-
const button = document.querySelector("#submit")!;
|
21 |
-
const output = document.querySelector("#logs")!;
|
22 |
-
const form = document.getElementsByTagName("form")[0];
|
23 |
-
|
24 |
-
const storedToken = window.localStorage.getItem("hf_token");
|
25 |
-
if (storedToken) {
|
26 |
-
tokenEl.value = storedToken;
|
27 |
-
/// ^to help in dev.
|
28 |
-
}
|
29 |
-
|
30 |
-
repoNameEl.value = `tfjs-mobilenet-${Date.now() % 1_000}`;
|
31 |
-
/// "random" repo name
|
32 |
-
|
33 |
-
form.addEventListener("submit", async (event) => {
|
34 |
-
event.preventDefault();
|
35 |
-
|
36 |
-
const token = tokenEl.value;
|
37 |
-
const repoName = repoNameEl.value;
|
38 |
-
if (!token || !repoName) {
|
39 |
-
alert("You need a token and a repo name");
|
40 |
-
return;
|
41 |
-
}
|
42 |
-
|
43 |
-
button.setAttribute("disabled", "disabled");
|
44 |
-
|
45 |
-
const credentials = {
|
46 |
-
accessToken: token,
|
47 |
-
};
|
48 |
-
|
49 |
-
try {
|
50 |
-
const { name: username } = await whoAmI({ credentials });
|
51 |
-
const name = `${username}/${repoName}`;
|
52 |
-
const { repoUrl } = await createRepo({
|
53 |
-
repo: {
|
54 |
-
type: "model",
|
55 |
-
name,
|
56 |
-
},
|
57 |
-
credentials,
|
58 |
-
});
|
59 |
-
|
60 |
-
const operations: CommitFile[] = await Promise.all(
|
61 |
-
FILES_TO_UPLOAD.map(async (file) => {
|
62 |
-
return {
|
63 |
-
operation: "addOrUpdate",
|
64 |
-
path: filenameFromURL(file),
|
65 |
-
// upload remote file
|
66 |
-
content: new URL(file),
|
67 |
-
};
|
68 |
-
})
|
69 |
-
);
|
70 |
-
const commitOutput = await commit({
|
71 |
-
repo: {
|
72 |
-
type: "model",
|
73 |
-
name,
|
74 |
-
},
|
75 |
-
credentials,
|
76 |
-
title: "upload model",
|
77 |
-
operations,
|
78 |
-
});
|
79 |
-
c.log(commitOutput);
|
80 |
-
|
81 |
-
button.insertAdjacentHTML(
|
82 |
-
"afterend",
|
83 |
-
`<div class="text-green-500 mb-6">🎉 Upload complete! Model page is <a target="_blank" class="text-bold underline" href="${repoUrl}">${repoUrl}</a></div>`
|
84 |
-
);
|
85 |
-
} catch (err) {
|
86 |
-
console.error(err);
|
87 |
-
output.append("\n" + err);
|
88 |
-
}
|
89 |
-
button.removeAttribute("disabled");
|
90 |
-
});
|
91 |
-
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|