6
6
import os
7
7
from oci .data_science .models import Metadata
8
8
import ads
9
+ from ads .common import logger
9
10
10
11
class ModelDescription :
11
12
12
- region = ''
13
-
14
13
empty_json = {
15
14
"version" : "1.0" ,
16
15
"type" : "modelOSSReferenceDescription" ,
@@ -29,31 +28,27 @@ def auth(self):
29
28
30
29
def __init__ (self , model_ocid = None ):
31
30
31
+ self .region = ''
32
32
self .auth ()
33
33
34
34
if model_ocid == None :
35
35
# if no model given then start from scratch
36
36
self .modelDescriptionJson = self .empty_json
37
37
else :
38
38
# 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" )
44
40
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 )
53
46
except json .JSONDecodeError as e :
54
- print (f"Error decoding JSON: { e } " )
47
+ logger .error (f"Error decoding JSON: { e } " )
48
+ raise e
55
49
except Exception as e :
56
- print (f"An unexpected error occurred: { e } " )
50
+ logger .error (f"An unexpected error occurred: { e } " )
51
+ raise e
57
52
58
53
def add (self , namespace , bucket , prefix = None , files = None ):
59
54
# Remove if the model already exists
@@ -67,9 +62,9 @@ def checkIfFileExists(fileName):
67
62
isExists = True
68
63
except Exception as e :
69
64
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 } " )
71
66
else :
72
- print (f"An error occured: { e } " )
67
+ logger . error (f"An error occured: { e } " )
73
68
return isExists
74
69
75
70
# Function to un-paginate the api call with while loop
@@ -110,8 +105,12 @@ def listObjectVersionsUnpaginated():
110
105
} for obj in objectStorageList if obj .size > 0 ]
111
106
112
107
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 )
115
114
116
115
self .modelDescriptionJson ['models' ].append ({
117
116
"namespace" : namespace ,
@@ -135,19 +134,19 @@ def findModelIdx():
135
134
self .modelDescriptionJson ['models' ].pop (modelSearchIdx )
136
135
137
136
def show (self ):
138
- print (json .dumps (self .modelDescriptionJson , indent = 4 ))
137
+ logger . info (json .dumps (self .modelDescriptionJson , indent = 4 ))
139
138
140
139
def build (self ):
141
- print ("Building..." )
140
+ logger . info ("Building..." )
142
141
file_path = "resultModelDescription.json"
143
142
try :
144
143
with open (file_path , "w" ) as json_file :
145
144
json .dump (self .modelDescriptionJson , json_file , indent = 2 )
146
145
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.
148
147
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'" )
151
150
return os .path .abspath (file_path )
152
151
153
152
def save (self , project_ocid , compartment_ocid , display_name = None ):
@@ -161,13 +160,13 @@ def save(self, project_ocid, compartment_ocid, display_name=None):
161
160
display_name = display_name ,
162
161
custom_metadata_list = customMetadataList
163
162
)
164
- print ("Created model details" )
163
+ logger . info ("Created model details" )
165
164
model = self .data_science_client .create_model (model_details )
166
- print ("Created model" )
165
+ logger . info ("Created model" )
167
166
self .data_science_client .create_model_artifact (
168
167
model .data .id ,
169
168
json .dumps (self .modelDescriptionJson ),
170
169
content_disposition = 'attachment; filename="modelDescription.json"'
171
170
)
172
- print ('Successfully created model with OCID: ' , model .data .id )
171
+ logger . info ('Successfully created model with OCID: ' , model .data .id )
173
172
return model .data .id
0 commit comments