-
-
Notifications
You must be signed in to change notification settings - Fork 113
Open
Labels
enhancementNew feature or requestNew feature or requestno-staleExempt from stale botExempt from stale bot
Description
Refactoring upload_artifact_api.
optuna-dashboard/optuna_dashboard/artifact/_backend.py
Lines 106 to 145 in 14c57ad
| @app.post("/api/artifacts/<study_id:int>/<trial_id:int>") | |
| @json_api_view | |
| def upload_artifact_api(study_id: int, trial_id: int) -> dict[str, Any]: | |
| trial = storage.get_trial(trial_id) | |
| if trial is None: | |
| response.status = 400 | |
| return {"reason": "Invalid study_id or trial_id"} | |
| elif trial.state.is_finished(): | |
| response.status = 400 | |
| return {"reason": "The trial is already finished."} | |
| # TODO(c-bata): Use optuna.artifacts.upload_artifact() | |
| if artifact_store is None: | |
| response.status = 400 # Bad Request | |
| return {"reason": "Cannot access to the artifacts."} | |
| file = request.json.get("file") | |
| if file is None: | |
| response.status = 400 | |
| return {"reason": "Please specify the 'file' key."} | |
| _, data = parse_data_uri(file) | |
| filename = request.json.get("filename", "") | |
| artifact_id = str(uuid.uuid4()) | |
| artifact_store.write(artifact_id, io.BytesIO(data)) | |
| mimetype, encoding = mimetypes.guess_type(filename) | |
| artifact = { | |
| "artifact_id": artifact_id, | |
| "filename": filename, | |
| "mimetype": mimetype or DEFAULT_MIME_TYPE, | |
| "encoding": encoding, | |
| } | |
| attr_key = ARTIFACTS_ATTR_PREFIX + artifact_id | |
| storage.set_trial_system_attr(trial_id, attr_key, json.dumps(artifact)) | |
| response.status = 201 | |
| return { | |
| "artifact_id": artifact_id, | |
| "artifacts": list_trial_artifacts(storage.get_study_system_attrs(study_id), trial), | |
| } |
In order to use optuna.upload_artifact it is necessary for the upload_artifact function to take study_id or trial_id in order to minimize storage access.
Metadata
Metadata
Assignees
Labels
enhancementNew feature or requestNew feature or requestno-staleExempt from stale botExempt from stale bot