|
| 1 | +"""Functions to import assets into an AUDIO project.""" |
| 2 | + |
| 3 | +import os |
| 4 | +from enum import Enum |
| 5 | + |
| 6 | +from kili.core.helpers import is_url |
| 7 | +from kili.domain.project import InputType |
| 8 | + |
| 9 | +from .base import ( |
| 10 | + BaseAbstractAssetImporter, |
| 11 | + BatchParams, |
| 12 | + ContentBatchImporter, |
| 13 | +) |
| 14 | +from .exceptions import ImportValidationError |
| 15 | +from .types import AssetLike |
| 16 | + |
| 17 | + |
| 18 | +class AudioDataType(Enum): |
| 19 | + """Audio data type.""" |
| 20 | + |
| 21 | + LOCAL_FILE = "LOCAL_FILE" |
| 22 | + HOSTED_FILE = "HOSTED_FILE" |
| 23 | + |
| 24 | + |
| 25 | +class AudioDataImporter(BaseAbstractAssetImporter): |
| 26 | + """Class for importing data into an AUDIO project.""" |
| 27 | + |
| 28 | + @staticmethod |
| 29 | + def get_data_type(assets: list[AssetLike]) -> AudioDataType: |
| 30 | + """Determine the type of data to upload from the service payload.""" |
| 31 | + content_array = [asset.get("content", "") for asset in assets] |
| 32 | + has_local_file = any(os.path.exists(content) for content in content_array) # type: ignore |
| 33 | + has_hosted_file = any(is_url(content) for content in content_array) |
| 34 | + if has_local_file and has_hosted_file: |
| 35 | + raise ImportValidationError( |
| 36 | + """ |
| 37 | + Cannot upload hosted data and local files at the same time. |
| 38 | + Please separate the assets into 2 calls |
| 39 | + """ |
| 40 | + ) |
| 41 | + if has_local_file: |
| 42 | + return AudioDataType.LOCAL_FILE |
| 43 | + return AudioDataType.HOSTED_FILE |
| 44 | + |
| 45 | + def import_assets(self, assets: list[AssetLike], input_type: InputType): |
| 46 | + """Import AUDIO assets into Kili.""" |
| 47 | + self._check_upload_is_allowed(assets) |
| 48 | + data_type = self.get_data_type(assets) |
| 49 | + assets = self.filter_duplicate_external_ids(assets) |
| 50 | + if data_type == AudioDataType.LOCAL_FILE: |
| 51 | + assets = self.filter_local_assets(assets, self.raise_error) |
| 52 | + batch_params = BatchParams(is_hosted=False, is_asynchronous=False) |
| 53 | + batch_importer = ContentBatchImporter( |
| 54 | + self.kili, self.project_params, batch_params, self.pbar |
| 55 | + ) |
| 56 | + elif data_type == AudioDataType.HOSTED_FILE: |
| 57 | + batch_params = BatchParams(is_hosted=True, is_asynchronous=False) |
| 58 | + batch_importer = ContentBatchImporter( |
| 59 | + self.kili, self.project_params, batch_params, self.pbar |
| 60 | + ) |
| 61 | + else: |
| 62 | + raise ImportValidationError |
| 63 | + return self.import_assets_by_batch(assets, batch_importer) |
0 commit comments