Skip to content

Using an input example to a Gradio interface that uses session state throws an IndexError. #851

@DJSapit

Description

@DJSapit

Describe the bug

I used the colab demo of using state provided in the docs

https://gradio.app/docs/#i_state
(https://colab.research.google.com/drive/11cv_v9r2O_L0abJJo5Wf9VB3WHmbvJMh).

I added
examples = [["How many"]],
inside gr.Interface().
The Gradio interface launches without error but throws an IndexError when the example input is submitted.

I was unable to find an example of a Gradio interface that uses session states and also provides input examples that runs properly for every session.

Reproduction

import random

import gradio as gr

def chat(message, history):
history = history or []
if message.startswith("How many"):
response = random.randint(1, 10)
elif message.startswith("How"):
response = random.choice(["Great", "Good", "Okay", "Bad"])
elif message.startswith("Where"):
response = random.choice(["Here", "There", "Somewhere"])
else:
response = "I don't know"
history.append((message, response))
return history, history

iface = gr.Interface(
chat,
["text", "state"],
["chatbot", "state"],
examples = [["How many"]], # this is the only edit to the provided code in the demo
allow_screenshot=False,
allow_flagging="never",
)
iface.launch(debug=True)

Screenshot

No response

Logs

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/gradio/routes.py", line 265, in predict
    output = await run_in_threadpool(app.launchable.process_api, body, username)
  File "/usr/local/lib/python3.7/dist-packages/starlette/concurrency.py", line 39, in run_in_threadpool
    return await anyio.to_thread.run_sync(func, *args)
  File "/usr/local/lib/python3.7/dist-packages/anyio/to_thread.py", line 29, in run_sync
    limiter=limiter)
  File "/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py", line 818, in run_sync_in_worker_thread
    return await future
  File "/usr/local/lib/python3.7/dist-packages/anyio/_backends/_asyncio.py", line 754, in run
    result = context.run(func, *args)
  File "/usr/local/lib/python3.7/dist-packages/gradio/interface.py", line 567, in process_api
    prediction, durations = process_example(self, data.example_id)
  File "/usr/local/lib/python3.7/dist-packages/gradio/process_examples.py", line 29, in process_example
    prediction, durations = interface.process(raw_input)
  File "/usr/local/lib/python3.7/dist-packages/gradio/interface.py", line 613, in process
    for i, input_component in enumerate(self.input_components)
  File "/usr/local/lib/python3.7/dist-packages/gradio/interface.py", line 613, in <listcomp>
    for i, input_component in enumerate(self.input_components)
IndexError: list index out of range

System Info

I used the colab demo provided in the docs.

https://gradio.app/docs/#i_state

Gradio version is 2.8.13

Severity

annoyance

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions