2

I am running a model inferencing on Colab using Gradio. The code is such that processing is done when we receive the input, in a function similar to 'test' below. In my original code design, 'test' spawns a thread that in turn updates a global variable 'label'. I need to update this variable in the textbox. Hence I call demo.load() with every=1 and the function 'updatePredLabel' is getting called every 1 second.

Below is the sample code that reproduces the issue.

import gradio as gr
import time
import threading
from threading import Lock

lock = threading.Lock()

label = ""
def test(x):
  global label
  global lock
  print("Text change {}".format(x))
  time.sleep(5)
  lock.acquire()
  label = x
  lock.release()
  return

def updatePredLabel():
  global label
  time.sleep(0.5)

  lock.acquire()
  out_text = label
  lock.release()

  return out_text


with gr.Blocks() as demo:
  with gr.Row():
    input = gr.Textbox()
    out = gr.Textbox()
  input.change(test, input, None) 
  demo.load(updatePredLabel, None, out, every=1)   

demo.queue().launch(debug=True)

The issue is that after sometime my code throws the error below and then the output textbox stops updating.

ERROR:asyncio:Task exception was never retrieved
future: <Task finished coro=<Queue.process_events() done, defined at /usr/local/lib/python3.7/dist-packages/gradio/queue.py:265> exception=ValueError('[<gradio.queue.Event object at 0x7f7de35f0350>] is not in list')>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/gradio/queue.py", line 341, in process_events
    self.active_jobs[self.active_jobs.index(events)] = None
ValueError: [<gradio.queue.Event object at 0x7f7de35f0350>] is not in list

What am I doing wrong here? How should i debug. Or is there possibly a different way of doing what I am trying to do.

I couldn't find any other way of updating the output text box periodically.

0 Answers0