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.
Python agentaichatgptdeepseekfine-tuninggemmaimage-generationllamallmllmsopenaipythonqwenreinforcement-learningself-hostedstable-diffusiontext-to-speechttsuiunsloth
Studio: skip the article and main scope passes when the page has no such tag (#15812)
Daniel Han5bc3b974ca06
diff --git a/studio/backend/core/inference/_html_to_md.py b/studio/backend/core/inference/_html_to_md.pyindex b6fac3731dc..5621fd9bf2c 100644--- a/studio/backend/core/inference/_html_to_md.py+++ b/studio/backend/core/inference/_html_to_md.py@@ -2416,9 +2416,11 @@ def _destination_ends(line: str) -> dict:# Scoped conversions shorter than this are rejected as main content, e.g. an empty article stub_MIN_MAIN_CONTENT_CHARS = 200-# absent text = no such tag can tokenize, so that scope finds nothing+# absent text = no such tag can tokenize, so that scope finds nothing and its pass is skipped_ARTICLE_START = re.compile(r"<article", re.IGNORECASE)_MAIN_START = re.compile(r"<main", re.IGNORECASE)+# absent text = no generated span cells, so a pass needs no span-free scoring re-render+_SPAN_ATTR = re.compile(r"(?:col|row)span", re.IGNORECASE)def html_to_markdown(@@ -2436,20 +2438,25 @@ def html_to_markdown(rendered = ""full_rendered = ""if main_content:- tokens = _TokenLog(armed = _ARTICLE_START.search(source_html) is None)- length, rendered, articles, article_visible = _select_main_scope_render(- source_html, "article", site_links, span_limit, tokens- )- if length < _MIN_MAIN_CONTENT_CHARS or articles > 1:- # no <main> = main scope scores 0, so the whole-page pass below runs- tokens.armed = _MAIN_START.search(source_html) is None and (- articles > 2 or length < _MIN_MAIN_CONTENT_CHARS- )- main_length, main_rendered, _, main_visible = _select_main_scope_render(- source_html, "main", site_links, span_limit, tokens+ has_article = _ARTICLE_START.search(source_html) is not None+ # without <article> the first pass may be followed by a title rescan or the whole-page pass+ tokens = _TokenLog(armed = not has_article)+ if has_article:+ length, rendered, articles, article_visible = _select_main_scope_render(+ source_html, "article", site_links, span_limit, tokens)+ else:+ length, rendered, articles, article_visible = 0, "", 0, 0+ if length < _MIN_MAIN_CONTENT_CHARS or articles > 1:+ if _MAIN_START.search(source_html) is not None:+ main_length, main_rendered, _, main_visible = _select_main_scope_render(+ source_html, "main", site_links, span_limit, tokens+ )+ else:+ main_length, main_rendered, main_visible = 0, "", 0+ # the whole-page pass follows and re-renders span pages to score them+ tokens.armed = tokens.armed or _SPAN_ATTR.search(source_html) is not Noneif articles > 2 and main_length < _MIN_MAIN_CONTENT_CHARS:- tokens.armed = Falsemain_length, full_rendered = _render_main_document(source_html, site_links, span_limit, tokens)@@ -2461,7 +2468,6 @@ def html_to_markdown(length, rendered = main_length, main_renderedif length < _MIN_MAIN_CONTENT_CHARS:if not full_rendered:- tokens.armed = False_, full_rendered = _render_main_document(source_html, site_links, span_limit, tokens)diff --git a/studio/backend/tests/test_html_to_md_absent_scope.py b/studio/backend/tests/test_html_to_md_absent_scope.pynew file mode 100644index 00000000000..6b36afc24cb--- /dev/null+++ b/studio/backend/tests/test_html_to_md_absent_scope.py@@ -0,0 +1,116 @@+# SPDX-License-Identifier: AGPL-3.0-only+# Copyright 2026-present the Unsloth AI Inc. team. All rights reserved. See /studio/LICENSE.AGPL-3.0++"""A scope pass whose tag never appears is skipped; output must equal running it."""++from __future__ import annotations++import re+import sys+from pathlib import Path++import pytest++_BACKEND_DIR = str(Path(__file__).resolve().parent.parent)+if _BACKEND_DIR not in sys.path:+ sys.path.insert(0, _BACKEND_DIR)++import core.inference._html_to_md as h2m+from core.inference._html_to_md import SiteLinks, html_to_markdown++_PROSE = " ".join(+ f"Sentence {i} of the body text, long enough to count as prose." for i in range(12)+)++PAGES = {+ "div_only": f"<div><h1>T</h1><p>{_PROSE}</p><p><a href='/next'>next</a></p></div>",+ "text_only": _PROSE,+ "main_only": f"<nav><a href='/a'>Home</a></nav><main><p>{_PROSE}</p></main>",+ "short_main": f"<main><p>tiny</p></main><div><p>{_PROSE}</p></div>",+ "article_only": f"<article><p>{_PROSE}</p></article>",+ "many_articles_no_main": "".join(f"<article><p>card {i}</p></article>" for i in range(4))+ + f"<div><p>{_PROSE}</p></div>",+ "tag_in_comment": f"<!-- <article> <main> --><div><p>{_PROSE}</p></div>",+ "tag_in_script": f"<script>var a = '<article><main>';</script><div><p>{_PROSE}</p></div>",+ "span_table": f"<div><table><tr><td colspan='40'>wide</td><td rowspan='30'>tall</td></tr></table><p>{_PROSE}</p></div>",+ "accordion": "".join(+ f"<h3><button>Question {i}?</button></h3><p>{_PROSE[:220]}</p>" for i in range(3)+ ),+ "empty": "",+}+++def _convert(html, **kwargs):+ links = SiteLinks("https://example.com/page")+ h2m._TITLE_BUTTON_CACHE.clear()+ out = html_to_markdown(html, main_content = True, site_links = links, **kwargs)+ return out, links.strip(out)++[email protected]("name", sorted(PAGES))[email protected]("span", [None, 50])+def test_skipping_an_absent_scope_renders_what_running_it_renders(monkeypatch, name, span):+ got = _convert(PAGES[name], max_span_chars = span)+ with monkeypatch.context() as m:+ m.setattr(h2m, "_ARTICLE_START", re.compile(""))+ m.setattr(h2m, "_MAIN_START", re.compile(""))+ expected = _convert(PAGES[name], max_span_chars = span)+ assert got == expected+++def _scopes_run(monkeypatch, html):+ seen = []+ real = h2m._select_main_scope_render++ def spy(source_html, tag, *args, **kwargs):+ seen.append(tag)+ return real(source_html, tag, *args, **kwargs)++ monkeypatch.setattr(h2m, "_select_main_scope_render", spy)+ h2m._TITLE_BUTTON_CACHE.clear()+ html_to_markdown(html, main_content = True)+ return seen+++def test_a_page_without_article_or_main_runs_no_scope_pass(monkeypatch):+ assert _scopes_run(monkeypatch, PAGES["div_only"]) == []+++def test_a_page_without_article_runs_only_the_main_pass(monkeypatch):+ assert _scopes_run(monkeypatch, PAGES["short_main"]) == ["main"]+++def test_a_page_without_main_runs_only_the_article_pass(monkeypatch):+ assert _scopes_run(monkeypatch, PAGES["many_articles_no_main"]) == ["article"]++[email protected]("name", ["short_main", "accordion", "div_only"])+def test_a_page_without_article_is_tokenized_once(monkeypatch, name):+ # a short main scope leaves the whole-page pass, an accordion page a title rescan: both replay+ feeds = []+ real = h2m._MarkdownRenderer.feed++ def counted(self, data):+ feeds.append(1)+ return real(self, data)++ monkeypatch.setattr(h2m._MarkdownRenderer, "feed", counted)+ h2m._TITLE_BUTTON_CACHE.clear()+ html_to_markdown(PAGES[name], main_content = True)+ assert len(feeds) == 1+++def test_an_article_page_without_main_replays_its_span_scoring_pass(monkeypatch):+ # article pass, then the whole-page pass and its span-free scoring render: the last replays+ html = f"<article><p>tiny</p></article><table><tr><td colspan='30'>c</td></tr></table><p>{_PROSE}</p>"+ feeds = []+ real = h2m._MarkdownRenderer.feed++ def counted(self, data):+ feeds.append(1)+ return real(self, data)++ monkeypatch.setattr(h2m._MarkdownRenderer, "feed", counted)+ h2m._TITLE_BUTTON_CACHE.clear()+ html_to_markdown(html, main_content = True)+ assert len(feeds) == 2