File size: 1,497 Bytes
431b4de
 
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
const c = console;

// const ENDPOINT = "https://huggingface.co";
const ENDPOINT = "http://localhost:5564";

async function whoami(token: string): Promise<{ name: string }> {
	const path = `${ENDPOINT}/api/whoami-v2`;
	const res = await fetch(path, {
		headers: {
			Authorization: `Bearer ${token}`,
			Origin: `127.0.0.1:8000`
		}
	});
	return await res.json();
}

async function createRepo(
	token: string,
	repoId: string,
	repoType: "model" | "dataset" | "space",
): Promise<string> {
	const path = `${ENDPOINT}/api/repos/create`;
	const res = await fetch(path, {
		method: "POST",
		headers: {
			Authorization: `Bearer ${token}`,
		},
		body: JSON.stringify({
			name: repoId,
			type: repoType,
		})
	});
	return (await res.json())["url"];
}

window.addEventListener("load", function () {
	const tokenEl = document.querySelector<HTMLInputElement>("#token")!;
	const repoNameEl = document.querySelector<HTMLInputElement>("#repo_name")!;
	const button = document.querySelector("#submit")!;
	const output = document.querySelector("#logs")!;

	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 {
			// c.log(await createRepo(token, repoName, "model"));
			c.log(await whoami(token));
		} catch (err) {
			output.append(err);
		}
		button.removeAttribute("disabled");
	});
});