Skip to content

Commit

Permalink
Use remote config file api.yaml
Browse files Browse the repository at this point in the history
  • Loading branch information
gmertes committed Feb 26, 2024
1 parent f6ba842 commit 061a70c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 19 deletions.
19 changes: 5 additions & 14 deletions ai_models/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,15 +221,10 @@ def _main(argv):
)

parser.add_argument(
"--remote-url",
default=os.getenv("AI_MODELS_REMOTE_URL"),
help="Remote endpoint URL",
)

parser.add_argument(
"--remote-token",
default=os.getenv("AI_MODELS_REMOTE_TOKEN"),
help="Remote endpoint auth token",
"--remote",
help="Enable remote execution, read url and token from ~/.config/ai-models/api.yaml",
action="store_true",
dest="remote_execution",
)

args, unknownargs = parser.parse_known_args(argv)
Expand Down Expand Up @@ -271,10 +266,6 @@ def _main(argv):
"You need to specify --retrieve-requests or --archive-requests"
)

if args.remote_url is not None:
if args.remote_token is None:
parser.error("You need to specify --remote-token")

run(vars(args), unknownargs)


Expand All @@ -297,7 +288,7 @@ def run(cfg: dict, model_args: list):
sys.exit(0)

try:
if cfg.get("remote_url", None) is not None:
if cfg["remote_execution"]:
model.remote(cfg, model_args)
else:
model.run()
Expand Down
2 changes: 0 additions & 2 deletions ai_models/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,8 +480,6 @@ def remote(self, cfg: dict, model_args: list):
self.all_fields.save(input_file)

client = RemoteClient(
url=cfg["remote_url"],
token=cfg["remote_token"],
input_file=input_file,
output_file=output_file,
)
Expand Down
40 changes: 37 additions & 3 deletions ai_models/remote.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import logging
import os
import sys
import time
from urllib.parse import urljoin
Expand All @@ -19,16 +20,49 @@ def __call__(self, r):


class RemoteClient:
def __init__(self, url: str, token: str, output_file: str, input_file: str = None):
def __init__(
self,
input_file: str = None,
output_file: str = "output.grib",
url: str = None,
token: str = None,
):
root = os.path.join(os.path.expanduser("~"), ".config", "ai-models")
os.makedirs(root, exist_ok=True)

configfile = os.path.join(root, "api.yaml")

if os.path.exists(configfile):
from yaml import safe_load

with open(configfile, "r") as f:
config = safe_load(f) or {}

url = config.get("url", None)
token = config.get("token", None)

if url is None:
url = os.getenv("AI_MODELS_REMOTE_URL", "https://ai-models.ecmwf.int")
LOG.info("Using remote %s", url)

token = token or os.getenv("AI_MODELS_REMOTE_TOKEN", None)

if token is None:
LOG.error(
"Missing remote token. Set it in %s or in env AI_MODELS_REMOTE_TOKEN",
configfile,
)
sys.exit(1)

self.url = url
self.token = token
self.auth = BearerAuth(token)
self.output_file = output_file
self.input_file = input_file
self._timeout = 300

def run(self, cfg: dict, model_args: list):
cfg.pop("remote_url", None)
cfg.pop("remote_token", None)
cfg.pop("remote_execution", None)
cfg["model_args"] = model_args

# upload file
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ def read(fname):
"ecmwflibs>=0.6.1",
"gputil",
"earthkit-meteo",
"pyyaml"
],
extras_require={
"provenance": [
Expand Down

0 comments on commit 061a70c

Please sign in to comment.