18 from typing
import Optional, Dict, List, Union, Any
19 from typing_extensions
import Self
20 from enum
import IntEnum
21 from pathlib
import Path
23 from ..._folder_manager
import folder_manager
24 from ...codable
import Codable, KeyDescriptor
25 from ...networking
import networkManager, FileData
26 from ...utils
import guessMimeType
32 Available types of Artifacts on Coretex
42 Artifact class represents a single result of a run\n
43 Result can be file of any kind
47 artifactType : ArtifactType
50 path of Artifact on Coretex
52 size of Artifact in bytes (not required)
61 artifactType: ArtifactType
71 Represents the local path of the Artifact
75 Path -> local path to Artifact
78 return folder_manager.getArtifactsFolder(self.taskRunId) / self.remoteFilePath
85 bool -> True if Artifact type is directory
88 return self.
artifactTypeartifactType == ArtifactType.directory
95 bool -> True if Artifact type is file
98 return self.
artifactTypeartifactType == ArtifactType.file
101 def _keyDescriptors(cls) -> Dict[str, KeyDescriptor]:
102 descriptors = super()._keyDescriptors()
104 descriptors[
"artifactType"] =
KeyDescriptor(
"type", ArtifactType)
107 descriptors[
"taskRunId"] =
KeyDescriptor(isEncodable =
False)
115 localFilePath: Union[Path, str],
117 mimeType: Optional[str] =
None
123 mimeType = guessMimeType(localFilePath)
125 mimeType =
"application/octet-stream"
128 "model_queue_id": taskRunId,
129 "path": remoteFilePath
133 FileData.createFromPath(
"file", localFilePath, mimeType = mimeType)
136 response = networkManager.formData(
"artifact/upload-file", parameters, files)
137 if response.hasFailed():
140 artifact = cls.
decodedecode(response.getJson(dict))
141 artifact.taskRunId = taskRunId
147 Downloads Artifact from Coretex.ai
151 bool -> False if response has failed, True otherwise
155 "model_queue_id": self.taskRunId,
156 "path": self.remoteFilePath
159 return not networkManager.download(
"artifact/download-file", str(self.
localFilePathlocalFilePath), params).hasFailed()
162 def fetchAll(cls, taskRunId: int, path: Optional[str] =
None, recursive: bool =
False) -> List[Self]:
164 Fetch all Artifacts from Coretex.ai for the specified run
171 local path where u want to store fetched Artifacts
173 True if you want list to be sorted recursively, False otherwise
176 params: Dict[str, Any] = {
177 "model_queue_id": taskRunId,
181 params[
"path"] = path
183 response = networkManager.get(
"artifact/list-contents", params)
184 if response.hasFailed():
187 artifacts = [cls.
decodedecode(element)
for element
in response.getJson(list)]
189 for artifact
in artifacts:
190 artifact.taskRunId = taskRunId
192 if recursive
and artifact.isDirectory:
194 cls.
fetchAllfetchAll(taskRunId, artifact.remoteFilePath)
Self decode(cls, Dict[str, Any] encodedObject)
List[Self] fetchAll(cls, int taskRunId, Optional[str] path=None, bool recursive=False)