-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add `post_to_studio` Co-authored-by: daniele <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci Co-authored-by: daniele <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
- Loading branch information
1 parent
74e4209
commit a4ca09e
Showing
13 changed files
with
388 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
from os import getenv | ||
|
||
from dvclive.env import STUDIO_ENDPOINT | ||
from dvclive.utils import parse_scalars | ||
|
||
|
||
def _get_unsent_datapoints(plot, latest_step): | ||
return [x for x in plot if int(x["step"]) >= latest_step] | ||
|
||
|
||
def _cast_to_numbers(datapoints): | ||
for datapoint in datapoints: | ||
for k, v in datapoint.items(): | ||
if k == "step": | ||
datapoint[k] = int(v) | ||
elif k == "timestamp": | ||
continue | ||
else: | ||
datapoint[k] = float(v) | ||
return datapoints | ||
|
||
|
||
def _to_dvc_format(plots): | ||
formatted = {} | ||
for k, v in plots.items(): | ||
formatted[k] = {"data": v} | ||
return formatted | ||
|
||
|
||
def _get_updates(live): | ||
plots, metrics = parse_scalars(live) | ||
latest_step = live._latest_studio_step # pylint: disable=protected-access | ||
|
||
for name, plot in plots.items(): | ||
datapoints = _get_unsent_datapoints(plot, latest_step) | ||
plots[name] = _cast_to_numbers(datapoints) | ||
|
||
metrics = {live.summary_path: {"data": metrics}} | ||
plots = _to_dvc_format(plots) | ||
return metrics, plots | ||
|
||
|
||
def post_to_studio(live, event_type, logger) -> bool: | ||
import requests | ||
from requests.exceptions import RequestException | ||
|
||
data = { | ||
"type": event_type, | ||
"repo_url": live.studio_url, | ||
"rev": live.rev, | ||
"client": "dvclive", | ||
} | ||
|
||
if event_type == "data": | ||
metrics, plots = _get_updates(live) | ||
data["metrics"] = metrics | ||
data["plots"] = plots | ||
data["step"] = live.get_step() | ||
|
||
logger.debug(f"post_to_studio `{event_type=}`") | ||
|
||
try: | ||
response = requests.post( | ||
getenv(STUDIO_ENDPOINT, "https://studio.iterative.ai/api/live"), | ||
json=data, | ||
headers={ | ||
"Content-type": "application/json", | ||
"Authorization": f"token {live.studio_token}", | ||
}, | ||
timeout=5, | ||
) | ||
except RequestException: | ||
return False | ||
|
||
message = response.content.decode() | ||
logger.debug( | ||
f"post_to_studio: {response.status_code=}" f", {message=}" | ||
if message | ||
else "" | ||
) | ||
|
||
return response.status_code == 200 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.