Skip to content

Local UI to run and train LLMs and diffusion models. Supports GGUF, MLX, Qwen3.8, DeepSeek-V4.1, MiniMax-H3, Gemma 4, FLUX and more.

Keep the transcribed repo id rule ASCII, as the Hub's is (#11204)

Daniel Han1155262b9a71
diff --git a/studio/backend/tests/test_base_model_dir_name_fallback.py b/studio/backend/tests/test_base_model_dir_name_fallback.py
index c67762ae250..9282bc98bde 100644
--- a/studio/backend/tests/test_base_model_dir_name_fallback.py
+++ b/studio/backend/tests/test_base_model_dir_name_fallback.py
@@ -39,9 +39,35 @@ from utils.models.model_config import ( # noqa: E402
from utils.training_runs import ( # noqa: E402
base_model_from_run_dir_name,
build_default_output_dir_name,
+ model_segment_from_default_output_dir_name,
)
+# One name per script that `\w` accepts and the Hub's ASCII charset does not. Built as
+# names rather than bare characters because that is the shape the parser sees, and checked
+# against `\w` below so a case cannot quietly stop testing anything.
+_NON_ASCII_NAMES = (
+ "Café-8B", # Latin-1 accent
+ "naïve_v2", # Latin-1 diaeresis
+ "модель-8B", # Cyrillic
+ "μοντέλο", # Greek
+ "מודל", # Hebrew
+ "نموذج", # Arabic
+ "文件夹", # Han
+ "モデル", # Katakana
+ "qwen", # fullwidth Latin
+ "A", # fullwidth capital
+)
+
+
+def test_the_non_ascii_cases_are_word_characters():
+ """Non-vacuity: every name above must be one `\\w` accepts, or it proves nothing."""
+ import re
+ for name in _NON_ASCII_NAMES:
+ assert re.fullmatch(r"[\w.-]+", name), name
+ assert not name.isascii(), name
+
+
@pytest.mark.parametrize(
"dir_name,expected",
[
@@ -176,8 +202,12 @@ def test_the_transcribed_repo_id_rule_is_never_looser_than_the_hubs():
" ",
"a b",
"a\tb",
- "Café-8B",
"a-b.c_d",
+ # Non-ASCII word characters. `\w` matches all of these and the Hub's charset
+ # matches none of them, which is the direction this test exists to catch:
+ # huggingface_hub 1.32.0 put re.ASCII on its own REPO_ID_REGEX and only
+ # "Café-8B" was listed here, so one name stood in for a whole class.
+ *_NON_ASCII_NAMES,
]
looser = [
n
@@ -188,6 +218,41 @@ def test_the_transcribed_repo_id_rule_is_never_looser_than_the_hubs():
assert looser == []
+def test_no_non_ascii_letter_or_digit_can_reach_a_repo_id():
+ """The charset rule, asked of the charset rather than of a list of names.
+
+ A list only ever covers the scripts somebody thought to write down. This sweeps one
+ character from each block that `\\w` accepts and ASCII does not, so a rule that goes
+ back to being Unicode aware fails here whichever script it lets through -- including
+ for a reader with no huggingface_hub installed, where the comparison above skips.
+ """
+ escaped = [
+ name
+ for name in _NON_ASCII_NAMES
+ if base_model_from_run_dir_name(f"unsloth_{name}_1771227800") is not None
+ ]
+ assert escaped == [], "a non-ASCII folder name parsed into a repo id the Hub rejects"
+
+
+ "stamp",
+ [
+ "١٧٧١٢٢٧٨٠٠", # Arabic-Indic
+ "१२३४५६", # Devanagari
+ "177122", # fullwidth
+ ],
+)
+def test_a_non_ascii_digit_run_is_not_a_timestamp(stamp):
+ """`\\d` and `str.isdigit()` are both Unicode aware; the writer emits `str(int(...))`.
+
+ Without the ASCII pins these folders read as ones we wrote, so a hand-made directory
+ got a base model attached to it on the strength of digits no run ever produced.
+ """
+ assert stamp.isdigit(), "the case is only meaningful if str.isdigit() accepts it"
+ assert base_model_from_run_dir_name(f"unsloth_Qwen3-8B_{stamp}") is None
+ assert model_segment_from_default_output_dir_name(f"unsloth_Qwen3-8B_{stamp}") is None
+
+
def _write_adapter(directory):
directory.mkdir(parents = True)
# No base_model_name_or_path, so detection has to fall through to the directory name.
diff --git a/studio/backend/utils/training_runs.py b/studio/backend/utils/training_runs.py
index f236ef36fa7..e6ad53d5842 100644
--- a/studio/backend/utils/training_runs.py
+++ b/studio/backend/utils/training_runs.py
@@ -18,11 +18,19 @@ _UNSLOTH_ORG_PREFIX = "unsloth_"
# We emit a bare epoch; hand-made folders often use a date-time. Anything else
# (``_final``, ``_v2``, ``_8b``) is part of the model name, not a stamp.
-_RUN_DIR_TIMESTAMP = re.compile(r"\A\d{6,}(?:[-_]\d{2,})?\Z")
+# re.ASCII for the same reason as ``_REPO_NAME`` below: ``\d`` otherwise matches every
+# Unicode decimal digit, so ``unsloth_Qwen3-8B_١٧٧١٢٢٧٨٠٠`` read as a folder we wrote.
+_RUN_DIR_TIMESTAMP = re.compile(r"\A\d{6,}(?:[-_]\d{2,})?\Z", re.ASCII)
# ``validate_repo_id`` transcribed to keep this module stdlib-only. A folder name is user
# input, so trust the parse only when the Hub would accept what falls out of it.
-_REPO_NAME = re.compile(r"\A(?!.*(?:--|\.\.))(?![-.])[\w.-]{1,96}(?<![-.])\Z")
+# re.ASCII because ``\w`` is Unicode aware by default and the Hub's charset is not:
+# huggingface_hub 1.32.0 added the same flag to its own REPO_ID_REGEX for exactly this,
+# and without it ``Café-8B``, ``文件夹`` and ``модель-8B`` parsed here into repo ids the
+# Hub rejects. We never write such a name ourselves (``_INVALID_SEGMENT_CHARS`` above
+# strips anything outside ``A-Za-z0-9._-``), so this only ever concerned hand-made
+# folders, which is precisely the input this parse exists to distrust.
+_REPO_NAME = re.compile(r"\A(?!.*(?:--|\.\.))(?![-.])[\w.-]{1,96}(?<![-.])\Z", re.ASCII)
def _is_valid_repo_name(name: str) -> bool:
@@ -101,7 +109,10 @@ def build_default_output_dir_name(
def model_segment_from_default_output_dir_name(output_dir_name: str) -> Optional[str]:
"""Return the encoded model segment from a default run folder name."""
parts = str(output_dir_name or "").rsplit("_", 1)
- if len(parts) != 2 or not parts[1].isdigit():
+ # isascii() first: str.isdigit() is true of Arabic-Indic and every other Unicode
+ # decimal digit, and this is the strict inverse of a writer that only ever emits
+ # `str(int(...))`.
+ if len(parts) != 2 or not (parts[1].isascii() and parts[1].isdigit()):
return None
model_segment = parts[0]
marker_index = _appended_project_marker_index(model_segment)