Spaces:
Running
Running
Working PoC
Browse files- app.ts +74 -10
- dist/app.js +60 -9
- index.html +0 -1
- mobilenet/README.md +12 -0
- mobilenet/coffee.jpg +0 -0
app.ts
CHANGED
|
@@ -1,22 +1,23 @@
|
|
| 1 |
const c = console;
|
| 2 |
|
| 3 |
-
|
| 4 |
-
const ENDPOINT = "http://localhost:5564";
|
| 5 |
|
| 6 |
async function whoami(token: string): Promise<{ name: string }> {
|
| 7 |
const path = `${ENDPOINT}/api/whoami-v2`;
|
| 8 |
const res = await fetch(path, {
|
| 9 |
headers: {
|
| 10 |
Authorization: `Bearer ${token}`,
|
| 11 |
-
Origin: `127.0.0.1:8000`
|
| 12 |
}
|
| 13 |
});
|
| 14 |
return await res.json();
|
| 15 |
}
|
| 16 |
|
|
|
|
|
|
|
|
|
|
| 17 |
async function createRepo(
|
| 18 |
token: string,
|
| 19 |
-
|
| 20 |
repoType: "model" | "dataset" | "space",
|
| 21 |
): Promise<string> {
|
| 22 |
const path = `${ENDPOINT}/api/repos/create`;
|
|
@@ -24,13 +25,59 @@ async function createRepo(
|
|
| 24 |
method: "POST",
|
| 25 |
headers: {
|
| 26 |
Authorization: `Bearer ${token}`,
|
|
|
|
| 27 |
},
|
| 28 |
body: JSON.stringify({
|
| 29 |
-
name:
|
| 30 |
type: repoType,
|
| 31 |
})
|
| 32 |
});
|
| 33 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
}
|
| 35 |
|
| 36 |
window.addEventListener("load", function () {
|
|
@@ -38,7 +85,16 @@ window.addEventListener("load", function () {
|
|
| 38 |
const repoNameEl = document.querySelector<HTMLInputElement>("#repo_name")!;
|
| 39 |
const button = document.querySelector("#submit")!;
|
| 40 |
const output = document.querySelector("#logs")!;
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
button.addEventListener("click", async function () {
|
| 43 |
const token = tokenEl.value;
|
| 44 |
const repoName = repoNameEl.value;
|
|
@@ -49,10 +105,18 @@ window.addEventListener("load", function () {
|
|
| 49 |
|
| 50 |
button.setAttribute("disabled", "disabled");
|
| 51 |
try {
|
| 52 |
-
|
| 53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
} catch (err) {
|
| 55 |
-
output.append(err);
|
| 56 |
}
|
| 57 |
button.removeAttribute("disabled");
|
| 58 |
});
|
|
|
|
| 1 |
const c = console;
|
| 2 |
|
| 3 |
+
const ENDPOINT = "https://huggingface.co";
|
|
|
|
| 4 |
|
| 5 |
async function whoami(token: string): Promise<{ name: string }> {
|
| 6 |
const path = `${ENDPOINT}/api/whoami-v2`;
|
| 7 |
const res = await fetch(path, {
|
| 8 |
headers: {
|
| 9 |
Authorization: `Bearer ${token}`,
|
|
|
|
| 10 |
}
|
| 11 |
});
|
| 12 |
return await res.json();
|
| 13 |
}
|
| 14 |
|
| 15 |
+
/**
|
| 16 |
+
* @returns `str`: full URL to the newly created repo.
|
| 17 |
+
*/
|
| 18 |
async function createRepo(
|
| 19 |
token: string,
|
| 20 |
+
repoName: string,
|
| 21 |
repoType: "model" | "dataset" | "space",
|
| 22 |
): Promise<string> {
|
| 23 |
const path = `${ENDPOINT}/api/repos/create`;
|
|
|
|
| 25 |
method: "POST",
|
| 26 |
headers: {
|
| 27 |
Authorization: `Bearer ${token}`,
|
| 28 |
+
"Content-Type": "application/json",
|
| 29 |
},
|
| 30 |
body: JSON.stringify({
|
| 31 |
+
name: repoName,
|
| 32 |
type: repoType,
|
| 33 |
})
|
| 34 |
});
|
| 35 |
+
const output = await res.json();
|
| 36 |
+
if (!res.ok) {
|
| 37 |
+
throw new Error(`Error ${res.status}: ${output.error ?? output}`);
|
| 38 |
+
}
|
| 39 |
+
return output.url;
|
| 40 |
+
}
|
| 41 |
+
|
| 42 |
+
/**
|
| 43 |
+
* @returns `str`: URL to the newly uploaded blob.
|
| 44 |
+
*/
|
| 45 |
+
async function uploadFile(
|
| 46 |
+
token: string,
|
| 47 |
+
repoId: string,
|
| 48 |
+
repoType: "model" | "dataset" | "space",
|
| 49 |
+
filename: string,
|
| 50 |
+
blob: Blob,
|
| 51 |
+
): Promise<string> {
|
| 52 |
+
const prefix = (repoType === "dataset") ? "/datasets"
|
| 53 |
+
: (repoType === "space") ? "/spaces"
|
| 54 |
+
: "";
|
| 55 |
+
const path = `${ENDPOINT}/api${prefix}/${repoId}/upload/main/${filename}`;
|
| 56 |
+
const res = await fetch(path, {
|
| 57 |
+
method: "POST",
|
| 58 |
+
headers: {
|
| 59 |
+
Authorization: `Bearer ${token}`,
|
| 60 |
+
},
|
| 61 |
+
body: blob
|
| 62 |
+
});
|
| 63 |
+
const output = await res.json();
|
| 64 |
+
if (!res.ok) {
|
| 65 |
+
throw new Error(`Error ${res.status}: ${output.error ?? output}`);
|
| 66 |
+
}
|
| 67 |
+
return output.url;
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
|
| 71 |
+
const FILES_TO_UPLOAD = [
|
| 72 |
+
"./mobilenet/model.json",
|
| 73 |
+
"./mobilenet/group1-shard1of2",
|
| 74 |
+
"./mobilenet/group1-shard2of2",
|
| 75 |
+
"./mobilenet/coffee.jpg",
|
| 76 |
+
"./mobilenet/README.md",
|
| 77 |
+
];
|
| 78 |
+
|
| 79 |
+
function filenameFromURL(url: string): string {
|
| 80 |
+
return url.substring(url.lastIndexOf("/") + 1);
|
| 81 |
}
|
| 82 |
|
| 83 |
window.addEventListener("load", function () {
|
|
|
|
| 85 |
const repoNameEl = document.querySelector<HTMLInputElement>("#repo_name")!;
|
| 86 |
const button = document.querySelector("#submit")!;
|
| 87 |
const output = document.querySelector("#logs")!;
|
| 88 |
+
|
| 89 |
+
const storedToken = window.localStorage.getItem("hf_token");
|
| 90 |
+
if (storedToken) {
|
| 91 |
+
tokenEl.value = storedToken;
|
| 92 |
+
/// ^to help in dev.
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
repoNameEl.value = `tfjs-mobilenet-${Date.now() % 1_000}`;
|
| 96 |
+
/// "random" repo name
|
| 97 |
+
|
| 98 |
button.addEventListener("click", async function () {
|
| 99 |
const token = tokenEl.value;
|
| 100 |
const repoName = repoNameEl.value;
|
|
|
|
| 105 |
|
| 106 |
button.setAttribute("disabled", "disabled");
|
| 107 |
try {
|
| 108 |
+
const fullUrl = await createRepo(token, repoName, "model");
|
| 109 |
+
const repoId = fullUrl.replace(ENDPOINT, "").replace(/^\//, "");
|
| 110 |
+
for (const file of FILES_TO_UPLOAD) {
|
| 111 |
+
const blob = await (await fetch(file)).blob();
|
| 112 |
+
await uploadFile(token, repoId, "model", filenameFromURL(file), blob);
|
| 113 |
+
}
|
| 114 |
+
button.insertAdjacentHTML(
|
| 115 |
+
"afterend",
|
| 116 |
+
`<div class="text-green-500 mb-6">🎉 Upload complete! Model page is <a target="_blank" class="text-bold underline" href="${fullUrl}">${fullUrl}</a></div>`
|
| 117 |
+
);
|
| 118 |
} catch (err) {
|
| 119 |
+
output.append("\n"+err);
|
| 120 |
}
|
| 121 |
button.removeAttribute("disabled");
|
| 122 |
});
|
dist/app.js
CHANGED
|
@@ -1,35 +1,81 @@
|
|
| 1 |
const c = console;
|
| 2 |
-
|
| 3 |
-
const ENDPOINT = "http://localhost:5564";
|
| 4 |
async function whoami(token) {
|
| 5 |
const path = `${ENDPOINT}/api/whoami-v2`;
|
| 6 |
const res = await fetch(path, {
|
| 7 |
headers: {
|
| 8 |
Authorization: `Bearer ${token}`,
|
| 9 |
-
Origin: `127.0.0.1:8000`
|
| 10 |
}
|
| 11 |
});
|
| 12 |
return await res.json();
|
| 13 |
}
|
| 14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
const path = `${ENDPOINT}/api/repos/create`;
|
| 16 |
const res = await fetch(path, {
|
| 17 |
method: "POST",
|
| 18 |
headers: {
|
| 19 |
Authorization: `Bearer ${token}`,
|
|
|
|
| 20 |
},
|
| 21 |
body: JSON.stringify({
|
| 22 |
-
name:
|
| 23 |
type: repoType,
|
| 24 |
})
|
| 25 |
});
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
}
|
| 28 |
window.addEventListener("load", function () {
|
| 29 |
const tokenEl = document.querySelector("#token");
|
| 30 |
const repoNameEl = document.querySelector("#repo_name");
|
| 31 |
const button = document.querySelector("#submit");
|
| 32 |
const output = document.querySelector("#logs");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
button.addEventListener("click", async function () {
|
| 34 |
const token = tokenEl.value;
|
| 35 |
const repoName = repoNameEl.value;
|
|
@@ -39,11 +85,16 @@ window.addEventListener("load", function () {
|
|
| 39 |
}
|
| 40 |
button.setAttribute("disabled", "disabled");
|
| 41 |
try {
|
| 42 |
-
|
| 43 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
}
|
| 45 |
catch (err) {
|
| 46 |
-
output.append(err);
|
| 47 |
}
|
| 48 |
button.removeAttribute("disabled");
|
| 49 |
});
|
|
|
|
| 1 |
const c = console;
|
| 2 |
+
const ENDPOINT = "https://huggingface.co";
|
|
|
|
| 3 |
async function whoami(token) {
|
| 4 |
const path = `${ENDPOINT}/api/whoami-v2`;
|
| 5 |
const res = await fetch(path, {
|
| 6 |
headers: {
|
| 7 |
Authorization: `Bearer ${token}`,
|
|
|
|
| 8 |
}
|
| 9 |
});
|
| 10 |
return await res.json();
|
| 11 |
}
|
| 12 |
+
/**
|
| 13 |
+
* @returns `str`: full URL to the newly created repo.
|
| 14 |
+
*/
|
| 15 |
+
async function createRepo(token, repoName, repoType) {
|
| 16 |
+
var _a;
|
| 17 |
const path = `${ENDPOINT}/api/repos/create`;
|
| 18 |
const res = await fetch(path, {
|
| 19 |
method: "POST",
|
| 20 |
headers: {
|
| 21 |
Authorization: `Bearer ${token}`,
|
| 22 |
+
"Content-Type": "application/json",
|
| 23 |
},
|
| 24 |
body: JSON.stringify({
|
| 25 |
+
name: repoName,
|
| 26 |
type: repoType,
|
| 27 |
})
|
| 28 |
});
|
| 29 |
+
const output = await res.json();
|
| 30 |
+
if (!res.ok) {
|
| 31 |
+
throw new Error(`Error ${res.status}: ${(_a = output.error) !== null && _a !== void 0 ? _a : output}`);
|
| 32 |
+
}
|
| 33 |
+
return output.url;
|
| 34 |
+
}
|
| 35 |
+
/**
|
| 36 |
+
* @returns `str`: URL to the newly uploaded blob.
|
| 37 |
+
*/
|
| 38 |
+
async function uploadFile(token, repoId, repoType, filename, blob) {
|
| 39 |
+
var _a;
|
| 40 |
+
const prefix = (repoType === "dataset") ? "/datasets"
|
| 41 |
+
: (repoType === "space") ? "/spaces"
|
| 42 |
+
: "";
|
| 43 |
+
const path = `${ENDPOINT}/api${prefix}/${repoId}/upload/main/${filename}`;
|
| 44 |
+
const res = await fetch(path, {
|
| 45 |
+
method: "POST",
|
| 46 |
+
headers: {
|
| 47 |
+
Authorization: `Bearer ${token}`,
|
| 48 |
+
},
|
| 49 |
+
body: blob
|
| 50 |
+
});
|
| 51 |
+
const output = await res.json();
|
| 52 |
+
if (!res.ok) {
|
| 53 |
+
throw new Error(`Error ${res.status}: ${(_a = output.error) !== null && _a !== void 0 ? _a : output}`);
|
| 54 |
+
}
|
| 55 |
+
return output.url;
|
| 56 |
+
}
|
| 57 |
+
const FILES_TO_UPLOAD = [
|
| 58 |
+
"./mobilenet/model.json",
|
| 59 |
+
"./mobilenet/group1-shard1of2",
|
| 60 |
+
"./mobilenet/group1-shard2of2",
|
| 61 |
+
"./mobilenet/coffee.jpg",
|
| 62 |
+
"./mobilenet/README.md",
|
| 63 |
+
];
|
| 64 |
+
function filenameFromURL(url) {
|
| 65 |
+
return url.substring(url.lastIndexOf("/") + 1);
|
| 66 |
}
|
| 67 |
window.addEventListener("load", function () {
|
| 68 |
const tokenEl = document.querySelector("#token");
|
| 69 |
const repoNameEl = document.querySelector("#repo_name");
|
| 70 |
const button = document.querySelector("#submit");
|
| 71 |
const output = document.querySelector("#logs");
|
| 72 |
+
const storedToken = window.localStorage.getItem("hf_token");
|
| 73 |
+
if (storedToken) {
|
| 74 |
+
tokenEl.value = storedToken;
|
| 75 |
+
/// ^to help in dev.
|
| 76 |
+
}
|
| 77 |
+
repoNameEl.value = `tfjs-mobilenet-${Date.now() % 1000}`;
|
| 78 |
+
/// "random" repo name
|
| 79 |
button.addEventListener("click", async function () {
|
| 80 |
const token = tokenEl.value;
|
| 81 |
const repoName = repoNameEl.value;
|
|
|
|
| 85 |
}
|
| 86 |
button.setAttribute("disabled", "disabled");
|
| 87 |
try {
|
| 88 |
+
const fullUrl = await createRepo(token, repoName, "model");
|
| 89 |
+
const repoId = fullUrl.replace(ENDPOINT, "").replace(/^\//, "");
|
| 90 |
+
for (const file of FILES_TO_UPLOAD) {
|
| 91 |
+
const blob = await (await fetch(file)).blob();
|
| 92 |
+
await uploadFile(token, repoId, "model", filenameFromURL(file), blob);
|
| 93 |
+
}
|
| 94 |
+
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>`);
|
| 95 |
}
|
| 96 |
catch (err) {
|
| 97 |
+
output.append("\n" + err);
|
| 98 |
}
|
| 99 |
button.removeAttribute("disabled");
|
| 100 |
});
|
index.html
CHANGED
|
@@ -121,7 +121,6 @@
|
|
| 121 |
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
|
| 122 |
placeholder="repo name"
|
| 123 |
required
|
| 124 |
-
value="tfjs-mobilenet"
|
| 125 |
/>
|
| 126 |
|
| 127 |
<p class="mt-8">
|
|
|
|
| 121 |
class="rounded border-2 border-blue-500 shadow-md px-3 py-2 w-96 mt-6"
|
| 122 |
placeholder="repo name"
|
| 123 |
required
|
|
|
|
| 124 |
/>
|
| 125 |
|
| 126 |
<p class="mt-8">
|
mobilenet/README.md
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
tags:
|
| 4 |
+
- image-classification
|
| 5 |
+
- tfjs
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## TensorFlow.js version of Mobilenet
|
| 9 |
+
|
| 10 |
+
Pushed from Web
|
| 11 |
+
|
| 12 |
+

|
mobilenet/coffee.jpg
ADDED
|