diff --git a/studio/backend/run.py b/studio/backend/run.py index 5388d3148b..2d86f70578 100644 --- a/studio/backend/run.py +++ b/studio/backend/run.py @@ -175,6 +175,14 @@ def run_server( """ global _server, _shutdown_event + # On Windows the default console encoding (cp1252) cannot encode emoji. + # Reconfigure stdout to UTF-8 so startup messages do not crash the server. + if sys.platform == "win32" and hasattr(sys.stdout, "reconfigure"): + try: + sys.stdout.reconfigure(encoding = "utf-8", errors = "replace") + except Exception: + pass + import nest_asyncio nest_asyncio.apply() diff --git a/unsloth_cli/commands/studio.py b/unsloth_cli/commands/studio.py index 8a2267a79e..c4008aa4a6 100644 --- a/unsloth_cli/commands/studio.py +++ b/unsloth_cli/commands/studio.py @@ -133,12 +133,10 @@ def studio_default( display_host = _resolve_external_ip() if host == "0.0.0.0" else host typer.echo(f"Starting Unsloth Studio on http://{display_host}:{port}") - run_server( - host = host, - port = port, - frontend_path = frontend, - silent = silent, - ) + run_kwargs = dict(host = host, port = port, silent = silent) + if frontend is not None: + run_kwargs["frontend_path"] = frontend + run_server(**run_kwargs) from studio.backend.run import _shutdown_event diff --git a/unsloth_cli/commands/ui.py b/unsloth_cli/commands/ui.py index 4457465e96..8f76636990 100644 --- a/unsloth_cli/commands/ui.py +++ b/unsloth_cli/commands/ui.py @@ -80,18 +80,19 @@ def ui( display_host = _resolve_external_ip() if host == "0.0.0.0" else host typer.echo(f"Starting Unsloth Studio on http://{display_host}:{port}") - run_server( - host = host, - port = port, - frontend_path = frontend, - silent = silent, - ) + run_kwargs = dict(host = host, port = port, silent = silent) + if frontend is not None: + run_kwargs["frontend_path"] = frontend + run_server(**run_kwargs) from studio.backend.run import _shutdown_event try: if _shutdown_event is not None: - _shutdown_event.wait() + # NOTE: Event.wait() without a timeout blocks at the C level + # on Linux, preventing Python from delivering SIGINT (Ctrl+C). + while not _shutdown_event.is_set(): + _shutdown_event.wait(timeout = 1) else: while True: time.sleep(1)