Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Blue hawk demo #1197

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/check_code_snippets.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: Test W&B Doc Code Snippets
on: [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: |
SCRIPTS=$(find code_examples/source -name "*.py" | jq -R -s -c 'split("\n")[:-1]')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a bit greedy. At a minimum I'd add -type f so you don't find directories that end in .py. I'm also confused why you have to take something that is going to be multi-line input with one result per line, join it up, and then split it again right away?

Suggested change
SCRIPTS=$(find code_examples/source -name "*.py" | jq -R -s -c 'split("\n")[:-1]')
SCRIPTS=$(find 'code_examples/source' -type f -name "*.py" | jq -R -s -c 'split("\n")[:-1]')

echo "scripts=$SCRIPTS" >> $GITHUB_ENV
echo "::set-output name=scripts::$SCRIPTS"

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 }}
37 changes: 33 additions & 4 deletions .github/workflows/gpt-editor-ci-version.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'):
Expand All @@ -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"""
Expand All @@ -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
Expand Down Expand Up @@ -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"
-d "$COMMENT_BODY"
5 changes: 5 additions & 0 deletions code_examples/check_dependencies.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
REQUIREMENTS_PATH="code_examples/requirements.txt"
pip install -r "$REQUIREMENTS_PATH" && echo "All packages installed successfully!" || echo "Installation failed."

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.')"
1 change: 1 addition & 0 deletions code_examples/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wandb
26 changes: 26 additions & 0 deletions code_examples/snippets/quickstart.snippet.all.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 2 additions & 0 deletions code_examples/snippets/quickstart.snippet.import.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import wandb
import random
7 changes: 7 additions & 0 deletions code_examples/snippets/quickstart.snippet.init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
run = wandb.init(
project="my-awesome-project", # Specify your project
config={ # Track hyperparameters and metadata
"learning_rate": lr,
"epochs": epochs,
},
)
1 change: 1 addition & 0 deletions code_examples/snippets/quickstart.snippet.login.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wandb.login()
41 changes: 41 additions & 0 deletions code_examples/source/quickstart.py
Original file line number Diff line number Diff line change
@@ -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)
54 changes: 11 additions & 43 deletions content/guides/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 %}}

Expand All @@ -63,52 +68,15 @@ 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.

## Assemble the components

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.

Expand Down
7 changes: 7 additions & 0 deletions layouts/shortcodes/code.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{{ $language := .Get "language" }}
{{ $source := .Get "source" }}
{{ $options := .Get "options" }}

{{ with $source | readFile }}
{{ highlight (trim . "\n\r") $language $options }}
{{ end }}
Loading