Spaces:
Running
Running
File size: 4,825 Bytes
3f209a4 9976904 3f209a4 9976904 |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
// Hugging Face Dataset Server API Service
import type {
DatasetRowsResponse,
DatasetFirstRowsResponse,
DatasetSplitsResponse,
DatasetError,
} from "./types";
import yaml from "js-yaml";
const BASE_URL = "https://datasets-server.huggingface.co";
/**
* Fetch the first 100 rows of a dataset
*/
export async function fetchFirstRows(
dataset: string,
config: string = "default",
split: string = "train",
token?: string
): Promise<DatasetFirstRowsResponse> {
const url = `${BASE_URL}/first-rows?dataset=${encodeURIComponent(
dataset
)}&config=${encodeURIComponent(config)}&split=${encodeURIComponent(split)}`;
const headers: HeadersInit = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, { headers });
if (!response.ok) {
const error: DatasetError = await response.json();
throw new Error(
error.error || `Failed to fetch dataset: ${response.statusText}`
);
}
return response.json();
}
/**
* Fetch paginated rows from a dataset
* @param offset Starting row index (0-based)
* @param length Number of rows to fetch (max 100)
*/
export async function fetchRows(
dataset: string,
config: string = "default",
split: string = "train",
offset: number = 0,
length: number = 100,
token?: string
): Promise<DatasetRowsResponse> {
const url = `${BASE_URL}/rows?dataset=${encodeURIComponent(
dataset
)}&config=${encodeURIComponent(config)}&split=${encodeURIComponent(
split
)}&offset=${offset}&length=${length}`;
const headers: HeadersInit = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, { headers });
if (!response.ok) {
const error: DatasetError = await response.json();
throw new Error(
error.error || `Failed to fetch dataset: ${response.statusText}`
);
}
return response.json();
}
/**
* List available splits for a dataset
*/
export async function fetchSplits(
dataset: string,
token?: string
): Promise<DatasetSplitsResponse> {
const url = `${BASE_URL}/splits?dataset=${encodeURIComponent(dataset)}`;
const headers: HeadersInit = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, { headers });
if (!response.ok) {
const error: DatasetError = await response.json();
throw new Error(
error.error || `Failed to fetch splits: ${response.statusText}`
);
}
return response.json();
}
/**
* Check if a dataset is valid and accessible
*/
export async function checkDatasetValid(
dataset: string,
token?: string
): Promise<boolean> {
const url = `${BASE_URL}/is-valid?dataset=${encodeURIComponent(dataset)}`;
const headers: HeadersInit = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const response = await fetch(url, { headers });
if (!response.ok) {
return false;
}
const data = await response.json();
return data.valid === true;
}
/**
* Fetch all rows from a jurisdiction config (for term/definition data)
*/
export async function fetchAllRows(
dataset: string,
config: string,
split: string = "train",
token?: string
): Promise<any[]> {
const allRows: any[] = [];
let offset = 0;
const batchSize = 100;
let hasMore = true;
while (hasMore) {
const response = await fetchRows(
dataset,
config,
split,
offset,
batchSize,
token
);
const rows = response.rows.map((r) => r.row);
allRows.push(...rows);
offset += rows.length;
hasMore = offset < response.num_rows_total;
}
return allRows;
}
export async function fetchDatasetFileRaw(
dataset: string, // "owner/dataset-name"
filePath: string, // "config.yaml"
branch = "main",
token?: string
): Promise<string> {
const url = `https://huggingface.co/datasets/${dataset}/raw/${encodeURIComponent(
branch
)}/${filePath}`;
const headers: HeadersInit = {};
if (token) {
headers["Authorization"] = `Bearer ${token}`;
}
const res = await fetch(url, { headers });
if (!res.ok)
throw new Error(`Failed to fetch file: ${res.status} ${res.statusText}`);
return res.text();
}
export interface DatasetConfigYaml {
scoring_model: string;
scoring_provider: string;
synthesis_model: string;
synthesis_provider: string;
jurisdiction1: string;
jurisdiction2: string;
description: string;
grading_templates_1_uid: string;
grading_templates_2_uid: string;
generation_date?: string;
}
export async function fetchDatasetConfigYaml(
dataset: string,
filePath = "config.yaml",
branch = "main",
token?: string
): Promise<DatasetConfigYaml | null> {
const text = await fetchDatasetFileRaw(dataset, filePath, branch, token);
return yaml.load(text) as DatasetConfigYaml;
}
|