cfahlgren1 HF Staff commited on
Commit
42fa072
·
1 Parent(s): 3c0c362

fix info x2

Browse files
Files changed (1) hide show
  1. src/utils.ts +20 -26
src/utils.ts CHANGED
@@ -16,21 +16,22 @@ export async function fetchJson<T = any>(url: URL, context: string): Promise<T>
16
 
17
  export async function getDefaultConfigAndSplit(datasetId: string): Promise<{ config: string; split: string }> {
18
  const infoUrl = buildUrl("https://datasets-server.huggingface.co/info", { dataset: datasetId });
19
- const infoResult = await fetchJson<any>(infoUrl, "Failed to get dataset info");
 
 
20
 
21
- const configs = Object.keys(infoResult.dataset_info || {});
22
- const firstConfig = configs[0];
23
- if (!firstConfig) {
24
- throw new Error("No configs found for dataset");
25
- }
26
-
27
- const splits = infoResult.dataset_info[firstConfig]?.splits || {};
28
- const firstSplit = Object.keys(splits)[0];
29
- if (!firstSplit) {
30
- throw new Error("No splits found for dataset");
31
- }
32
-
33
- return { config: firstConfig, split: firstSplit };
34
  }
35
 
36
  export async function searchDatasets(query: string): Promise<Array<{ id: string; title: string; url: string }>> {
@@ -51,12 +52,6 @@ export async function fetchDatasetAggregate(datasetId: string): Promise<{
51
  }> {
52
  const { config, split } = await getDefaultConfigAndSplit(datasetId);
53
 
54
- const infoUrl = buildUrl("https://datasets-server.huggingface.co/info", {
55
- dataset: datasetId,
56
- config,
57
- });
58
- const infoResult = await fetchJson<any>(infoUrl, "Failed to get dataset info");
59
-
60
  const rowsUrl = buildUrl("https://datasets-server.huggingface.co/rows", {
61
  dataset: datasetId,
62
  config,
@@ -65,23 +60,23 @@ export async function fetchDatasetAggregate(datasetId: string): Promise<{
65
  length: "50",
66
  });
67
 
68
- let sampleData = "";
69
  try {
70
  const rowsResponse = await fetch(rowsUrl);
71
  if (rowsResponse.ok) {
72
  const rowsResult = await rowsResponse.json();
73
- sampleData = `\n\nSample data (${config}/${split}):\n${JSON.stringify(rowsResult.rows || [], null, 2)}`;
74
  } else {
75
- sampleData = "\n\nSample data: Not available";
76
  }
77
  } catch {
78
- sampleData = "\n\nSample data: Not available";
79
  }
80
 
81
  return {
82
  id: datasetId,
83
  title: datasetId,
84
- text: `Dataset Information:\n${JSON.stringify(infoResult.dataset_info, null, 2)}${sampleData}`,
85
  url: `https://huggingface.co/datasets/${datasetId}`,
86
  };
87
  }
@@ -89,4 +84,3 @@ export async function fetchDatasetAggregate(datasetId: string): Promise<{
89
  export function textContent(payload: unknown) {
90
  return { content: [{ type: "text" as const, text: JSON.stringify(payload) }] };
91
  }
92
-
 
16
 
17
  export async function getDefaultConfigAndSplit(datasetId: string): Promise<{ config: string; split: string }> {
18
  const infoUrl = buildUrl("https://datasets-server.huggingface.co/info", { dataset: datasetId });
19
+ const info = await fetchJson<any>(infoUrl, "Failed to get dataset info");
20
+ const datasetInfo = info?.dataset_info;
21
+ if (!datasetInfo) throw new Error("dataset_info missing in /info response");
22
 
23
+ // Get the first available config
24
+ const configNames = Object.keys(datasetInfo);
25
+ if (configNames.length === 0) throw new Error("No configurations found for dataset");
26
+
27
+ const config = configNames[0];
28
+ const configInfo = datasetInfo[config];
29
+ const splits = configInfo.splits || {};
30
+ const splitNames = Object.keys(splits);
31
+ if (splitNames.length === 0) throw new Error("No splits found for dataset");
32
+
33
+ const split = splitNames[0];
34
+ return { config, split };
 
35
  }
36
 
37
  export async function searchDatasets(query: string): Promise<Array<{ id: string; title: string; url: string }>> {
 
52
  }> {
53
  const { config, split } = await getDefaultConfigAndSplit(datasetId);
54
 
 
 
 
 
 
 
55
  const rowsUrl = buildUrl("https://datasets-server.huggingface.co/rows", {
56
  dataset: datasetId,
57
  config,
 
60
  length: "50",
61
  });
62
 
63
+ let text: string;
64
  try {
65
  const rowsResponse = await fetch(rowsUrl);
66
  if (rowsResponse.ok) {
67
  const rowsResult = await rowsResponse.json();
68
+ text = `Sample data (${config}/${split}):\n${JSON.stringify(rowsResult.rows || [], null, 2)}`;
69
  } else {
70
+ text = "Sample data: Not available";
71
  }
72
  } catch {
73
+ text = "Sample data: Not available";
74
  }
75
 
76
  return {
77
  id: datasetId,
78
  title: datasetId,
79
+ text,
80
  url: `https://huggingface.co/datasets/${datasetId}`,
81
  };
82
  }
 
84
  export function textContent(payload: unknown) {
85
  return { content: [{ type: "text" as const, text: JSON.stringify(payload) }] };
86
  }