Fine-tune LLMs from one YAML. Layer streaming trains an 8B model on a 4 GB laptop GPU.
Python cliconsumer-gpudpofine-tuningggufhuggingfacellmllmopslocal-ailocal-llmloralow-vrammachine-learningollamapeftpythonpytorchqlorasfttransformers
fix(ui): open a loopback URL when the server binds to all interfaces (#2054) (#2110)
Rahul Seervibb3694df50ed
diff --git a/changelog.d/0.76.0/2110.fixed.md b/changelog.d/0.76.0/2110.fixed.mdnew file mode 100644index 00000000..bbeece6e--- /dev/null+++ b/changelog.d/0.76.0/2110.fixed.md@@ -0,0 +1,5 @@+- `soup ui --public`, `--host 0.0.0.0` and `--host ::` now print and open a loopback URL+ (`http://127.0.0.1:<port>` or `http://[::1]:<port>`) instead of the bind address, which+ Windows cannot connect to; the server still binds to all interfaces and the `--public`+ phone URL keeps the LAN address. An IPv6 host such as `--host ::1` is now bracketed in the+ URL (#2054 by @Rahul-cs50 in #2110).diff --git a/src/soup_cli/commands/ui.py b/src/soup_cli/commands/ui.pyindex d58a0a74..6e3c0688 100644--- a/src/soup_cli/commands/ui.py+++ b/src/soup_cli/commands/ui.py@@ -9,6 +9,8 @@ import typerfrom rich.console import Consolefrom rich.panel import Panel+from soup_cli.utils.terminal import for_terminal+console = Console()@@ -22,6 +24,22 @@ def _is_loopback(host: str) -> bool:return False+def _url_host(host: str) -> str:+ """Return the host to put in the URL a browser opens for a server bound to ``host``.++ ``0.0.0.0`` and ``::`` are bind addresses, not destinations: Windows refuses+ to connect to them (#2054), so they map to loopback. IPv6 literals are+ bracketed, as a URL needs.+ """+ try:+ addr = ipaddress.ip_address(host.strip("[]"))+ except ValueError:+ return host+ if addr.is_unspecified:+ addr = ipaddress.ip_address("127.0.0.1" if addr.version == 4 else "::1")+ return f"[{addr}]" if addr.version == 6 else str(addr)++def ui(port: int = typer.Option(7860,@@ -128,11 +146,12 @@ def ui(app = create_app(host=host, port=port)- url = f"http://{host}:{port}"+ # The server stays bound to `host`; only the printed and opened URL changes.+ url = f"http://{_url_host(host)}:{port}"login_url = f"{url}/?token={token}"panel_body = (- f"URL: [bold]{login_url}[/]\n"+ f"URL: [bold]{for_terminal(login_url)}[/]\n"f"Token: [bold]{token}[/]\n\n"f"API endpoints require:\n"f" [dim]Authorization: Bearer {token}[/]\n\n"diff --git a/tests/test_ui_url_host.py b/tests/test_ui_url_host.pynew file mode 100644index 00000000..9e72c1f0--- /dev/null+++ b/tests/test_ui_url_host.py@@ -0,0 +1,121 @@+"""The URL ``soup ui`` prints and opens names a host the browser can reach.++The server binds to the host it is given, but a bind-any address is not a+destination: ``0.0.0.0`` (``--public`` or ``--host 0.0.0.0``) and ``::`` are+printed and opened as ``127.0.0.1`` and ``[::1]``, since Windows cannot connect+to ``0.0.0.0``. IPv6 literals are bracketed, and the printed line is escaped so+that Rich does not read a host such as ``[fd00::1]`` as a style tag. The app,+``uvicorn.run`` and the ``--public`` phone URL keep the bind host or the LAN+address. See #2054.+"""++from __future__ import annotations++import re+from unittest.mock import MagicMock, patch++import pytest+from typer.testing import CliRunner++from tests.conftest import strip_ansi++pytest.importorskip("fastapi")+pytest.importorskip("uvicorn")++from soup_cli.cli import app # noqa: E402+from soup_cli.ui.app import create_app # noqa: E402++_TOKEN = "abcdefghijklmnopqrstuvwx"+_LAN_IP = "192.168.1.23"+++def _collapse(text: str) -> str:+ return re.sub(r"\s+", " ", strip_ansi(text))+++def _immediate_thread(target=None, daemon=None, **kwargs):+ thread = MagicMock()+ thread.start.side_effect = lambda: target() if target else None+ return thread+++def _lan_socket(*args, **kwargs):+ sock = MagicMock()+ sock.__enter__.return_value.getsockname.return_value = (_LAN_IP, 0)+ return sock+++def _run_ui(*args: str) -> tuple[object, list[str], str, str]:+ """Invoke ``soup ui`` with the browser thread inline.++ Returns the result, the opened URLs, the host given to ``uvicorn.run`` and the+ host given to ``create_app``.+ """+ opened: list[str] = []++ def _open(url, *a, **kw):+ opened.append(url)+ return True++ with (+ patch("uvicorn.run") as run,+ patch("soup_cli.ui.app.create_app", wraps=create_app) as made,+ patch("webbrowser.open", side_effect=_open),+ patch("threading.Thread", side_effect=_immediate_thread),+ patch("time.sleep"),+ patch("socket.socket", side_effect=_lan_socket),+ ):+ result = CliRunner().invoke(app, ["ui", "--port", "7861", "--auth-token", _TOKEN, *args])+ bind_host = run.call_args.kwargs["host"] if run.called else ""+ app_host = made.call_args.kwargs["host"] if made.called else ""+ return result, opened, bind_host, app_host+++class TestUnspecifiedBindOpensLoopback:+ @pytest.mark.parametrize(+ ("args", "bind_host", "login_url"),+ [+ (("--public",), "0.0.0.0", f"http://127.0.0.1:7861/?token={_TOKEN}"),+ (("--host", "0.0.0.0"), "0.0.0.0", f"http://127.0.0.1:7861/?token={_TOKEN}"),+ (("--host", "::"), "::", f"http://[::1]:7861/?token={_TOKEN}"),+ ],+ )+ def test_printed_and_opened_url_use_loopback(self, args, bind_host, login_url):+ result, opened, bound, app_host = _run_ui(*args)++ assert result.exit_code == 0, (result.output, repr(result.exception))+ output = _collapse(result.output)+ assert f"URL: {login_url}" in output+ assert opened == [login_url]+ assert "0.0.0.0:7861" not in output+ assert bound == bind_host+ assert app_host == bind_host++ def test_public_phone_url_keeps_the_lan_address(self):+ result, _, _, _ = _run_ui("--public")++ assert result.exit_code == 0, (result.output, repr(result.exception))+ assert f"http://{_LAN_IP}:7861/?token={_TOKEN}" in _collapse(result.output)+++class TestOtherHostsUnchanged:+ @pytest.mark.parametrize("host", ["127.0.0.1", "192.168.1.5", "localhost"])+ def test_named_host_is_used_as_given(self, host):+ login_url = f"http://{host}:7861/?token={_TOKEN}"+ result, opened, bound, app_host = _run_ui("--host", host)++ assert result.exit_code == 0, (result.output, repr(result.exception))+ assert f"URL: {login_url}" in _collapse(result.output)+ assert opened == [login_url]+ assert bound == app_host == host++ @pytest.mark.parametrize("host", ["::1", "fd00::1", "abcd::1"])+ def test_ipv6_literal_is_bracketed_in_the_printed_url(self, host):+ # fd00::1 and abcd::1 start with a letter, so unescaped "[fd00::1]" is a Rich tag+ login_url = f"http://[{host}]:7861/?token={_TOKEN}"+ result, opened, bound, app_host = _run_ui("--host", host)++ assert result.exit_code == 0, (result.output, repr(result.exception))+ assert f"URL: {login_url}" in _collapse(result.output)+ assert opened == [login_url]+ assert bound == app_host == host