-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathaudio.py
More file actions
63 lines (53 loc) · 2.31 KB
/
Copy pathaudio.py
File metadata and controls
63 lines (53 loc) · 2.31 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
"""Functions to import assets into an AUDIO project."""
import os
from enum import Enum
from kili.core.helpers import is_url
from kili.domain.project import InputType
from .base import (
BaseAbstractAssetImporter,
BatchParams,
ContentBatchImporter,
)
from .exceptions import ImportValidationError
from .types import AssetLike
class AudioDataType(Enum):
"""Audio data type."""
LOCAL_FILE = "LOCAL_FILE"
HOSTED_FILE = "HOSTED_FILE"
class AudioDataImporter(BaseAbstractAssetImporter):
"""Class for importing data into an AUDIO project."""
@staticmethod
def get_data_type(assets: list[AssetLike]) -> AudioDataType:
"""Determine the type of data to upload from the service payload."""
content_array = [asset.get("content", "") for asset in assets]
has_local_file = any(os.path.exists(content) for content in content_array) # type: ignore
has_hosted_file = any(is_url(content) for content in content_array)
if has_local_file and has_hosted_file:
raise ImportValidationError(
"""
Cannot upload hosted data and local files at the same time.
Please separate the assets into 2 calls
"""
)
if has_local_file:
return AudioDataType.LOCAL_FILE
return AudioDataType.HOSTED_FILE
def import_assets(self, assets: list[AssetLike], input_type: InputType):
"""Import AUDIO assets into Kili."""
self._check_upload_is_allowed(assets)
data_type = self.get_data_type(assets)
assets = self.filter_duplicate_external_ids(assets)
if data_type == AudioDataType.LOCAL_FILE:
assets = self.filter_local_assets(assets, self.raise_error)
batch_params = BatchParams(is_hosted=False, is_asynchronous=False)
batch_importer = ContentBatchImporter(
self.kili, self.project_params, batch_params, self.pbar
)
elif data_type == AudioDataType.HOSTED_FILE:
batch_params = BatchParams(is_hosted=True, is_asynchronous=False)
batch_importer = ContentBatchImporter(
self.kili, self.project_params, batch_params, self.pbar
)
else:
raise ImportValidationError
return self.import_assets_by_batch(assets, batch_importer)