Add fine-tuned Qwen3.5-2B distill-structure model with Gradio demo
Browse files- .gitattributes +1 -0
- README.md +95 -0
- app.py +266 -0
- chat_template.jinja +154 -0
- config.json +75 -0
- generation_config.json +10 -0
- model.safetensors +3 -0
- tokenizer.json +3 -0
- tokenizer_config.json +31 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- en
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
base_model: Qwen/Qwen3.5-2B
|
| 6 |
+
tags:
|
| 7 |
+
- html
|
| 8 |
+
- structure-analysis
|
| 9 |
+
- information-extraction
|
| 10 |
+
- web-scraping
|
| 11 |
+
- lora
|
| 12 |
+
- fine-tuned
|
| 13 |
+
pipeline_tag: text-generation
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# distill-structure
|
| 17 |
+
|
| 18 |
+
A fine-tuned [Qwen3.5-2B](https://huggingface.co/Qwen/Qwen3.5-2B) model for **HTML structure analysis** — given a compact DOM representation of a web page, it identifies the logical sections and outputs structured JSON.
|
| 19 |
+
|
| 20 |
+
## What it does
|
| 21 |
+
|
| 22 |
+
Takes a cleaned, heading-stripped HTML page and returns a JSON array describing its sections:
|
| 23 |
+
|
| 24 |
+
```json
|
| 25 |
+
[
|
| 26 |
+
{
|
| 27 |
+
"title": "Main News Feed Content",
|
| 28 |
+
"start_text": "1. Canada's bill C-22 mandates...",
|
| 29 |
+
"content_type": "article",
|
| 30 |
+
"assets": [{"type": "link", "value": "Canada's bill C-22..."}]
|
| 31 |
+
},
|
| 32 |
+
{
|
| 33 |
+
"title": "Site Footer Navigation",
|
| 34 |
+
"start_text": "Guidelines | FAQ | Lists",
|
| 35 |
+
"content_type": "footer",
|
| 36 |
+
"assets": []
|
| 37 |
+
}
|
| 38 |
+
]
|
| 39 |
+
```
|
| 40 |
+
|
| 41 |
+
## Use case
|
| 42 |
+
|
| 43 |
+
This model powers the `StructureAgent` inside the [distill](https://github.com/nahidstaq/distill) pipeline — it handles pages with **no heading tags** where rule-based sectioning fails. The model is trained to recover section structure that headings would normally provide.
|
| 44 |
+
|
| 45 |
+
## Training
|
| 46 |
+
|
| 47 |
+
- **Base model**: `Qwen/Qwen3.5-2B`
|
| 48 |
+
- **Method**: LoRA fine-tuning (r=32, α=64) via TRL SFTTrainer
|
| 49 |
+
- **Dataset**: ~3,455 training / 384 eval examples generated from heading-rich web pages (headings stripped and used as labels)
|
| 50 |
+
- **Epochs**: 3 — Train loss: 1.009 — Token accuracy: 80.5%
|
| 51 |
+
|
| 52 |
+
## Quick start
|
| 53 |
+
|
| 54 |
+
```python
|
| 55 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 56 |
+
import torch, json
|
| 57 |
+
|
| 58 |
+
model_id = "nahidstaq/distill-structure"
|
| 59 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 60 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, dtype=torch.bfloat16, device_map="auto")
|
| 61 |
+
|
| 62 |
+
SYSTEM = (
|
| 63 |
+
"You are an HTML structure analyzer. Given a compact DOM representation "
|
| 64 |
+
"of a web page (with headings removed), identify the logical sections. "
|
| 65 |
+
"Output a JSON array of sections, each with title, start_text, content_type, and assets fields."
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def analyze(page_title: str, compact_dom: str) -> list[dict]:
|
| 69 |
+
messages = [
|
| 70 |
+
{"role": "system", "content": SYSTEM},
|
| 71 |
+
{"role": "user", "content": f"Page: {page_title}\n\n{compact_dom}"},
|
| 72 |
+
]
|
| 73 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 74 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 75 |
+
with torch.no_grad():
|
| 76 |
+
ids = model.generate(**inputs, max_new_tokens=512, do_sample=False,
|
| 77 |
+
pad_token_id=tokenizer.eos_token_id)
|
| 78 |
+
raw = tokenizer.decode(ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True)
|
| 79 |
+
return json.loads(raw)
|
| 80 |
+
```
|
| 81 |
+
|
| 82 |
+
## Output fields
|
| 83 |
+
|
| 84 |
+
| Field | Description |
|
| 85 |
+
|---|---|
|
| 86 |
+
| `title` | Short descriptive section title |
|
| 87 |
+
| `start_text` | First ~50 chars of the section's text (for anchoring) |
|
| 88 |
+
| `content_type` | One of: `article`, `list`, `hero`, `navigation`, `footer`, `table`, `faq`, `other` |
|
| 89 |
+
| `assets` | Extracted links, images, or list items relevant to the section |
|
| 90 |
+
|
| 91 |
+
## Limitations
|
| 92 |
+
|
| 93 |
+
- Works best on English pages
|
| 94 |
+
- Table-heavy layouts (e.g. nested `<td>`) may collapse into fewer sections
|
| 95 |
+
- `content_type` classification skews toward `other` for ambiguous sections
|
app.py
ADDED
|
@@ -0,0 +1,266 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Gradio demo for distill-structure model."""
|
| 2 |
+
|
| 3 |
+
import json
|
| 4 |
+
import re
|
| 5 |
+
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import torch
|
| 8 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 9 |
+
|
| 10 |
+
# ---------------------------------------------------------------------------
|
| 11 |
+
# Model
|
| 12 |
+
# ---------------------------------------------------------------------------
|
| 13 |
+
|
| 14 |
+
MODEL_ID = "nahidstaq/distill-structure"
|
| 15 |
+
|
| 16 |
+
SYSTEM = (
|
| 17 |
+
"You are an HTML structure analyzer. Given a compact DOM representation "
|
| 18 |
+
"of a web page (with headings removed), identify the logical sections. "
|
| 19 |
+
"Output a JSON array of sections, each with title, start_text, content_type, and assets fields."
|
| 20 |
+
)
|
| 21 |
+
|
| 22 |
+
_model = None
|
| 23 |
+
_tokenizer = None
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
def _load():
|
| 27 |
+
global _model, _tokenizer
|
| 28 |
+
if _model is None:
|
| 29 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
+
dtype = torch.bfloat16 if device == "cuda" else torch.float32
|
| 31 |
+
_tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
|
| 32 |
+
_model = AutoModelForCausalLM.from_pretrained(
|
| 33 |
+
MODEL_ID, dtype=dtype, device_map="auto"
|
| 34 |
+
)
|
| 35 |
+
_model.eval()
|
| 36 |
+
return _model, _tokenizer
|
| 37 |
+
|
| 38 |
+
|
| 39 |
+
# ---------------------------------------------------------------------------
|
| 40 |
+
# Helpers
|
| 41 |
+
# ---------------------------------------------------------------------------
|
| 42 |
+
|
| 43 |
+
def _compact_dom(html: str) -> str:
|
| 44 |
+
from lxml import html as lxml_html
|
| 45 |
+
|
| 46 |
+
try:
|
| 47 |
+
doc = lxml_html.fromstring(html)
|
| 48 |
+
except Exception:
|
| 49 |
+
return html[:3000]
|
| 50 |
+
|
| 51 |
+
for tag in ("h1", "h2", "h3", "h4", "h5", "h6", "script", "style", "head"):
|
| 52 |
+
for el in doc.findall(f".//{tag}"):
|
| 53 |
+
p = el.getparent()
|
| 54 |
+
if p is not None:
|
| 55 |
+
p.remove(el)
|
| 56 |
+
|
| 57 |
+
def _walk(el, depth=0):
|
| 58 |
+
if not hasattr(el, "tag") or not isinstance(el.tag, str):
|
| 59 |
+
return ""
|
| 60 |
+
tag = el.tag
|
| 61 |
+
indent = " " * depth
|
| 62 |
+
|
| 63 |
+
if tag == "img":
|
| 64 |
+
alt = el.get("alt", "")
|
| 65 |
+
return f'{indent}<img alt="{alt}">' if alt else f'{indent}<img>'
|
| 66 |
+
|
| 67 |
+
if tag == "a":
|
| 68 |
+
text = (el.text_content() or "").strip()[:40]
|
| 69 |
+
href = (el.get("href") or "")[:60]
|
| 70 |
+
return f'{indent}<a href="{href}"> {text}'
|
| 71 |
+
|
| 72 |
+
if tag in ("td", "th"):
|
| 73 |
+
# Recurse into td if it has block children, otherwise truncate
|
| 74 |
+
children = [c for c in el if hasattr(c, "tag") and isinstance(c.tag, str)]
|
| 75 |
+
if children and depth < 8:
|
| 76 |
+
lines = [f"{indent}<{tag}>"]
|
| 77 |
+
for child in children:
|
| 78 |
+
r = _walk(child, depth + 1)
|
| 79 |
+
if r:
|
| 80 |
+
lines.append(r)
|
| 81 |
+
return "\n".join(lines)
|
| 82 |
+
text = (el.text_content() or "").strip()[:60]
|
| 83 |
+
return f"{indent}<{tag}> {text}" if text else ""
|
| 84 |
+
|
| 85 |
+
if depth > 7:
|
| 86 |
+
text = (el.text_content() or "").strip()[:80]
|
| 87 |
+
return f"{indent}[... {text}...]" if text else ""
|
| 88 |
+
|
| 89 |
+
text = (el.text or "").strip()[:50]
|
| 90 |
+
attrs = ""
|
| 91 |
+
for a in ("id", "class", "role"):
|
| 92 |
+
v = el.get(a)
|
| 93 |
+
if v:
|
| 94 |
+
attrs += f' {a}="{v[:30]}"'
|
| 95 |
+
|
| 96 |
+
line = f"{indent}<{tag}{attrs}>"
|
| 97 |
+
if text:
|
| 98 |
+
line += f" {text}"
|
| 99 |
+
lines = [line]
|
| 100 |
+
for child in el:
|
| 101 |
+
r = _walk(child, depth + 1)
|
| 102 |
+
if r:
|
| 103 |
+
lines.append(r)
|
| 104 |
+
return "\n".join(lines)
|
| 105 |
+
|
| 106 |
+
body = doc.find(".//body") or doc
|
| 107 |
+
result = _walk(body)
|
| 108 |
+
# Truncate to 4096 chars
|
| 109 |
+
if len(result) > 4096:
|
| 110 |
+
result = result[:4096] + "\n... (truncated)"
|
| 111 |
+
return result
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
def _extract_title(html: str) -> str:
|
| 115 |
+
m = re.search(r"<title>(.*?)</title>", html, re.I | re.S)
|
| 116 |
+
return m.group(1).strip() if m else "Untitled"
|
| 117 |
+
|
| 118 |
+
|
| 119 |
+
def _parse(raw: str) -> list[dict]:
|
| 120 |
+
try:
|
| 121 |
+
data = json.loads(raw)
|
| 122 |
+
if isinstance(data, list):
|
| 123 |
+
return data
|
| 124 |
+
except json.JSONDecodeError:
|
| 125 |
+
pass
|
| 126 |
+
m = re.search(r"\[.*?\]", raw, re.S)
|
| 127 |
+
if m:
|
| 128 |
+
try:
|
| 129 |
+
return json.loads(m.group())
|
| 130 |
+
except json.JSONDecodeError:
|
| 131 |
+
pass
|
| 132 |
+
return []
|
| 133 |
+
|
| 134 |
+
|
| 135 |
+
# ---------------------------------------------------------------------------
|
| 136 |
+
# Inference
|
| 137 |
+
# ---------------------------------------------------------------------------
|
| 138 |
+
|
| 139 |
+
def analyze_html(html: str, page_title: str) -> tuple[str, str]:
|
| 140 |
+
if not html.strip():
|
| 141 |
+
return "Please paste some HTML.", ""
|
| 142 |
+
|
| 143 |
+
model, tokenizer = _load()
|
| 144 |
+
|
| 145 |
+
compact = _compact_dom(html)
|
| 146 |
+
title = page_title.strip() or _extract_title(html)
|
| 147 |
+
|
| 148 |
+
messages = [
|
| 149 |
+
{"role": "system", "content": SYSTEM},
|
| 150 |
+
{"role": "user", "content": f"Page: {title}\n\n{compact}"},
|
| 151 |
+
]
|
| 152 |
+
prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
|
| 153 |
+
inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
|
| 154 |
+
|
| 155 |
+
with torch.no_grad():
|
| 156 |
+
ids = model.generate(
|
| 157 |
+
**inputs,
|
| 158 |
+
max_new_tokens=512,
|
| 159 |
+
do_sample=False,
|
| 160 |
+
temperature=None,
|
| 161 |
+
top_p=None,
|
| 162 |
+
pad_token_id=tokenizer.eos_token_id,
|
| 163 |
+
)
|
| 164 |
+
|
| 165 |
+
raw = tokenizer.decode(ids[0][inputs["input_ids"].shape[1]:], skip_special_tokens=True).strip()
|
| 166 |
+
sections = _parse(raw)
|
| 167 |
+
|
| 168 |
+
# Pretty format sections as markdown table
|
| 169 |
+
if sections:
|
| 170 |
+
md = "| # | Type | Title | Start text |\n|---|---|---|---|\n"
|
| 171 |
+
for i, s in enumerate(sections, 1):
|
| 172 |
+
title_s = s.get("title", "")
|
| 173 |
+
ctype = s.get("content_type", "?")
|
| 174 |
+
start = (s.get("start_text") or "")[:50]
|
| 175 |
+
md += f"| {i} | `{ctype}` | {title_s} | {start} |\n"
|
| 176 |
+
else:
|
| 177 |
+
md = "_Could not parse sections from model output._"
|
| 178 |
+
|
| 179 |
+
return md, raw
|
| 180 |
+
|
| 181 |
+
|
| 182 |
+
def analyze_url(url: str) -> tuple[str, str, str]:
|
| 183 |
+
if not url.strip():
|
| 184 |
+
return "", "Please enter a URL.", ""
|
| 185 |
+
try:
|
| 186 |
+
import httpx
|
| 187 |
+
r = httpx.get(url, follow_redirects=True, timeout=10,
|
| 188 |
+
headers={"User-Agent": "Mozilla/5.0"})
|
| 189 |
+
html = r.text
|
| 190 |
+
title = _extract_title(html)
|
| 191 |
+
md, raw = analyze_html(html, title)
|
| 192 |
+
return html[:5000] + ("..." if len(html) > 5000 else ""), md, raw
|
| 193 |
+
except Exception as e:
|
| 194 |
+
return "", f"Error fetching URL: {e}", ""
|
| 195 |
+
|
| 196 |
+
|
| 197 |
+
# ---------------------------------------------------------------------------
|
| 198 |
+
# UI
|
| 199 |
+
# ---------------------------------------------------------------------------
|
| 200 |
+
|
| 201 |
+
EXAMPLE_HTML = """<!DOCTYPE html>
|
| 202 |
+
<html>
|
| 203 |
+
<head><title>Product Page</title></head>
|
| 204 |
+
<body>
|
| 205 |
+
<h1>Our Amazing Product</h1>
|
| 206 |
+
<p>Welcome to the best product you've ever seen.</p>
|
| 207 |
+
<h2>Features</h2>
|
| 208 |
+
<ul>
|
| 209 |
+
<li>Lightning fast</li>
|
| 210 |
+
<li>Easy to use</li>
|
| 211 |
+
<li>Affordable pricing</li>
|
| 212 |
+
</ul>
|
| 213 |
+
<h2>Pricing</h2>
|
| 214 |
+
<table>
|
| 215 |
+
<tr><th>Plan</th><th>Price</th></tr>
|
| 216 |
+
<tr><td>Starter</td><td>$9/mo</td></tr>
|
| 217 |
+
<tr><td>Pro</td><td>$29/mo</td></tr>
|
| 218 |
+
</table>
|
| 219 |
+
<h2>FAQ</h2>
|
| 220 |
+
<h3>Is there a free trial?</h3>
|
| 221 |
+
<p>Yes! 14 days free, no credit card required.</p>
|
| 222 |
+
</body>
|
| 223 |
+
</html>"""
|
| 224 |
+
|
| 225 |
+
with gr.Blocks(title="distill-structure", theme=gr.themes.Soft()) as demo:
|
| 226 |
+
gr.Markdown("# distill-structure\nHTML section analyzer — fine-tuned Qwen3.5-2B")
|
| 227 |
+
|
| 228 |
+
with gr.Tabs():
|
| 229 |
+
with gr.Tab("Paste HTML"):
|
| 230 |
+
with gr.Row():
|
| 231 |
+
with gr.Column():
|
| 232 |
+
html_input = gr.Textbox(
|
| 233 |
+
label="HTML",
|
| 234 |
+
placeholder="Paste HTML here...",
|
| 235 |
+
lines=15,
|
| 236 |
+
value=EXAMPLE_HTML,
|
| 237 |
+
)
|
| 238 |
+
title_input = gr.Textbox(label="Page title (optional)", placeholder="Auto-detected from <title>")
|
| 239 |
+
btn_html = gr.Button("Analyze", variant="primary")
|
| 240 |
+
with gr.Column():
|
| 241 |
+
sections_out = gr.Markdown(label="Sections")
|
| 242 |
+
raw_out = gr.Textbox(label="Raw JSON output", lines=10)
|
| 243 |
+
|
| 244 |
+
btn_html.click(analyze_html, inputs=[html_input, title_input], outputs=[sections_out, raw_out])
|
| 245 |
+
|
| 246 |
+
with gr.Tab("From URL"):
|
| 247 |
+
with gr.Row():
|
| 248 |
+
with gr.Column():
|
| 249 |
+
url_input = gr.Textbox(label="URL", placeholder="https://news.ycombinator.com")
|
| 250 |
+
btn_url = gr.Button("Fetch & Analyze", variant="primary")
|
| 251 |
+
html_preview = gr.Textbox(label="Fetched HTML (preview)", lines=8)
|
| 252 |
+
with gr.Column():
|
| 253 |
+
sections_out2 = gr.Markdown(label="Sections")
|
| 254 |
+
raw_out2 = gr.Textbox(label="Raw JSON output", lines=10)
|
| 255 |
+
|
| 256 |
+
btn_url.click(analyze_url, inputs=[url_input], outputs=[html_preview, sections_out2, raw_out2])
|
| 257 |
+
|
| 258 |
+
gr.Markdown("""
|
| 259 |
+
---
|
| 260 |
+
**Model**: [nahidstaq/distill-structure](https://huggingface.co/nahidstaq/distill-structure) ·
|
| 261 |
+
**Base**: Qwen3.5-2B ·
|
| 262 |
+
**Task**: HTML structure analysis
|
| 263 |
+
""")
|
| 264 |
+
|
| 265 |
+
if __name__ == "__main__":
|
| 266 |
+
demo.launch()
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,154 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{%- set image_count = namespace(value=0) %}
|
| 2 |
+
{%- set video_count = namespace(value=0) %}
|
| 3 |
+
{%- macro render_content(content, do_vision_count, is_system_content=false) %}
|
| 4 |
+
{%- if content is string %}
|
| 5 |
+
{{- content }}
|
| 6 |
+
{%- elif content is iterable and content is not mapping %}
|
| 7 |
+
{%- for item in content %}
|
| 8 |
+
{%- if 'image' in item or 'image_url' in item or item.type == 'image' %}
|
| 9 |
+
{%- if is_system_content %}
|
| 10 |
+
{{- raise_exception('System message cannot contain images.') }}
|
| 11 |
+
{%- endif %}
|
| 12 |
+
{%- if do_vision_count %}
|
| 13 |
+
{%- set image_count.value = image_count.value + 1 %}
|
| 14 |
+
{%- endif %}
|
| 15 |
+
{%- if add_vision_id %}
|
| 16 |
+
{{- 'Picture ' ~ image_count.value ~ ': ' }}
|
| 17 |
+
{%- endif %}
|
| 18 |
+
{{- '<|vision_start|><|image_pad|><|vision_end|>' }}
|
| 19 |
+
{%- elif 'video' in item or item.type == 'video' %}
|
| 20 |
+
{%- if is_system_content %}
|
| 21 |
+
{{- raise_exception('System message cannot contain videos.') }}
|
| 22 |
+
{%- endif %}
|
| 23 |
+
{%- if do_vision_count %}
|
| 24 |
+
{%- set video_count.value = video_count.value + 1 %}
|
| 25 |
+
{%- endif %}
|
| 26 |
+
{%- if add_vision_id %}
|
| 27 |
+
{{- 'Video ' ~ video_count.value ~ ': ' }}
|
| 28 |
+
{%- endif %}
|
| 29 |
+
{{- '<|vision_start|><|video_pad|><|vision_end|>' }}
|
| 30 |
+
{%- elif 'text' in item %}
|
| 31 |
+
{{- item.text }}
|
| 32 |
+
{%- else %}
|
| 33 |
+
{{- raise_exception('Unexpected item type in content.') }}
|
| 34 |
+
{%- endif %}
|
| 35 |
+
{%- endfor %}
|
| 36 |
+
{%- elif content is none or content is undefined %}
|
| 37 |
+
{{- '' }}
|
| 38 |
+
{%- else %}
|
| 39 |
+
{{- raise_exception('Unexpected content type.') }}
|
| 40 |
+
{%- endif %}
|
| 41 |
+
{%- endmacro %}
|
| 42 |
+
{%- if not messages %}
|
| 43 |
+
{{- raise_exception('No messages provided.') }}
|
| 44 |
+
{%- endif %}
|
| 45 |
+
{%- if tools and tools is iterable and tools is not mapping %}
|
| 46 |
+
{{- '<|im_start|>system\n' }}
|
| 47 |
+
{{- "# Tools\n\nYou have access to the following functions:\n\n<tools>" }}
|
| 48 |
+
{%- for tool in tools %}
|
| 49 |
+
{{- "\n" }}
|
| 50 |
+
{{- tool | tojson }}
|
| 51 |
+
{%- endfor %}
|
| 52 |
+
{{- "\n</tools>" }}
|
| 53 |
+
{{- '\n\nIf you choose to call a function ONLY reply in the following format with NO suffix:\n\n<tool_call>\n<function=example_function_name>\n<parameter=example_parameter_1>\nvalue_1\n</parameter>\n<parameter=example_parameter_2>\nThis is the value for the second parameter\nthat can span\nmultiple lines\n</parameter>\n</function>\n</tool_call>\n\n<IMPORTANT>\nReminder:\n- Function calls MUST follow the specified format: an inner <function=...></function> block must be nested within <tool_call></tool_call> XML tags\n- Required parameters MUST be specified\n- You may provide optional reasoning for your function call in natural language BEFORE the function call, but NOT after\n- If there is no function call available, answer the question like normal with your current knowledge and do not tell the user about function calls\n</IMPORTANT>' }}
|
| 54 |
+
{%- if messages[0].role == 'system' %}
|
| 55 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 56 |
+
{%- if content %}
|
| 57 |
+
{{- '\n\n' + content }}
|
| 58 |
+
{%- endif %}
|
| 59 |
+
{%- endif %}
|
| 60 |
+
{{- '<|im_end|>\n' }}
|
| 61 |
+
{%- else %}
|
| 62 |
+
{%- if messages[0].role == 'system' %}
|
| 63 |
+
{%- set content = render_content(messages[0].content, false, true)|trim %}
|
| 64 |
+
{{- '<|im_start|>system\n' + content + '<|im_end|>\n' }}
|
| 65 |
+
{%- endif %}
|
| 66 |
+
{%- endif %}
|
| 67 |
+
{%- set ns = namespace(multi_step_tool=true, last_query_index=messages|length - 1) %}
|
| 68 |
+
{%- for message in messages[::-1] %}
|
| 69 |
+
{%- set index = (messages|length - 1) - loop.index0 %}
|
| 70 |
+
{%- if ns.multi_step_tool and message.role == "user" %}
|
| 71 |
+
{%- set content = render_content(message.content, false)|trim %}
|
| 72 |
+
{%- if not(content.startswith('<tool_response>') and content.endswith('</tool_response>')) %}
|
| 73 |
+
{%- set ns.multi_step_tool = false %}
|
| 74 |
+
{%- set ns.last_query_index = index %}
|
| 75 |
+
{%- endif %}
|
| 76 |
+
{%- endif %}
|
| 77 |
+
{%- endfor %}
|
| 78 |
+
{%- if ns.multi_step_tool %}
|
| 79 |
+
{{- raise_exception('No user query found in messages.') }}
|
| 80 |
+
{%- endif %}
|
| 81 |
+
{%- for message in messages %}
|
| 82 |
+
{%- set content = render_content(message.content, true)|trim %}
|
| 83 |
+
{%- if message.role == "system" %}
|
| 84 |
+
{%- if not loop.first %}
|
| 85 |
+
{{- raise_exception('System message must be at the beginning.') }}
|
| 86 |
+
{%- endif %}
|
| 87 |
+
{%- elif message.role == "user" %}
|
| 88 |
+
{{- '<|im_start|>' + message.role + '\n' + content + '<|im_end|>' + '\n' }}
|
| 89 |
+
{%- elif message.role == "assistant" %}
|
| 90 |
+
{%- set reasoning_content = '' %}
|
| 91 |
+
{%- if message.reasoning_content is string %}
|
| 92 |
+
{%- set reasoning_content = message.reasoning_content %}
|
| 93 |
+
{%- else %}
|
| 94 |
+
{%- if '</think>' in content %}
|
| 95 |
+
{%- set reasoning_content = content.split('</think>')[0].rstrip('\n').split('<think>')[-1].lstrip('\n') %}
|
| 96 |
+
{%- set content = content.split('</think>')[-1].lstrip('\n') %}
|
| 97 |
+
{%- endif %}
|
| 98 |
+
{%- endif %}
|
| 99 |
+
{%- set reasoning_content = reasoning_content|trim %}
|
| 100 |
+
{%- if loop.index0 > ns.last_query_index %}
|
| 101 |
+
{{- '<|im_start|>' + message.role + '\n<think>\n' + reasoning_content + '\n</think>\n\n' + content }}
|
| 102 |
+
{%- else %}
|
| 103 |
+
{{- '<|im_start|>' + message.role + '\n' + content }}
|
| 104 |
+
{%- endif %}
|
| 105 |
+
{%- if message.tool_calls and message.tool_calls is iterable and message.tool_calls is not mapping %}
|
| 106 |
+
{%- for tool_call in message.tool_calls %}
|
| 107 |
+
{%- if tool_call.function is defined %}
|
| 108 |
+
{%- set tool_call = tool_call.function %}
|
| 109 |
+
{%- endif %}
|
| 110 |
+
{%- if loop.first %}
|
| 111 |
+
{%- if content|trim %}
|
| 112 |
+
{{- '\n\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 113 |
+
{%- else %}
|
| 114 |
+
{{- '<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 115 |
+
{%- endif %}
|
| 116 |
+
{%- else %}
|
| 117 |
+
{{- '\n<tool_call>\n<function=' + tool_call.name + '>\n' }}
|
| 118 |
+
{%- endif %}
|
| 119 |
+
{%- if tool_call.arguments is defined %}
|
| 120 |
+
{%- for args_name, args_value in tool_call.arguments|items %}
|
| 121 |
+
{{- '<parameter=' + args_name + '>\n' }}
|
| 122 |
+
{%- set args_value = args_value | tojson | safe if args_value is mapping or (args_value is sequence and args_value is not string) else args_value | string %}
|
| 123 |
+
{{- args_value }}
|
| 124 |
+
{{- '\n</parameter>\n' }}
|
| 125 |
+
{%- endfor %}
|
| 126 |
+
{%- endif %}
|
| 127 |
+
{{- '</function>\n</tool_call>' }}
|
| 128 |
+
{%- endfor %}
|
| 129 |
+
{%- endif %}
|
| 130 |
+
{{- '<|im_end|>\n' }}
|
| 131 |
+
{%- elif message.role == "tool" %}
|
| 132 |
+
{%- if loop.previtem and loop.previtem.role != "tool" %}
|
| 133 |
+
{{- '<|im_start|>user' }}
|
| 134 |
+
{%- endif %}
|
| 135 |
+
{{- '\n<tool_response>\n' }}
|
| 136 |
+
{{- content }}
|
| 137 |
+
{{- '\n</tool_response>' }}
|
| 138 |
+
{%- if not loop.last and loop.nextitem.role != "tool" %}
|
| 139 |
+
{{- '<|im_end|>\n' }}
|
| 140 |
+
{%- elif loop.last %}
|
| 141 |
+
{{- '<|im_end|>\n' }}
|
| 142 |
+
{%- endif %}
|
| 143 |
+
{%- else %}
|
| 144 |
+
{{- raise_exception('Unexpected message role.') }}
|
| 145 |
+
{%- endif %}
|
| 146 |
+
{%- endfor %}
|
| 147 |
+
{%- if add_generation_prompt %}
|
| 148 |
+
{{- '<|im_start|>assistant\n' }}
|
| 149 |
+
{%- if enable_thinking is defined and enable_thinking is true %}
|
| 150 |
+
{{- '<think>\n' }}
|
| 151 |
+
{%- else %}
|
| 152 |
+
{{- '<think>\n\n</think>\n\n' }}
|
| 153 |
+
{%- endif %}
|
| 154 |
+
{%- endif %}
|
config.json
ADDED
|
@@ -0,0 +1,75 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen3_5ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_bias": false,
|
| 6 |
+
"attention_dropout": 0.0,
|
| 7 |
+
"attn_output_gate": true,
|
| 8 |
+
"bos_token_id": null,
|
| 9 |
+
"dtype": "bfloat16",
|
| 10 |
+
"eos_token_id": 248046,
|
| 11 |
+
"full_attention_interval": 4,
|
| 12 |
+
"head_dim": 256,
|
| 13 |
+
"hidden_act": "silu",
|
| 14 |
+
"hidden_size": 2048,
|
| 15 |
+
"initializer_range": 0.02,
|
| 16 |
+
"intermediate_size": 6144,
|
| 17 |
+
"layer_types": [
|
| 18 |
+
"linear_attention",
|
| 19 |
+
"linear_attention",
|
| 20 |
+
"linear_attention",
|
| 21 |
+
"full_attention",
|
| 22 |
+
"linear_attention",
|
| 23 |
+
"linear_attention",
|
| 24 |
+
"linear_attention",
|
| 25 |
+
"full_attention",
|
| 26 |
+
"linear_attention",
|
| 27 |
+
"linear_attention",
|
| 28 |
+
"linear_attention",
|
| 29 |
+
"full_attention",
|
| 30 |
+
"linear_attention",
|
| 31 |
+
"linear_attention",
|
| 32 |
+
"linear_attention",
|
| 33 |
+
"full_attention",
|
| 34 |
+
"linear_attention",
|
| 35 |
+
"linear_attention",
|
| 36 |
+
"linear_attention",
|
| 37 |
+
"full_attention",
|
| 38 |
+
"linear_attention",
|
| 39 |
+
"linear_attention",
|
| 40 |
+
"linear_attention",
|
| 41 |
+
"full_attention"
|
| 42 |
+
],
|
| 43 |
+
"linear_conv_kernel_dim": 4,
|
| 44 |
+
"linear_key_head_dim": 128,
|
| 45 |
+
"linear_num_key_heads": 16,
|
| 46 |
+
"linear_num_value_heads": 16,
|
| 47 |
+
"linear_value_head_dim": 128,
|
| 48 |
+
"mamba_ssm_dtype": "float32",
|
| 49 |
+
"max_position_embeddings": 262144,
|
| 50 |
+
"mlp_only_layers": [],
|
| 51 |
+
"model_type": "qwen3_5_text",
|
| 52 |
+
"mtp_num_hidden_layers": 1,
|
| 53 |
+
"mtp_use_dedicated_embeddings": false,
|
| 54 |
+
"num_attention_heads": 8,
|
| 55 |
+
"num_hidden_layers": 24,
|
| 56 |
+
"num_key_value_heads": 2,
|
| 57 |
+
"pad_token_id": 248044,
|
| 58 |
+
"partial_rotary_factor": 0.25,
|
| 59 |
+
"rms_norm_eps": 1e-06,
|
| 60 |
+
"rope_parameters": {
|
| 61 |
+
"mrope_interleaved": true,
|
| 62 |
+
"mrope_section": [
|
| 63 |
+
11,
|
| 64 |
+
11,
|
| 65 |
+
10
|
| 66 |
+
],
|
| 67 |
+
"partial_rotary_factor": 0.25,
|
| 68 |
+
"rope_theta": 10000000,
|
| 69 |
+
"rope_type": "default"
|
| 70 |
+
},
|
| 71 |
+
"tie_word_embeddings": true,
|
| 72 |
+
"transformers_version": "5.3.0",
|
| 73 |
+
"use_cache": false,
|
| 74 |
+
"vocab_size": 248320
|
| 75 |
+
}
|
generation_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"eos_token_id": [
|
| 4 |
+
248046,
|
| 5 |
+
248044
|
| 6 |
+
],
|
| 7 |
+
"pad_token_id": 248044,
|
| 8 |
+
"transformers_version": "5.3.0",
|
| 9 |
+
"use_cache": true
|
| 10 |
+
}
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0ac8fe6e5c61f34e58a1247604e167a91dd5cacd984ff7798d8cebedc2fe9fed
|
| 3 |
+
size 3763692048
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:87a7830d63fcf43bf241c3c5242e96e62dd3fdc29224ca26fed8ea333db72de4
|
| 3 |
+
size 19989343
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"audio_bos_token": "<|audio_start|>",
|
| 4 |
+
"audio_eos_token": "<|audio_end|>",
|
| 5 |
+
"audio_token": "<|audio_pad|>",
|
| 6 |
+
"backend": "tokenizers",
|
| 7 |
+
"bos_token": null,
|
| 8 |
+
"clean_up_tokenization_spaces": false,
|
| 9 |
+
"eos_token": "<|im_end|>",
|
| 10 |
+
"errors": "replace",
|
| 11 |
+
"image_token": "<|image_pad|>",
|
| 12 |
+
"is_local": false,
|
| 13 |
+
"model_max_length": 4096,
|
| 14 |
+
"model_specific_special_tokens": {
|
| 15 |
+
"audio_bos_token": "<|audio_start|>",
|
| 16 |
+
"audio_eos_token": "<|audio_end|>",
|
| 17 |
+
"audio_token": "<|audio_pad|>",
|
| 18 |
+
"image_token": "<|image_pad|>",
|
| 19 |
+
"video_token": "<|video_pad|>",
|
| 20 |
+
"vision_bos_token": "<|vision_start|>",
|
| 21 |
+
"vision_eos_token": "<|vision_end|>"
|
| 22 |
+
},
|
| 23 |
+
"pad_token": "<|endoftext|>",
|
| 24 |
+
"pretokenize_regex": "(?i:'s|'t|'re|'ve|'m|'ll|'d)|[^\\r\\n\\p{L}\\p{N}]?[\\p{L}\\p{M}]+|\\p{N}| ?[^\\s\\p{L}\\p{M}\\p{N}]+[\\r\\n]*|\\s*[\\r\\n]+|\\s+(?!\\S)|\\s+",
|
| 25 |
+
"split_special_tokens": false,
|
| 26 |
+
"tokenizer_class": "TokenizersBackend",
|
| 27 |
+
"unk_token": null,
|
| 28 |
+
"video_token": "<|video_pad|>",
|
| 29 |
+
"vision_bos_token": "<|vision_start|>",
|
| 30 |
+
"vision_eos_token": "<|vision_end|>"
|
| 31 |
+
}
|