File size: 11,408 Bytes
c49cb47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e32a60
c49cb47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e32a60
c49cb47
 
1e32a60
 
 
 
 
c49cb47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e32a60
c49cb47
 
1e32a60
 
 
 
 
c49cb47
 
 
 
 
1e32a60
 
c49cb47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1e32a60
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c49cb47
 
 
 
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/**
 * HuggingFace Dataset Viewer API wrapper
 * Handles fetching data from the datasets-server API with caching and error handling
 */

class DatasetAPI {
    constructor() {
        this.baseURL = 'https://datasets-server.huggingface.co';
        this.cache = new Map();
        this.cacheExpiry = 45 * 60 * 1000; // 45 minutes (conservative for signed URLs)
        this.rowsPerFetch = 100; // API maximum
    }

    /**
     * Check if a dataset is valid and has viewer enabled
     */
    async validateDataset(datasetId) {
        try {
            const response = await fetch(`${this.baseURL}/is-valid?dataset=${encodeURIComponent(datasetId)}`);
            if (!response.ok) {
                throw new Error(`Failed to validate dataset: ${response.statusText}`);
            }
            const data = await response.json();
            
            if (!data.viewer) {
                throw new Error('Dataset viewer is not available for this dataset');
            }
            
            return true;
        } catch (error) {
            throw new Error(`Dataset validation failed: ${error.message}`);
        }
    }

    /**
     * Get dataset info including splits and configs
     */
    async getDatasetInfo(datasetId) {
        const cacheKey = `info_${datasetId}`;
        const cached = this.getFromCache(cacheKey);
        if (cached) return cached;

        try {
            const response = await fetch(`${this.baseURL}/splits?dataset=${encodeURIComponent(datasetId)}`);
            if (!response.ok) {
                throw new Error(`Failed to get dataset info: ${response.statusText}`);
            }
            const data = await response.json();
            
            // Extract the default config and split
            const defaultConfig = data.splits[0]?.config || 'default';
            const defaultSplit = data.splits.find(s => s.split === 'train')?.split || data.splits[0]?.split || 'train';
            
            const info = {
                configs: [...new Set(data.splits.map(s => s.config))],
                splits: [...new Set(data.splits.map(s => s.split))],
                defaultConfig,
                defaultSplit,
                raw: data
            };
            
            this.setCache(cacheKey, info);
            return info;
        } catch (error) {
            throw new Error(`Failed to get dataset info: ${error.message}`);
        }
    }

    /**
     * Get the total number of rows in a dataset
     */
    async getTotalRows(datasetId, config, split) {
        const cacheKey = `size_${datasetId}_${config}_${split}`;
        const cached = this.getFromCache(cacheKey);
        if (cached) return cached;

        try {
            // First try to get from the size endpoint
            const sizeResponse = await fetch(
                `${this.baseURL}/size?dataset=${encodeURIComponent(datasetId)}&config=${encodeURIComponent(config)}&split=${encodeURIComponent(split)}`
            );
            
            if (sizeResponse.ok) {
                const sizeData = await sizeResponse.json();
                // The API returns num_rows in size.config or size.splits[0]
                const size = sizeData.size?.config?.num_rows || 
                           sizeData.size?.splits?.[0]?.num_rows || 
                           0;
                this.setCache(cacheKey, size);
                return size;
            }

            // Fallback: get first rows and check num_rows_total
            const rowsResponse = await fetch(
                `${this.baseURL}/first-rows?dataset=${encodeURIComponent(datasetId)}&config=${encodeURIComponent(config)}&split=${encodeURIComponent(split)}`
            );
            
            if (!rowsResponse.ok) {
                throw new Error('Unable to determine dataset size');
            }
            
            const rowsData = await rowsResponse.json();
            const size = rowsData.num_rows_total || rowsData.rows?.length || 0;
            this.setCache(cacheKey, size);
            return size;
        } catch (error) {
            console.warn('Failed to get total rows:', error);
            return null;
        }
    }

    /**
     * Fetch rows from the dataset
     */
    async fetchRows(datasetId, config, split, offset, length = this.rowsPerFetch) {
        const cacheKey = `rows_${datasetId}_${config}_${split}_${offset}_${length}`;
        const cached = this.getFromCache(cacheKey);
        if (cached) return cached;

        try {
            const response = await fetch(
                `${this.baseURL}/rows?dataset=${encodeURIComponent(datasetId)}&config=${encodeURIComponent(config)}&split=${encodeURIComponent(split)}&offset=${offset}&length=${length}`
            );
            
            if (!response.ok) {
                if (response.status === 403) {
                    throw new Error('Access denied. This dataset may be private or gated.');
                }
                throw new Error(`Failed to fetch rows: ${response.statusText}`);
            }
            
            const data = await response.json();
            
            // Extract column information
            const columns = this.detectColumns(data.features, data.rows[0]?.row);
            
            const result = {
                rows: data.rows,
                features: data.features,
                columns,
                numRowsTotal: data.num_rows_total,
                partial: data.partial || false
            };
            
            this.setCache(cacheKey, result);
            return result;
        } catch (error) {
            throw new Error(`Failed to fetch rows: ${error.message}`);
        }
    }

    /**
     * Get a single row by index with smart batching
     */
    async getRow(datasetId, config, split, index) {
        // Calculate which batch this index falls into
        const batchStart = Math.floor(index / this.rowsPerFetch) * this.rowsPerFetch;
        const batchData = await this.fetchRows(datasetId, config, split, batchStart, this.rowsPerFetch);
        
        const localIndex = index - batchStart;
        if (localIndex >= 0 && localIndex < batchData.rows.length) {
            return {
                row: batchData.rows[localIndex].row,
                columns: batchData.columns,
                numRowsTotal: batchData.numRowsTotal
            };
        }
        
        throw new Error(`Row ${index} not found`);
    }

    /**
     * Detect column names for image and text data
     */
    detectColumns(features, sampleRow) {
        let imageColumn = null;
        let originalTextColumn = null;
        let improvedTextColumn = null;
        let inferenceInfoColumn = null;

        // Try to detect from features first
        for (const feature of features || []) {
            const name = feature.name;
            const type = feature.type;
            
            // Detect image column
            if (type._type === 'Image' || type.dtype === 'image' || type.feature?._type === 'Image') {
                imageColumn = name;
            }
            
            // Detect text columns based on common patterns
            if (!originalTextColumn && ['text', 'ocr', 'original_text', 'original', 'ground_truth'].includes(name)) {
                originalTextColumn = name;
            }
            
            if (!improvedTextColumn && ['markdown', 'new_ocr', 'corrected_text', 'improved', 'vlm_ocr', 'corrected', 'rolmocr_text'].includes(name)) {
                improvedTextColumn = name;
            }
            
            // Detect inference info column
            if (name === 'inference_info') {
                inferenceInfoColumn = name;
            }
        }

        // Fallback: detect from sample row
        if (sampleRow) {
            const keys = Object.keys(sampleRow);
            
            if (!imageColumn) {
                for (const key of keys) {
                    if (sampleRow[key]?.src && sampleRow[key]?.height !== undefined) {
                        imageColumn = key;
                        break;
                    }
                }
            }
            
            // Additional text column detection from row data
            if (!originalTextColumn) {
                const candidates = ['text', 'ocr', 'original_text', 'original'];
                originalTextColumn = keys.find(k => candidates.includes(k)) || null;
            }
            
            if (!improvedTextColumn) {
                const candidates = ['markdown', 'new_ocr', 'corrected_text', 'improved', 'rolmocr_text'];
                improvedTextColumn = keys.find(k => candidates.includes(k)) || null;
            }
            
            // Check for inference info in sample row
            if (!inferenceInfoColumn && keys.includes('inference_info')) {
                inferenceInfoColumn = 'inference_info';
            }
        }

        return {
            image: imageColumn,
            originalText: originalTextColumn,
            improvedText: improvedTextColumn,
            inferenceInfo: inferenceInfoColumn
        };
    }

    /**
     * Refresh expired image URL by re-fetching the row
     */
    async refreshImageUrl(datasetId, config, split, index) {
        // Clear cache for this specific row batch
        const batchStart = Math.floor(index / this.rowsPerFetch) * this.rowsPerFetch;
        const cacheKey = `rows_${datasetId}_${config}_${split}_${batchStart}_${this.rowsPerFetch}`;
        this.cache.delete(cacheKey);
        
        // Re-fetch the row
        return await this.getRow(datasetId, config, split, index);
    }

    /**
     * Cache management utilities
     */
    getFromCache(key) {
        const cached = this.cache.get(key);
        if (!cached) return null;
        
        if (Date.now() - cached.timestamp > this.cacheExpiry) {
            this.cache.delete(key);
            return null;
        }
        
        return cached.data;
    }

    setCache(key, data) {
        this.cache.set(key, {
            data,
            timestamp: Date.now()
        });
    }

    clearCache() {
        this.cache.clear();
    }
    
    /**
     * Parse inference info JSON safely
     */
    parseInferenceInfo(inferenceInfoData) {
        if (!inferenceInfoData) return null;
        
        try {
            // Handle if it's already an object (some datasets might store it as object)
            if (typeof inferenceInfoData === 'object' && !Array.isArray(inferenceInfoData)) {
                return inferenceInfoData;
            }
            
            // Handle if it's a JSON string
            if (typeof inferenceInfoData === 'string') {
                const parsed = JSON.parse(inferenceInfoData);
                // If it's an array, take the first item
                if (Array.isArray(parsed) && parsed.length > 0) {
                    return parsed[0];
                }
                return parsed;
            }
            
            // Handle if it's already an array
            if (Array.isArray(inferenceInfoData) && inferenceInfoData.length > 0) {
                return inferenceInfoData[0];
            }
            
            return null;
        } catch (error) {
            console.warn('Failed to parse inference info:', error);
            return null;
        }
    }
}

// Export for use in other scripts
window.DatasetAPI = DatasetAPI;