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

allow the canvas size to be set on the ImageEditor #8127

Merged
merged 16 commits into from
May 3, 2024
Merged

allow the canvas size to be set on the ImageEditor #8127

merged 16 commits into from
May 3, 2024

Conversation

pngwn
Copy link
Member

@pngwn pngwn commented Apr 25, 2024

Description

Please include a concise summary, in clear English, of the changes in this pull request. If it closes an issue, please mention it here.

Closes: #(issue)

🎯 PRs Should Target Issues

Before your create a PR, please check to see if there is an existing issue for this change. If not, please create an issue before you create this PR, unless the fix is very small.

Not adhering to this guideline will result in the PR being closed.

Tests

  1. PRs will only be merged if tests pass on CI. To run the tests locally, please set up your Gradio environment locally and run the tests: bash scripts/run_all_tests.sh

  2. You may need to run the linters: bash scripts/format_backend.sh and bash scripts/format_frontend.sh

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 25, 2024

🪼 branch checks and previews

Name Status URL
Spaces ready! Spaces preview
Website ready! Website preview
Storybook ready! Storybook preview
🦄 Changes detected! Details

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/866f2f72139d9bbb5b6d01617671fb04b985385f/gradio-4.28.3-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@866f2f72139d9bbb5b6d01617671fb04b985385f#subdirectory=client/python"

@gradio-pr-bot
Copy link
Contributor

gradio-pr-bot commented Apr 25, 2024

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/imageeditor minor
gradio minor
  • Maintainers can select this checkbox to manually select packages to update.

With the following changelog entry.

allow the canvas size to be set on the ImageEditor

Maintainers or the PR author can modify the PR title to modify this entry.

Something isn't right?

  • Maintainers can change the version label to modify the version bump.
  • If the bot has failed to detect any changes, or if this pull request needs to update multiple packages to different versions or requires a more comprehensive changelog entry, maintainers can update the changelog file directly.

@pngwn
Copy link
Member Author

pngwn commented Apr 30, 2024

Just laying out some scenarios that I need to test:

  • canvas_size but no sources. canvas_size always applies.
  • canvas_size but no crop size (+ sources). canvas_size should apply at load. Adding a new source may alter that. reset should use the canvas_size again.
  • canvas_size with crop_size. This doesn't make any sense if they are different. crop_size is really a ratio-based constraint / maximum so they don't directly conflict but they should be equivalent. canvas_size=[512, 512] and crop_size="1:1:" or crop_size="[100, 100]" is fine (it will get resized to 100 x 100 but the UI will be larger). crop_size will take precedence if values are incompatible.

@abidlabs
Copy link
Member

abidlabs commented Apr 30, 2024

canvas_size but no crop size (+ sources). canvas_size should apply at load. Adding a new source may alter that. reset should use the canvas_size again.

Just to confirm, source here refers to the background image, right? So the idea here is that if you set a background image, its dimensions will set the canvas size?

@abidlabs
Copy link
Member

Just laying out some scenarios that I need to test:

Yes these are the only scenarios I can think of as well @pngwn

@abidlabs
Copy link
Member

Noticed that the docstrings for height and width are incorrect:

height: The height of the displayed images, specified in pixels if a number is passed, or in CSS units if a string is passed.
width: The width of the displayed images, specified in pixels if a number is passed, or in CSS units if a string is passed.

This is not the behavior, at least when interactive=True. These arguments do not affect the size of the images, but rather of the container of the component as a whole (which is consistent with other components).

e.g. width=500

image

e.g. width=1000

image

This means we shoudn't worry about these arguments conflicting with canvas_size (unless they are too small), but we should fix the docstrings

@pngwn
Copy link
Member Author

pngwn commented Apr 30, 2024

I'll fix the docstrings as well. That has actually been a source of confusion so it will be good to clean them up.

@pngwn
Copy link
Member Author

pngwn commented May 1, 2024

Just to confirm, source here refers to the background image, right? So the idea here is that if you set a background image, its dimensions will set the canvas size?

Yes that is correct. The crop is what acts as an actual constraint, canvas_size is just a default / clear value really.

@pngwn
Copy link
Member Author

pngwn commented May 1, 2024

Tested all and works fine. I have also added the ability to set sources=None because sources=[] was annoying.

Some demo's below if anyone wants to test:

Scenario 1
canvas_size but sources=None

import gradio as gr


def predict(im):
    return im["composite"]

def get_image_size(im):
    return f"{im['background'].shape[0]} x {im['background'].shape[1]}"


with gr.Blocks() as demo:
    with gr.Group():
        with gr.Row():
            im = gr.ImageEditor(
                canvas_size=(1000, 1000),
                sources=None,
            )
            im_preview = gr.Image()
        txt = gr.Textbox(label="image size")
        im.change(predict, outputs=im_preview, inputs=im, show_progress="hidden")
        im.change(get_image_size, inputs=im, outputs=txt)


if __name__ == "__main__":
    demo.launch()

Scenario 2

canvas_size with sources available (default)

import gradio as gr


def predict(im):
    return im["composite"]

def get_image_size(im):
    return f"{im['background'].shape[0]} x {im['background'].shape[1]}"


with gr.Blocks() as demo:
    with gr.Group():
        with gr.Row():
            im = gr.ImageEditor(
                canvas_size=(1000, 1000),
            )
            im_preview = gr.Image()
        txt = gr.Textbox(label="image size")
        im.change(predict, outputs=im_preview, inputs=im, show_progress="hidden")
        im.change(get_image_size, inputs=im, outputs=txt)


if __name__ == "__main__":
    demo.launch()

Scenario 3

canvas_size and crop_size.

import gradio as gr


def predict(im):
    return im["composite"]

def get_image_size(im):
    return f"{im['background'].shape[0]} x {im['background'].shape[1]}"


with gr.Blocks() as demo:
    with gr.Group():
        with gr.Row():
            im = gr.ImageEditor(
                canvas_size=(1000, 1000),
                crop_size=(200, 200),
            )
            im_preview = gr.Image()
        txt = gr.Textbox(label="image size")
        im.change(predict, outputs=im_preview, inputs=im, show_progress="hidden")
        im.change(get_image_size, inputs=im, outputs=txt)


if __name__ == "__main__":
    demo.launch()

@pngwn
Copy link
Member Author

pngwn commented May 1, 2024

Also fixed the docstrings. Automated tests are challenging for the image editor, so I'll look into that elsewhere. Existing visual tests for the image editor are always causing flake, so I don't want to add to that right now.

@pngwn
Copy link
Member Author

pngwn commented May 2, 2024

@abidlabs @freddyaboulton should be fine now, silly thing.

Copy link
Member

@abidlabs abidlabs left a comment

Choose a reason for hiding this comment

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

I tried different setups and works beautifully! Just left one nit on the python type hint, thanks @pngwn

pngwn and others added 4 commits May 3, 2024 10:02
@pngwn pngwn merged commit 24b2286 into main May 3, 2024
8 checks passed
@pngwn pngwn deleted the imed-canvas branch May 3, 2024 09:55
@pngwn pngwn mentioned this pull request May 3, 2024
dawoodkhan82 pushed a commit that referenced this pull request May 6, 2024
* add canvas size kwarg to imageeditor

* add changeset

* fix tests

* fix cropsize

* changes

* notebooks

* update docstrings

* fix type

* fix undefined dimensions

* Update image_editor.py

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* fix type

* format

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
pngwn added a commit that referenced this pull request Jun 14, 2024
* chatbot components

* demoi

* add changeset

* preprocess fix

* add changeset

* Make guide for tailwind more verbose (#8152)

* Lite wheel optimization (#7855)

* 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>

* Add ETag to `/custom_component` route to control browser caching (#8170)

* Add code

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Implement JS Client tests (#8109)

* add msw setup and initialisation tests

* add changeset

* add walk_and_store_blobs improvements and add tests

* add changeset

* api_info tests

* add direct space URL link tests

* fix tests

* add view_api tests

* add post_message test

* tweak

* add spaces tests

* jwt and protocol tests

* add post_data tests

* test tweaks

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Remove hatch installation in js/app/package.json which is no longer needed (#8174)

* Remove hatch installation in js/app/package.json which is no longer needed

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Update test-functional.yml - Fix vulnerability code injection (#8145)

* Update test-functional.yml

* Update test-functional.yml

* tweaks

---------

Co-authored-by: pngwn <hello@pngwn.io>

* rework upload to be a class method + pass client into each component (#8179)

* rework upload to be a class method + pass client into each component

* add changeset

* Update client/js/src/utils/upload_files.ts

* fix storybook

* review comments

* Apply suggestions from code review

Co-authored-by: Hannah <hannahblair@users.noreply.github.com>

* format

* ts fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Hannah <hannahblair@users.noreply.github.com>

* chore(deps): update pnpm to v9 (#8123)

* chore(deps): update pnpm to v9

* update workflow

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: pngwn <hello@pngwn.io>

* Use workspace version for code in _website (#8189)

* workspace

* add changeset

* remove circular import from preview

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Pass Error status in /dev/reload stream (#8106)

* get error message

* Support multiple clients

* add changeset

* add changeset

* add changeset

* Display in UI

* console.error the python traceback

* lint

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Convert sse calls in client from async to sync (#8182)

* convert sse calls in client from async to sync

* add changeset

* more sync

* lint

* more sync

* fix threadpool

* fix timeouts

* reuse executor

* lint

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* run python reload only if python file changed (#8194)

* run python reload only if python file changed

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Freddy Boulton <alfonsoboulton@gmail.com>

* fix: handling SIGINT correctly in reload.py, single entrance of block_thread in blocks.py (#8158)

* fix: handling SIGINT, single block_thread and fix popen

* Use pass

---------

Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>

* Add eventsource polyfill for Node.js and browser environments (#8118)

* add msw setup and initialisation tests

* add changeset

* add eventsource polyfill for node and browser envs

* add changeset

* add changeset

* config tweak

* types

* update eventsource usage

* add changeset

* add walk_and_store_blobs improvements and add tests

* add changeset

* api_info tests

* add direct space URL link tests

* fix tests

* add view_api tests

* add post_message test

* tweak

* add spaces tests

* jwt and protocol tests

* add post_data tests

* test tweaks

* dynamically import eventsource

* revet eventsource imports

* add node test

* lockfile

* add client test in root pkg file

* lcokfile

* remove eventsource from js/app

* add changeset

* remove ts ignore

* move eventsource polyfill to eventsource factory

* add changeset

* tweak

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Ensure connectivity to private HF spaces with SSE protocol (#8181)

* add msw setup and initialisation tests

* add changeset

* add eventsource polyfill for node and browser envs

* add changeset

* add changeset

* config tweak

* types

* update eventsource usage

* add changeset

* add walk_and_store_blobs improvements and add tests

* add changeset

* api_info tests

* add direct space URL link tests

* fix tests

* add view_api tests

* add post_message test

* tweak

* add spaces tests

* jwt and protocol tests

* add post_data tests

* test tweaks

* dynamically import eventsource

* revet eventsource imports

* add jwt param to sse requests

* add stream test

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Support custom components in gr.load (#8200)

* Add code

* add changeset

* Update fuzzy-mirrors-scream.md

* Update fuzzy-mirrors-scream.md

* Fix tests

* Update .changeset/fuzzy-mirrors-scream.md

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Fix code

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Refactor analytics to not use api.gradio.app (#8180)

* Analytics refactor

* add changeset

* add changeset

* Fix wasm?

* Fix python tests'

* Revert changes chrome

* use util function

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Specify the fastapi version on Lite to avoid ujson installation which is not available on Pyodide yet (#8204)

* Specify the fastapi version on Lite to avoid ujson installation which is not available on Pyodide yet

* add changeset

* Refactoring

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Set the show_api flag on Lite (#8205)

* Set the show_api flag on Lite

* add changeset

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Extend Interface.from_pipeline() to support Transformers.js.py pipelines on Lite (#8052)

* Extend Interface.from_pipeline() to support Transformers.js.py pipelines on Lite (wip: only object-detection in this commit)

* add changeset

* Add image-classification and image-segmentation

* Add zero-shot-image-classification and zero-shot-object-detection

* Add document-question-answering

* Add feature-extraction and fill-mask

* Add question-answering and summarization

* Fix an error message

* Add text2text-generation, text-classification, and text-generation

* Add translation andtranslation_xx_to_yy

* Add zero-shot-classification

* Add postprocess_takes_inputs to control the args passed to the postprocess function of each pipeline

* Add topk option to image-classification

* format_backend

* Add audio-classification, automatic-speech-recognition, and zero-shot-audio-classification

* Add image-to-text

* Add token-classification (with JSON component as an output. Is it correct?)

* Ignore import type failure of transformers_js_py

* Add image-feature-extraction

* Add image-to-image

* Add text-to-audio

* Add depth-estimation

* Remove `render=False`

* Reorder the if-blocks following the Transformers.js doc

* Update gradio/pipelines_utils.py

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Update gradio/pipelines_utils.py

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Fix feature-extraction demo

* Fix demo title

* Add guides/08_gradio-clients-and-lite/gradio-lite-and-transformers-js.md without contents

* Rename guides/08_gradio-clients-and-lite/*.md to fix the order

* Use pipeline.model.config._name_or_path for the demo title instead of pipeline.model.config.model_type

* Fix normal Interface.from_pipeline to use pipeline.model.config.name_or_path as the demo title

* Write an article about Gradio-Lite and Transformers.js

* Update the doc

* tweaks

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* merge

* allow the canvas size to be set on the `ImageEditor` (#8127)

* add canvas size kwarg to imageeditor

* add changeset

* fix tests

* fix cropsize

* changes

* notebooks

* update docstrings

* fix type

* fix undefined dimensions

* Update image_editor.py

Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* fix type

* format

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>

* Rename `eventSource_Factory` and `fetch_implementation` (#8209)

* rename eventSource_factory -> stream_factory + rename event_source -> steam

* rename fetch_implementation -> fetch

* rename fetch to _fetch due to global.fetch conflict

* add changeset

* format

* format

* format

* format

* fix

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* remove redundant event source logic (#8211)

* remove redundant event source logic

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* Only connect to heartbeat if needed (#8169)

* Add connect_heartbeat field

* fix types

* add changeset

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>

* chore: update versions (#8172)

* fixes

* type fixes

* type fixes

* notebook fix

* type ignore

* data object model

* remove component in tuple

* more fixes

* extend components

* remove test var

* extend to all components backend

* remove loading data models

* conflict fix

* test and type fixes

* playwright test

* PR fixes

* final changes

* Add pltly for 2e2 test

* pass loader to Gradio helper class

* fix things

* add changeset

* checks

* more fixy

* more fixy

* more fixy

---------

Co-authored-by: gradio-pr-bot <gradio-pr-bot@users.noreply.github.com>
Co-authored-by: Simon Duerr <dev@simonduerr.eu>
Co-authored-by: Yuichiro Tachibana (Tsuchiya) <t.yic.yt@gmail.com>
Co-authored-by: freddyaboulton <alfonsoboulton@gmail.com>
Co-authored-by: Hannah <hannahblair@users.noreply.github.com>
Co-authored-by: Lê Ngọc Hoa <114990730+h2oa@users.noreply.github.com>
Co-authored-by: pngwn <hello@pngwn.io>
Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Ali Abdalla <ali.si3luwa@gmail.com>
Co-authored-by: Abubakar Abid <abubakar@huggingface.co>
Co-authored-by: James Zhou <61927718+jameszhou02@users.noreply.github.com>
Co-authored-by: Tiger3018 <tiger3018of02@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants