From 14aef14314fc0a1ee566df75c99338f817cf5b59 Mon Sep 17 00:00:00 2001 From: John Mulhausen Date: Mon, 17 Mar 2025 13:56:04 -0700 Subject: [PATCH 1/6] GPT Editor improvement: Properly align suggestions (#1196) Suggestions are drifting from the originating lines; added more logic to ensure the suggestions line up properly --- .github/workflows/gpt-editor-ci-version.yml | 37 ++++++++++++++++++--- 1 file changed, 33 insertions(+), 4 deletions(-) diff --git a/.github/workflows/gpt-editor-ci-version.yml b/.github/workflows/gpt-editor-ci-version.yml index 3e45f35934..e4986b4ddb 100644 --- a/.github/workflows/gpt-editor-ci-version.yml +++ b/.github/workflows/gpt-editor-ci-version.yml @@ -249,6 +249,9 @@ jobs: # Find changed sections using sequence matcher matcher = difflib.SequenceMatcher(None, original_lines, improved_lines) + # Get sections that have changes + sections_to_process = [] + for tag, i1, i2, j1, j2 in matcher.get_opcodes(): # Only process changes (not matches) if tag in ('replace', 'delete', 'insert'): @@ -262,13 +265,29 @@ jobs: line_end = i2 # i2 is exclusive in Python, but we need inclusive for line ranges # Only create suggestions for sections that overlap with PR changes if filtering - if not filter_by_changes or (modified_line_ranges and any( + should_process = not filter_by_changes or (modified_line_ranges and any( max(range_start, line_start) <= min(range_end, line_end) for range_start, range_end in modified_line_ranges - )): - create_suggestion(file_path, line_start, line_end, original_section, improved_section) + )) + + if should_process: + # Store the section for processing + sections_to_process.append((line_start, line_end, original_section, improved_section)) else: print(f"Skipping suggestion for {file_path} lines {line_start}-{line_end} as they weren't modified in the PR") + + # Process accumulated sections with proper line information + # Sort by starting line to process in order + for line_start, line_end, original_section, improved_section in sorted(sections_to_process, key=lambda x: x[0]): + if len(original_section) == 0 and len(improved_section) == 0: + continue # Skip if both sections are empty + + # Check for empty original section (insertion case) + if len(original_section) == 0: + # Insertion - point to the line where we want to insert + create_suggestion(file_path, line_start, line_start, original_section, improved_section) + else: + create_suggestion(file_path, line_start, line_end, original_section, improved_section) def create_suggestion(file_path, start_line, end_line, original_section, improved_section): """Create a GitHub PR review comment with a suggestion""" @@ -277,12 +296,22 @@ jobs: repo = os.environ.get('GITHUB_REPOSITORY') action_url = f"https://github.com/{repo}/actions/runs/{run_id}" + # Validate that the line numbers make sense + if start_line < 1: + print(f"Warning: Invalid start line {start_line}, adjusting to 1") + start_line = 1 + # Format the suggestion with link to the action run body = f"```suggestion\n{chr(10).join(improved_section)}\n```\n\n*[Generated by GPT Editor]({action_url})*" pr_number = os.environ.get('PR_NUMBER') github_token = os.environ.get('GITHUB_TOKEN') + # Debug output to help track line mappings + print(f"Suggesting change for {file_path} lines {start_line}-{end_line}") + print(f"Original ({len(original_section)} lines): {original_section[:1]}...") + print(f"Improved ({len(improved_section)} lines): {improved_section[:1]}...") + # Create a review comment using GitHub API try: # First get the latest commit SHA on the PR @@ -374,4 +403,4 @@ jobs: -H "Authorization: token $GITHUB_TOKEN" \ -H "Accept: application/vnd.github.v3+json" \ https://api.github.com/repos/$GITHUB_REPOSITORY/issues/$PR_NUMBER/comments \ - -d "$COMMENT_BODY" \ No newline at end of file + -d "$COMMENT_BODY" From 944ae1e564e794a5a219b5dc830c4e81cb47d51a Mon Sep 17 00:00:00 2001 From: Noah Luna <15202580+ngrayluna@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:29:53 -0700 Subject: [PATCH 2/6] First draft of BlueHawk code checker --- .github/workflows/check_code_snippets.yml | 36 +++++++++++++ code_examples/check_dependencies.sh | 5 ++ code_examples/requirements.txt | 1 + .../snippets/quickstart.snippet.all.py | 26 +++++++++ .../snippets/quickstart.snippet.import.py | 2 + .../snippets/quickstart.snippet.init.py | 7 +++ .../snippets/quickstart.snippet.login.py | 1 + code_examples/source/quickstart.py | 41 ++++++++++++++ content/guides/quickstart.md | 54 ++++--------------- layouts/shortcodes/code.html | 7 +++ 10 files changed, 137 insertions(+), 43 deletions(-) create mode 100644 .github/workflows/check_code_snippets.yml create mode 100644 code_examples/check_dependencies.sh create mode 100755 code_examples/requirements.txt create mode 100644 code_examples/snippets/quickstart.snippet.all.py create mode 100644 code_examples/snippets/quickstart.snippet.import.py create mode 100644 code_examples/snippets/quickstart.snippet.init.py create mode 100644 code_examples/snippets/quickstart.snippet.login.py create mode 100644 code_examples/source/quickstart.py create mode 100644 layouts/shortcodes/code.html diff --git a/.github/workflows/check_code_snippets.yml b/.github/workflows/check_code_snippets.yml new file mode 100644 index 0000000000..76f4a2a104 --- /dev/null +++ b/.github/workflows/check_code_snippets.yml @@ -0,0 +1,36 @@ +name: Test W&B Doc Code Snippets +on: [push, pull_request] + +jobs: + generate-matrix: + runs-on: ubuntu-latest + outputs: + scripts: ${{ steps.list-scripts.outputs.scripts }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Find Python scripts + id: list-scripts + run: echo "scripts=$(jq -c -R 'split("\n")[:-1]' < <(ls code_examples/source/*.py))" >> $GITHUB_ENV + + test: + needs: generate-matrix + runs-on: ubuntu-latest + strategy: + matrix: + script: ${{ fromJson(needs.generate-matrix.outputs.scripts) }} + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Install and check dependencies + run: bash code_examples/check_dependencies.sh + + - name: Run W&B script + run: python3 ${{ matrix.script }} diff --git a/code_examples/check_dependencies.sh b/code_examples/check_dependencies.sh new file mode 100644 index 0000000000..864d483a8e --- /dev/null +++ b/code_examples/check_dependencies.sh @@ -0,0 +1,5 @@ +#!/bin/bash + +pip install -r requirements.txt && echo "All packages installed successfully!" || echo "Installation failed." + +python -c "import pkgutil; import sys; packages = [pkg.strip() for pkg in open('requirements.txt')]; missing = [p for p in packages if not pkgutil.find_loader(p)]; sys.exit(1) if missing else print('✅ All packages are installed.')" diff --git a/code_examples/requirements.txt b/code_examples/requirements.txt new file mode 100755 index 0000000000..8a70eb6fdb --- /dev/null +++ b/code_examples/requirements.txt @@ -0,0 +1 @@ +wandb \ No newline at end of file diff --git a/code_examples/snippets/quickstart.snippet.all.py b/code_examples/snippets/quickstart.snippet.all.py new file mode 100644 index 0000000000..a5cb2a17a8 --- /dev/null +++ b/code_examples/snippets/quickstart.snippet.all.py @@ -0,0 +1,26 @@ +import wandb +import random + +wandb.login() + +epochs = 10 +lr = 0.01 + +run = wandb.init( + project="my-awesome-project", # Specify your project + config={ # Track hyperparameters and metadata + "learning_rate": lr, + "epochs": epochs, + }, +) + +offset = random.random() / 5 +print(f"lr: {lr}") + +# Simulate a training run +for epoch in range(2, epochs): + acc = 1 - 2**-epoch - random.random() / epoch - offset + loss = 2**-epoch + random.random() / epoch + offset + print(f"epoch={epoch}, accuracy={acc}, loss={loss}") + wandb.log({"accuracy": acc, "loss": loss}) +run.finish() diff --git a/code_examples/snippets/quickstart.snippet.import.py b/code_examples/snippets/quickstart.snippet.import.py new file mode 100644 index 0000000000..f8ed4b6b22 --- /dev/null +++ b/code_examples/snippets/quickstart.snippet.import.py @@ -0,0 +1,2 @@ +import wandb +import random diff --git a/code_examples/snippets/quickstart.snippet.init.py b/code_examples/snippets/quickstart.snippet.init.py new file mode 100644 index 0000000000..9b21272057 --- /dev/null +++ b/code_examples/snippets/quickstart.snippet.init.py @@ -0,0 +1,7 @@ +run = wandb.init( + project="my-awesome-project", # Specify your project + config={ # Track hyperparameters and metadata + "learning_rate": lr, + "epochs": epochs, + }, +) diff --git a/code_examples/snippets/quickstart.snippet.login.py b/code_examples/snippets/quickstart.snippet.login.py new file mode 100644 index 0000000000..82b1430e9a --- /dev/null +++ b/code_examples/snippets/quickstart.snippet.login.py @@ -0,0 +1 @@ +wandb.login() diff --git a/code_examples/source/quickstart.py b/code_examples/source/quickstart.py new file mode 100644 index 0000000000..fca3dd06d3 --- /dev/null +++ b/code_examples/source/quickstart.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +try: + # :snippet-start: all + # :snippet-start: import + import wandb + import random + # :snippet-end: import + + # :snippet-start: login + wandb.login() + # :snippet-end: login + + epochs = 10 + lr = 0.01 + + # :snippet-start: init + run = wandb.init( + project="my-awesome-project", # Specify your project + config={ # Track hyperparameters and metadata + "learning_rate": lr, + "epochs": epochs, + }, + ) + # :snippet-end: init + + offset = random.random() / 5 + print(f"lr: {lr}") + + # Simulate a training run + for epoch in range(2, epochs): + acc = 1 - 2**-epoch - random.random() / epoch - offset + loss = 2**-epoch + random.random() / epoch + offset + print(f"epoch={epoch}, accuracy={acc}, loss={loss}") + wandb.log({"accuracy": acc, "loss": loss}) + run.finish() + # :snippet-end: all + exit(0) +except Exception as e: + print(f"Error: {e}") + exit(1) \ No newline at end of file diff --git a/content/guides/quickstart.md b/content/guides/quickstart.md index e974e6f0c7..b446a000b3 100644 --- a/content/guides/quickstart.md +++ b/content/guides/quickstart.md @@ -38,13 +38,18 @@ Install the `wandb` library and log in by following these steps: {{% tab header="Python" value="python" %}} +Navigate to your terminal and run the following command: ```bash pip install wandb ``` -```python -import wandb -wandb.login() -``` + +Within your Python script, import the `wandb` library: + +{{< code language="python" source="/code_examples/snippets/quickstart.snippet.import.py" >}} + +Next, log in to W&B using the `wandb.login()` method: + +{{< code language="python" source="/code_examples/snippets/quickstart.snippet.login.py" >}} {{% /tab %}} @@ -63,15 +68,7 @@ wandb.login() In your Python script or notebook, initialize a W&B run object with [`wandb.init()`]({{< relref "/ref/python/run.md" >}}). Use a dictionary for the `config` parameter to specify hyperparameter names and values: -```python -run = wandb.init( - project="my-awesome-project", # Specify your project - config={ # Track hyperparameters and metadata - "learning_rate": 0.01, - "epochs": 10, - }, -) -``` +{{< code language="python" source="/code_examples/snippets/quickstart.snippet.init.py" >}} A [run]({{< relref "/guides/models/track/runs/" >}}) serves as the core element of W&B, used to [track metrics]({{< relref "/guides/models/track/" >}}), [create logs]({{< relref "/guides/core/artifacts/" >}}), and more. @@ -79,36 +76,7 @@ A [run]({{< relref "/guides/models/track/runs/" >}}) serves as the core element This mock training script logs simulated accuracy and loss metrics to W&B: -```python -# train.py -import wandb -import random - -wandb.login() - -epochs = 10 -lr = 0.01 - -run = wandb.init( - project="my-awesome-project", # Specify your project - config={ # Track hyperparameters and metadata - "learning_rate": lr, - "epochs": epochs, - }, -) - -offset = random.random() / 5 -print(f"lr: {lr}") - -# Simulate a training run -for epoch in range(2, epochs): - acc = 1 - 2**-epoch - random.random() / epoch - offset - loss = 2**-epoch + random.random() / epoch + offset - print(f"epoch={epoch}, accuracy={acc}, loss={loss}") - wandb.log({"accuracy": acc, "loss": loss}) - -# run.log_code() -``` +{{< code language="python" source="/code_examples/snippets/quickstart.snippet.all.py" >}} Visit the W&B App at [wandb.ai/home](https://wandb.ai/home) to view recorded metrics such as accuracy and loss during each training step. diff --git a/layouts/shortcodes/code.html b/layouts/shortcodes/code.html new file mode 100644 index 0000000000..5d2b9edde7 --- /dev/null +++ b/layouts/shortcodes/code.html @@ -0,0 +1,7 @@ +{{ $language := .Get "language" }} +{{ $source := .Get "source" }} +{{ $options := .Get "options" }} + +{{ with $source | readFile }} + {{ highlight (trim . "\n\r") $language $options }} +{{ end }} \ No newline at end of file From 8bd3a26cfa02584903860f1fa8f2cd66b09c1b64 Mon Sep 17 00:00:00 2001 From: Noah Luna <15202580+ngrayluna@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:46:57 -0700 Subject: [PATCH 3/6] updates scripts output --- .github/workflows/check_code_snippets.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/check_code_snippets.yml b/.github/workflows/check_code_snippets.yml index 76f4a2a104..59dc3c92e1 100644 --- a/.github/workflows/check_code_snippets.yml +++ b/.github/workflows/check_code_snippets.yml @@ -12,7 +12,10 @@ jobs: - name: Find Python scripts id: list-scripts - run: echo "scripts=$(jq -c -R 'split("\n")[:-1]' < <(ls code_examples/source/*.py))" >> $GITHUB_ENV + run: | + SCRIPTS=$(find scripts -name "*.py" | jq -R -s -c 'split("\n")[:-1]') + echo "scripts=$SCRIPTS" >> $GITHUB_ENV + echo "::set-output name=scripts::$SCRIPTS" test: needs: generate-matrix From 011aada449c3eda45efe7d5537601406f7e9032a Mon Sep 17 00:00:00 2001 From: Noah Luna <15202580+ngrayluna@users.noreply.github.com> Date: Mon, 17 Mar 2025 16:49:14 -0700 Subject: [PATCH 4/6] Updated source path --- .github/workflows/check_code_snippets.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check_code_snippets.yml b/.github/workflows/check_code_snippets.yml index 59dc3c92e1..213a414ab0 100644 --- a/.github/workflows/check_code_snippets.yml +++ b/.github/workflows/check_code_snippets.yml @@ -13,7 +13,7 @@ jobs: - name: Find Python scripts id: list-scripts run: | - SCRIPTS=$(find scripts -name "*.py" | jq -R -s -c 'split("\n")[:-1]') + SCRIPTS=$(find code_examples/source -name "*.py" | jq -R -s -c 'split("\n")[:-1]') echo "scripts=$SCRIPTS" >> $GITHUB_ENV echo "::set-output name=scripts::$SCRIPTS" From ff83f15a17799ed83e23fccb5c8493ffff8693ba Mon Sep 17 00:00:00 2001 From: Noah Luna <15202580+ngrayluna@users.noreply.github.com> Date: Mon, 17 Mar 2025 17:02:20 -0700 Subject: [PATCH 5/6] Fixed requirements.txt file paht --- .github/workflows/check_code_snippets.yml | 2 +- code_examples/check_dependencies.sh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/check_code_snippets.yml b/.github/workflows/check_code_snippets.yml index 213a414ab0..0fe94a9188 100644 --- a/.github/workflows/check_code_snippets.yml +++ b/.github/workflows/check_code_snippets.yml @@ -1,5 +1,5 @@ name: Test W&B Doc Code Snippets -on: [push, pull_request] +on: [pull_request] jobs: generate-matrix: diff --git a/code_examples/check_dependencies.sh b/code_examples/check_dependencies.sh index 864d483a8e..85fccc3b86 100644 --- a/code_examples/check_dependencies.sh +++ b/code_examples/check_dependencies.sh @@ -1,5 +1,5 @@ #!/bin/bash +REQUIREMENTS_PATH="code_examples/requirements.txt" +pip install -r "$REQUIREMENTS_PATH" && echo "All packages installed successfully!" || echo "Installation failed." -pip install -r requirements.txt && echo "All packages installed successfully!" || echo "Installation failed." - -python -c "import pkgutil; import sys; packages = [pkg.strip() for pkg in open('requirements.txt')]; missing = [p for p in packages if not pkgutil.find_loader(p)]; sys.exit(1) if missing else print('✅ All packages are installed.')" +python -c "import pkgutil; import sys; packages = [pkg.strip() for pkg in open('$REQUIREMENTS_PATH')]; missing = [p for p in packages if not pkgutil.find_loader(p)]; sys.exit(1) if missing else print('✅ All packages are installed.')" \ No newline at end of file From e368b541d39bd8fc84c04c2cb159241aa9ed4273 Mon Sep 17 00:00:00 2001 From: Noah Luna <15202580+ngrayluna@users.noreply.github.com> Date: Wed, 3 Sep 2025 14:47:39 -0700 Subject: [PATCH 6/6] fix merge conflict --- content/en/guides/quickstart.md | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/content/en/guides/quickstart.md b/content/en/guides/quickstart.md index 0c0c3a118d..1c9fec8af7 100644 --- a/content/en/guides/quickstart.md +++ b/content/en/guides/quickstart.md @@ -78,36 +78,7 @@ A [run]({{< relref "/guides/models/track/runs/" >}}) serves as the core element This mock training script logs simulated accuracy and loss metrics to W&B: -<<<<<<< HEAD:content/guides/quickstart.md {{< code language="python" source="/code_examples/snippets/quickstart.snippet.all.py" >}} -======= -```python -import wandb -import random - -wandb.login() - -# Project that the run is recorded to -project = "my-awesome-project" - -# Dictionary with hyperparameters -config = { - 'epochs' : 10, - 'lr' : 0.01 -} - -with wandb.init(project=project, config=config) as run: - offset = random.random() / 5 - print(f"lr: {config['lr']}") - - # Simulate a training run - for epoch in range(2, config['epochs']): - acc = 1 - 2**-config['epochs'] - random.random() / config['epochs'] - offset - loss = 2**-config['epochs'] + random.random() / config['epochs'] + offset - print(f"epoch={config['epochs']}, accuracy={acc}, loss={loss}") - run.log({"accuracy": acc, "loss": loss}) -``` ->>>>>>> main:content/en/guides/quickstart.md Visit [wandb.ai/home](https://wandb.ai/home) to view recorded metrics such as accuracy and loss and how they changed during each training step. The following image shows the loss and accuracy tracked from each run. Each run object appears in the **Runs** column with generated names.