Skip to content

Commit 5f3b316

Browse files
committed
Logger used for prints, error handling improved, one extra file creation removed.
1 parent 6a930f1 commit 5f3b316

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

ads/model/model_description.py

Lines changed: 28 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import os
77
from oci.data_science.models import Metadata
88
import ads
9+
from ads.common import logger
910

1011
class ModelDescription:
1112

12-
region = ''
13-
1413
empty_json = {
1514
"version": "1.0",
1615
"type": "modelOSSReferenceDescription",
@@ -29,31 +28,27 @@ def auth(self):
2928

3029
def __init__(self, model_ocid=None):
3130

31+
self.region = ''
3232
self.auth()
3333

3434
if model_ocid == None:
3535
# if no model given then start from scratch
3636
self.modelDescriptionJson = self.empty_json
3737
else:
3838
# if model given then get that as the starting reference point
39-
print("Getting model details from backend")
40-
destination_file_path = "downloaded_artifact.json"
41-
get_model_artifact_content_response = self.data_science_client.get_model_artifact_content(
42-
model_id=model_ocid,
43-
)
39+
logger.info("Getting model details from backend")
4440
try:
45-
with open(destination_file_path, "wb") as f:
46-
f.write(get_model_artifact_content_response.data.content)
47-
with open(destination_file_path, 'r') as f:
48-
self.modelDescriptionJson = json.load(f)
49-
except FileNotFoundError:
50-
print(f"File '{destination_file_path}' not found.")
51-
except IOError as e:
52-
print(f"Error reading or writing to file: {e}")
41+
get_model_artifact_content_response = self.data_science_client.get_model_artifact_content(
42+
model_id=model_ocid,
43+
)
44+
content = get_model_artifact_content_response.data.content
45+
self.modelDescriptionJson = json.loads(content)
5346
except json.JSONDecodeError as e:
54-
print(f"Error decoding JSON: {e}")
47+
logger.error(f"Error decoding JSON: {e}")
48+
raise e
5549
except Exception as e:
56-
print(f"An unexpected error occurred: {e}")
50+
logger.error(f"An unexpected error occurred: {e}")
51+
raise e
5752

5853
def add(self, namespace, bucket, prefix=None, files=None):
5954
# Remove if the model already exists
@@ -67,9 +62,9 @@ def checkIfFileExists(fileName):
6762
isExists = True
6863
except Exception as e:
6964
if hasattr(e, 'status') and e.status == 404:
70-
print(f"File not found in bucket: {fileName}")
65+
logger.error(f"File not found in bucket: {fileName}")
7166
else:
72-
print(f"An error occured: {e}")
67+
logger.error(f"An error occured: {e}")
7368
return isExists
7469

7570
# Function to un-paginate the api call with while loop
@@ -110,8 +105,12 @@ def listObjectVersionsUnpaginated():
110105
} for obj in objectStorageList if obj.size > 0]
111106

112107
if len(objects) == 0:
113-
print("No files to add in the bucket: ", bucket, " with namespace: ", namespace, " and prefix: ", prefix, " file names: ", files)
114-
return
108+
error_message = (
109+
f"No files to add in the bucket: {bucket} with namespace: {namespace} "
110+
f"and prefix: {prefix}. File names: {files}"
111+
)
112+
logger.error(error_message)
113+
raise ValueError(error_message)
115114

116115
self.modelDescriptionJson['models'].append({
117116
"namespace": namespace,
@@ -135,19 +134,19 @@ def findModelIdx():
135134
self.modelDescriptionJson['models'].pop(modelSearchIdx)
136135

137136
def show(self):
138-
print(json.dumps(self.modelDescriptionJson, indent=4))
137+
logger.info(json.dumps(self.modelDescriptionJson, indent=4))
139138

140139
def build(self):
141-
print("Building...")
140+
logger.info("Building...")
142141
file_path = "resultModelDescription.json"
143142
try:
144143
with open(file_path, "w") as json_file:
145144
json.dump(self.modelDescriptionJson, json_file, indent=2)
146145
except IOError as e:
147-
print(f"Error writing to file '{file_path}': {e}") # Handle the exception accordingly, e.g., log the error, retry writing, etc.
146+
logger.error(f"Error writing to file '{file_path}': {e}") # Handle the exception accordingly, e.g., log the error, retry writing, etc.
148147
except Exception as e:
149-
print(f"An unexpected error occurred: {e}") # Handle other unexpected exceptions
150-
print("Model Artifact stored at location: 'resultModelDescription.json'")
148+
logger.error(f"An unexpected error occurred: {e}") # Handle other unexpected exceptions
149+
logger.info("Model Artifact stored at location: 'resultModelDescription.json'")
151150
return os.path.abspath(file_path)
152151

153152
def save(self, project_ocid, compartment_ocid, display_name=None):
@@ -161,13 +160,13 @@ def save(self, project_ocid, compartment_ocid, display_name=None):
161160
display_name = display_name,
162161
custom_metadata_list = customMetadataList
163162
)
164-
print("Created model details")
163+
logger.info("Created model details")
165164
model = self.data_science_client.create_model(model_details)
166-
print("Created model")
165+
logger.info("Created model")
167166
self.data_science_client.create_model_artifact(
168167
model.data.id,
169168
json.dumps(self.modelDescriptionJson),
170169
content_disposition='attachment; filename="modelDescription.json"'
171170
)
172-
print('Successfully created model with OCID: ', model.data.id)
171+
logger.info('Successfully created model with OCID: ', model.data.id)
173172
return model.data.id

0 commit comments

Comments
 (0)