Skip to content

Commit 623ffd9

Browse files
committed
WIP hypha upload
1 parent c7b29a2 commit 623ffd9

File tree

1 file changed

+40
-14
lines changed

1 file changed

+40
-14
lines changed

bioimageio/spec/_upload.py

Lines changed: 40 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import collections.abc
2+
import io
13
from typing import Union
24
from zipfile import ZipFile
35

@@ -15,6 +17,7 @@
1517
from ._internal.common_nodes import ResourceDescrBase
1618
from ._internal.io import BioimageioYamlContent, get_reader
1719
from ._internal.io_basics import BIOIMAGEIO_YAML
20+
from ._internal.io_utils import write_yaml
1821
from ._package import get_resource_package_content
1922
from .common import PermissiveFileSource
2023

@@ -56,24 +59,36 @@ def upload(
5659
if isinstance(descr, InvalidDescr):
5760
raise ValueError("Uploading invalid resource descriptions is not allowed.")
5861

62+
if descr.type != "model":
63+
raise NotImplementedError(
64+
f"For now, only model resources can be uploaded (got type={descr.type})."
65+
)
66+
67+
if descr.id is not None:
68+
raise ValueError(
69+
"You cannot upload a resource with an id. Please remove the id from the description and make sure to upload a new non-existing resource. To edit an existing resource, please use the web interface at https://bioimage.io."
70+
)
71+
5972
content = get_resource_package_content(descr)
6073

61-
manifest = content.pop(BIOIMAGEIO_YAML)
62-
assert isinstance(manifest, dict)
74+
metadata = content[BIOIMAGEIO_YAML]
75+
assert isinstance(metadata, dict)
76+
manifest = dict(metadata)
77+
78+
# only admins can upload a resource with a version
79+
artifact_version = "stage" # if descr.version is None else str(descr.version)
6380

6481
# Create new model
6582
r = httpx.post(
6683
settings.hypha_upload,
6784
json={
6885
"parent_id": "bioimage-io/bioimage.io",
69-
"alias": descr.id,
86+
"alias": (
87+
descr.id or "{animal_adjective}-{animal}"
88+
), # TODO: adapt for non-model uploads,
7089
"type": descr.type,
7190
"manifest": manifest,
72-
"version": (
73-
artifact_version := (
74-
"stage" if descr.version is None else str(descr.version)
75-
)
76-
),
91+
"version": artifact_version,
7792
},
7893
headers=(
7994
headers := {
@@ -86,29 +101,38 @@ def upload(
86101
response = r.json()
87102
artifact_id = response.get("id")
88103
if artifact_id is None:
104+
try:
105+
logger.error("Response detail: {}", "".join(response["detail"]))
106+
except Exception:
107+
logger.error("Response: {}", response)
108+
89109
raise RuntimeError(f"Upload did not return resource id: {response}")
90110
else:
91111
logger.info("Uploaded resource description {}", artifact_id)
92112

93113
for file_name, file_source in content.items():
94-
assert not isinstance(file_source, dict)
95114
# Get upload URL for a file
96115
response = httpx.post(
97116
settings.hypha_upload.replace("/create", "/put_file"),
98117
json={
99118
"artifact_id": artifact_id,
100-
"version": artifact_version, # TODO: is version valid here?
101119
"file_path": file_name,
102120
},
103121
headers=headers,
104122
)
105-
upload_url = response.json()
123+
upload_url = response.raise_for_status().json()
106124

107125
# Upload file to the provided URL
108-
reader = get_reader(file_source)
126+
if isinstance(file_source, collections.abc.Mapping):
127+
buf = io.BytesIO()
128+
write_yaml(file_source, buf)
129+
files = {file_name: buf}
130+
else:
131+
files = {file_name: get_reader(file_source)}
132+
109133
response = httpx.put(
110134
upload_url,
111-
files={file_name: reader}, # pyright: ignore[reportArgumentType]
135+
files=files, # pyright: ignore[reportArgumentType]
112136
# TODO: follow up on https://github.com/encode/httpx/discussions/3611
113137
headers={"Content-Type": ""}, # Important for S3 uploads
114138
)
@@ -129,4 +153,6 @@ def upload(
129153
"Updated status of {}/{} to 'request-review'", artifact_id, artifact_version
130154
)
131155

132-
return load_description(f"{artifact_id}/{artifact_version}")
156+
return load_description(
157+
f"https://hypha.aicell.io/bioimage-io/artifacts/{artifact_id}/files/rdf.yaml?version={artifact_version}"
158+
)

0 commit comments

Comments
 (0)