1- import os
2-
3- from git import Optional
4-
5- import medperf .config as config
6- from medperf .entities .model import Model
1+ from typing import Optional
72from medperf .exceptions import InvalidArgumentError
8- from medperf .utils import remove_path
9- from medperf .enums import ModelType
103from medperf .commands .mlcube .submit import SubmitCube
114from medperf .commands .asset .submit import SubmitAsset
125
@@ -42,7 +35,6 @@ def run(
4235 asset_url (Optional[str]): URL to download the asset from (for ASSET type).
4336 operational (bool): Whether to submit as OPERATIONAL state.
4437 """
45- ui = config .ui
4638 submission = cls (
4739 name ,
4840 operational ,
@@ -56,24 +48,9 @@ def run(
5648 asset_url ,
5749 )
5850 submission .validate_inputs ()
59-
60- with ui .interactive ():
61- ui .text = "Submitting model to MedPerf"
62- if submission .model_type == ModelType .CONTAINER .value :
63- submission .prepare_container_submission ()
64- submission .create_model_object ()
65- updated_model_body , updated_container_body = submission .upload ()
66- submission .finalize_container_submission (updated_container_body )
67- else :
68- submission .prepare_asset_submission ()
69- submission .create_model_object ()
70- updated_model_body , updated_asset_body = submission .upload ()
71- submission .finalize_asset_submission (updated_asset_body )
72- submission .to_permanent_path (updated_model_body )
73- submission .write (updated_model_body )
74-
75- ui .print (f"Model registered with UID: { submission .model .id } " )
76- return submission .model .id
51+ if submission .is_container :
52+ return submission .run_container_submission ()
53+ return submission .run_asset_submission ()
7754
7855 def __init__ (
7956 self ,
@@ -98,9 +75,7 @@ def __init__(
9875 self .asset_path = asset_path
9976 self .asset_url = asset_url
10077 self .operational = operational
101- self .model = None
102- self .model_type = None
103- self .subentity_submission = None
78+ self .is_container : bool = None
10479
10580 def validate_inputs (self ):
10681 container_provided = (
@@ -124,90 +99,24 @@ def validate_inputs(self):
12499 raise InvalidArgumentError (
125100 "Container config file is required for container-backed model."
126101 )
127- self .model_type = (
128- ModelType .CONTAINER .value if container_provided else ModelType .ASSET .value
129- )
102+ self .is_container = container_provided
130103
131- def prepare_container_submission (self ):
104+ def run_container_submission (self ):
132105 submit_info = {
133106 "name" : self .name ,
134107 "image_hash" : self .image_hash ,
135108 "additional_files_tarball_url" : self .additional_file ,
136109 "additional_files_tarball_hash" : self .additional_hash ,
137110 "state" : "OPERATION" if self .operational else "DEVELOPMENT" ,
138111 }
139- ui = config .ui
140- submission = SubmitCube (
112+ return SubmitCube .run (
141113 submit_info ,
142114 self .container_config_file ,
143115 self .parameters_config_file ,
144116 self .decryption_key ,
145117 )
146- submission .read_config_files ()
147- submission .create_cube_object ()
148118
149- ui .text = "Validating Container can be downloaded"
150- submission .validate ()
151- submission .download_run_files ()
152- self .subentity_submission = submission
153-
154- def prepare_asset_submission (self ):
155- ui = config .ui
156- submission = SubmitAsset (
119+ def run_asset_submission (self ):
120+ return SubmitAsset .run (
157121 self .name , self .asset_path , self .asset_url , self .operational
158122 )
159- submission .validate_inputs ()
160-
161- ui .text = "Preparing asset"
162- submission .prepare_asset ()
163- submission .create_asset_object ()
164- submission .validate_asset_file ()
165- self .subentity_submission = submission
166-
167- def create_model_object (self ):
168- state = "OPERATION" if self .operational else "DEVELOPMENT"
169- container = None
170- asset = None
171- if self .model_type == ModelType .CONTAINER .value :
172- container = self .subentity_submission .cube
173- else :
174- asset = self .subentity_submission .asset
175-
176- self .model = Model (
177- name = self .name ,
178- type = self .model_type ,
179- container = container ,
180- asset = asset ,
181- state = state ,
182- )
183- config .tmp_paths .append (self .model .path )
184- os .makedirs (self .model .path , exist_ok = True )
185-
186- def upload (self ):
187- updated_body = self .model .upload ()
188- subentity_body = (
189- updated_body ["container" ]
190- if self .model_type == ModelType .CONTAINER .value
191- else updated_body ["asset" ]
192- )
193- return updated_body , subentity_body
194-
195- def finalize_container_submission (self , updated_body : dict ):
196- self .subentity_submission .to_permanent_path (updated_body )
197- self .subentity_submission .write (updated_body )
198- self .subentity_submission .store_decryption_key ()
199-
200- def finalize_asset_submission (self , updated_body : dict ):
201- self .subentity_submission .to_permanent_path (updated_body )
202- self .subentity_submission .write (updated_body )
203-
204- def to_permanent_path (self , model_dict ):
205- old_path = self .model .path
206- updated_model = Model (** model_dict )
207- new_path = updated_model .path
208- remove_path (new_path )
209- os .rename (old_path , new_path )
210-
211- def write (self , updated_body ):
212- self .model = Model (** updated_body )
213- self .model .write ()
0 commit comments