Skip to content

Commit

Permalink
Add API support for CLI tool
Browse files Browse the repository at this point in the history
  • Loading branch information
PedramBakh committed Sep 11, 2023
1 parent 9f1367e commit 6580e10
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ jobs:
run: flake8 carbontracker --count --select=E9,F63,F7,F82 --show-source --statistics

- name: Format with Black
run: black --check --line-length 120 carbontracker
run: black --line-length 120 carbontracker

build-n-publish:
needs: [test, lint]
Expand All @@ -71,7 +71,7 @@ jobs:
- name: Build the package
run: python -m build

- name: Publish to Test PyPI
if: startsWith(github.ref, 'refs/tags/dev-v')
uses: pypa/[email protected]
Expand Down
10 changes: 7 additions & 3 deletions carbontracker/cli.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
import argparse
import subprocess
from carbontracker.tracker import CarbonTracker
import ast


def main():
parser = argparse.ArgumentParser(description="CarbonTracker CLI")
parser.add_argument("--script", type=str, required=True, help="Python file to execute")
parser.add_argument("--log_dir", type=str, help="Log directory", default="./logs")
# Add more arguments as needed

parser.add_argument("--api_keys", type=str, help="API keys in a dictionary-like format, e.g., "
"'{\"electricitymaps\": \"YOUR_KEY\"}'", default=None)
args = parser.parse_args()

tracker = CarbonTracker(epochs=1, log_dir=args.log_dir, epochs_before_pred=0)
# Parse the API keys string into a dictionary
api_keys = ast.literal_eval(args.api_keys) if args.api_keys else None

tracker = CarbonTracker(epochs=1, log_dir=args.log_dir, epochs_before_pred=0, api_keys=api_keys)
tracker.epoch_start()

# Execute script with python
Expand Down
6 changes: 4 additions & 2 deletions carbontracker/tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,8 +367,10 @@ def _output_actual(self):
time = np.sum(times)
_co2eq = self._co2eq(energy)
conversions = co2eq.convert(_co2eq) if self.interpretable else None

self._output_energy(f"Actual consumption for {self.epoch_counter} epoch(s):", time, energy, _co2eq, conversions)
if self.epochs_before_pred == 0:
self._output_energy("Actual consumption:", time, energy, _co2eq, conversions)
else:
self._output_energy(f"Actual consumption for {self.epoch_counter} epoch(s):", time, energy, _co2eq, conversions)

def _output_pred(self):
"""Output predicted usage for full training epochs."""
Expand Down

0 comments on commit 6580e10

Please sign in to comment.