-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfind_artifacts.py
56 lines (43 loc) · 1.72 KB
/
find_artifacts.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
Find artifacts of a run that match a name.
"""
import sys
import os
import click
import mlflow
client = mlflow.client.MlflowClient()
def find_run_model_names(run_id):
"""
Return a list of model artifact directory paths of an MLflow run.
Looks for any directory with an 'MLmodel' file and returns its directory.
"""
matches = find_artifacts(run_id, "", "MLmodel")
return [ m.replace("/MLmodel","").replace("MLmodel","") for m in matches ]
def find_artifacts(run_id, path, target, max_level=sys.maxsize):
return _find_artifacts(run_id, path, target, max_level, 0, [])
def _find_artifacts(run_id, path, target, max_level, level, matches):
if level+1 > max_level:
return matches
artifacts = client.list_artifacts(run_id, path)
for art in artifacts:
filename = os.path.basename(art.path)
if filename == target:
matches.append(art.path)
if art.is_dir:
_find_artifacts(run_id, art.path, target, max_level, level+1, matches)
return matches
@click.command()
@click.option("--run-id", help="Run ID.", required=True, type=str)
@click.option("--path", help="Relative artifact path.", default="", type=str, show_default=True)
@click.option("--target", help="Target filename to search for.", required=True, type=str)
@click.option("--max-level", help="Number of artifact levels to recurse.", default=sys.maxsize, type=int, show_default=True)
def main(run_id, path, target, max_level): # pragma: no cover
print("Options:")
for k,v in locals().items():
print(f" {k}: {v}")
matches = find_artifacts(run_id, path, target, max_level)
print("Matches:")
for m in matches:
print(" ",m)
if __name__ == "__main__":
main()