multimodalart HF Staff commited on
Commit
b014700
·
verified ·
1 Parent(s): 5e5693e

Update js/index.js

Browse files
Files changed (1) hide show
  1. js/index.js +23 -3
js/index.js CHANGED
@@ -41,13 +41,17 @@ document.getElementById('repoForm').addEventListener('submit', async function (e
41
  document.getElementById('copyButton').style.display = 'none';
42
  document.getElementById('downloadButton').style.display = 'none';
43
 
 
 
44
 
45
  try {
46
  const repoInfo = parseRepoUrl(repoUrl);
 
47
  currentRepoInfo = { ...repoInfo, accessToken }; // Store for later use
48
 
49
  let tree;
50
  if (repoInfo.source === 'github') {
 
51
  const { owner, repo, lastString } = repoInfo;
52
  let refFromUrl = '';
53
  let pathFromUrl = '';
@@ -68,12 +72,15 @@ document.getElementById('repoForm').addEventListener('submit', async function (e
68
  tree = await fetchGitHubRepoTree(owner, repo, sha, accessToken);
69
 
70
  } else if (repoInfo.source === 'huggingface') {
 
71
  const { owner, repo, repo_type, lastString } = repoInfo;
72
  let refFromUrl = 'main'; // Default branch
73
  let pathFromUrl = '';
74
 
75
  if (lastString) {
 
76
  const refs = await getHuggingFaceReferences(owner, repo, repo_type, accessToken);
 
77
  const matchingRef = refs.find(ref => lastString.startsWith(ref + '/'));
78
  if (matchingRef) {
79
  refFromUrl = matchingRef;
@@ -84,16 +91,19 @@ document.getElementById('repoForm').addEventListener('submit', async function (e
84
  }
85
  }
86
 
 
87
  currentRepoInfo.ref = refFromUrl;
88
  tree = await fetchHuggingFaceTree(owner, repo, repo_type, refFromUrl, pathFromUrl, accessToken);
89
  }
90
-
 
91
  displayDirectoryStructure(tree);
92
  document.getElementById('generateTextButton').style.display = 'flex';
93
  document.getElementById('downloadZipButton').style.display = 'flex';
94
  outputText.value = 'Select files and click "Generate Text File" or "Download Zip".';
95
 
96
  } catch (error) {
 
97
  outputText.value = `Error fetching repository contents: ${error.message}\n\n` +
98
  "Please ensure:\n" +
99
  "1. The repository URL is correct and accessible.\n" +
@@ -279,6 +289,7 @@ async function fetchGitHubRepoTree(owner, repo, sha, token) {
279
  async function fetchHuggingFaceTree(owner, repo, repo_type, ref, path, token) {
280
  const typePath = repo_type === 'model' ? 'models' : repo_type === 'dataset' ? 'datasets' : 'spaces';
281
  const url = `https://huggingface.co/api/${typePath}/${owner}/${repo}/tree/${ref}`;
 
282
 
283
  const headers = {};
284
  if (token) headers['Authorization'] = `Bearer ${token}`;
@@ -287,12 +298,16 @@ async function fetchHuggingFaceTree(owner, repo, repo_type, ref, path, token) {
287
  if (!response.ok) handleFetchError(response, 'huggingface');
288
 
289
  let tree = await response.json();
 
 
290
 
291
  if (path) {
 
292
  tree = tree.filter(item => item.path.startsWith(path + '/') || item.path === path);
 
293
  }
294
 
295
- return tree.map(item => {
296
  let repoIdForUrl;
297
  switch (repo_type) {
298
  case 'dataset':
@@ -304,13 +319,18 @@ async function fetchHuggingFaceTree(owner, repo, repo_type, ref, path, token) {
304
  default: // model
305
  repoIdForUrl = `${owner}/${repo}`;
306
  }
307
- return {
308
  path: item.path,
309
  type: (item.type === 'file' || item.type === 'lfs') ? 'blob' : 'tree',
310
  urlType: 'hf',
311
  url: `https://huggingface.co/${repoIdForUrl}/raw/${ref}/${item.path}`
312
  };
 
 
313
  });
 
 
 
314
  }
315
 
316
  // Handle fetch errors
 
41
  document.getElementById('copyButton').style.display = 'none';
42
  document.getElementById('downloadButton').style.display = 'none';
43
 
44
+ console.log("--- Repo Form Submitted ---");
45
+ console.log("URL:", repoUrl);
46
 
47
  try {
48
  const repoInfo = parseRepoUrl(repoUrl);
49
+ console.log("Parsed Repo Info:", repoInfo);
50
  currentRepoInfo = { ...repoInfo, accessToken }; // Store for later use
51
 
52
  let tree;
53
  if (repoInfo.source === 'github') {
54
+ console.log("Fetching from GitHub...");
55
  const { owner, repo, lastString } = repoInfo;
56
  let refFromUrl = '';
57
  let pathFromUrl = '';
 
72
  tree = await fetchGitHubRepoTree(owner, repo, sha, accessToken);
73
 
74
  } else if (repoInfo.source === 'huggingface') {
75
+ console.log("Fetching from Hugging Face...");
76
  const { owner, repo, repo_type, lastString } = repoInfo;
77
  let refFromUrl = 'main'; // Default branch
78
  let pathFromUrl = '';
79
 
80
  if (lastString) {
81
+ console.log("Fetching HF references for lastString:", lastString);
82
  const refs = await getHuggingFaceReferences(owner, repo, repo_type, accessToken);
83
+ console.log("Available HF Refs:", refs);
84
  const matchingRef = refs.find(ref => lastString.startsWith(ref + '/'));
85
  if (matchingRef) {
86
  refFromUrl = matchingRef;
 
91
  }
92
  }
93
 
94
+ console.log(`Determined HF Ref: '${refFromUrl}', Path: '${pathFromUrl}'`);
95
  currentRepoInfo.ref = refFromUrl;
96
  tree = await fetchHuggingFaceTree(owner, repo, repo_type, refFromUrl, pathFromUrl, accessToken);
97
  }
98
+
99
+ console.log("Final tree object before display:", tree);
100
  displayDirectoryStructure(tree);
101
  document.getElementById('generateTextButton').style.display = 'flex';
102
  document.getElementById('downloadZipButton').style.display = 'flex';
103
  outputText.value = 'Select files and click "Generate Text File" or "Download Zip".';
104
 
105
  } catch (error) {
106
+ console.error("Error fetching repository contents:", error);
107
  outputText.value = `Error fetching repository contents: ${error.message}\n\n` +
108
  "Please ensure:\n" +
109
  "1. The repository URL is correct and accessible.\n" +
 
289
  async function fetchHuggingFaceTree(owner, repo, repo_type, ref, path, token) {
290
  const typePath = repo_type === 'model' ? 'models' : repo_type === 'dataset' ? 'datasets' : 'spaces';
291
  const url = `https://huggingface.co/api/${typePath}/${owner}/${repo}/tree/${ref}`;
292
+ console.log("Fetching HF tree from URL:", url);
293
 
294
  const headers = {};
295
  if (token) headers['Authorization'] = `Bearer ${token}`;
 
298
  if (!response.ok) handleFetchError(response, 'huggingface');
299
 
300
  let tree = await response.json();
301
+ console.log("Raw HF API response:", JSON.parse(JSON.stringify(tree)));
302
+
303
 
304
  if (path) {
305
+ console.log("Filtering HF tree with path:", path);
306
  tree = tree.filter(item => item.path.startsWith(path + '/') || item.path === path);
307
+ console.log("Filtered HF tree:", JSON.parse(JSON.stringify(tree)));
308
  }
309
 
310
+ const mappedTree = tree.map(item => {
311
  let repoIdForUrl;
312
  switch (repo_type) {
313
  case 'dataset':
 
319
  default: // model
320
  repoIdForUrl = `${owner}/${repo}`;
321
  }
322
+ const mappedItem = {
323
  path: item.path,
324
  type: (item.type === 'file' || item.type === 'lfs') ? 'blob' : 'tree',
325
  urlType: 'hf',
326
  url: `https://huggingface.co/${repoIdForUrl}/raw/${ref}/${item.path}`
327
  };
328
+ // console.log("Mapping item:", item, "to:", mappedItem);
329
+ return mappedItem;
330
  });
331
+
332
+ console.log("Mapped HF tree:", mappedTree);
333
+ return mappedTree;
334
  }
335
 
336
  // Handle fetch errors