gradio icon indicating copy to clipboard operation
gradio copied to clipboard

gr.State JSON serializable, when there's a reload

Open miguelwon opened this issue 8 months ago • 1 comments

Describe the bug

Usually I use in my gradio demos gr.State() to define a class with methods and attributes, that can be used at different steps or functionalities within the application. For example, I can define a MainState outside the gradio main Blocks:

class MainState:
    """ Class with the main state. Needed to be able to have a persistent state between interactions, as well as unique state for each user.
    """
    def __init__(self):
        self.index_name = "Leg"

and then instantiate it:

with gr.Blocks(theme=theme, css = css_custom) as demo:
    main_state = gr.State(MainState())

The issue is that I'm having problems when working in development and there's a reload of the application. I'm getting the error:

TypeError: Object of type MainState is not JSON serializable

It works at first, but if I edit my source code, save it and gradio reloads it, then it tries to serialize the state and the error occurs.

Have you searched existing issues? 🔎

  • [X] I have searched and found no existing issues

Reproduction

import gradio as gr

class MainState:
    """ Class with the main state. Needed to be able to have a persistent state between interactions, as well as unique state for each user.
    """

    def __init__(self):
        self.index_name = "Leg"


################
## GRADIO APP ##
################

with gr.Blocks() as demo:

    main_state = gr.State(MainState())

    def set_index_name(state,index_name):
        state.index_name = index_name
        print(state.index_name)

    with gr.Row():
        with gr.Column(scale = 30):
            
            message = gr.Textbox(show_label = False,lines =3)
            btn = gr.Button(value="Procurar",size="sm")

            choice = gr.Radio(choices=['Leg', 'IPV'], label="Legislação ou IPVs", value = "Leg")
            choice.change(fn=set_index_name, inputs=[main_state,choice], outputs=None)


demo.queue()
demo.launch()

Screenshot

No response

Logs

No response

System Info

Gradio Environment Information:
------------------------------
Operating System: Darwin
gradio version: 4.31.5
gradio_client version: 0.16.4

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

aiofiles: 23.2.1
altair: 5.2.0
fastapi: 0.108.0
ffmpy: 0.3.2
gradio-client==0.16.4 is not installed.
httpx: 0.27.0
huggingface-hub: 0.21.2
importlib-resources: 6.1.2
jinja2: 3.1.3
markupsafe: 2.1.5
matplotlib: 3.8.3
numpy: 1.26.4
orjson: 3.9.15
packaging: 23.2
pandas: 2.2.1
pillow: 10.2.0
pydantic: 2.6.3
pydub: 0.25.1
python-multipart: 0.0.9
pyyaml: 6.0.1
ruff: 0.2.2
semantic-version: 2.10.0
tomlkit==0.12.0 is not installed.
typer: 0.12.3
typing-extensions: 4.10.0
urllib3: 2.2.1
uvicorn: 0.25.0
authlib; extra == 'oauth' is not installed.
itsdangerous; extra == 'oauth' is not installed.


gradio_client dependencies in your environment:

fsspec: 2024.2.0
httpx: 0.27.0
huggingface-hub: 0.21.2
packaging: 23.2
typing-extensions: 4.10.0
websockets: 11.0.3

Severity

I can work around it

miguelwon avatar May 27 '24 13:05 miguelwon