|
| 1 | +"""Functions to import assets into a TEXT project.""" |
| 2 | + |
| 3 | +import json |
| 4 | +import os |
| 5 | +from enum import Enum |
| 6 | +from typing import List, Optional, Tuple |
| 7 | + |
| 8 | +from kili.core.helpers import is_url |
| 9 | + |
| 10 | +from .base import ( |
| 11 | + BaseAbstractAssetImporter, |
| 12 | + BatchParams, |
| 13 | + ContentBatchImporter, |
| 14 | +) |
| 15 | +from .exceptions import ImportValidationError |
| 16 | +from .types import AssetLike |
| 17 | + |
| 18 | + |
| 19 | +class LLMDataType(Enum): |
| 20 | + """LLM data type.""" |
| 21 | + |
| 22 | + DICT = "DICT" |
| 23 | + LOCAL_FILE = "LOCAL_FILE" |
| 24 | + HOSTED_FILE = "HOSTED_FILE" |
| 25 | + |
| 26 | + |
| 27 | +class JSONBatchImporter(ContentBatchImporter): |
| 28 | + """Class for importing a batch of LLM assets with dict content into a LLM_RLHF project.""" |
| 29 | + |
| 30 | + def get_content_type_and_data_from_content(self, content: Optional[str]) -> Tuple[str, str]: |
| 31 | + """Returns the data of the content (path) and its content type.""" |
| 32 | + return content or "", "application/json" |
| 33 | + |
| 34 | + |
| 35 | +class LLMDataImporter(BaseAbstractAssetImporter): |
| 36 | + """Class for importing data into a TEXT project.""" |
| 37 | + |
| 38 | + @staticmethod |
| 39 | + def get_data_type(assets: List[AssetLike]) -> LLMDataType: |
| 40 | + """Determine the type of data to upload from the service payload.""" |
| 41 | + content_array = [asset.get("content", None) for asset in assets] |
| 42 | + if all(is_url(content) for content in content_array): |
| 43 | + return LLMDataType.HOSTED_FILE |
| 44 | + if all(isinstance(content, str) and os.path.exists(content) for content in content_array): |
| 45 | + return LLMDataType.LOCAL_FILE |
| 46 | + if all(isinstance(content, dict) for content in content_array): |
| 47 | + return LLMDataType.DICT |
| 48 | + raise ImportValidationError("Invalid value in content for LLM project.") |
| 49 | + |
| 50 | + def import_assets(self, assets: List[AssetLike]): |
| 51 | + """Import LLM assets into Kili.""" |
| 52 | + self._check_upload_is_allowed(assets) |
| 53 | + data_type = self.get_data_type(assets) |
| 54 | + assets = self.filter_duplicate_external_ids(assets) |
| 55 | + if data_type == LLMDataType.LOCAL_FILE: |
| 56 | + assets = self.filter_local_assets(assets, self.raise_error) |
| 57 | + batch_params = BatchParams(is_hosted=False, is_asynchronous=False) |
| 58 | + batch_importer = ContentBatchImporter( |
| 59 | + self.kili, self.project_params, batch_params, self.pbar |
| 60 | + ) |
| 61 | + elif data_type == LLMDataType.HOSTED_FILE: |
| 62 | + batch_params = BatchParams(is_hosted=True, is_asynchronous=False) |
| 63 | + batch_importer = ContentBatchImporter( |
| 64 | + self.kili, self.project_params, batch_params, self.pbar |
| 65 | + ) |
| 66 | + elif data_type == LLMDataType.DICT: |
| 67 | + for asset in assets: |
| 68 | + if "content" in asset and isinstance(asset["content"], dict): |
| 69 | + asset["content"] = json.dumps(asset["content"]).encode("utf-8") |
| 70 | + batch_params = BatchParams(is_hosted=False, is_asynchronous=False) |
| 71 | + batch_importer = JSONBatchImporter( |
| 72 | + self.kili, self.project_params, batch_params, self.pbar |
| 73 | + ) |
| 74 | + else: |
| 75 | + raise ImportValidationError |
| 76 | + return self.import_assets_by_batch(assets, batch_importer) |
0 commit comments