Skip to content

Commit

Permalink
Make MODEL_SIZE_FILE always go in the model dest directory (#1212)
Browse files Browse the repository at this point in the history
* Make size.json always go in the model dest directory

* Write to model source file when the model is actually fetched and the dest directory is created in eos

* Fix modifying model size for dockerized models in information.json file
  • Loading branch information
DhanshreeA authored Jul 26, 2024
1 parent b381ed6 commit a1dd2b2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 40 deletions.
3 changes: 1 addition & 2 deletions ersilia/hub/content/information.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
CARD_FILE,
SERVICE_CLASS_FILE,
APIS_LIST_FILE,
EOS,
MODEL_SOURCE_FILE,
)

Expand Down Expand Up @@ -47,7 +46,7 @@ def _get_service_class(self):
return None

def _get_model_source(self):
model_source_file = os.path.join(EOS, MODEL_SOURCE_FILE)
model_source_file = os.path.join((self.dest_folder), MODEL_SOURCE_FILE)
if os.path.exists(model_source_file):
with open(model_source_file) as f:
return f.read().rstrip()
Expand Down
11 changes: 5 additions & 6 deletions ersilia/hub/fetch/fetch.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def __init__(
self.model_source = next(
(source for condition, source in sources.items() if condition), "DockerHub"
)
self.logger.debug("Model was fetched from {0}".format(self.model_source))
self.logger.debug("Model getting fetched from {0}".format(self.model_source))

@throw_ersilia_exception
def _decide_fetcher(self, model_id):
Expand Down Expand Up @@ -228,8 +228,7 @@ def _fetch(self, model_id):
return
do_dockerhub = self._decide_if_use_dockerhub(model_id=model_id)
if do_dockerhub:
print("Fetching from DockerHub")
self.logger.debug("Fetching from DockerHub")
self.logger.debug("Decided to fetch from DockerHub")
self._fetch_from_dockerhub(model_id=model_id)
return
if self.overwrite is None:
Expand All @@ -239,9 +238,9 @@ def _fetch(self, model_id):
self._fetch_not_from_dockerhub(model_id=model_id)

def fetch(self, model_id):
self._fetch(model_id)
self._standard_csv_example(model_id)
self.logger.debug("Writing model source to file")
model_source_file = os.path.join(EOS, MODEL_SOURCE_FILE)
model_source_file = os.path.join(self._model_path(model_id), MODEL_SOURCE_FILE)
with open(model_source_file, "w") as f:
f.write(self.model_source)
self._fetch(model_id)
self._standard_csv_example(model_id)
34 changes: 9 additions & 25 deletions ersilia/hub/fetch/lazy_fetchers/dockerhub.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
PREDEFINED_EXAMPLE_FILES,
INFORMATION_FILE,
API_SCHEMA_FILE,
SERVICE_CLASS_FILE,
MODEL_SIZE_FILE,
)

from ...pull.pull import ModelPuller
Expand Down Expand Up @@ -108,41 +106,27 @@ def modify_information(self, model_id):
:param service_class_file: File containing the model service class.
:size_file: File containing the size of the pulled docker image.
"""
file = "{0}/dest/{1}/{2}".format(EOS, model_id, INFORMATION_FILE)
service_class_file = os.path.join(
self._get_bundle_location(model_id), SERVICE_CLASS_FILE
)
size_file = os.path.join(EOS, MODEL_SIZE_FILE)

try:
with open(service_class_file, "r") as f:
service_class = f.read().strip()
except FileNotFoundError:
return None

try:
with open(size_file, "r") as m:
size = json.load(m)
except FileNotFoundError:
return None

information_file = "{0}/dest/{1}/{2}".format(EOS, model_id, INFORMATION_FILE)
mp = ModelPuller(model_id=model_id, config_json=self.config_json)
try:
with open(file, "r") as infile:
with open(information_file, "r") as infile:
data = json.load(infile)
except FileNotFoundError:
self.logger.error("Information file not found, not modifying anything")
return None

data["service_class"] = service_class
data["size"] = size

with open(file, "w") as outfile:
data["service_class"] = "pulled_docker" # Using this literal here to prevent a file read
# from service class file for a model fetched through DockerHub since we already know the service class.
data["size"] = mp._get_size_of_local_docker_image_in_mb()
with open(information_file, "w") as outfile:
json.dump(data, outfile, indent=4)

@throw_ersilia_exception
def fetch(self, model_id):
if not DockerRequirement().is_active():
raise DockerNotActiveError()
mp = ModelPuller(model_id=model_id, config_json=self.config_json)
self.logger.debug("Pulling model image from DockerHub")
mp.pull()
mr = ModelRegisterer(model_id=model_id, config_json=self.config_json)
mr.register(is_from_dockerhub=True)
Expand Down
2 changes: 1 addition & 1 deletion ersilia/hub/fetch/register/register.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def register(self, is_from_dockerhub=False, is_from_hosted=False):
raise Exception
if is_from_dockerhub and not is_from_hosted:
self.register_from_dockerhub()
self.register_not_from_dockerhub()
self.register_not_from_hosted()
if not is_from_dockerhub and is_from_hosted:
self.register_from_hosted()
self.register_not_from_dockerhub()
Expand Down
11 changes: 5 additions & 6 deletions ersilia/hub/pull/pull.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,14 +141,13 @@ def pull(self):
size = self._get_size_of_local_docker_image_in_mb()
if size:
self.logger.debug("Size of image {0} MB".format(size))
path = os.path.join(EOS, MODEL_SIZE_FILE)
with open(path, "w") as f:
json.dump({"size": size, "units": "MB"}, f, indent=4)
self.logger.debug("Size written to {}".format(path))
# path = os.path.join(self._model_path(self.model_id), MODEL_SIZE_FILE)
# with open(path, "w") as f:
# json.dump({"size": size, "units": "MB"}, f, indent=4)
# self.logger.debug("Size written to {}".format(path))
else:
self.logger.warning("Could not obtain size of image")
# except: #TODO add better error
# raise DockerImageArchitectureNotAvailableError(model=self.model_id)
return size
else:
self.logger.info("Image {0} is not available".format(self.image_name))
raise DockerImageNotAvailableError(model=self.model_id)

0 comments on commit a1dd2b2

Please sign in to comment.