rafmacalaba commited on
Commit
ca7d483
·
1 Parent(s): b675de3

Fix production crash: bundle corpora.json in Git and fetch annotations from HF Dataset

Browse files
Files changed (2) hide show
  1. annotation_data/corpora.json +8 -0
  2. utils/storage.js +55 -24
annotation_data/corpora.json ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ [
2
+ {
3
+ "id": "sample",
4
+ "name": "Sample PDFs",
5
+ "extractions_dir": "sample_extractions",
6
+ "links_file": "sample_data/sample_pdf_links.json"
7
+ }
8
+ ]
utils/storage.js CHANGED
@@ -197,43 +197,74 @@ export async function updateAnnotation(timestamp, docIndex, pageNumber, updates,
197
  }
198
 
199
  /**
200
- * Retrieves all human annotations from local files.
201
  */
202
  export async function getAnnotations(docIndex = null, corpusId = null) {
203
- const { getCorpora } = await import('./config.js');
204
  const corporaList = corpusId ? [getCorpus(corpusId)] : getCorpora();
205
  const results = [];
206
 
207
  for (const corpus of corporaList) {
208
- const extractionsDir = path.join(process.cwd(), 'annotation_data', corpus.extractions_dir);
209
- if (!fs.existsSync(extractionsDir)) continue;
210
-
211
- const dirs = fs.readdirSync(extractionsDir).filter(d => d.startsWith('doc_'));
212
-
213
- for (const dir of dirs) {
214
- const idx = parseInt(dir.replace('doc_', ''), 10);
215
- if (docIndex !== null && idx !== docIndex) continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
216
 
217
- const filePath = path.join(extractionsDir, dir, 'raw', `${dir}_direct_judged.jsonl`);
218
- if (!fs.existsSync(filePath)) continue;
219
 
 
 
220
  try {
221
- const pagesData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
222
- for (const page of pagesData) {
223
- const pageNum = page.document?.pages?.[0];
224
- for (const ds of (page.datasets || [])) {
225
- if (ds.annotator) {
226
- results.push({
227
- ...ds,
228
- corpus: corpus.id,
229
- document_index: idx,
230
- page_number: pageNum,
231
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
232
  }
233
  }
234
  }
235
  } catch (e) {
236
- console.error(`Error reading ${filePath}:`, e);
237
  }
238
  }
239
  }
 
197
  }
198
 
199
  /**
200
+ * Retrieves all human annotations.
201
  */
202
  export async function getAnnotations(docIndex = null, corpusId = null) {
203
+ const { getCorpora, HF_DATASET_BASE_URL, getLinksRepoPath, getDocRepoPath } = await import('./config.js');
204
  const corporaList = corpusId ? [getCorpus(corpusId)] : getCorpora();
205
  const results = [];
206
 
207
  for (const corpus of corporaList) {
208
+ let links = [];
209
+ if (isHFSpace()) {
210
+ try {
211
+ const linksPath = getLinksRepoPath(corpus);
212
+ const linksUrl = `${HF_DATASET_BASE_URL}/raw/main/${linksPath}`;
213
+ const res = await fetch(linksUrl, {
214
+ headers: { 'Authorization': `Bearer ${process.env.HF_TOKEN}` },
215
+ cache: 'no-store'
216
+ });
217
+ if (res.ok) {
218
+ links = await res.json();
219
+ }
220
+ } catch (e) {
221
+ console.warn(`Error fetching links file for ${corpus.id} in getAnnotations:`, e.message);
222
+ }
223
+ } else {
224
+ const filePath = path.join(process.cwd(), 'annotation_data', corpus.links_file);
225
+ if (fs.existsSync(filePath)) {
226
+ links = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
227
+ }
228
+ }
229
 
230
+ const targetLinks = links.filter(l => l.status === 'success' && (docIndex === null || l.index === docIndex));
 
231
 
232
+ for (const link of targetLinks) {
233
+ let pagesData = null;
234
  try {
235
+ if (isHFSpace()) {
236
+ const docRepoPath = getDocRepoPath(corpus, link.index);
237
+ const docUrl = `${HF_DATASET_BASE_URL}/raw/main/${docRepoPath}`;
238
+ const res = await fetch(docUrl, {
239
+ headers: { 'Authorization': `Bearer ${process.env.HF_TOKEN}` }
240
+ });
241
+ if (res.ok) {
242
+ pagesData = await res.json();
243
+ }
244
+ } else {
245
+ const filePath = getDocLocalPath(corpus, link.index);
246
+ if (fs.existsSync(filePath)) {
247
+ pagesData = JSON.parse(fs.readFileSync(filePath, 'utf-8'));
248
+ }
249
+ }
250
+
251
+ if (pagesData) {
252
+ for (const page of pagesData) {
253
+ const pageNum = page.document?.pages?.[0];
254
+ for (const ds of (page.datasets || [])) {
255
+ if (ds.annotator) {
256
+ results.push({
257
+ ...ds,
258
+ corpus: corpus.id,
259
+ document_index: link.index,
260
+ page_number: pageNum,
261
+ });
262
+ }
263
  }
264
  }
265
  }
266
  } catch (e) {
267
+ console.error(`Error loading annotations for doc_${link.index}:`, e);
268
  }
269
  }
270
  }