Skip to content
Merged
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
80 changes: 68 additions & 12 deletions .github/workflows/translate-notebooks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,90 @@ on:
push:
branches:
- main
paths:
- "**/*.ipynb"
- "translate_notebooks.py"
- ".github/workflows/translate-notebooks.yml"
pull_request:
branches:
- main
paths:
- "**/*.ipynb"
- "translate_notebooks.py"
- ".github/workflows/translate-notebooks.yml"
workflow_dispatch:

permissions:
contents: write

concurrency:
group: translate-notebooks-${{ github.ref }}
cancel-in-progress: true

jobs:
translate:
if: github.actor != 'github-actions[bot]' && (github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository)
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v4
uses: actions/setup-python@v5
with:
python-version: '3.x'
python-version: "3.12"

- name: Install dependencies
run: |
pip install nbformat requests # nbformat for reading/writing notebooks
pip install nbformat requests

- name: Determine notebooks to translate
id: notebooks
shell: bash
run: |
set -euo pipefail

NOTEBOOK_LIST=.changed-notebooks.txt
: > "$NOTEBOOK_LIST"

if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
find . -type f -name '*.ipynb' -not -path './translated-notebooks/*' | sed 's|^\./||' | sort > "$NOTEBOOK_LIST"
elif [[ "${{ github.event_name }}" == "pull_request" ]]; then
git diff --name-only "${{ github.event.pull_request.base.sha }}" "${{ github.event.pull_request.head.sha }}" -- '*.ipynb' ':(exclude)translated-notebooks/**' | sort > "$NOTEBOOK_LIST"
elif [[ "${{ github.event.before }}" == "0000000000000000000000000000000000000000" ]]; then
find . -type f -name '*.ipynb' -not -path './translated-notebooks/*' | sed 's|^\./||' | sort > "$NOTEBOOK_LIST"
else
git diff --name-only "${{ github.event.before }}" "${{ github.sha }}" -- '*.ipynb' ':(exclude)translated-notebooks/**' | sort > "$NOTEBOOK_LIST"
fi

NOTEBOOK_COUNT="$(grep -c . "$NOTEBOOK_LIST" || true)"
echo "count=$NOTEBOOK_COUNT" >> "$GITHUB_OUTPUT"
echo "file_list=$NOTEBOOK_LIST" >> "$GITHUB_OUTPUT"
echo "Selected notebooks:"
cat "$NOTEBOOK_LIST" || true

- name: Translate Notebooks
if: steps.notebooks.outputs.count != '0'
env:
DASHSCOPE_API_KEY: ${{ secrets.DASHSCOPE_API_KEY }}
QWEN_API_KEY: ${{ secrets.QWEN_API_KEY }}
DASHSCOPE_API_ENDPOINT: ${{ vars.DASHSCOPE_API_ENDPOINT }}
QWEN_API_ENDPOINT: ${{ vars.QWEN_API_ENDPOINT }}
QWEN_MODEL: ${{ vars.QWEN_MODEL }}
run: |
python translate-notebooks.py # Translation script
python translate_notebooks.py --file-list "${{ steps.notebooks.outputs.file_list }}"

- name: Commit and push changes
- name: Commit translated notebooks
if: github.event_name != 'pull_request' && steps.notebooks.outputs.count != '0'
run: |
git config --global user.email "github-actions[bot]@users.noreply.github.com"
git config --global user.name "GitHub Actions Bot"
git add .
git commit -m "Translate notebooks to Chinese"
git checkout -b translated # Create a new branch called 'translated'
git push origin translated # Push to the 'translated' branch
if [ -z "$(git status --porcelain -- translated-notebooks)" ]; then
echo "No translated notebook changes to commit."
exit 0
fi
git config user.email "github-actions[bot]@users.noreply.github.com"
git config user.name "GitHub Actions Bot"
git add translated-notebooks
git commit -m "chore: update Chinese notebook translations"
git push origin HEAD:main
Loading
Loading