Support artifact_object empty string

Fix pandas passed as upload_artifact preview object
This commit is contained in:
allegroai 2022-01-31 11:03:49 +02:00
parent 857206a9b6
commit 6229d5a67a

View File

@ -319,7 +319,7 @@ class Artifacts(object):
raise ValueError("Artifact by the name of {} is already registered, use register_artifact".format(name)) raise ValueError("Artifact by the name of {} is already registered, use register_artifact".format(name))
# cast preview to string # cast preview to string
if preview not in (None, False): if preview is not None and not (isinstance(preview, bool) and preview is False):
preview = str(preview) preview = str(preview)
# evaluate lazy proxy object # evaluate lazy proxy object
@ -332,7 +332,7 @@ class Artifacts(object):
# try to convert string Path object (it might reference a file/folder) # try to convert string Path object (it might reference a file/folder)
# dont not try to serialize long texts. # dont not try to serialize long texts.
if isinstance(artifact_object, six.string_types) and len(artifact_object) < 2048: if isinstance(artifact_object, six.string_types) and artifact_object and len(artifact_object) < 2048:
# noinspection PyBroadException # noinspection PyBroadException
try: try:
artifact_path = Path(artifact_object) artifact_path = Path(artifact_object)
@ -537,19 +537,23 @@ class Artifacts(object):
artifact_type_data.preview = '# full text too large to store, storing first {}kb\n{}'.format( artifact_type_data.preview = '# full text too large to store, storing first {}kb\n{}'.format(
self.max_preview_size_bytes//1024, artifact_object[:self.max_preview_size_bytes] self.max_preview_size_bytes//1024, artifact_object[:self.max_preview_size_bytes]
) )
delete_after_upload = True if artifact_object:
override_filename_ext_in_uri = '.txt' delete_after_upload = True
override_filename_in_uri = name + override_filename_ext_in_uri override_filename_ext_in_uri = '.txt'
fd, local_filename = mkstemp(prefix=quote(name, safe="") + '.', suffix=override_filename_ext_in_uri) override_filename_in_uri = name + override_filename_ext_in_uri
os.close(fd) fd, local_filename = mkstemp(prefix=quote(name, safe="") + '.', suffix=override_filename_ext_in_uri)
# noinspection PyBroadException os.close(fd)
try: # noinspection PyBroadException
with open(local_filename, 'wt') as f: try:
f.write(artifact_object) with open(local_filename, 'wt') as f:
except Exception: f.write(artifact_object)
# cleanup and raise exception except Exception:
os.unlink(local_filename) # cleanup and raise exception
raise os.unlink(local_filename)
raise
elif artifact_object is None or (isinstance(artifact_object, str) and artifact_object == ""):
artifact_type = ''
store_as_pickle = False
elif auto_pickle: elif auto_pickle:
# revert to pickling the object # revert to pickling the object
store_as_pickle = True store_as_pickle = True