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
33.72k stars 2.56k forks source link

`gr.HighlightedText.update` not working well with `color_map` #5290

Open ydshieh opened 1 year ago

ydshieh commented 1 year ago

Describe the bug

When using gr.HighlightedText.update to update its color_map, if the new color_map remove some keys from the original color_map, those deleted labels is still displayed.

See Reproduction.

Have you searched existing issues? 🔎

Reproduction

Colab

(running on Colab)

!pip install -q gradio 

Click the first radio component to see things showing, then click the second radio component to change color_map. It's easy to see the deleted labels are not removed from the display.

import gradio as gr

def change_color_map(choice):
    if choice == "color map 1":
        return gr.HighlightedText.update(visible=True, color_map={"R": "red", "L": "lime"})
    elif choice == "color map 2":
        return gr.HighlightedText.update(visible=True, color_map={"G": "green", "P": "pink"})

def change_text(choice):
    if choice == "text 1":
        return [("a", None), ("b", "R")]
    elif choice == "text 2":
        return [("c", None), ("d", "G")]

with gr.Blocks() as demo:
    radio = gr.Radio(
        ["text 1", "text 2"], label="change text"
    )

    radio2 = gr.Radio(
        ["color map 1", "color map 2"], label="change color map"
    )

    text = gr.HighlightedText(
        label="Diff",
        combine_adjacent=True,
        show_legend=True,
    ).style(color_map={"R": "red", "G": "green", "B": "blue", "Y": "yellow"})

    radio.change(fn=change_text, inputs=radio, outputs=[text])
    radio2.change(fn=change_color_map, inputs=radio2, outputs=[text])

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

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Linux
gradio version: 3.40.1
gradio_client version: 0.4.0

------------------------------------------------
gradio dependencies in your environment:

aiofiles: 23.2.1
aiohttp: 3.8.5
altair: 4.2.2
fastapi: 0.101.1
ffmpy: 0.3.1
gradio-client: 0.4.0
httpx: 0.24.1
huggingface-hub: 0.16.4
importlib-resources: 6.0.1
jinja2: 3.1.2
markdown-it-py: 2.2.0
markupsafe: 2.1.3
matplotlib: 3.7.1
mdit-py-plugins: 0.3.3
numpy: 1.23.5
orjson: 3.9.5
packaging: 23.1
pandas: 1.5.3
pillow: 9.4.0
pydantic: 2.2.0
pydub: 0.25.1
python-multipart: 0.0.6
pyyaml: 6.0.1
requests: 2.31.0
semantic-version: 2.10.0
typing-extensions: 4.7.1
uvicorn: 0.23.2
websockets: 11.0.3
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.

gradio_client dependencies in your environment:

fsspec: 2023.6.0
httpx: 0.24.1
huggingface-hub: 0.16.4
packaging: 23.1
requests: 2.31.0
typing-extensions: 4.7.1
websockets: 11.0.3

Severity

I can work around it

abidlabs commented 1 year ago

Can repro, thanks for creating the issue @ydshieh -- we'll look into it!

abidlabs commented 2 weeks ago

Hi, apologies for the late follow up. We haven't had a chance to look into this issue, but the Gradio codebase has changed quite significantly since this issue was created. Could you let us know if this is still an issue in the latest version of Gradio (pip install --upgrade gradio)? Thanks!

ydshieh commented 1 week ago

I am not able to use update and style attributes anymore. When I remove them and just use gr.HighlightedText, it doesn't change the labels at all.