Skip to content

Commit

Permalink
Optimize /api/templates
Browse files Browse the repository at this point in the history
  • Loading branch information
nik committed Dec 18, 2024
1 parent db0aeb9 commit 842c951
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions label_studio/projects/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -786,30 +786,36 @@ def perform_create(self, serializer):
self.request.user.active_organization, project, WebhookAction.TASKS_CREATED, [instance]
)
return instance


def read_templates_and_groups():
annotation_templates_dir = find_dir('annotation_templates')
configs = []
for config_file in pathlib.Path(annotation_templates_dir).glob('**/*.yml'):
config = read_yaml(config_file)
if settings.VERSION_EDITION == 'Community':
if settings.VERSION_EDITION.lower() != config.get('type', 'community'):
continue
if config.get('image', '').startswith('/static') and settings.HOSTNAME:
# if hostname set manually, create full image urls
config['image'] = settings.HOSTNAME + config['image']
configs.append(config)
template_groups_file = find_file(os.path.join('annotation_templates', 'groups.txt'))
with open(template_groups_file, encoding='utf-8') as f:
groups = f.read().splitlines()
logger.debug(f'{len(configs)} templates found.')
return {'templates': configs, 'groups': groups}


class TemplateListAPI(generics.ListAPIView):
parser_classes = (JSONParser, FormParser, MultiPartParser)
permission_required = all_permissions.projects_view
swagger_schema = None
# load this once in memory for performance
templates_and_groups = read_templates_and_groups()

def list(self, request, *args, **kwargs):
annotation_templates_dir = find_dir('annotation_templates')
configs = []
for config_file in pathlib.Path(annotation_templates_dir).glob('**/*.yml'):
config = read_yaml(config_file)
if settings.VERSION_EDITION == 'Community':
if settings.VERSION_EDITION.lower() != config.get('type', 'community'):
continue
if config.get('image', '').startswith('/static') and settings.HOSTNAME:
# if hostname set manually, create full image urls
config['image'] = settings.HOSTNAME + config['image']
configs.append(config)
template_groups_file = find_file(os.path.join('annotation_templates', 'groups.txt'))
with open(template_groups_file, encoding='utf-8') as f:
groups = f.read().splitlines()
logger.debug(f'{len(configs)} templates found.')
return Response({'templates': configs, 'groups': groups})
return Response(self.templates_and_groups)


class ProjectSampleTask(generics.RetrieveAPIView):
Expand Down

0 comments on commit 842c951

Please sign in to comment.