Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to load signed result URL in load_ml_model #732

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions openeogeotrellis/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1190,15 +1190,27 @@ def _set_permissions(job_dir: Path):
with requests.get(model_id) as resp:
resp.raise_for_status()
metadata = resp.json()
if deep_get(metadata, "properties", "ml-model:architecture", default=None) is None:
raise OpenEOApiException(
message=f"{model_id} does not specify a model architecture under properties.ml-model:architecture.",
status_code=400)
checkpoints = []
assets = metadata.get('assets', {})
for asset in assets:
if "ml-model:checkpoint" in assets[asset].get('roles', []):
checkpoints.append(assets[asset])
if "ml_model_metadata.json" in model_id:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use endswith instead of in. I don't think this edge case will ever occur but better to be safe.

architecture = deep_get(metadata, "properties", "ml-model:architecture", default=None)
if architecture is None:
raise OpenEOApiException(
message=f"{model_id} does not specify a model architecture under properties.ml-model:architecture.",
status_code=400)
checkpoints = []
assets = metadata.get('assets', {})
for asset in assets:
if "ml-model:checkpoint" in assets[asset].get('roles', []):
checkpoints.append(assets[asset])
else:
architecture = deep_get(metadata, "summaries", "ml-model:architecture", 0, None)
if architecture is None:
raise OpenEOApiException(
message=f"{model_id} does not specify a model architecture under summaries.ml-model:architecture[0].",
status_code=400)
checkpoints = [
metadata.get("assets").get("randomforest.model.tar.gz")
]

if len(checkpoints) == 0 or checkpoints[0].get("href", None) is None:
raise OpenEOApiException(
message=f"{model_id} does not contain a link to the ml model in its assets section.",
Expand All @@ -1208,10 +1220,8 @@ def _set_permissions(job_dir: Path):
raise OpenEOApiException(
message=f"{model_id} contains multiple checkpoints.",
status_code=400)

# Get the url for the actual model from the STAC metadata.
model_url = checkpoints[0]["href"]
architecture = metadata["properties"]["ml-model:architecture"]
# Download the model to the ml_models folder and load it as a java object.
model_dir_path = _create_model_dir()
if architecture == "random-forest":
Expand Down