File size: 3,880 Bytes
59cd560
d607a39
59cd560
58ae063
59cd560
16c1c4c
b200338
 
59cd560
b200338
 
 
58ae063
59cd560
 
b200338
 
 
 
59cd560
 
 
 
b200338
 
 
 
6a276be
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59cd560
d25d788
 
6a276be
d25d788
6a276be
 
 
 
 
 
 
 
 
 
 
 
b200338
6a276be
 
 
 
 
 
b200338
 
 
6a276be
 
 
 
 
 
 
b200338
 
59cd560
b200338
 
6a276be
59cd560
b200338
 
 
 
 
c433511
b200338
59cd560
b200338
 
2fc767f
b200338
 
6a276be
b200338
 
 
6a276be
 
 
 
 
 
 
 
 
 
 
 
58ae063
 
59cd560
 
 
 
 
 
 
 
 
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import { tokenManager } from "./token";
import { Client } from "./client";
import { WhoAmIUser } from "@huggingface/hub";

const baseUrl = "https://huggingface.co/api";

const showAuthenticatedUI = async () => {
  const accessToken = tokenManager.getAccessToken();

  if (!accessToken) {
    throw new Error("Access token not found");
  }

  const client = new Client(baseUrl, accessToken);

  const tokenForm = document.getElementById("token-form");
  const repoForm = document.getElementById("repo-form");
  const signoutButton = document.getElementById("signout");
  const createRepoButton = document.getElementById("create-repo");
  const repoNameInput = <HTMLInputElement>document.getElementById("repo-name");
  const resourceGroupInput = <HTMLInputElement>(
    document.getElementById("resource-group-name")
  );
  const resourceGroupContainer = document.getElementById(
    "resource-group-container"
  );

  tokenForm!.style.display = "none";
  repoForm!.style.removeProperty("display");
  signoutButton!.style.removeProperty("display");
  signoutButton!.onclick = () => tokenManager.logout();
  createRepoButton!.onclick = async () => {
    const repoName = repoNameInput.value;
    if (!repoName) return alert("Provide a repo name");

    const orgSelect = <HTMLSelectElement>document.getElementById("org-select");

    try {
      const organizationName = orgSelect?.value || undefined;
      const resourceGroupName = organizationName
        ? resourceGroupInput?.value
        : undefined;

      // create repo
      const repo = await client.post<{ name: string }>("repos/create", {
        type: "model",
        name: repoName,
        organization: organizationName,
        private: true,
      });

      console.log({ repo });

      // create RG wi for repo
      if (resourceGroupName) {
        await client.post<void>(
          `organizations/${organizationName}/resource-groups`,
          {
            name: resourceGroupName,
            description: `Resource group for repository ${repo.name}`,
            repos: [repo],
            autoJoin: {
              enabled: true,
              role: "admin",
            },
          }
        );
      }

      alert("Repository and resource group created successfully!");
    } catch (error) {
      console.error(error);
    }
  };

  // Handle org select change to show/hide resource group input
  const orgSelect = document.getElementById("org-select") as HTMLSelectElement;
  orgSelect.onchange = () => {
    if (orgSelect.value) {
      resourceGroupContainer!.style.removeProperty("display");
    } else {
      resourceGroupContainer!.style.display = "none";
    }
  };

  try {
    const userInfo = await client.get<WhoAmIUser>("whoami-v2");

    // Populate org select
    if (userInfo.orgs.length) {
      userInfo.orgs.forEach((org) => {
        const option = document.createElement("option");
        option.value = org.name;
        option.textContent = org.name;
        orgSelect.appendChild(option);
      });
    }
  } catch (error) {
    console.error(error);
  }
};

const showUnauthenticatedUI = () => {
  const tokenForm = document.getElementById("token-form");
  const tokenInput = <HTMLInputElement>document.getElementById("token-input");
  const tokenSubmit = document.getElementById("token-submit");
  const repoForm = document.getElementById("repo-form");
  const signoutButton = document.getElementById("signout");

  tokenForm!.style.removeProperty("display");
  tokenSubmit!.onclick = () => {
    const token = tokenInput.value;
    if (token) {
      tokenManager.setToken(token);
      showAuthenticatedUI();
    }
  };

  repoForm!.style.display = "none";
  signoutButton!.style.display = "none";
};

const init = async (): Promise<void> => {
  if (tokenManager.isAuthenticated()) {
    showAuthenticatedUI();
  } else {
    showUnauthenticatedUI();
  }
};

init();