gradio: 3.42.0 breaks automatic reload after a change is detect

Describe the bug

in Gradio 3.42.0 the autoreload when a change is detected breaks with an error and the only way to continue is to kill and restart gradio again.

My only action was to upgrade the package from 3.41.2 to 3.42.0 (side note: why those packages are not present in the releases in github?)

Have you searched existing issues? 🔎

  • I have searched and found no existing issues

Reproduction

Upgrade Gradio to 3.42.0 and run gradio app.py, then change something in app.py

Screenshot

No response

Logs

Changes detected in: /Users/stefanobaccianella/Repositories/difficult-conversations-bot/app.py
/Users/stefanobaccianella/Repositories/difficult-conversations-bot/.venv/lib/python3.11/site-packages/gradio/components/annotated_image.py:7: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  import numpy as np
Reloading app failed with the following exception: 
ImportError: PyO3 modules may only be initialized once per interpreter process
Changes detected in: /Users/stefanobaccianella/Repositories/difficult-conversations-bot/app.py
/Users/stefanobaccianella/Repositories/difficult-conversations-bot/.venv/lib/python3.11/site-packages/gradio/components/annotated_image.py:7: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  import numpy as np
Reloading app failed with the following exception: 
ImportError: PyO3 modules may only be initialized once per interpreter process

System Info

Running python 3.11.4

pip freeze:
aiofiles==23.2.1
aiohttp==3.8.5
aiosignal==1.3.1
alt-profanity-check==1.3.0
altair==5.0.1
annotated-types==0.5.0
anyio==3.7.1
async-timeout==4.0.3
attrs==23.1.0
cachetools==5.2.0
certifi==2023.7.22
cfgv==3.4.0
charset-normalizer==3.2.0
click==8.1.7
contourpy==1.1.0
cycler==0.11.0
dataclasses-json==0.5.14
distlib==0.3.7
ecdsa==0.18.0
fastapi==0.101.1
ffmpy==0.3.1
filelock==3.12.2
fonttools==4.42.1
frozenlist==1.4.0
fsspec==2023.6.0
google-api-core==2.11.1
google-auth==2.22.0
googleapis-common-protos==1.60.0
gradio==3.42.0
gradio_client==0.5.0
h11==0.14.0
httpcore==0.17.3
httpx==0.24.1
huggingface-hub==0.16.4
identify==2.5.27
idna==3.4
importlib-resources==6.0.1
iniconfig==2.0.0
Jinja2==3.1.2
joblib==1.3.2
jsonschema==4.19.0
jsonschema-specifications==2023.7.1
kiwisolver==1.4.5
langchain==0.0.279
langsmith==0.0.26
MarkupSafe==2.1.3
marshmallow==3.20.1
matplotlib==3.7.2
mona-openai==0.1.3
mona-sdk==0.0.51
multidict==6.0.4
mypy==1.5.1
mypy-extensions==1.0.0
nest-asyncio==1.5.7
nodeenv==1.8.0
numexpr==2.8.5
numpy==1.25.2
openai==0.28.0
orjson==3.9.5
packaging==23.1
pandas==2.0.3
phonenumberslite==8.13.19
Pillow==10.0.0
platformdirs==3.10.0
pluggy==1.3.0
pre-commit==3.3.3
protobuf==4.24.1
pyasn1==0.5.0
pyasn1-modules==0.3.0
pydantic==2.3.0
pydantic_core==2.6.3
pydub==0.25.1
PyJWT==1.7.1
pyparsing==3.0.9
pytest==7.4.0
pytest-mock==3.11.1
python-dateutil==2.8.2
python-dotenv==1.0.0
python-jose==3.3.0
python-multipart==0.0.6
pytz==2023.3
PyYAML==6.0.1
referencing==0.30.2
regex==2023.8.8
requests==2.31.0
requests-mock==1.11.0
rpds-py==0.9.2
rsa==4.9
scikit-learn==1.3.0
scipy==1.11.2
semantic-version==2.10.0
six==1.16.0
sniffio==1.3.0
SQLAlchemy==2.0.20
starlette==0.27.0
tenacity==8.2.3
threadpoolctl==3.2.0
tiktoken==0.4.0
toolz==0.12.0
tqdm==4.66.1
typing-inspect==0.9.0
typing_extensions==4.7.1
tzdata==2023.3
urllib3==1.26.16
uvicorn==0.23.2
virtualenv==20.24.4
websockets==11.0.3
yarl==1.9.2

Severity

I can work around it

About this issue

  • Original URL
  • State: closed
  • Created 10 months ago
  • Comments: 27 (2 by maintainers)

Most upvoted comments

Thank you @freddyaboulton! I changed my code like this,and it won’t crash when automatic reload.

code

import gradio as gr
import os
print(gr.__version__)
os.environ["no_proxy"] = "localhost,127.0.0.1,::1"
def create_webui():
    with gr.Blocks() as demo:
        with gr.Row():
            gpt_dropdown = gr.Dropdown()
            sovits_dropdown = gr.Dropdown()
            model_refresh_button = gr.Button("Refresh Model" ,variant="primary")
    return demo    
    
demo = create_webui()    

if __name__ == "__main__":
    demo.launch(server_port=2777)

logs

PS C:\ _MYPATH_> gradio .\_FILENAME_.py
Watching: 'C:\_MYPATH_\myenv\Lib\site-packages\gradio', 'C:\_MYPATH_', 'C:\_MYPATH_'

4.15.0
Running on local URL:  http://127.0.0.1:2777

To create a public link, set `share=True` in `launch()`.
Changes detected in: _MYPATH_\_FILENAME_.py
C:\_PATH_\myenv\Lib\site-packages\gradio\components\annotated_image.py:7: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues 
and is discouraged.
  import numpy as np
4.15.0
4.15.0

I believe this should be resolved as of gradio==4.14.0. Can someone please let us know if that’s not the case and provide a simple repro?

I’m still having the same issue as above, even on 4.14.0.

An update here, the folks over at tiktoken have upgraded to the latest pyo3 that seems to solve this problem https://github.com/openai/tiktoken/issues/141

I think now we need to wait for all rust-binded libraries to upgrade (orjson comes to mind for gradio)

+1 to please implementing this feature. Very cool system, otherwise.

So I tried it, it worked fine this way. Not sure how this tests the reload feature for you but it worked as expected

It’s definitely a bug in tiktoken, but is triggered by the PR I mentioned above. Not sure it’s possible to fix without reverting that PR (or we all try to fix tiktoken but not sure if that’s a good use of our collective time)

It seems that this bug is still unresolved. The same issue occurs in version 3.50.2. When I downgraded to 3.41.2 as a test, the error no longer occurred. It appears to be related to the openai module.

$ gradio main.py 
Watching: '/home/kiri/Code/ChatRead/.venv/lib/python3.10/site-packages/gradio', '/home/kiri/Code/ChatRead'

Running on local URL:  http://127.0.0.1:7860

To create a public link, set `share=True` in `launch()`.

Changes detected in: /home/kiri/Code/ChatRead/main.py
/home/kiri/Code/ChatRead/.venv/lib/python3.10/site-packages/openai/datalib/numpy_helper.py:4: UserWarning: The NumPy module was reloaded (imported a second time). This can in some cases result in small but subtle issues and is discouraged.
  import numpy
Reloading main failed with the following exception: 
ImportError: PyO3 modules may only be initialized once per interpreter process

Sure thing, the repo is public https://huggingface.co/spaces/mangiucugna/difficult-conversations-bot/tree/main

just upgrade gradio to latest to reproduce