gradio-app / gradio

Build and share delightful machine learning apps, all in Python. 🌟 Star to support our work!
http://www.gradio.app
Apache License 2.0
33.88k stars 2.57k forks source link

Unable to launch with reload mode with default port #4755

Closed ManHinnn0509 closed 1 year ago

ManHinnn0509 commented 1 year ago

Describe the bug

Unable to launch the script with reload mode with default port, if I launch it with gradio script.py it shows this error:

ERROR:    [Errno 10048] error while attempting to bind on address ('127.0.0.1', 7860)

I checked but port 7860 isn't in use

I can launch it and see the page if I specify the port but unable to connect to it again after reload, it shows the following message after saving the file:

WARNING:  StatReload detected changes in 'tabs-test.py'. Reloading...
Keyboard interruption in main thread... closing server.

Have you searched existing issues? 🔎

Reproduction

Gradio version: 3.35.2

The following code is my script:

import gradio as gr

demo = gr.Blocks()

with demo:
    with gr.Tabs():
        with gr.TabItem("AAA"):
            with gr.Tabs():
                with gr.TabItem("111"):
                    pass

                with gr.TabItem("222"):
                    pass

        with gr.TabItem("BBB"):
            with gr.Tabs():
                with gr.TabItem("B333"):
                    pass

                with gr.TabItem("444"):
                    pass

demo.launch(
    # server_port=6969,
    # inbrowser=True,
    # debug=True
)

Screenshot

No response

Logs

If I hit save file with default port it shows the following error in the console:

Process SpawnProcess-1:
Traceback (most recent call last):
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 315, in _bootstrap
    self.run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\multiprocessing\process.py", line 108, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\_subprocess.py", line 76, in subprocess_started   
    target(sockets=sockets)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py", line 61, in run
    return asyncio.run(self.serve(sockets=sockets))
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\runners.py", line 44, in run
    return loop.run_until_complete(main)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 634, in run_until_complete
    self.run_forever()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 601, in run_forever
    self._run_once()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1905, in _run_once
    handle._run()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\asyncio\events.py", line 80, in _run
    self._context.run(self._callback, *self._args)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\server.py", line 68, in serve
    config.load()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\config.py", line 473, in load
    self.loaded_app = import_from_string(self.app)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\uvicorn\importer.py", line 21, in import_from_string      
    module = importlib.import_module(module_str)
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
  File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 850, in exec_module
  File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
  File "C:\Users\User\Documents\Python\gradio\tabs-test.py", line 23, in <module>
    demo.launch(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\gradio\blocks.py", line 1777, in launch
    server_name, server_port, local_url, app, server = networking.start_server(
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\gradio\networking.py", line 164, in start_server
    server.run_in_thread()
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\site-packages\gradio\networking.py", line 39, in run_in_thread
    time.sleep(1e-3)
KeyboardInterrupt

System Info

Gradio version: `3.35.2`
OS: Windows 10

Severity

I can work around it

yola-0316 commented 1 year ago

Same issue

zhangyisheng0629 commented 1 year ago

Same issue

freddyaboulton commented 1 year ago

Hi all! This is expected. reload mode cannot know the parameters passed to launch because we don't actually run your script, we just import the demo defined in that file.

This is stated in the guide

image

Also, when using reload mode it is recommended to place the launch in an if __name__ == "__main__" block.