SHK4K commited on
Commit
ef55d62
ยท
verified ยท
1 Parent(s): 9829939

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +215 -54
app.py CHANGED
@@ -1,69 +1,230 @@
 
 
 
 
 
 
 
1
  import gradio as gr
2
- from huggingface_hub import InferenceClient
 
3
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- def respond(
6
- message,
7
- history: list[dict[str, str]],
8
- system_message,
9
- max_tokens,
10
- temperature,
11
- top_p,
12
- hf_token: gr.OAuthToken,
13
- ):
14
- """
15
- For more information on `huggingface_hub` Inference API support, please check the docs: https://huggingface.co/docs/huggingface_hub/v0.22.2/en/guides/inference
16
- """
17
- client = InferenceClient(token=hf_token.token, model="openai/gpt-oss-20b")
18
 
19
- messages = [{"role": "system", "content": system_message}]
20
 
21
- messages.extend(history)
22
 
23
- messages.append({"role": "user", "content": message})
 
 
 
 
 
 
24
 
25
- response = ""
26
 
27
- for message in client.chat_completion(
28
- messages,
29
- max_tokens=max_tokens,
30
- stream=True,
31
- temperature=temperature,
32
- top_p=top_p,
33
- ):
34
- choices = message.choices
35
- token = ""
36
- if len(choices) and choices[0].delta.content:
37
- token = choices[0].delta.content
38
 
39
- response += token
40
- yield response
 
 
 
 
 
 
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  """
44
- For information on how to customize the ChatInterface, peruse the gradio docs: https://www.gradio.app/docs/chatinterface
45
- """
46
- chatbot = gr.ChatInterface(
47
- respond,
48
- additional_inputs=[
49
- gr.Textbox(value="You are a friendly Chatbot.", label="System message"),
50
- gr.Slider(minimum=1, maximum=2048, value=512, step=1, label="Max new tokens"),
51
- gr.Slider(minimum=0.1, maximum=4.0, value=0.7, step=0.1, label="Temperature"),
52
- gr.Slider(
53
- minimum=0.1,
54
- maximum=1.0,
55
- value=0.95,
56
- step=0.05,
57
- label="Top-p (nucleus sampling)",
58
- ),
59
- ],
60
- )
61
-
62
- with gr.Blocks() as demo:
63
- with gr.Sidebar():
64
- gr.LoginButton()
65
- chatbot.render()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
 
68
  if __name__ == "__main__":
69
- demo.launch()
 
 
 
 
1
+ """Hadith splitter: sanad / matn segmentation for SHK4K/hadith-segmentation-bert."""
2
+
3
+ import html
4
+ import os
5
+ import re
6
+ import sys
7
+
8
  import gradio as gr
9
+ import torch
10
+ from transformers import AutoModelForTokenClassification, AutoTokenizer
11
 
12
+ MODEL_ID = os.environ.get("MODEL_ID", "SHK4K/hadith-segmentation-bert")
13
+ # The Hub config still ships LABEL_0..3, so model.config.id2label is useless here.
14
+ SANAD_IDS, MATN_IDS = (0, 1), (2, 3)
15
+ MAX_LEN, STRIDE, BATCH = 512, 128, 8
16
+ # ponytail: hard cap on very long pastes. 64 windows ~= 25k subword tokens, far
17
+ # beyond any single hadith; drop the cap if the demo ever faces books.
18
+ MAX_WINDOWS = 64
19
+ # The model was trained on undiacritized text; feeding it harakat produces
20
+ # garbage, so they are stripped for inference and mapped back for the output.
21
+ _HARAKAT = re.compile("[\u064b-\u0652\u0670\u0640]")
22
 
23
+ EXAMPLES = [
24
+ ['ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุนูŽู…ู’ุฑููˆ ุจู’ู†ู ุฎูŽุงู„ูุฏูุŒ ู‚ูŽุงู„ูŽ ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุงู„ู„ู‘ูŽูŠู’ุซูุŒ ุนูŽู†ู’ ูŠูŽุฒููŠุฏูŽุŒ ุนูŽู†ู’ ุฃูŽุจููŠ ุงู„ู’ุฎูŽูŠู’ุฑูุŒ ุนูŽู†ู’ ุนูŽุจู’ุฏู ุงู„ู„ู‘ูŽู‡ู ุจู’ู†ู ุนูŽู…ู’ุฑููˆ ู€ ุฑุถู‰ ุงู„ู„ู‡ ุนู†ู‡ู…ุง ู€ ุฃูŽู†ู‘ูŽ ุฑูŽุฌูู„ุงู‹ุŒ ุณูŽุฃูŽู„ูŽ ุงู„ู†ู‘ูŽุจููŠู‘ูŽ ุตู„ู‰ ุงู„ู„ู‡ ุนู„ูŠู‡ ูˆุณู„ู… ุฃูŽู‰ู‘ู ุงู„ุฅูุณู’ู„ุงูŽู…ู ุฎูŽูŠู’ุฑูŒ ู‚ูŽุงู„ูŽ \u200f "\u200f ุชูุทู’ุนูู…ู ุงู„ุทู‘ูŽุนูŽุงู…ูŽุŒ ูˆูŽุชูŽู‚ู’ุฑูŽุฃู ุงู„ุณู‘ูŽู„ุงูŽู…ูŽ ุนูŽู„ูŽู‰ ู…ูŽู†ู’ ุนูŽุฑูŽูู’ุชูŽ ูˆูŽู…ูŽู†ู’ ู„ูŽู…ู’ ุชูŽุนู’ุฑููู’ \u200f"\u200f\u200f.\u200f'],
25
+ ['ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุฃูŽุจููˆ ุงู„ู’ูŠูŽู…ูŽุงู†ูุŒ ู‚ูŽุงู„ูŽ ุฃูŽุฎู’ุจูŽุฑูŽู†ูŽุง ุดูุนูŽูŠู’ุจูŒุŒ ู‚ูŽุงู„ูŽ ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุฃูŽุจููˆ ุงู„ุฒู‘ูู†ูŽุงุฏูุŒ ุนูŽู†ู ุงู„ุฃูŽุนู’ุฑูŽุฌูุŒ ุนูŽู†ู’ ุฃูŽุจููŠ ู‡ูุฑูŽูŠู’ุฑูŽุฉูŽ ู€ ุฑุถู‰ ุงู„ู„ู‡ ุนู†ู‡ ู€ ุฃูŽู†ู‘ูŽ ุฑูŽุณููˆู„ูŽ ุงู„ู„ู‘ูŽู‡ู ุตู„ู‰ ุงู„ู„ู‡ ุนู„ูŠู‡ ูˆุณู„ู… ู‚ูŽุงู„ูŽ \u200f "\u200f ููŽูˆูŽุงู„ู‘ูŽุฐููŠ ู†ูŽูู’ุณููŠ ุจููŠูŽุฏูู‡ู ู„ุงูŽ ูŠูุคู’ู…ูู†ู ุฃูŽุญูŽุฏููƒูู…ู’ ุญูŽุชู‘ูŽู‰ ุฃูŽูƒููˆู†ูŽ ุฃูŽุญูŽุจู‘ูŽ ุฅูู„ูŽูŠู’ู‡ู ู…ูู†ู’ ูˆูŽุงู„ูุฏูู‡ู ูˆูŽูˆูŽู„ูŽุฏูู‡ู \u200f"\u200f\u200f.\u200f'],
26
+ ['ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุฃูŽุจููˆ ุงู„ู’ูˆูŽู„ููŠุฏูุŒ ู‚ูŽุงู„ูŽ ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุดูุนู’ุจูŽุฉูุŒ ู‚ูŽุงู„ูŽ ุฃูŽุฎู’ุจูŽุฑูŽู†ููŠ ุนูŽุจู’ุฏู ุงู„ู„ู‘ูŽู‡ู ุจู’ู†ู ุนูŽุจู’ุฏู ุงู„ู„ู‘ูŽู‡ู ุจู’ู†ู ุฌูŽุจู’ุฑูุŒ ู‚ูŽุงู„ูŽ ุณูŽู…ูุนู’ุชู ุฃูŽู†ูŽุณู‹ุงุŒ ุนูŽู†ู ุงู„ู†ู‘ูŽุจููŠู‘ู ุตู„ู‰ ุงู„ู„ู‡ ุนู„ูŠู‡ ูˆุณู„ู… ู‚ูŽุงู„ูŽ \u200f "\u200f ุขูŠูŽุฉู ุงู„ุฅููŠู…ูŽุงู†ู ุญูุจู‘ู ุงู„ุฃูŽู†ู’ุตูŽุงุฑูุŒ ูˆูŽุขูŠูŽุฉู ุงู„ู†ู‘ูููŽุงู‚ู ุจูุบู’ุถู ุงู„ุฃูŽู†ู’ุตูŽุงุฑู \u200f"\u200f\u200f.\u200f'],
27
+ ['ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุงู„ู’ุญูู…ูŽูŠู’ุฏููŠู‘ู ุนูŽุจู’ุฏู ุงู„ู„ู‘ูŽู‡ู ุจู’ู†ู ุงู„ุฒู‘ูุจูŽูŠู’ุฑู ุŒ ู‚ูŽุงู„ูŽ : ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ุณููู’ูŠูŽุงู†ู ุŒ ู‚ูŽุงู„ูŽ : ุญูŽุฏู‘ูŽุซูŽู†ูŽุง ูŠูŽุญู’ูŠูŽู‰ ุจู’ู†ู ุณูŽุนููŠุฏู ุงู„ู’ุฃูŽู†ู’ุตูŽุงุฑููŠู‘ู ุŒ ู‚ูŽุงู„ูŽ : ุฃูŽุฎู’ุจูŽุฑูŽู†ููŠ ู…ูุญูŽู…ู‘ูŽุฏู ุจู’ู†ู ุฅูุจู’ุฑูŽุงู‡ููŠู…ูŽ ุงู„ุชู‘ูŽูŠู’ู…ููŠู‘ู ุŒ ุฃูŽู†ู‘ูŽู‡ู ุณูŽู…ูุนูŽ ุนูŽู„ู’ู‚ูŽู…ูŽุฉูŽ ุจู’ู†ูŽ ูˆูŽู‚ู‘ูŽุงุตู ุงู„ู„ู‘ูŽูŠู’ุซููŠู‘ูŽ ุŒ ูŠูŽู‚ููˆู„ู : ุณูŽู…ูุนู’ุชู ุนูู…ูŽุฑูŽ ุจู’ู†ูŽ ุงู„ู’ุฎูŽุทู‘ูŽุงุจู ุฑูŽุถููŠูŽ ุงู„ู„ู‘ูŽู‡ู ุนูŽู†ู’ู‡ู ุนูŽู„ูŽู‰ ุงู„ู’ู…ูู†ู’ุจูŽุฑูุŒ ู‚ูŽุงู„ูŽ : ุณูŽู…ูุนู’ุชู ุฑูŽุณููˆู„ูŽ ุงู„ู„ู‘ูŽู‡ู ุตูŽู„ู‘ูŽู‰ ุงู„ู„ู‘ูŽู‡ู ุนูŽู„ูŽูŠู’ู‡ู ูˆูŽุณูŽู„ู‘ูŽู…ูŽุŒ ูŠูŽู‚ููˆู„ู : " ุฅูู†ู‘ูŽู…ูŽุง ุงู„ู’ุฃูŽุนู’ู…ูŽุงู„ู ุจูุงู„ู†ู‘ููŠู‘ูŽุงุชูุŒ ูˆูŽุฅูู†ู‘ูŽู…ูŽุง ู„ููƒูู„ู‘ู ุงู…ู’ุฑูุฆู ู…ูŽุง ู†ูŽูˆูŽู‰ุŒ ููŽู…ูŽู†ู’ ูƒูŽุงู†ูŽุชู’ ู‡ูุฌู’ุฑูŽุชูู‡ู ุฅูู„ูŽู‰ ุฏูู†ู’ูŠูŽุง ูŠูุตููŠุจูู‡ูŽุง ุฃูŽูˆู’ ุฅูู„ูŽู‰ ุงู…ู’ุฑูŽุฃูŽุฉู ูŠูŽู†ู’ูƒูุญูู‡ูŽุงุŒ ููŽู‡ูุฌู’ุฑูŽุชูู‡ู ุฅูู„ูŽู‰ ู…ูŽุง ู‡ูŽุงุฌูŽุฑูŽ ุฅูู„ูŽูŠู’ู‡ู'],
28
+ ]
 
 
 
 
 
 
 
29
 
30
+ _model = _tokenizer = None
31
 
 
32
 
33
+ def load():
34
+ global _model, _tokenizer
35
+ if _model is None:
36
+ _tokenizer = AutoTokenizer.from_pretrained(MODEL_ID)
37
+ _model = AutoModelForTokenClassification.from_pretrained(MODEL_ID).eval()
38
+ _model.to("cuda" if torch.cuda.is_available() else "cpu")
39
+ return _tokenizer, _model
40
 
 
41
 
42
+ def segment(text):
43
+ """Return {'sanad', 'matn', 'html', 'note'} for raw hadith text."""
44
+ if not text or not text.strip():
45
+ raise ValueError("empty input")
 
 
 
 
 
 
 
46
 
47
+ # Strip harakat for inference, remembering where each character came from,
48
+ # so the cut can be mapped back onto the original diacritized text.
49
+ norm_chars, origin = [], []
50
+ for i, ch in enumerate(text):
51
+ if not _HARAKAT.match(ch):
52
+ norm_chars.append(ch)
53
+ origin.append(i)
54
+ norm = "".join(norm_chars)
55
 
56
+ tok, model = load()
57
+ enc = tok(
58
+ norm,
59
+ truncation=True,
60
+ max_length=MAX_LEN,
61
+ stride=STRIDE,
62
+ return_overflowing_tokens=True,
63
+ return_offsets_mapping=True,
64
+ return_tensors="pt",
65
+ padding=True,
66
+ )
67
+ offsets = enc.pop("offset_mapping")
68
+ enc.pop("overflow_to_sample_mapping", None)
69
+ windows = min(enc["input_ids"].shape[0], MAX_WINDOWS)
70
 
71
+ preds = []
72
+ with torch.inference_mode():
73
+ for i in range(0, windows, BATCH):
74
+ batch = {k: v[i : i + BATCH].to(model.device) for k, v in enc.items()}
75
+ preds.append(model(**batch).logits.argmax(-1).cpu())
76
+ preds = torch.cat(preds)
77
+
78
+ # Majority vote per word. A word straddling two windows is keyed by its
79
+ # character start, which both windows report identically.
80
+ votes = {}
81
+ for w in range(windows):
82
+ for pos, word_id in enumerate(enc.word_ids(w)):
83
+ if word_id is None:
84
+ continue
85
+ start, end = (int(x) for x in offsets[w][pos])
86
+ if start == end:
87
+ continue
88
+ label = int(preds[w][pos])
89
+ votes.setdefault(start, {}).setdefault(label, 0)
90
+ votes[start][label] += 1
91
+
92
+ # The labels describe a single cut, so take the first matn word as the
93
+ # boundary: sanad + matn always cover the whole input, in order.
94
+ cut = next(
95
+ (s for s in sorted(votes) if max(votes[s], key=votes[s].get) in MATN_IDS),
96
+ len(norm),
97
+ )
98
+ cut = origin[cut] if cut < len(origin) else len(text)
99
+
100
+ note = f"{len(text.split())} words ยท {windows} window(s)"
101
+ if windows < enc["input_ids"].shape[0]:
102
+ note += f" ยท only the first {MAX_WINDOWS} windows were processed"
103
+ if not text[cut:].strip():
104
+ note += " ยท model found no matn boundary"
105
+
106
+ return {
107
+ "sanad": text[:cut].strip(),
108
+ "matn": text[cut:].strip(),
109
+ "html": _highlight(text, cut),
110
+ "note": note,
111
+ }
112
+
113
+
114
+ def _highlight(text, cut):
115
+ escape = html.escape # before wrapping: the text comes from a public textbox
116
+ return (
117
+ '<div class="hadith" dir="rtl">'
118
+ f'<span class="sanad">{escape(text[:cut])}</span>'
119
+ f'<span class="matn">{escape(text[cut:])}</span>'
120
+ "</div>"
121
+ )
122
+
123
+
124
+ def run(text):
125
+ try:
126
+ r = segment(text)
127
+ except ValueError:
128
+ raise gr.Error("ุฃุฏุฎู„ ู†ุต ุงู„ุญุฏูŠุซ ุฃูˆู„ุงู‹ / Please paste a hadith first.")
129
+ return r["html"], r["sanad"], r["matn"], r["note"]
130
+
131
+
132
+ CSS = """
133
+ #hadith-input textarea { direction: rtl; font-size: 1.05rem; line-height: 1.9; }
134
+ .hadith { font-size: 1.15rem; line-height: 2.1; padding: 1rem; border-radius: .5rem;
135
+ background: #fffdf7; color: #1a1a1a; min-height: 3rem; }
136
+ .hadith .sanad { background: #fde68a; }
137
+ .hadith .matn { background: #bbf7d0; }
138
+ @media (prefers-color-scheme: dark) {
139
+ .hadith { background: #1f2937; color: #f3f4f6; }
140
+ .hadith .sanad { background: #92400e; }
141
+ .hadith .matn { background: #166534; }
142
+ }
143
  """
144
+
145
+ with gr.Blocks(title="Hadith Splitter") as demo:
146
+ gr.Markdown(
147
+ "# ูุงุตู„ ุงู„ุญุฏูŠุซ โ€” Hadith Splitter\n"
148
+ "ูŠู‚ุณู… ุงู„ุญุฏูŠุซ ุงู„ุฎุงู… ุฅู„ู‰ **ุณู†ุฏ** ูˆ**ู…ุชู†**. ยท Splits a raw hadith into its "
149
+ "**sanad** (chain of narrators) and **matn** (the saying itself)."
150
+ )
151
+ inp = gr.Textbox(
152
+ label="ุงู„ุญุฏูŠุซ / Hadith",
153
+ lines=8,
154
+ rtl=True,
155
+ elem_id="hadith-input",
156
+ placeholder="ุญุฏุซู†ุง ... ู‚ุงู„ ุฑุณูˆู„ ุงู„ู„ู‡ ุตู„ู‰ ุงู„ู„ู‡ ุนู„ูŠู‡ ูˆุณู„ู… ...",
157
+ )
158
+ with gr.Row():
159
+ btn = gr.Button("ู‚ุณู‘ู… / Segment", variant="primary")
160
+ clear = gr.Button("ู…ุณุญ / Clear")
161
+
162
+ out_html = gr.HTML()
163
+ note = gr.Markdown()
164
+ with gr.Row():
165
+ sanad = gr.Textbox(
166
+ label="ุงู„ุณู†ุฏ / Sanad", lines=6, buttons=["copy"], rtl=True
167
+ )
168
+ matn = gr.Textbox(
169
+ label="ุงู„ู…ุชู† / Matn", lines=6, buttons=["copy"], rtl=True
170
+ )
171
+
172
+ gr.Examples(examples=EXAMPLES, inputs=inp, label="ุฃู…ุซู„ุฉ / Examples")
173
+
174
+ gr.Markdown(
175
+ "> **ู…ู„ุงุญุธุงุช / Notes:** ูŠูุฌุฑูŽู‘ุฏ ุงู„ุชุดูƒูŠู„ ุชู„ู‚ุงุฆูŠู‹ุง ู‚ุจู„ ุงู„ุชุญู„ูŠู„ุŒ ูˆูŠูุนุงุฏ ุงู„ู†ุต ุงู„ุฃุตู„ูŠ "
176
+ "ุจุชุดูƒูŠู„ู‡ ููŠ ุงู„ู…ุฎุฑุฌุงุช ยท diacritics are stripped for inference, your original "
177
+ "text is returned unchanged. "
178
+ "ุงู„ู†ุต ุงู„ุฃุทูˆู„ ู…ู† 512 ุฑู…ุฒู‹ุง ูŠูุนุงู„ุฌ ุจู†ูˆุงูุฐ ู…ุชุฏุงุฎู„ุฉ ยท longer text is processed in "
179
+ "overlapping windows, never truncated. "
180
+ "ุงู„ู†ู…ูˆุฐุฌ: [SHK4K/hadith-segmentation-bert]"
181
+ "(https://huggingface.co/SHK4K/hadith-segmentation-bert)."
182
+ )
183
+
184
+ outputs = [out_html, sanad, matn, note]
185
+ btn.click(run, inp, outputs)
186
+ inp.submit(run, inp, outputs)
187
+ clear.click(lambda: (None, "", "", "", ""), None, [inp] + outputs)
188
+
189
+
190
+ def _selftest():
191
+ sample = (
192
+ "ุญุฏุซู†ุง ุงู„ุญู…ูŠุฏูŠ ุนุจุฏ ุงู„ู„ู‡ ุจู† ุงู„ุฒุจูŠุฑ ุŒ ู‚ุงู„ : ุญุฏุซู†ุง ุณููŠุงู† ุŒ ู‚ุงู„ : ุญุฏุซู†ุง ูŠุญูŠู‰ ุจู† ุณุนูŠุฏ "
193
+ "ุงู„ุฃู†ุตุงุฑูŠ ุŒ ู‚ุงู„ : ุฃุฎุจุฑู†ูŠ ู…ุญู…ุฏ ุจู† ุฅุจุฑุงู‡ูŠู… ุงู„ุชูŠู…ูŠ ุŒ ุฃู†ู‡ ุณู…ุน ุนู„ู‚ู…ุฉ ุจู† ูˆู‚ุงุต ุงู„ู„ูŠุซูŠ ุŒ "
194
+ 'ูŠู‚ูˆู„ : ุณู…ุนุช ุนู…ุฑ ุจู† ุงู„ุฎุทุงุจ ุฑุถูŠ ุงู„ู„ู‡ ุนู†ู‡ ุนู„ู‰ ุงู„ู…ู†ุจุฑุŒ ู‚ุงู„ : ุณู…ุนุช ุฑุณูˆู„ ุงู„ู„ู‡ ุตู„ู‰ ุงู„ู„ู‡ '
195
+ 'ุนู„ูŠู‡ ูˆุณู„ู…ุŒ ูŠู‚ูˆู„ : " ุฅู†ู…ุง ุงู„ุฃุนู…ุงู„ ุจุงู„ู†ูŠุงุชุŒ ูˆุฅู†ู…ุง ู„ูƒู„ ุงู…ุฑุฆ ู…ุง ู†ูˆู‰ุŒ ูู…ู† ูƒุงู†ุช ู‡ุฌุฑุชู‡ '
196
+ "ุฅู„ู‰ ุฏู†ูŠุง ูŠุตูŠุจู‡ุง ุฃูˆ ุฅู„ู‰ ุงู…ุฑุฃุฉ ูŠู†ูƒุญู‡ุงุŒ ูู‡ุฌุฑุชู‡ ุฅู„ู‰ ู…ุง ู‡ุงุฌุฑ ุฅู„ูŠู‡"
197
+ )
198
+ r = segment(sample)
199
+ print("SANAD:", r["sanad"][:100])
200
+ print("MATN:", r["matn"][:100])
201
+ assert r["sanad"].startswith("ุญุฏุซู†ุง ุงู„ุญู…ูŠุฏูŠ"), r["sanad"][:80]
202
+ assert "ุฅู†ู…ุง ุงู„ุฃุนู…ุงู„ ุจุงู„ู†ูŠุงุช" in r["matn"]
203
+ assert sample.strip().startswith(r["sanad"]) and sample.strip().endswith(r["matn"])
204
+
205
+ long_text = " ".join([sample] * 6)
206
+ lr = segment(long_text)
207
+ assert lr["sanad"] and lr["matn"], "long input lost its boundary"
208
+ assert sample.strip()[-40:] in lr["matn"], "tail of a long input was dropped"
209
+
210
+ xss = '<script>alert(1)</script> ' + sample
211
+ assert "<script>" not in segment(xss)["html"], "input was not escaped"
212
+
213
+ # Diacritized input must segment too, and keep its diacritics in the output.
214
+ vocalized = (
215
+ "ุญูŽุฏูŽู‘ุซูŽู†ูŽุง ุฃูŽุจููˆ ุงู„ู’ูˆูŽู„ููŠุฏูุŒ ู‚ูŽุงู„ูŽ ุญูŽุฏูŽู‘ุซูŽู†ูŽุง ุดูุนู’ุจูŽุฉูุŒ ุนูŽู†ู’ ุฃูŽู†ูŽุณูุŒ ู‚ูŽุงู„ูŽ: "
216
+ "ู‚ูŽุงู„ูŽ ุฑูŽุณููˆู„ู ุงู„ู„ูŽู‘ู‡ู ุตูŽู„ูŽู‘ู‰ ุงู„ู„ูŽู‘ู‡ู ุนูŽู„ูŽูŠู’ู‡ู ูˆูŽุณูŽู„ูŽู‘ู…ูŽ: ยซุงู„ุฏูู‘ูŠู†ู ุงู„ู†ูŽู‘ุตููŠุญูŽุฉูยป"
217
+ )
218
+ vr = segment(vocalized)
219
+ print("VOCALIZED SANAD:", vr["sanad"][:80])
220
+ print("VOCALIZED MATN:", vr["matn"][:80])
221
+ assert vr["sanad"].startswith("ุญูŽุฏูŽู‘ุซูŽู†ูŽุง"), "diacritics broke the sanad"
222
+ assert vr["matn"], "diacritized input lost its matn"
223
+ print("selftest ok")
224
 
225
 
226
  if __name__ == "__main__":
227
+ if "--selftest" in sys.argv:
228
+ _selftest()
229
+ else:
230
+ demo.launch(css=CSS)