Skip to content

Commit 6d62a7a

Browse files
committed
Add an option to load W&B API key
1 parent a48466c commit 6d62a7a

File tree

6 files changed

+16
-1
lines changed

6 files changed

+16
-1
lines changed

compute_metrics.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import logging
22
from pprint import pprint
3-
3+
import os
44
import hydra
55
import pandas as pd
66
import wandb
@@ -34,6 +34,9 @@ def main(cfg: MetricsConfig):
3434
print(f"==== Using config ====\n{OmegaConf.to_yaml(cfg)}")
3535

3636
if cfg.logger.use_wandb:
37+
if cfg.logger.use_api_key:
38+
with open(hydra.utils.to_absolute_path("wandb_api_key.txt"), "r") as f:
39+
os.environ["WANDB_API_KEY"] = f.read().strip()
3740
wandb.Table.MAX_ROWS = 50000
3841
run: wandb.wandb_sdk.wandb_run.Run = wandb.init(
3942
project=cfg.logger.project,

conf/eval_config.py

+2
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,13 @@ class WandbEvalConfig:
4949
use_wandb: Whether W&B will be used for logging or not.
5050
project: Name of project this run will appear in.
5151
load_artifact: Whether model checkpoint should be loaded from W&B artifact or not.
52+
use_api_key: True to read an API key from a local file (expected to be stored in `wandb_api_key.txt`).
5253
"""
5354

5455
use_wandb: bool = True
5556
project: str = "commit_message_completion"
5657
load_artifact: bool = True
58+
use_api_key: bool = False
5759
artifact_config: ArtifactEvalConfig = field(default_factory=ArtifactEvalConfig)
5860

5961

conf/metrics_config.py

+2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ class WandbMetricConfig:
3939
use_wandb: Whether W&B will be used for logging or not.
4040
project: Name of project this run will appear in.
4141
load_artifact: Whether model predictions should be loaded from W&B artifact or not.
42+
use_api_key: True to read an API key from a local file (expected to be stored in `wandb_api_key.txt`).
4243
"""
4344

4445
use_wandb: bool = True
4546
project: str = "commit_message_completion"
4647
load_artifact: bool = True
48+
use_api_key: bool = False
4749
artifact_config: ArtifactMetricConfig = field(default_factory=ArtifactMetricConfig)
4850

4951

conf/train_config.py

+2
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ class WandbTrainConfig:
5050
use_wandb: Whether W&B will be used for logging or not.
5151
project: Name of a project this run will appear in.
5252
save_artifact: True to load model checkpoints to W&B as artifacts, False otherwise.
53+
use_api_key: True to read an API key from a local file (expected to be stored in `wandb_api_key.txt`).
5354
"""
5455

5556
use_wandb: bool = True
5657
project: str = "commit_message_completion"
5758
save_artifact: bool = True
59+
use_api_key: bool = False
5860

5961

6062
@dataclass

eval.py

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ def main(cfg: EvalConfig) -> None:
5454
)
5555

5656
if cfg.logger.use_wandb and cfg.logger.load_artifact:
57+
if cfg.logger.use_api_key:
58+
with open(hydra.utils.to_absolute_path("wandb_api_key.txt"), "r") as f:
59+
os.environ["WANDB_API_KEY"] = f.read().strip()
5760
artifact_name = f"{cfg.logger.artifact_config.project}/{run_name}:{cfg.logger.artifact_config.version}"
5861
artifact = trainer_logger.experiment.use_artifact(artifact_name)
5962
if "tags" in artifact.metadata:

train.py

+3
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ def main(cfg: TrainConfig) -> None:
9595

9696
# logger
9797
if cfg.logger.use_wandb:
98+
if cfg.logger.use_api_key:
99+
with open(hydra.utils.to_absolute_path("wandb_api_key.txt"), "r") as f:
100+
os.environ["WANDB_API_KEY"] = f.read().strip()
98101
trainer_logger = pl.loggers.WandbLogger(
99102
name=run_name,
100103
project=cfg.logger.project,

0 commit comments

Comments
 (0)