gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
30.73k stars 2.29k forks source link

Enable hiding the inline category in HighlightedText with a `show_inline_category` argument #8355

Closed xu-song closed 1 month ago

xu-song commented 1 month ago

Description

Target Issue #8356

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

Demo

This is a demo of tokenization.

import gradio as gr
from transformers import AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("google/mt5-large")

def tokenize_text(text, color_num=5):
    tokens = []
    encoding = tokenizer.encode(text)
    for idx, token_id in enumerate(encoding):
        decode_text = tokenizer.decode([token_id])
        tokens.extend([(decode_text, str(idx % color_num))])
    return tokens

demo = gr.Interface(
    tokenize_text,
    gr.Textbox(
        label="Text",
        info="Input text",
        lines=3,
        value="Replace this text in the input field to see how tokenization works. Buenos días!",
    ),
    gr.HighlightedText(
        label="Tokenization",
        show_legend=False,
        show_inline_category=False,
        interactive=False
    )
)

if __name__ == "__main__":
    demo.launch()
gradio-pr-bot commented 1 month ago

🪼 branch checks and previews

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

Install Gradio from this PR

pip install https://gradio-builds.s3.amazonaws.com/7e867d8f55575d49b96f5ae00b6ad92029acc52b/gradio-4.31.5-py3-none-any.whl

Install Gradio Python Client from this PR

pip install "gradio-client @ git+https://github.com/gradio-app/gradio@7e867d8f55575d49b96f5ae00b6ad92029acc52b#subdirectory=client/python"
gradio-pr-bot commented 1 month ago

🦄 change detected

This Pull Request includes changes to the following packages.

Package Version
@gradio/highlightedtext minor
gradio minor

With the following changelog entry.

Enable hiding the inline category in HighlightedText with a show_inline_category argument

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](https://github.com/xu-song/gradio/edit/patch-1/.changeset/public-socks-shout.md).
abidlabs commented 1 month ago

Looks reasonable to me as well. Down the line, we may want to lift the restriction that:'

Only works if show_legend=False and interactive=False.

For example, it could be that a developer wants to show the categories in the legend and in inline, which this argument could enable. But its not necessary in this PR.

hannahblair commented 1 month ago

Looks good! It would be awesome to have a storybook test added to this. If you're not sure how to write it, let me know and I can help :)

xu-song commented 1 month ago

Looks good! It would be awesome to have a storybook test added to this. If you're not sure how to write it, let me know and I can help :)

@hannahblair I'm not sure how to write a storybook test. I really appreciated if you can help.