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

Refactoring upload_*_artifact with optuna.upload_artifact. #693

Open
gen740 opened this issue Nov 10, 2023 · 1 comment
Open

Refactoring upload_*_artifact with optuna.upload_artifact. #693

gen740 opened this issue Nov 10, 2023 · 1 comment
Labels
enhancement New feature or request no-stale Exempt from stale bot

Comments

@gen740
Copy link
Contributor

gen740 commented Nov 10, 2023

Refactoring upload_artifact_api.

@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.

@gen740 gen740 added the enhancement New feature or request label Nov 10, 2023
Copy link

github-actions bot commented Jan 9, 2024

This issue has not seen any recent activity.

@github-actions github-actions bot added the stale for stale bot label Jan 9, 2024
@c-bata c-bata added no-stale Exempt from stale bot and removed stale for stale bot labels Jan 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request no-stale Exempt from stale bot
Projects
None yet
Development

No branches or pull requests

2 participants