Repository navigation
Expand file tree
/
Copy pathimport-ner-task.ts
More file actions
73 lines (59 loc) · 2.15 KB
/
Copy pathimport-ner-task.ts
File metadata and controls
73 lines (59 loc) · 2.15 KB
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
/* eslint-disable no-continue */
/* eslint-disable no-restricted-syntax */
import { readFileSync } from 'fs';
import { DEFAULT_TASK_DIR } from '@/constants';
import { walkDirectoryByGenre } from '@/lib/crawler/fileUtils';
import { type GenreParams } from '@/lib/crawler/schema';
import { NerDataSchema } from '@/lib/ner/schema';
import { logger } from '@/logger/logger';
import 'dotenv/config';
const main = async () => {
const BASE_URL = process.env.LABEL_STUDIO_URL || 'http://localhost:8080';
const LABEL_STUDIO_LEGACY_TOKEN = process.env.LABEL_STUDIO_LEGACY_TOKEN || '';
const LABEL_STUDIO_PROJECT_TITLE =
process.env.LABEL_STUDIO_PROJECT_TITLE || 'v-bible';
const currentGenre = 'N' satisfies GenreParams['genre'];
// NOTE: Get all json files from dir.
const files = walkDirectoryByGenre(DEFAULT_TASK_DIR, currentGenre);
const jsonFiles = files.filter((file) => file.endsWith('.json'));
const allProjects = await (
await fetch(`${BASE_URL}/api/projects/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${LABEL_STUDIO_LEGACY_TOKEN}`,
},
})
).json();
if (allProjects?.status_code === 401) {
logger.error(
'Invalid Label Studio legacy token. Please check your environment variables.',
);
}
const projectInfo = allProjects?.results?.find(
(project: Record<string, unknown>) =>
project.title === LABEL_STUDIO_PROJECT_TITLE,
);
for await (const taskFilePath of jsonFiles) {
const fileData = JSON.parse(readFileSync(taskFilePath, 'utf-8'));
const nerParse = NerDataSchema.array().safeParse(fileData);
if (!nerParse.success) {
logger.error(
`Invalid NER data in file ${taskFilePath}: ${nerParse.error.message}`,
);
continue;
}
await fetch(`${BASE_URL}/api/projects/${projectInfo.id}/import`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Token ${LABEL_STUDIO_LEGACY_TOKEN}`,
},
body: JSON.stringify(nerParse.data),
});
logger.info(
`Imported NER data from ${taskFilePath} to Label Studio project`,
);
}
};
main();