-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathapp_configs_builder.py
73 lines (63 loc) · 2.83 KB
/
app_configs_builder.py
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
import io
import requests
import json
# NOTE: The app configs are mostly taken from https://github.com./HumanSignal/label-studio/tree/develop/web/libs/editor/src/examples
# You can find more examples there and add them to `app_configs.json`
# Loading the task configs from the `app_configs.json` file.
# When uroll=True, configs with a list of tasks will be expanded into multiple
# configs with one task each. (I have not been able to make LSF accept a
# list of tasks, so I send each task individually.)
# The config name will have the task id appended to it, <config name>@<task id>.
# The function will provide task lists when unroll=False.
def get_app_config(unroll=True):
print('get_app_config() - Cache Hit!')
label_studio_app_config = json.loads(io.open('app_configs.json', 'r', encoding='utf8').read())
user = label_studio_app_config['user']
interfaces = label_studio_app_config['interfaces']
task_configs_source = label_studio_app_config['task_configs']
task_configs = []
for i, tc in enumerate(task_configs_source):
# Inflating the task configs.
config_spec = tc['config']
if config_spec.get('url', None):
config = requests.get(config_spec['url']).text
elif config_spec.get('file', None):
config = io.open(config_spec['file'], 'r', encoding='utf8').read()
else:
raise ValueError(f'Valid config spec not found in {tc}')
task_spec = tc['task']
if task_spec.get('url', None):
task = json.loads(requests.get(task_spec['url']).text)
elif task_spec.get('file', None):
task = json.loads(io.open(task_spec['file'], 'r', encoding='utf8').read())
elif task_spec.get('object', None):
task = task_spec['object']
else:
raise ValueError(f'Valid task spec not found in {tc}')
if unroll:
if isinstance(task, dict):
task = [task]
for j, task_item in enumerate(task):
id = task_item.get('id', j)
name = f"{tc['name']}@{id}"
tc_unrolled = {
'id': j,
'name': name,
'description': tc['description'],
'annotation_type': tc['annotation_type'],
'config': config,
'task': task_item,
}
task_configs.append(tc_unrolled)
else:
tc_untouched = {
'id': tc.get('id', i),
'name': tc['name'],
'description': tc['description'],
'annotation_type': tc['annotation_type'],
'config': config,
'task': task,
}
task_configs.append(tc_untouched)
print('Task Configs Head:\n', task_configs[:5])
return user, interfaces, task_configs