Charlie commited on
Commit
c433511
·
1 Parent(s): c63e17f

update files

Browse files
Files changed (4) hide show
  1. dist/main.js +40 -0
  2. index.html +4 -0
  3. src/main.ts +49 -0
  4. styles/main.css +37 -0
dist/main.js CHANGED
@@ -299,6 +299,23 @@ async function getOrganizationInfo(accessToken) {
299
  }
300
  return response.json();
301
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
302
  var init = async () => {
303
  await oauthManager.handleRedirect();
304
  if (oauthManager.isAuthenticated()) {
@@ -306,6 +323,29 @@ var init = async () => {
306
  if (!accessToken) {
307
  throw new Error("Access token not found");
308
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
309
  try {
310
  const orgInfo = await getOrganizationInfo(accessToken);
311
  const preElement = document.querySelector("pre");
 
299
  }
300
  return response.json();
301
  }
302
+ async function createRepository(accessToken, name) {
303
+ const response = await fetch("https://huggingface.co/api/repos/create", {
304
+ method: "POST",
305
+ headers: {
306
+ "Content-Type": "application/json",
307
+ Authorization: `Bearer ${accessToken}`
308
+ },
309
+ body: JSON.stringify({
310
+ type: "model",
311
+ name,
312
+ private: false
313
+ })
314
+ });
315
+ if (!response.ok) {
316
+ throw new Error(`Failed to create repository: ${response.statusText}`);
317
+ }
318
+ }
319
  var init = async () => {
320
  await oauthManager.handleRedirect();
321
  if (oauthManager.isAuthenticated()) {
 
323
  if (!accessToken) {
324
  throw new Error("Access token not found");
325
  }
326
+ const repoForm = document.getElementById("repo-form");
327
+ if (repoForm) {
328
+ repoForm.style.removeProperty("display");
329
+ }
330
+ const createRepoButton = document.getElementById("create-repo");
331
+ const repoNameInput = document.getElementById(
332
+ "repo-name"
333
+ );
334
+ if (createRepoButton && repoNameInput) {
335
+ createRepoButton.onclick = async () => {
336
+ const repoName = repoNameInput.value.trim();
337
+ if (repoName) {
338
+ try {
339
+ await createRepository(accessToken, repoName);
340
+ repoNameInput.value = "";
341
+ alert("Repository created successfully!");
342
+ } catch (error) {
343
+ console.error("Failed to create repository:", error);
344
+ alert("Failed to create repository. Please try again.");
345
+ }
346
+ }
347
+ };
348
+ }
349
  try {
350
  const orgInfo = await getOrganizationInfo(accessToken);
351
  const preElement = document.querySelector("pre");
index.html CHANGED
@@ -15,6 +15,10 @@
15
  <button id="signin" style="display: none;" class="auth-button">Sign in with HuggingFace</button>
16
  <button id="signout" style="display: none;" class="auth-button">Sign out</button>
17
  </div>
 
 
 
 
18
  <div id="content">
19
  <h2>OAuth Result</h2>
20
  <pre class="oauth-result"></pre>
 
15
  <button id="signin" style="display: none;" class="auth-button">Sign in with HuggingFace</button>
16
  <button id="signout" style="display: none;" class="auth-button">Sign out</button>
17
  </div>
18
+ <div id="repo-form" style="display: none;" class="repo-form">
19
+ <input type="text" id="repo-name" placeholder="Enter repository name" class="repo-input">
20
+ <button id="create-repo" class="repo-button">Create Repository</button>
21
+ </div>
22
  <div id="content">
23
  <h2>OAuth Result</h2>
24
  <pre class="oauth-result"></pre>
src/main.ts CHANGED
@@ -39,6 +39,28 @@ async function getOrganizationInfo(
39
  return response.json();
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  const init = async (): Promise<void> => {
43
  await oauthManager.handleRedirect();
44
 
@@ -48,6 +70,33 @@ const init = async (): Promise<void> => {
48
  throw new Error("Access token not found");
49
  }
50
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
51
  // Get organization info after successful authentication
52
  try {
53
  const orgInfo = await getOrganizationInfo(accessToken);
 
39
  return response.json();
40
  }
41
 
42
+ async function createRepository(
43
+ accessToken: string,
44
+ name: string
45
+ ): Promise<void> {
46
+ const response = await fetch("https://huggingface.co/api/repos/create", {
47
+ method: "POST",
48
+ headers: {
49
+ "Content-Type": "application/json",
50
+ Authorization: `Bearer ${accessToken}`,
51
+ },
52
+ body: JSON.stringify({
53
+ type: "model",
54
+ name: name,
55
+ private: false,
56
+ }),
57
+ });
58
+
59
+ if (!response.ok) {
60
+ throw new Error(`Failed to create repository: ${response.statusText}`);
61
+ }
62
+ }
63
+
64
  const init = async (): Promise<void> => {
65
  await oauthManager.handleRedirect();
66
 
 
70
  throw new Error("Access token not found");
71
  }
72
 
73
+ // Show repo form when logged in
74
+ const repoForm = document.getElementById("repo-form");
75
+ if (repoForm) {
76
+ repoForm.style.removeProperty("display");
77
+ }
78
+
79
+ // Add create repo functionality
80
+ const createRepoButton = document.getElementById("create-repo");
81
+ const repoNameInput = document.getElementById(
82
+ "repo-name"
83
+ ) as HTMLInputElement;
84
+ if (createRepoButton && repoNameInput) {
85
+ createRepoButton.onclick = async () => {
86
+ const repoName = repoNameInput.value.trim();
87
+ if (repoName) {
88
+ try {
89
+ await createRepository(accessToken, repoName);
90
+ repoNameInput.value = ""; // Clear input after success
91
+ alert("Repository created successfully!");
92
+ } catch (error) {
93
+ console.error("Failed to create repository:", error);
94
+ alert("Failed to create repository. Please try again.");
95
+ }
96
+ }
97
+ };
98
+ }
99
+
100
  // Get organization info after successful authentication
101
  try {
102
  const orgInfo = await getOrganizationInfo(accessToken);
styles/main.css CHANGED
@@ -49,6 +49,43 @@ h2 {
49
  background-color: #ff8500;
50
  }
51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
52
  #content {
53
  padding: 2rem;
54
  background-color: white;
 
49
  background-color: #ff8500;
50
  }
51
 
52
+ .repo-form {
53
+ margin: 2rem auto;
54
+ max-width: 400px;
55
+ display: flex;
56
+ gap: 1rem;
57
+ }
58
+
59
+ .repo-input {
60
+ flex: 1;
61
+ padding: 0.8rem;
62
+ border: 2px solid #e0e0e0;
63
+ border-radius: 6px;
64
+ font-size: 1rem;
65
+ transition: border-color 0.2s ease;
66
+ }
67
+
68
+ .repo-input:focus {
69
+ outline: none;
70
+ border-color: #ff9d00;
71
+ }
72
+
73
+ .repo-button {
74
+ background-color: #ff9d00;
75
+ color: white;
76
+ border: none;
77
+ padding: 0.8rem 1.5rem;
78
+ border-radius: 6px;
79
+ font-size: 1rem;
80
+ cursor: pointer;
81
+ transition: background-color 0.2s ease;
82
+ white-space: nowrap;
83
+ }
84
+
85
+ .repo-button:hover {
86
+ background-color: #ff8500;
87
+ }
88
+
89
  #content {
90
  padding: 2rem;
91
  background-color: white;