Spaces:
Running on CPU Upgrade
Running on CPU Upgrade
File size: 7,305 Bytes
61d29fc | 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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | /**
* HuggingFace Datasets Server API Client
*
* Query datasets hosted on HuggingFace Hub using the free Datasets Server API.
* No authentication required for public datasets!
*
* API Docs: https://huggingface.co/docs/datasets-server
*/
const DATASETS_SERVER_URL = 'https://datasets-server.huggingface.co';
export interface HFDatasetConfig {
dataset: string; // e.g., "CommunityOne/oral-health-nonprofits"
config?: string; // Default: "default"
split: string; // e.g., "organizations", "financials"
}
export interface HFRow {
row_idx: number;
row: Record<string, any>;
truncated_cells: string[];
}
export interface HFRowsResponse {
features: Array<{
feature_idx: number;
name: string;
type: any;
}>;
rows: HFRow[];
num_rows_total: number;
num_rows_per_page: number;
partial: boolean;
}
export interface HFSearchResponse {
features: Array<{
feature_idx: number;
name: string;
type: any;
}>;
rows: HFRow[];
num_rows_total: number;
num_rows_per_page: number;
partial: boolean;
truncated: boolean;
}
/**
* Fetch rows from a HuggingFace dataset
*
* @param config - Dataset configuration
* @param offset - Starting row (default: 0)
* @param length - Number of rows to fetch (max: 100)
* @returns Promise with dataset rows
*
* @example
* const nonprofits = await fetchHFRows({
* dataset: "CommunityOne/oral-health-nonprofits",
* split: "organizations"
* }, 0, 100);
*/
export async function fetchHFRows(
config: HFDatasetConfig,
offset = 0,
length = 100
): Promise<HFRowsResponse> {
const params = new URLSearchParams({
dataset: config.dataset,
config: config.config || 'default',
split: config.split,
offset: offset.toString(),
length: Math.min(length, 100).toString() // API max is 100
});
const url = `${DATASETS_SERVER_URL}/rows?${params}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HuggingFace API error: ${response.statusText}`);
}
return response.json();
}
/**
* Search within a HuggingFace dataset
*
* @param config - Dataset configuration
* @param query - Search query string
* @param offset - Starting row (default: 0)
* @param length - Number of results (max: 100)
* @returns Promise with search results
*
* @example
* const dentalOrgs = await searchHFDataset({
* dataset: "CommunityOne/oral-health-nonprofits",
* split: "organizations"
* }, "dental");
*/
export async function searchHFDataset(
config: HFDatasetConfig,
query: string,
offset = 0,
length = 100
): Promise<HFSearchResponse> {
const params = new URLSearchParams({
dataset: config.dataset,
config: config.config || 'default',
split: config.split,
query: query,
offset: offset.toString(),
length: Math.min(length, 100).toString()
});
const url = `${DATASETS_SERVER_URL}/search?${params}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HuggingFace search error: ${response.statusText}`);
}
return response.json();
}
/**
* Get dataset size (number of rows)
*
* @param config - Dataset configuration
* @returns Promise with dataset size info
*/
export async function getHFDatasetSize(config: HFDatasetConfig): Promise<{
dataset: string;
config: string;
split: string;
num_rows_total: number;
num_rows_per_page: number;
partial: boolean;
}> {
const params = new URLSearchParams({
dataset: config.dataset,
config: config.config || 'default',
split: config.split
});
const url = `${DATASETS_SERVER_URL}/size?${params}`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`HuggingFace API error: ${response.statusText}`);
}
return response.json();
}
/**
* Fetch all nonprofits (paginated helper)
*
* @param dataset - HuggingFace dataset name
* @param split - Dataset split (default: "organizations")
* @param maxRows - Maximum total rows to fetch
* @returns Promise with all rows combined
*
* @example
* const allNonprofits = await fetchAllNonprofits(
* "CommunityOne/oral-health-nonprofits",
* "organizations",
* 1000
* );
*/
export async function fetchAllNonprofits(
dataset: string,
split = 'organizations',
maxRows = 1000
): Promise<any[]> {
const allRows: any[] = [];
let offset = 0;
const batchSize = 100; // API max per request
while (offset < maxRows) {
const response = await fetchHFRows(
{ dataset, split },
offset,
Math.min(batchSize, maxRows - offset)
);
const rows = response.rows.map(r => r.row);
allRows.push(...rows);
// Stop if we've fetched all available rows
if (rows.length < batchSize) {
break;
}
offset += batchSize;
}
return allRows;
}
/**
* Filter nonprofits by state
*
* @param dataset - HuggingFace dataset name
* @param stateCode - Two-letter state code (e.g., "AL", "CA")
* @param maxRows - Maximum rows to fetch
* @returns Promise with filtered nonprofits
*
* @example
* const alabamaNonprofits = await fetchNonprofitsByState(
* "CommunityOne/one-nonprofits-organizations",
* "AL",
* 5000
* );
*/
export async function fetchNonprofitsByState(
dataset: string,
stateCode: string,
maxRows = 5000
): Promise<any[]> {
const allRows = await fetchAllNonprofits(dataset, 'organizations', maxRows);
return allRows.filter(row => row.state === stateCode);
}
/**
* Filter nonprofits by NTEE code
*
* @param dataset - HuggingFace dataset name
* @param nteePrefix - NTEE code prefix (e.g., "E" for health, "X" for religion)
* @param maxRows - Maximum rows to fetch
* @returns Promise with filtered nonprofits
*
* @example
* const healthOrgs = await fetchNonprofitsByNTEE(
* "CommunityOne/one-nonprofits-organizations",
* "E",
* 10000
* );
*/
export async function fetchNonprofitsByNTEE(
dataset: string,
nteePrefix: string,
maxRows = 10000
): Promise<any[]> {
const allRows = await fetchAllNonprofits(dataset, 'organizations', maxRows);
return allRows.filter(row => row.ntee_code?.startsWith(nteePrefix));
}
/**
* Nonprofit search with filters
*
* @param options - Search options
* @returns Promise with search results
*
* @example
* const results = await searchNonprofits({
* dataset: "CommunityOne/one-nonprofits-organizations",
* query: "dental",
* state: "CA",
* nteeCode: "E",
* limit: 100
* });
*/
export async function searchNonprofits(options: {
dataset: string;
query?: string;
state?: string;
nteeCode?: string;
limit?: number;
}): Promise<any[]> {
const { dataset, query, state, nteeCode, limit = 100 } = options;
let results: any[];
if (query) {
// Use HuggingFace search API
const response = await searchHFDataset(
{ dataset, split: 'organizations' },
query,
0,
limit
);
results = response.rows.map(r => r.row);
} else {
// Fetch and filter
results = await fetchAllNonprofits(dataset, 'organizations', limit);
}
// Apply filters
if (state) {
results = results.filter(row => row.state === state);
}
if (nteeCode) {
results = results.filter(row => row.ntee_code?.startsWith(nteeCode));
}
return results.slice(0, limit);
}
|