gradio: Unable to update gr.Examples getting AttributeError: 'Examples' object has no attribute '_id'

Describe the bug

while looking deeply for the answers myself I am frustrated and could not find the solution

so Below is some code


# Function for generating some examples
def generate_examples():    
    extracted_list = ['completion1','completion2','completion3','completion4']
    return gr.Examples.update(examples=extracted_list)


# Gradio Interface
with gr.Blocks() as demo: 
   
    with gr.Row():
        with gr.Column(scale=0.7):
            chatbot = gr.Chatbot(
                value=[[None,"Hey, can I help you get something?"],],
                height=500,
                label="Stickse Sales Assistant",
                )
            with gr.Row():
                with gr.Column(scale=0.85):
                    txt = gr.Textbox(
                        show_label=False,
                        container=False,
                        placeholder="Enter text and press enter",
                    )
                    _examples = gr.Examples(
                        examples=['mice','ants','snails', 'bees', 'moth'], 
                        inputs=txt,
                        label="Suggestions"
                        )

                with gr.Column(scale=0.15, min_width=0):
                    btn = gr.Button("Send")

        with gr.Column(scale=0.3):
            radio = gr.Dropdown(
                container=False,
                choices=['gpt-3.5-turbo','gpt-3.5-turbo-16k','gpt-3.5-turbo-0613','gpt-3.5-turbo-16k-0613'],
                label="Model",
                value='gpt-3.5-turbo-16k',
            )
            model_details = gr.Markdown(
                container=True,
                value=MODELS[radio.value], label="Model Details",)
            clear = gr.Button("Clear Conversation")

          # this is where the issue is
            s = gr.Button("UPDATE  EXAMPLES")


    
    
    radio.change(change_model_details, [radio], [model_details])
    clear.click(init_history, inputs =[],outputs = chatbot,)

    btn.click(add_text, [chatbot, txt], [chatbot, txt], queue=True).then(
            generate_response, inputs =[chatbot,radio],outputs = [chatbot])
      
    
    txt.submit(add_text, [chatbot, txt], [chatbot, txt], queue=True).then(
            generate_response, inputs =[chatbot,radio],outputs = [chatbot])
    
   # this is where I am calling the button click
    s.click(generate_examples,inputs=[],outputs = [_examples])

demo.queue()
demo.launch()

This is the issue I get on my terminal

 > python main.py
Traceback (most recent call last):
  File "D:\projects\SalesRep\main.py", line 272, in <module>
    s.click(fn=generate_examples,inputs=[],outputs = [_examples])
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\events.py", line 139, in __call__
    dep, dep_index = self.trigger.set_event_trigger(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\blocks.py", line 285, in set_event_trigger
    "outputs": [block._id for block in outputs],
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\blocks.py", line 285, in <listcomp>
    "outputs": [block._id for block in outputs],
                ^^^^^^^^^
AttributeError: 'Examples' object has no attribute '_id'

so The gr.Examples component is what I want to update and I am unable to do it. There was a similar issue #3817 but was closed and the discussion form linked does not exist
https://github.com/gradio-app/gradio/discussions/3169

when I comment the line s.click(generate_examples,inputs=[],outputs = [_examples])

The gradio app launches else it doesnt even lauch now

Have you searched existing issues? šŸ”Ž

  • I have searched and found no existing issues

Reproduction

import gradio as gr

def generate_examples():    
    extracted_list = ['completion1','completion2','completion3','completion4']
    return gr.Examples.update(examples=extracted_list)


with gr.Blocks() as demo: 
   
    with gr.Row():
        with gr.Column(scale=0.7):
            chatbot = gr.Chatbot(
                value=[[None,"Hey, can I help you get something?"],],
                height=500,
                label="Stickse Sales Assistant",
                )
            with gr.Row():
                with gr.Column(scale=0.85):
                    txt = gr.Textbox(
                        show_label=False,
                        container=False,
                        placeholder="Enter text and press enter",
                    )
                    _examples = gr.Examples(
                        examples=['mice','ants','snails', 'bees', 'moth'], 
                        inputs=txt,
                        label="Suggestions"
                        )

                with gr.Column(scale=0.15, min_width=0):
                    btn = gr.Button("Send")

        with gr.Column(scale=0.3):
            radio = gr.Dropdown(
                container=False,
                choices=['gpt-3.5-turbo','gpt-3.5-turbo-16k','gpt-3.5-turbo-0613','gpt-3.5-turbo-16k-0613'],
                label="Model",
                value='gpt-3.5-turbo-16k',
            )
           

          # this is where the issue is
            s = gr.Button("UPDATE  EXAMPLES")

    
   # this is where I am calling the button click
    # s.click(generate_examples,inputs=[],outputs = [_examples])

demo.queue()
demo.launch()

Logs

Traceback (most recent call last):
  File "D:\projects\SalesRep\main.py", line 272, in <module>
    s.click(fn=generate_examples,inputs=[],outputs = [_examples])
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\events.py", line 139, in __call__
    dep, dep_index = self.trigger.set_event_trigger(
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\blocks.py", line 285, in set_event_trigger
    "outputs": [block._id for block in outputs],
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\projects\SalesRep\env\Lib\site-packages\gradio\blocks.py", line 285, in <listcomp>
    "outputs": [block._id for block in outputs],
                ^^^^^^^^^
AttributeError: 'Examples' object has no attribute '_id'

System Info

Gradio Environment Information:
Operating System:  Windows


gradio version:  3.39.0

gradio Dependencies:
  aiofiles: 23.1.0
  aiohttp: 3.8.5
  altair: 5.0.1
  fastapi: 0.99.1
  ffmpy: 0.3.1
  gradio-client: 0.3.0
  httpx: 0.24.1
  huggingface-hub: 0.16.4
  jinja2: 3.1.2
  markdown-it-py: 2.2.0
  markupsafe: 2.1.3
  matplotlib: 3.7.2
  mdit-py-plugins: 0.3.3
  numpy: 1.25.1
  orjson: 3.9.2
  packaging: 23.1
  pandas: 2.0.3
  pillow: 10.0.0
  pydantic: 1.10.12
  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.1
  websockets: 11.0.3


gradio_client version:  0.3.0

gradio_client Dependencies:
  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

Blocking usage of gradio

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 23 (5 by maintainers)

Most upvoted comments

Apologies for the long resolution on this. I am working on a PR that will allow this to happen. You can test it by doing:

pip install https://gradio-builds.s3.amazonaws.com/c2dc8a1bd9585c3170a116d12a465be9223f1310/gradio-4.37.2-py3-none-any.whl

The syntax you’ll need to use is to pass in the example.dataset as the output component. There’s an example in the PR if you’d like to take a look!

For now, can you have multiple predefined gr.Examples objects, and hide the visibility based on the interaction?

I figured it out! First:

user_chat_bot_suggest = gr.Example(...)

def fn(...):
    return gr.Dataset.update(samples=suggests).  # suggests: List[List[str]]

then:

gr.SOMETHING.change().then(fn,
    inputs=[...],
    outputs=[user_chat_bot_suggest.dataset])

It works for me!

I am getting this error when trying to pass a list of dataframes as an output in a click function. Is this related or a separate issue? My UI code:

with gr.Tabs() as tabs:
                                        positive_dataframes = []
                                        negative_dataframes = []
                                        for i in range(10):
                                                with gr.Tab(label=f"Tab {i+1}"):
                                                        with gr.Row():
                                                                positive_df = gr.DataFrame(id=f"pos_df_{i}", label="Positive Replace")
                                                                positive_dataframes.append(positive_df)
                                                        with gr.Row():
                                                                negative_df = gr.DataFrame(id=f"neg_df_{i}", label="Negative Replace")
                                                                negative_dataframes.append(negative_df)

And my button click code:

feature_analysis_button.click(
                        fn=analyze,
                        inputs=[spectrum_feature_vectors],
                        outputs=[spectrum_analysis_results, positive_dataframes, negative_dataframes]
                )

Errors on the click with: ā€œoutputsā€: [block._id for block in outputs], AttributeError: ā€˜list’ object has no attribute ā€˜_id’

I’m using gradio 3.41.2 in Automatic1111