Spaces:
Runtime error
Runtime error
Charlie
commited on
Commit
·
59cd560
1
Parent(s):
16d55ea
update files
Browse files- dist/main.js +75 -115
- index.html +2 -2
- src/api-client.ts +44 -0
- src/main.ts +55 -174
- src/{oauth.ts → token.ts} +0 -0
dist/main.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
| 1 |
-
// src/
|
| 2 |
var TokenManager = class {
|
| 3 |
constructor() {
|
| 4 |
this.token = null;
|
|
@@ -25,98 +25,53 @@ var TokenManager = class {
|
|
| 25 |
};
|
| 26 |
var tokenManager = new TokenManager();
|
| 27 |
|
| 28 |
-
// src/
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
}
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
}
|
| 40 |
-
return response.json();
|
| 41 |
-
}
|
| 42 |
-
async function getOrganizationMembers(accessToken, organization) {
|
| 43 |
-
const response = await fetch(
|
| 44 |
-
`https://huggingface.co/api/organizations/${organization}/members`,
|
| 45 |
-
{
|
| 46 |
-
headers: {
|
| 47 |
-
Authorization: `Bearer ${accessToken}`
|
| 48 |
-
}
|
| 49 |
}
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
{
|
| 62 |
method: "POST",
|
| 63 |
-
headers:
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
body: JSON.stringify({
|
| 68 |
-
name,
|
| 69 |
-
description: `Resource group for repository ${name}`,
|
| 70 |
-
autoJoin: {
|
| 71 |
-
enabled: true,
|
| 72 |
-
role: "admin"
|
| 73 |
-
}
|
| 74 |
-
})
|
| 75 |
-
}
|
| 76 |
-
);
|
| 77 |
-
if (!response.ok) {
|
| 78 |
-
throw new Error(`Failed to create resource group: ${response.statusText}`);
|
| 79 |
-
}
|
| 80 |
-
}
|
| 81 |
-
async function createRepository(accessToken, name, organization, resourceGroupName) {
|
| 82 |
-
const response = await fetch("https://huggingface.co/api/repos/create", {
|
| 83 |
-
method: "POST",
|
| 84 |
-
headers: {
|
| 85 |
-
"Content-Type": "application/json",
|
| 86 |
-
Authorization: `Bearer ${accessToken}`
|
| 87 |
-
},
|
| 88 |
-
body: JSON.stringify({
|
| 89 |
-
type: "model",
|
| 90 |
-
name,
|
| 91 |
-
organization,
|
| 92 |
-
private: false
|
| 93 |
-
})
|
| 94 |
-
});
|
| 95 |
-
if (!response.ok) {
|
| 96 |
-
throw new Error(`Failed to create repository: ${response.statusText}`);
|
| 97 |
-
}
|
| 98 |
-
if (organization && resourceGroupName) {
|
| 99 |
-
const members = await getOrganizationMembers(accessToken, organization);
|
| 100 |
-
await createResourceGroup(
|
| 101 |
-
accessToken,
|
| 102 |
-
organization,
|
| 103 |
-
resourceGroupName,
|
| 104 |
-
members
|
| 105 |
-
);
|
| 106 |
-
}
|
| 107 |
-
}
|
| 108 |
-
var init = async () => {
|
| 109 |
-
if (tokenManager.isAuthenticated()) {
|
| 110 |
-
showAuthenticatedUI();
|
| 111 |
-
} else {
|
| 112 |
-
showUnauthenticatedUI();
|
| 113 |
}
|
| 114 |
};
|
|
|
|
|
|
|
|
|
|
| 115 |
var showAuthenticatedUI = async () => {
|
| 116 |
const accessToken = tokenManager.getAccessToken();
|
| 117 |
if (!accessToken) {
|
| 118 |
throw new Error("Access token not found");
|
| 119 |
}
|
|
|
|
| 120 |
const tokenForm = document.getElementById("token-form");
|
| 121 |
if (tokenForm) {
|
| 122 |
tokenForm.style.display = "none";
|
|
@@ -131,40 +86,42 @@ var showAuthenticatedUI = async () => {
|
|
| 131 |
signoutButton.onclick = () => tokenManager.logout();
|
| 132 |
}
|
| 133 |
const createRepoButton = document.getElementById("create-repo");
|
| 134 |
-
const repoNameInput = document.getElementById(
|
| 135 |
-
|
| 136 |
-
);
|
| 137 |
-
const resourceGroupInput = document.getElementById(
|
| 138 |
-
"resource-group-name"
|
| 139 |
-
);
|
| 140 |
const resourceGroupContainer = document.getElementById(
|
| 141 |
"resource-group-container"
|
| 142 |
);
|
| 143 |
if (createRepoButton && repoNameInput) {
|
| 144 |
createRepoButton.onclick = async () => {
|
| 145 |
const repoName = repoNameInput.value.trim();
|
| 146 |
-
const orgSelect2 = document.getElementById(
|
| 147 |
-
"org-select"
|
| 148 |
-
);
|
| 149 |
if (repoName) {
|
| 150 |
try {
|
| 151 |
-
const
|
| 152 |
-
const resourceGroupName =
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
);
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
}
|
| 164 |
alert("Repository and resource group created successfully!");
|
| 165 |
} catch (error) {
|
| 166 |
-
console.error(
|
| 167 |
-
alert("Failed to create repository. Please try again.");
|
| 168 |
}
|
| 169 |
}
|
| 170 |
};
|
|
@@ -180,21 +137,17 @@ var showAuthenticatedUI = async () => {
|
|
| 180 |
};
|
| 181 |
}
|
| 182 |
try {
|
| 183 |
-
const
|
| 184 |
-
if (orgSelect &&
|
| 185 |
-
|
| 186 |
const option = document.createElement("option");
|
| 187 |
option.value = org.name;
|
| 188 |
option.textContent = org.name;
|
| 189 |
orgSelect.appendChild(option);
|
| 190 |
});
|
| 191 |
}
|
| 192 |
-
const preElement = document.querySelector("pre");
|
| 193 |
-
if (preElement) {
|
| 194 |
-
preElement.textContent = JSON.stringify(orgInfo, null, 2);
|
| 195 |
-
}
|
| 196 |
} catch (error) {
|
| 197 |
-
console.error(
|
| 198 |
}
|
| 199 |
};
|
| 200 |
var showUnauthenticatedUI = () => {
|
|
@@ -220,4 +173,11 @@ var showUnauthenticatedUI = () => {
|
|
| 220 |
signoutButton.style.display = "none";
|
| 221 |
}
|
| 222 |
};
|
| 223 |
-
init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
// src/token.ts
|
| 2 |
var TokenManager = class {
|
| 3 |
constructor() {
|
| 4 |
this.token = null;
|
|
|
|
| 25 |
};
|
| 26 |
var tokenManager = new TokenManager();
|
| 27 |
|
| 28 |
+
// src/api-client.ts
|
| 29 |
+
var Client = class {
|
| 30 |
+
constructor(baseUrl2, token) {
|
| 31 |
+
this.baseUrl = baseUrl2.endsWith("/") ? baseUrl2.slice(0, -1) : baseUrl2;
|
| 32 |
+
this.token = token;
|
| 33 |
+
}
|
| 34 |
+
getHeaders(contentType) {
|
| 35 |
+
const headers = {
|
| 36 |
+
Authorization: `Bearer ${this.token}`
|
| 37 |
+
};
|
| 38 |
+
if (contentType) {
|
| 39 |
+
headers["Content-Type"] = contentType;
|
| 40 |
}
|
| 41 |
+
return headers;
|
| 42 |
+
}
|
| 43 |
+
async handleResponse(response) {
|
| 44 |
+
if (!response.ok) {
|
| 45 |
+
throw new Error(`API request failed: ${response.statusText}`);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
}
|
| 47 |
+
return response.json();
|
| 48 |
+
}
|
| 49 |
+
async get(path) {
|
| 50 |
+
const url = `${this.baseUrl}${path.startsWith("/") ? path : "/" + path}`;
|
| 51 |
+
const response = await fetch(url, {
|
| 52 |
+
headers: this.getHeaders()
|
| 53 |
+
});
|
| 54 |
+
return this.handleResponse(response);
|
| 55 |
+
}
|
| 56 |
+
async post(path, payload) {
|
| 57 |
+
const url = `${this.baseUrl}${path.startsWith("/") ? path : "/" + path}`;
|
| 58 |
+
const response = await fetch(url, {
|
| 59 |
method: "POST",
|
| 60 |
+
headers: this.getHeaders("application/json"),
|
| 61 |
+
body: JSON.stringify(payload)
|
| 62 |
+
});
|
| 63 |
+
return this.handleResponse(response);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
};
|
| 66 |
+
|
| 67 |
+
// src/main.ts
|
| 68 |
+
var baseUrl = "https://huggingface.co/api";
|
| 69 |
var showAuthenticatedUI = async () => {
|
| 70 |
const accessToken = tokenManager.getAccessToken();
|
| 71 |
if (!accessToken) {
|
| 72 |
throw new Error("Access token not found");
|
| 73 |
}
|
| 74 |
+
const client = new Client(baseUrl, accessToken);
|
| 75 |
const tokenForm = document.getElementById("token-form");
|
| 76 |
if (tokenForm) {
|
| 77 |
tokenForm.style.display = "none";
|
|
|
|
| 86 |
signoutButton.onclick = () => tokenManager.logout();
|
| 87 |
}
|
| 88 |
const createRepoButton = document.getElementById("create-repo");
|
| 89 |
+
const repoNameInput = document.getElementById("repo-name");
|
| 90 |
+
const resourceGroupInput = document.getElementById("resource-group-name");
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
const resourceGroupContainer = document.getElementById(
|
| 92 |
"resource-group-container"
|
| 93 |
);
|
| 94 |
if (createRepoButton && repoNameInput) {
|
| 95 |
createRepoButton.onclick = async () => {
|
| 96 |
const repoName = repoNameInput.value.trim();
|
| 97 |
+
const orgSelect2 = document.getElementById("org-select");
|
|
|
|
|
|
|
| 98 |
if (repoName) {
|
| 99 |
try {
|
| 100 |
+
const organizationName = orgSelect2?.value || void 0;
|
| 101 |
+
const resourceGroupName = organizationName ? resourceGroupInput?.value.trim() : void 0;
|
| 102 |
+
const repo = await client.post("repos/create", {
|
| 103 |
+
type: "model",
|
| 104 |
+
name: repoName,
|
| 105 |
+
organization: organizationName,
|
| 106 |
+
private: true
|
| 107 |
+
});
|
| 108 |
+
console.log({ repo });
|
| 109 |
+
if (organizationName && resourceGroupName) {
|
| 110 |
+
await client.post(
|
| 111 |
+
`organizations/${organizationName}/resource-groups`,
|
| 112 |
+
{
|
| 113 |
+
name: resourceGroupName,
|
| 114 |
+
description: `Resource group for repository ${name}`,
|
| 115 |
+
autoJoin: {
|
| 116 |
+
enabled: true,
|
| 117 |
+
role: "admin"
|
| 118 |
+
}
|
| 119 |
+
}
|
| 120 |
+
);
|
| 121 |
}
|
| 122 |
alert("Repository and resource group created successfully!");
|
| 123 |
} catch (error) {
|
| 124 |
+
console.error(error);
|
|
|
|
| 125 |
}
|
| 126 |
}
|
| 127 |
};
|
|
|
|
| 137 |
};
|
| 138 |
}
|
| 139 |
try {
|
| 140 |
+
const userInfo = await client.get("whoami-v2");
|
| 141 |
+
if (orgSelect && userInfo.orgs) {
|
| 142 |
+
userInfo.orgs.forEach((org) => {
|
| 143 |
const option = document.createElement("option");
|
| 144 |
option.value = org.name;
|
| 145 |
option.textContent = org.name;
|
| 146 |
orgSelect.appendChild(option);
|
| 147 |
});
|
| 148 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
| 149 |
} catch (error) {
|
| 150 |
+
console.error(error);
|
| 151 |
}
|
| 152 |
};
|
| 153 |
var showUnauthenticatedUI = () => {
|
|
|
|
| 173 |
signoutButton.style.display = "none";
|
| 174 |
}
|
| 175 |
};
|
| 176 |
+
var init = async () => {
|
| 177 |
+
if (tokenManager.isAuthenticated()) {
|
| 178 |
+
showAuthenticatedUI();
|
| 179 |
+
} else {
|
| 180 |
+
showUnauthenticatedUI();
|
| 181 |
+
}
|
| 182 |
+
};
|
| 183 |
+
init();
|
index.html
CHANGED
|
@@ -21,10 +21,10 @@
|
|
| 21 |
</div>
|
| 22 |
<div id="repo-form" style="display: none;" class="repo-form">
|
| 23 |
<select id="org-select" class="repo-input">
|
| 24 |
-
<option value="">
|
| 25 |
</select>
|
| 26 |
<input type="text" id="repo-name" placeholder="Enter repository name" class="repo-input">
|
| 27 |
-
<div id="resource-group-container" style="display: none;">
|
| 28 |
<input type="text" id="resource-group-name" placeholder="Enter resource group name" class="repo-input">
|
| 29 |
</div>
|
| 30 |
<button id="create-repo" class="repo-button">Create Repository</button>
|
|
|
|
| 21 |
</div>
|
| 22 |
<div id="repo-form" style="display: none;" class="repo-form">
|
| 23 |
<select id="org-select" class="repo-input">
|
| 24 |
+
<option value="">Own account</option>
|
| 25 |
</select>
|
| 26 |
<input type="text" id="repo-name" placeholder="Enter repository name" class="repo-input">
|
| 27 |
+
<div id="resource-group-container" style="display: none; width: 100%;">
|
| 28 |
<input type="text" id="resource-group-name" placeholder="Enter resource group name" class="repo-input">
|
| 29 |
</div>
|
| 30 |
<button id="create-repo" class="repo-button">Create Repository</button>
|
src/api-client.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
export class Client {
|
| 2 |
+
private baseUrl: string;
|
| 3 |
+
private token: string;
|
| 4 |
+
|
| 5 |
+
constructor(baseUrl: string, token: string) {
|
| 6 |
+
this.baseUrl = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
| 7 |
+
this.token = token;
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
private getHeaders(contentType?: string): HeadersInit {
|
| 11 |
+
const headers: HeadersInit = {
|
| 12 |
+
Authorization: `Bearer ${this.token}`,
|
| 13 |
+
};
|
| 14 |
+
if (contentType) {
|
| 15 |
+
headers["Content-Type"] = contentType;
|
| 16 |
+
}
|
| 17 |
+
return headers;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
private async handleResponse<T>(response: Response): Promise<T> {
|
| 21 |
+
if (!response.ok) {
|
| 22 |
+
throw new Error(`API request failed: ${response.statusText}`);
|
| 23 |
+
}
|
| 24 |
+
return response.json();
|
| 25 |
+
}
|
| 26 |
+
|
| 27 |
+
async get<T>(path: string): Promise<T> {
|
| 28 |
+
const url = `${this.baseUrl}${path.startsWith("/") ? path : "/" + path}`;
|
| 29 |
+
const response = await fetch(url, {
|
| 30 |
+
headers: this.getHeaders(),
|
| 31 |
+
});
|
| 32 |
+
return this.handleResponse<T>(response);
|
| 33 |
+
}
|
| 34 |
+
|
| 35 |
+
async post<T>(path: string, payload: unknown): Promise<T> {
|
| 36 |
+
const url = `${this.baseUrl}${path.startsWith("/") ? path : "/" + path}`;
|
| 37 |
+
const response = await fetch(url, {
|
| 38 |
+
method: "POST",
|
| 39 |
+
headers: this.getHeaders("application/json"),
|
| 40 |
+
body: JSON.stringify(payload),
|
| 41 |
+
});
|
| 42 |
+
return this.handleResponse<T>(response);
|
| 43 |
+
}
|
| 44 |
+
}
|
src/main.ts
CHANGED
|
@@ -1,147 +1,18 @@
|
|
| 1 |
-
import { tokenManager } from "./
|
|
|
|
|
|
|
| 2 |
|
| 3 |
-
|
| 4 |
-
type: string;
|
| 5 |
-
id: string;
|
| 6 |
-
name: string;
|
| 7 |
-
role: string;
|
| 8 |
-
}
|
| 9 |
-
|
| 10 |
-
interface WhoAmIResponse {
|
| 11 |
-
type: string;
|
| 12 |
-
id: string;
|
| 13 |
-
name: string;
|
| 14 |
-
email?: string;
|
| 15 |
-
fullname?: string;
|
| 16 |
-
avatarUrl?: string;
|
| 17 |
-
orgs: OrganizationInfo[];
|
| 18 |
-
}
|
| 19 |
-
|
| 20 |
-
interface OrganizationMember {
|
| 21 |
-
user: string;
|
| 22 |
-
role: string;
|
| 23 |
-
}
|
| 24 |
-
|
| 25 |
-
async function getOrganizationInfo(
|
| 26 |
-
accessToken: string
|
| 27 |
-
): Promise<WhoAmIResponse> {
|
| 28 |
-
const response = await fetch("https://huggingface.co/api/whoami-v2", {
|
| 29 |
-
headers: {
|
| 30 |
-
Authorization: `Bearer ${accessToken}`,
|
| 31 |
-
},
|
| 32 |
-
});
|
| 33 |
-
|
| 34 |
-
if (!response.ok) {
|
| 35 |
-
throw new Error(
|
| 36 |
-
`Failed to fetch organization info: ${response.statusText}`
|
| 37 |
-
);
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
return response.json();
|
| 41 |
-
}
|
| 42 |
-
|
| 43 |
-
async function getOrganizationMembers(
|
| 44 |
-
accessToken: string,
|
| 45 |
-
organization: string
|
| 46 |
-
): Promise<OrganizationMember[]> {
|
| 47 |
-
const response = await fetch(
|
| 48 |
-
`https://huggingface.co/api/organizations/${organization}/members`,
|
| 49 |
-
{
|
| 50 |
-
headers: {
|
| 51 |
-
Authorization: `Bearer ${accessToken}`,
|
| 52 |
-
},
|
| 53 |
-
}
|
| 54 |
-
);
|
| 55 |
-
|
| 56 |
-
if (!response.ok) {
|
| 57 |
-
throw new Error(
|
| 58 |
-
`Failed to fetch organization members: ${response.statusText}`
|
| 59 |
-
);
|
| 60 |
-
}
|
| 61 |
-
|
| 62 |
-
return response.json();
|
| 63 |
-
}
|
| 64 |
-
|
| 65 |
-
async function createResourceGroup(
|
| 66 |
-
accessToken: string,
|
| 67 |
-
organization: string,
|
| 68 |
-
name: string,
|
| 69 |
-
members: OrganizationMember[]
|
| 70 |
-
): Promise<void> {
|
| 71 |
-
const response = await fetch(
|
| 72 |
-
`https://huggingface.co/api/organizations/${organization}/resource-groups`,
|
| 73 |
-
{
|
| 74 |
-
method: "POST",
|
| 75 |
-
headers: {
|
| 76 |
-
"Content-Type": "application/json",
|
| 77 |
-
Authorization: `Bearer ${accessToken}`,
|
| 78 |
-
},
|
| 79 |
-
body: JSON.stringify({
|
| 80 |
-
name: name,
|
| 81 |
-
description: `Resource group for repository ${name}`,
|
| 82 |
-
autoJoin: {
|
| 83 |
-
enabled: true,
|
| 84 |
-
role: "admin",
|
| 85 |
-
},
|
| 86 |
-
}),
|
| 87 |
-
}
|
| 88 |
-
);
|
| 89 |
-
|
| 90 |
-
if (!response.ok) {
|
| 91 |
-
throw new Error(`Failed to create resource group: ${response.statusText}`);
|
| 92 |
-
}
|
| 93 |
-
}
|
| 94 |
-
|
| 95 |
-
async function createRepository(
|
| 96 |
-
accessToken: string,
|
| 97 |
-
name: string,
|
| 98 |
-
organization?: string,
|
| 99 |
-
resourceGroupName?: string
|
| 100 |
-
): Promise<void> {
|
| 101 |
-
const response = await fetch("https://huggingface.co/api/repos/create", {
|
| 102 |
-
method: "POST",
|
| 103 |
-
headers: {
|
| 104 |
-
"Content-Type": "application/json",
|
| 105 |
-
Authorization: `Bearer ${accessToken}`,
|
| 106 |
-
},
|
| 107 |
-
body: JSON.stringify({
|
| 108 |
-
type: "model",
|
| 109 |
-
name: name,
|
| 110 |
-
organization: organization,
|
| 111 |
-
private: false,
|
| 112 |
-
}),
|
| 113 |
-
});
|
| 114 |
-
|
| 115 |
-
if (!response.ok) {
|
| 116 |
-
throw new Error(`Failed to create repository: ${response.statusText}`);
|
| 117 |
-
}
|
| 118 |
-
|
| 119 |
-
// If organization and resource group name are provided, create a resource group
|
| 120 |
-
if (organization && resourceGroupName) {
|
| 121 |
-
const members = await getOrganizationMembers(accessToken, organization);
|
| 122 |
-
await createResourceGroup(
|
| 123 |
-
accessToken,
|
| 124 |
-
organization,
|
| 125 |
-
resourceGroupName,
|
| 126 |
-
members
|
| 127 |
-
);
|
| 128 |
-
}
|
| 129 |
-
}
|
| 130 |
-
|
| 131 |
-
const init = async (): Promise<void> => {
|
| 132 |
-
if (tokenManager.isAuthenticated()) {
|
| 133 |
-
showAuthenticatedUI();
|
| 134 |
-
} else {
|
| 135 |
-
showUnauthenticatedUI();
|
| 136 |
-
}
|
| 137 |
-
};
|
| 138 |
|
| 139 |
const showAuthenticatedUI = async () => {
|
| 140 |
const accessToken = tokenManager.getAccessToken();
|
|
|
|
| 141 |
if (!accessToken) {
|
| 142 |
throw new Error("Access token not found");
|
| 143 |
}
|
| 144 |
|
|
|
|
|
|
|
| 145 |
// Hide token form
|
| 146 |
const tokenForm = document.getElementById("token-form");
|
| 147 |
if (tokenForm) {
|
|
@@ -159,14 +30,12 @@ const showAuthenticatedUI = async () => {
|
|
| 159 |
signoutButton.onclick = () => tokenManager.logout();
|
| 160 |
}
|
| 161 |
|
| 162 |
-
// Add create repo
|
| 163 |
const createRepoButton = document.getElementById("create-repo");
|
| 164 |
-
const repoNameInput = document.getElementById(
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
"resource-group-name"
|
| 169 |
-
) as HTMLInputElement;
|
| 170 |
const resourceGroupContainer = document.getElementById(
|
| 171 |
"resource-group-container"
|
| 172 |
);
|
|
@@ -174,31 +43,44 @@ const showAuthenticatedUI = async () => {
|
|
| 174 |
if (createRepoButton && repoNameInput) {
|
| 175 |
createRepoButton.onclick = async () => {
|
| 176 |
const repoName = repoNameInput.value.trim();
|
| 177 |
-
const orgSelect =
|
| 178 |
-
"org-select"
|
| 179 |
-
)
|
|
|
|
| 180 |
if (repoName) {
|
| 181 |
try {
|
| 182 |
-
const
|
| 183 |
-
const resourceGroupName =
|
| 184 |
? resourceGroupInput?.value.trim()
|
| 185 |
: undefined;
|
| 186 |
|
| 187 |
-
|
| 188 |
-
await
|
| 189 |
-
|
| 190 |
-
repoName,
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
);
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
}
|
|
|
|
| 198 |
alert("Repository and resource group created successfully!");
|
| 199 |
} catch (error) {
|
| 200 |
-
console.error(
|
| 201 |
-
alert("Failed to create repository. Please try again.");
|
| 202 |
}
|
| 203 |
}
|
| 204 |
};
|
|
@@ -216,32 +98,24 @@ const showAuthenticatedUI = async () => {
|
|
| 216 |
};
|
| 217 |
}
|
| 218 |
|
| 219 |
-
// Get organization info and populate org select
|
| 220 |
try {
|
| 221 |
-
const
|
| 222 |
|
| 223 |
// Populate org select
|
| 224 |
-
if (orgSelect &&
|
| 225 |
-
|
| 226 |
const option = document.createElement("option");
|
| 227 |
option.value = org.name;
|
| 228 |
option.textContent = org.name;
|
| 229 |
orgSelect.appendChild(option);
|
| 230 |
});
|
| 231 |
}
|
| 232 |
-
|
| 233 |
-
// Display user info
|
| 234 |
-
const preElement = document.querySelector("pre");
|
| 235 |
-
if (preElement) {
|
| 236 |
-
preElement.textContent = JSON.stringify(orgInfo, null, 2);
|
| 237 |
-
}
|
| 238 |
} catch (error) {
|
| 239 |
-
console.error(
|
| 240 |
}
|
| 241 |
};
|
| 242 |
|
| 243 |
const showUnauthenticatedUI = () => {
|
| 244 |
-
// Show token form
|
| 245 |
const tokenForm = document.getElementById("token-form");
|
| 246 |
const tokenInput = document.getElementById("token-input") as HTMLInputElement;
|
| 247 |
const tokenSubmit = document.getElementById("token-submit");
|
|
@@ -257,7 +131,6 @@ const showUnauthenticatedUI = () => {
|
|
| 257 |
};
|
| 258 |
}
|
| 259 |
|
| 260 |
-
// Hide other UI elements
|
| 261 |
const repoForm = document.getElementById("repo-form");
|
| 262 |
const signoutButton = document.getElementById("signout");
|
| 263 |
if (repoForm) {
|
|
@@ -268,4 +141,12 @@ const showUnauthenticatedUI = () => {
|
|
| 268 |
}
|
| 269 |
};
|
| 270 |
|
| 271 |
-
init()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import { tokenManager } from "./token";
|
| 2 |
+
import { Client } from "./api-client";
|
| 3 |
+
import { WhoAmIUser } from "@huggingface/hub";
|
| 4 |
|
| 5 |
+
const baseUrl = "https://huggingface.co/api";
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
const showAuthenticatedUI = async () => {
|
| 8 |
const accessToken = tokenManager.getAccessToken();
|
| 9 |
+
|
| 10 |
if (!accessToken) {
|
| 11 |
throw new Error("Access token not found");
|
| 12 |
}
|
| 13 |
|
| 14 |
+
const client = new Client(baseUrl, accessToken);
|
| 15 |
+
|
| 16 |
// Hide token form
|
| 17 |
const tokenForm = document.getElementById("token-form");
|
| 18 |
if (tokenForm) {
|
|
|
|
| 30 |
signoutButton.onclick = () => tokenManager.logout();
|
| 31 |
}
|
| 32 |
|
| 33 |
+
// Add create repo ui
|
| 34 |
const createRepoButton = document.getElementById("create-repo");
|
| 35 |
+
const repoNameInput = <HTMLInputElement>document.getElementById("repo-name");
|
| 36 |
+
const resourceGroupInput = <HTMLInputElement>(
|
| 37 |
+
document.getElementById("resource-group-name")
|
| 38 |
+
);
|
|
|
|
|
|
|
| 39 |
const resourceGroupContainer = document.getElementById(
|
| 40 |
"resource-group-container"
|
| 41 |
);
|
|
|
|
| 43 |
if (createRepoButton && repoNameInput) {
|
| 44 |
createRepoButton.onclick = async () => {
|
| 45 |
const repoName = repoNameInput.value.trim();
|
| 46 |
+
const orgSelect = <HTMLSelectElement>(
|
| 47 |
+
document.getElementById("org-select")
|
| 48 |
+
);
|
| 49 |
+
|
| 50 |
if (repoName) {
|
| 51 |
try {
|
| 52 |
+
const organizationName = orgSelect?.value || undefined;
|
| 53 |
+
const resourceGroupName = organizationName
|
| 54 |
? resourceGroupInput?.value.trim()
|
| 55 |
: undefined;
|
| 56 |
|
| 57 |
+
// create repo
|
| 58 |
+
const repo = await client.post<void>("repos/create", {
|
| 59 |
+
type: "model",
|
| 60 |
+
name: repoName,
|
| 61 |
+
organization: organizationName,
|
| 62 |
+
private: true,
|
| 63 |
+
});
|
| 64 |
+
|
| 65 |
+
console.log({ repo });
|
| 66 |
+
// create RG
|
| 67 |
+
if (organizationName && resourceGroupName) {
|
| 68 |
+
await client.post<void>(
|
| 69 |
+
`organizations/${organizationName}/resource-groups`,
|
| 70 |
+
{
|
| 71 |
+
name: resourceGroupName,
|
| 72 |
+
description: `Resource group for repository ${name}`,
|
| 73 |
+
autoJoin: {
|
| 74 |
+
enabled: true,
|
| 75 |
+
role: "admin",
|
| 76 |
+
},
|
| 77 |
+
}
|
| 78 |
+
);
|
| 79 |
}
|
| 80 |
+
|
| 81 |
alert("Repository and resource group created successfully!");
|
| 82 |
} catch (error) {
|
| 83 |
+
console.error(error);
|
|
|
|
| 84 |
}
|
| 85 |
}
|
| 86 |
};
|
|
|
|
| 98 |
};
|
| 99 |
}
|
| 100 |
|
|
|
|
| 101 |
try {
|
| 102 |
+
const userInfo = await client.get<WhoAmIUser>("whoami-v2");
|
| 103 |
|
| 104 |
// Populate org select
|
| 105 |
+
if (orgSelect && userInfo.orgs) {
|
| 106 |
+
userInfo.orgs.forEach((org) => {
|
| 107 |
const option = document.createElement("option");
|
| 108 |
option.value = org.name;
|
| 109 |
option.textContent = org.name;
|
| 110 |
orgSelect.appendChild(option);
|
| 111 |
});
|
| 112 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 113 |
} catch (error) {
|
| 114 |
+
console.error(error);
|
| 115 |
}
|
| 116 |
};
|
| 117 |
|
| 118 |
const showUnauthenticatedUI = () => {
|
|
|
|
| 119 |
const tokenForm = document.getElementById("token-form");
|
| 120 |
const tokenInput = document.getElementById("token-input") as HTMLInputElement;
|
| 121 |
const tokenSubmit = document.getElementById("token-submit");
|
|
|
|
| 131 |
};
|
| 132 |
}
|
| 133 |
|
|
|
|
| 134 |
const repoForm = document.getElementById("repo-form");
|
| 135 |
const signoutButton = document.getElementById("signout");
|
| 136 |
if (repoForm) {
|
|
|
|
| 141 |
}
|
| 142 |
};
|
| 143 |
|
| 144 |
+
const init = async (): Promise<void> => {
|
| 145 |
+
if (tokenManager.isAuthenticated()) {
|
| 146 |
+
showAuthenticatedUI();
|
| 147 |
+
} else {
|
| 148 |
+
showUnauthenticatedUI();
|
| 149 |
+
}
|
| 150 |
+
};
|
| 151 |
+
|
| 152 |
+
init();
|
src/{oauth.ts → token.ts}
RENAMED
|
File without changes
|