Skip to content

Commit

Permalink
Lite wheel optimization (#7855)
Browse files Browse the repository at this point in the history
* Add `pull_request.branches.main` as a trigger of the `publish` workflow

* [WIP] Comment out the publish steps

* Package and upload the NPM package for debug

* Skip the copy_frontend.py hook in the Lite build

* add changeset

* [WIP] Show gradio files

* [WIP] Show gradio files

* Comment out installing the gradio and gradio_client libraries

* Restore installing gradio_client because it's used by `python js/_website/generate_jsons/generate.py` that follows

* Restore installing gradio because it's used by `python js/_website/generate_jsons/generate.py` that follows

* Add code

* Revert "[WIP] Show gradio files"

This reverts commit e15fef2.

* Build the gradio wheel with the custom lite target

* add changeset

* Revert "[WIP] Show gradio files"

This reverts commit aef053f.

* Revert "Skip the copy_frontend.py hook in the Lite build"

This reverts commit ca296d0.

* Update .github/actions/install-frontend-deps/action.yml for hatch installation

* [WIP] Fix test-functional.yml and .github/actions/install-all-deps/action.yml to call the setup actions in this branch

* Revert "[WIP] Fix test-functional.yml and .github/actions/install-all-deps/action.yml to call the setup actions in this branch"

This reverts commit 571823b.

* Comment-in lines in publish.yml

* Move Lite build from publish.yml to deploy-spaces.yml

* Use the build_lite option of install-all-deps instead of running the build command

* [TMP] Change the branch of action files

* Fix the hatch Lite build setting

* Return pnpm pack to deploy-space

* Revert "[TMP] Change the branch of action files"

This reverts commit fe4e1c8.

* Remove dependencies for lite build

* [TMP] Change the branch of action files

* Revert "Remove dependencies for lite build"

This reverts commit 856a858.

* Install packaging>=23.2

* [TMP] Show packaging version

* Fix pip install

* Fix

* Uninstall packaging once

* Use `pip install -U` instead of uninstalling the exiting version

* Revert "[TMP] Show packaging version"

This reverts commit 910b6bb.

* Add `-U` flag

* Set packaging version as >=23.2

* Revert the changes on pip install

* Set packaging version as >=23.2 in requirements.txt

* Revert "Set packaging version as >=23.2"

This reverts commit 8aa77c8.

* Fix hook name

* Revert "Set packaging version as >=23.2 in requirements.txt"

This reverts commit fbd605c.

* Revert "Revert the changes on pip install"

This reverts commit 81ff38a.

* Add comments

* Revert "[TMP] Change the branch of action files"

This reverts commit 0d6aa48.

* Revert the trigger of .github/workflows/deploy-spaces.yml

* Remove unused `node_auth_token` and `npm_token` inputs from the `install-all-deps` action

* [TMP] Trigger CI based on this PR

* Remove packging installation

* Revert "Remove packging installation"

This reverts commit 4a4f18d.

* Revert "[TMP] Trigger CI based on this PR"

This reverts commit 6cea830.

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
  • Loading branch information
3 people committed Apr 30, 2024
1 parent 989fe25 commit 611c927
Show file tree
Hide file tree
Showing 13 changed files with 92 additions and 55 deletions.
6 changes: 6 additions & 0 deletions .changeset/spotty-toes-argue.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@gradio/app": minor
"gradio": minor
---

feat:Lite wheel optimization
33 changes: 33 additions & 0 deletions .config/lite-builder/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[build-system]
requires = ["hatchling",]
build-backend = "hatchling.build"

[project]
name = "lite-builder"
description = "Python library for easily interacting with trained machine learning models"
license = "Apache-2.0"
version = "0.0.2"
requires-python = ">=3.8"
authors = [
{ name = "Abubakar Abid", email = "gradio-team@huggingface.co" },
{ name = "Ali Abid", email = "gradio-team@huggingface.co" },
{ name = "Ali Abdalla", email = "gradio-team@huggingface.co" },
{ name = "Dawood Khan", email = "gradio-team@huggingface.co" },
{ name = "Ahsen Khaliq", email = "gradio-team@huggingface.co" },
{ name = "Pete Allen", email = "gradio-team@huggingface.co" },
{ name = "脰mer Faruk 脰zdemir", email = "gradio-team@huggingface.co" },
{ name = "Freddy A Boulton", email = "gradio-team@huggingface.co" },
{ name = "Hannah Blair", email = "gradio-team@huggingface.co" },
]
keywords = ["machine learning", "reproducibility", "visualization"]

classifiers = [
'Development Status :: 5 - Production/Stable',
]

[tool.hatch.build]
sources = ["src"]
only-packages = true

[project.entry-points.hatch]
lite_builder = "lite_builder.hooks"
Empty file.
5 changes: 5 additions & 0 deletions .config/lite-builder/src/lite_builder/builder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from hatchling.builders.wheel import WheelBuilder


class LiteBuilder(WheelBuilder):
PLUGIN_NAME = 'lite'
6 changes: 6 additions & 0 deletions .config/lite-builder/src/lite_builder/hooks.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from hatchling.plugin import hookimpl
from .builder import LiteBuilder

@hookimpl
def hatch_register_builder():
return LiteBuilder
12 changes: 2 additions & 10 deletions .github/actions/install-all-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,9 @@ name: 'install all deps'
description: 'Install all deps'

inputs:
always_install_pnpm:
always_install_pnpm:
description: 'Dictates whether or not we should install pnpm & dependencies, regardless of the cache'
default: 'false'
node_auth_token:
description: 'Node auth token'
default: ""
npm_token:
description: 'npm token'
default: ""
skip_build:
description: 'Skip build'
default: 'false'
Expand Down Expand Up @@ -85,14 +79,12 @@ runs:
shell: bash
run: |
. ${{ env.VENV_ACTIVATE }}
python -m pip install -e client/python
python -m pip install -e client/python
python -m pip install -e ".[oauth]"
- name: install-frontend
uses: "gradio-app/gradio/.github/actions/install-frontend-deps@main"
with:
always_install_pnpm: ${{ inputs.always_install_pnpm }}
node_auth_token: ${{ inputs.node_auth_token }}
npm_token: ${{ inputs.npm_token }}
skip_build: ${{ inputs.skip_build }}
build_lite: ${{ inputs.build_lite }}
- name: generate json
Expand Down
13 changes: 2 additions & 11 deletions .github/actions/install-frontend-deps/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,12 @@ inputs:
always_install_pnpm:
description: 'Dictates whether or not we should install pnpm & dependencies, regardless of the cache'
default: 'false'
node_auth_token:
description: 'Node auth token'
default: ""
npm_token:
description: 'npm token'
default: ""
skip_build:
description: 'Skip build'
default: 'false'
build_lite:
description: 'Build lite'
default: 'false'
os:
description: 'OS'
default: 'ubuntu-latest'

runs:
using: "composite"
Expand Down Expand Up @@ -56,5 +47,5 @@ runs:
shell: bash
run: |
. venv/bin/activate
python -m pip install build
pnpm --filter @gradio/app build:lite
python -m pip install -U build hatch packaging>=23.2 # packaging>=23.2 is needed to build Lite due to https://github.com/pypa/hatch/issues/1381
pnpm --filter @gradio/app build:lite
40 changes: 20 additions & 20 deletions .github/workflows/deploy-spaces.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ on:
workflow_dispatch:
workflow_run:
workflows: ["trigger"]
types:
types:
- requested

permissions:
statuses: write
concurrency:
group: "${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}-${{ github.workflow_ref }}"
cancel-in-progress: true

jobs:
changes:
name: "changes"
Expand All @@ -35,7 +35,7 @@ jobs:
name: "deploy / spaces"
token: ${{ secrets.GITHUB_TOKEN }}
commit_status: false

comment-spaces-start:
needs: changes
uses: "./.github/workflows/comment-queue.yml"
Expand All @@ -57,19 +57,23 @@ jobs:
with:
ref: ${{ needs.changes.outputs.merge_sha }}
repository: ${{ needs.changes.outputs.mergeable == 'true' && github.repository || needs.changes.outputs.source_repo }}
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: '3.9'
- name: Install pnpm
uses: pnpm/action-setup@v2

- name: install dependencies
uses: "gradio-app/gradio/.github/actions/install-all-deps@main"
with:
version: 8.15
- uses: actions/setup-node@v4
always_install_pnpm: true
python_version: '3.9'
build_lite: 'true'

- name: Package Lite NPM package
working-directory: js/lite
run: pnpm pack --pack-destination .
- name: Upload Lite NPM package
uses: actions/upload-artifact@v4
with:
node-version: 18
cache: pnpm
cache-dependency-path: pnpm-lock.yaml
name: gradio-lite-${{ github.sha }}
path: js/lite/gradio-lite-*.tgz

- name: Install pip
run: python -m pip install build requests
- name: Get Gradio Version
Expand All @@ -78,17 +82,13 @@ jobs:
if ${{ github.event_name == 'pull_request' || github.event.workflow_run.event == 'pull_request' }}; then
echo "GRADIO_VERSION=$(python -c 'import requests;print(requests.get("https://pypi.org/pypi/gradio/json").json()["info"]["version"])')" >> $GITHUB_OUTPUT
python -c "import os;print(os.environ['GITHUB_REF'].split('/')[2])" > pr_number.txt
else
else
echo "GRADIO_VERSION=$(python -c 'import json; print(json.load(open("gradio/package.json"))["version"])')" >> $GITHUB_OUTPUT
fi
- name: Build pr package
run: |
python -c 'import json; j = json.load(open("gradio/package.json")); j["version"] = "${{ steps.get_gradio_version.outputs.GRADIO_VERSION }}"; json.dump(j, open("gradio/package.json", "w"))'
pnpm i --frozen-lockfile --ignore-scripts
pnpm build
python3 -m build -w
env:
NODE_OPTIONS: --max_old_space_size=8192
- name: Set up Demos
run: |
Expand Down Expand Up @@ -123,7 +123,7 @@ jobs:
run: |
echo ${{github.event.workflow_run.event }}
echo ${{ github.event.workflow_run.conclusion }}
comment-spaces-success:
uses: "./.github/workflows/comment-queue.yml"
needs: [deploy-spaces, changes]
Expand Down
22 changes: 10 additions & 12 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ jobs:
uses: "gradio-app/gradio/.github/actions/install-all-deps@main"
with:
always_install_pnpm: true
node_auth_token: ${{ secrets.NPM_TOKEN }}
npm_token: ${{ secrets.NPM_TOKEN }}
skip_build: 'false'
- name: Build packages
run: |
run: |
. venv/bin/activate
pip install build
pip install -U build hatch packaging>=23.2 # packaging>=23.2 is needed to build Lite due to https://github.com/pypa/hatch/issues/1381
pnpm --filter @gradio/client --filter @gradio/lite --filter @gradio/preview build
- name: create and publish versions
id: changesets
Expand All @@ -43,26 +41,26 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
- name: add label to skip chromatic build
if: ${{ steps.changesets.outputs.pullRequestNumber != '' && steps.changesets.outputs.pullRequestNumber != 'undefined' }}
run: gh pr edit "$PR_NUMBER" --add-label "no-visual-update"
run: gh pr edit "$PR_NUMBER" --add-label "no-visual-update"
env:
PR_NUMBER: ${{ steps.changesets.outputs.pullRequestNumber }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
- name: add label to run flaky tests
if: ${{ steps.changesets.outputs.pullRequestNumber != '' && steps.changesets.outputs.pullRequestNumber != 'undefined' }}
run: gh pr edit "$PR_NUMBER" --add-label "flaky-tests"
run: gh pr edit "$PR_NUMBER" --add-label "flaky-tests"
env:
PR_NUMBER: ${{ steps.changesets.outputs.pullRequestNumber }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
- name: add label to run backend tests on Windows
if: ${{ steps.changesets.outputs.pullRequestNumber != '' && steps.changesets.outputs.pullRequestNumber != 'undefined' }}
run: gh pr edit "$PR_NUMBER" --add-label "windows-tests"
run: gh pr edit "$PR_NUMBER" --add-label "windows-tests"
env:
PR_NUMBER: ${{ steps.changesets.outputs.pullRequestNumber }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
GITHUB_TOKEN: ${{ secrets.GRADIO_PAT }}
- name: publish to pypi
if: steps.changesets.outputs.hasChangesets != 'true'
uses: "gradio-app/github/actions/publish-pypi@main"
env:
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWSACCESSKEYID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWSSECRETKEY }}
AWS_DEFAULT_REGION: us-west-2
Expand All @@ -72,6 +70,6 @@ jobs:
gradio:${{ secrets.PYPI_API_TOKEN }}
gradio_client:${{ secrets.PYPI_GRADIO_CLIENT_TOKEN }}
- name: trigger spaces deploy workflow
env:
env:
GITHUB_TOKEN: ${{ secrets.COMMENT_TOKEN }}
run: gh workflow run deploy-spaces.yml
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.eggs/
gradio.egg-info
dist/
dist-lite/
*.pyc
__pycache__/
*.py[cod]
Expand Down
2 changes: 1 addition & 1 deletion js/app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"dev:lite:worker": "pnpm --filter @gradio/wasm dev",
"build": "vite build --mode production --emptyOutDir",
"cssbuild": "python ../../scripts/generate_theme.py --outfile ./src/lite/theme.css",
"pybuild:gradio": "cd ../../ && python -m build",
"pybuild:gradio": "python -m pip install -U hatch packaging>=23.2 && cd ../../ && hatch build -t lite",
"pybuild:gradio-client": "cd ../../client/python && python -m build",
"pybuild": "run-p pybuild:*",
"build:lite": "pnpm pybuild && pnpm cssbuild && pnpm --filter @gradio/client build && pnpm --filter @gradio/wasm build && vite build --mode production:lite",
Expand Down
2 changes: 1 addition & 1 deletion js/app/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ export default defineConfig(({ mode }) => {
// For the Wasm app to import the wheel file URLs.
"gradio.whl": resolve(
__dirname,
`../../dist/gradio-${version_raw}-py3-none-any.whl`
`../../dist-lite/gradio-${version_raw}-py3-none-any.whl`
),
"gradio_client.whl": resolve(
__dirname,
Expand Down
5 changes: 5 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ artifacts = [
"py.typed",
]

[tool.hatch.build.targets.lite]
artifacts = ["!gradio/templates", "!*.pyi", "!/gradio/test_data", "!/gradio/_frontend_code", "!/gradio/node"]
directory = "dist-lite"
dependencies = ["lite-builder @ {root:uri}/../.config/lite-builder"]

[tool.hatch.build.targets.wheel.hooks.custom]
path = ".config/copy_frontend.py"

Expand Down

0 comments on commit 611c927

Please sign in to comment.