This repository has been archived by the owner on Sep 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 44
Expose GTO version in FastAPI's interface #681
Open
aguschin
wants to merge
2
commits into
main
Choose a base branch
from
expose-gto-version
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,7 @@ | |
get_local_file_info, | ||
) | ||
from mlem.core.meta_io import get_fs | ||
from mlem.core.registry import ArtifactInRegistry | ||
|
||
BATCH_SIZE = 10**5 | ||
|
||
|
@@ -32,7 +33,7 @@ def find_dvc_repo_root(path: str): | |
while True: | ||
if os.path.isdir(os.path.join(_path, ".dvc")): | ||
return _path | ||
if _path == "/": | ||
if _path == "/" or not _path: | ||
break | ||
_path = os.path.dirname(_path) | ||
raise NotDvcRepoError(f"Path {path} is not in dvc repo") | ||
|
@@ -118,3 +119,43 @@ def open(self) -> Iterator[IO]: | |
def relative(self, fs: AbstractFileSystem, path: str) -> "DVCArtifact": | ||
relative = super().relative(fs, path) | ||
return DVCArtifact(uri=relative.uri, size=self.size, hash=self.hash) | ||
|
||
|
||
def find_artifact_name_by_path(path): | ||
if not os.path.abspath(path): | ||
raise ValueError(f"Path {path} is not absolute") | ||
|
||
from dvc.repo import Repo | ||
|
||
root = find_dvc_repo_root(path) | ||
relpath = os.path.relpath(path, root) | ||
if relpath.endswith(".mlem"): | ||
relpath = relpath[:-5] | ||
repo = Repo(root) | ||
for _dvcyaml, artifacts in repo.artifacts.read().items(): | ||
for name, value in artifacts.items(): | ||
if value.path == relpath: | ||
return root, name | ||
return root, None | ||
|
||
|
||
def find_version(root, name): | ||
from gto.api import _show_versions | ||
|
||
version = _show_versions(root, name, ref="HEAD") | ||
if version: | ||
return version[0]["version"] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make this more flexible and support other version metadata, I suggest
this will also make it possible to return some version information even if there's no GTO tracking and also support other version in the future, like people manually git-tagging, not via gto |
||
|
||
|
||
class DVCArtifactInRegistry(ArtifactInRegistry): | ||
"""Artifact registered within an Artifact Registry.""" | ||
|
||
type: ClassVar = "dvc" | ||
uri: str | ||
"""Local path to file""" | ||
|
||
@property | ||
def version(self): | ||
root, name = find_artifact_name_by_path(self.uri) | ||
if name: | ||
return find_version(root, name) |
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,26 @@ | ||
from abc import ABC | ||
from typing import ClassVar | ||
|
||
from mlem.core.base import MlemABC | ||
|
||
|
||
class ArtifactInRegistry(MlemABC, ABC): | ||
"""Artifact registered within an Artifact Registry. | ||
Can provide version and stage it's promoted to. | ||
""" | ||
|
||
class Config: | ||
type_root = True | ||
default_type = "gto" | ||
|
||
abs_name: ClassVar = "artifact_in_registry" | ||
uri: str | ||
"""location""" | ||
|
||
@property | ||
def version(self): | ||
raise NotImplementedError | ||
|
||
@property | ||
def stage(self): | ||
raise NotImplementedError |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.