Search code examples
pythongradio

How to use additional_inputs and examples at the same time?


In Gradio, I tried chatinterface with "additional_inputs" and "examples" at the same time.

But, only either of them can work.

How do I use those two features at the same time?

When "examples" is commented out, then, "additional_inputs" can work. When "additional_inputs" is commented out, "examples" can work.

Here is the example I tried:

import gradio as gr

def yes_man(message, history, input1):
    return f"{input1}" 
  
with gr.Blocks() as demo:
      input1 = gr.Textbox("", label="input1")
     
      gr.ChatInterface(
          yes_man, 
          examples=["Hello", "Am I cool?", "Are tomatoes vegetables?"],
          additional_inputs=[input1],
      )
        
demo.launch()
  • In this case, if input1 has "good", the return should be "good" in the chatbot.
  • Actually, not only "examples" but also other functions canot work, if "additional_inputs is used. textbox=gr.Textbox(placeholder="Ask me a yes or no question", container=False, scale=7),

Solution

  • Normally you have only one input - for message - so every example needs only one value.

    When you use additional_inputs then you have more inputs
    and every example must have more value - so every example has to be a list (it can't be a tuple).

    examples=[
      ### [value for textbox, value for input1]  # it can't be tuple
      ["Hello", ""], 
      ["Am I cool?", ""],
      ["Are tomatoes vegetables?", ""]
    ],
    

    Documentation: Example Inputs


    Full working example with 2 additional inputs - so every example must have 3 values.

    import gradio as gr
    
    print('gradio:', gr.__version__)
    
    def yes_man(message, history, input_1, input_2):
        print('message:', message)
        print('history:', history)
        print('input_1:', input_1)
        print('input_2:', input_2)
        
        return f"{message} (I've been {input_1} for {input_2} days)" 
      
    with gr.Blocks() as demo:     
          gr.ChatInterface(
              yes_man, 
              examples=[
                ### [Example Inputs](https://www.gradio.app/guides/the-interface-class#example-inputs)
                ### [value for textbox, value for input_1, value for input_2]  # it can't be tuple
                ["Hello",                    "nice", "123"],
                ["Am I cool?",               "fun",  "456"], 
                ["Are tomatoes vegetables?", "sad",  "789"],
                #["Say something",           "",     ""],  # empty values 
              ],
              additional_inputs=[
                #gr.Dropdown(["", "nice", "fun", "sad", "angry"], label="Mood"), #, allow_custom_value=True),
                gr.Textbox("", label="Mood"),
                gr.Textbox("", label="Days")
              ],
          )
            
    demo.launch()
    

    enter image description here


    If you set None for some input in all examples then it will not display this column in example and example will not change current value in for this input.

    Documentation: Providing Partial Examples

      examples=[
        ### [value for textbox, value for input_1, value for input_2]  # it can't be tuple
        ### [Providing Partial Examples](https://www.gradio.app/guides/more-on-examples#providing-partial-examples)
        # if column has only `None` then it doesn't display this input  # this will skip `Mood`
        ["Hello",                    None, "123"],       
        ["Am I cool?",               None, "456"],
        ["Are tomatoes vegetables?", None, "789"],
        ["Say something",            None, ""],
      ],
    

    enter image description here