Upload pasita.py with huggingface_hub
Browse files
pasita.py
CHANGED
|
@@ -549,6 +549,49 @@ def _strip_leading_dot(text: str) -> str:
|
|
| 549 |
return re.sub(r"(?m)^(\s*)\.\s+", r"\1", text)
|
| 550 |
|
| 551 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
def _passthrough(text: str) -> str:
|
| 553 |
lines = [l.rstrip() for l in text.splitlines()]
|
| 554 |
out: list[str] = []
|
|
@@ -571,6 +614,7 @@ def postprocess(raw: str, input_text: str | None = None) -> str:
|
|
| 571 |
original = text
|
| 572 |
text = _unwrap_code_fence(text)
|
| 573 |
text = _strip_prompt_echo(text)
|
|
|
|
| 574 |
text = _cut_input_echo(text, input_text or "")
|
| 575 |
text = _dedup_leading_h1(text)
|
| 576 |
text = _repair_headings(text, input_text or "")
|
|
@@ -584,7 +628,9 @@ def postprocess(raw: str, input_text: str | None = None) -> str:
|
|
| 584 |
text = _cut_repeated_tail_ngram(text, input_text or "")
|
| 585 |
text = _cut_repeated_char_tail(text)
|
| 586 |
text = _strip_leading_dot(text)
|
|
|
|
| 587 |
text = _strip_prompt_echo(text)
|
|
|
|
| 588 |
text = "\n".join(l.rstrip() for l in text.splitlines()).strip()
|
| 589 |
return text if text else original
|
| 590 |
|
|
@@ -1103,6 +1149,7 @@ def structured_candidates(text: str) -> list[str]:
|
|
| 1103 |
|
| 1104 |
|
| 1105 |
LONG_PROMPT_TOKENS = 512
|
|
|
|
| 1106 |
|
| 1107 |
|
| 1108 |
def convert_to_markdown(
|
|
@@ -1118,6 +1165,23 @@ def convert_to_markdown(
|
|
| 1118 |
) -> dict[str, Any]:
|
| 1119 |
if kind == "generic":
|
| 1120 |
kind = infer_kind(text)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1121 |
if kind == "generic" and skip_model_for_structured:
|
| 1122 |
prompt_len = len(tokenizer(build_prompt(text), add_special_tokens=True)["input_ids"])
|
| 1123 |
if prompt_len > LONG_PROMPT_TOKENS:
|
|
|
|
| 549 |
return re.sub(r"(?m)^(\s*)\.\s+", r"\1", text)
|
| 550 |
|
| 551 |
|
| 552 |
+
def _strip_hallucinated_links(text: str, input_text: str) -> str:
|
| 553 |
+
if "http" not in text or not input_text:
|
| 554 |
+
return text
|
| 555 |
+
in_norm = _norm_cmp(input_text)
|
| 556 |
+
|
| 557 |
+
def keep_link(match: re.Match) -> str:
|
| 558 |
+
label, dest = match.group(1), match.group(2)
|
| 559 |
+
if _norm_cmp(dest) and _norm_cmp(dest) in in_norm:
|
| 560 |
+
return match.group(0)
|
| 561 |
+
return label
|
| 562 |
+
|
| 563 |
+
return re.sub(r"\[([^\]\n]+)\]\(([^)\n]+)\)", keep_link, text)
|
| 564 |
+
|
| 565 |
+
|
| 566 |
+
def _strip_dangling_tail(text: str) -> str:
|
| 567 |
+
text = re.sub(r"[\s\[\(]+$", "", text)
|
| 568 |
+
text = re.sub(r"(.)\1{7,}\s*$", "", text)
|
| 569 |
+
return text.rstrip()
|
| 570 |
+
|
| 571 |
+
|
| 572 |
+
_RUBRIC_RE = re.compile(
|
| 573 |
+
r"^\s*(?:\d+[.)]\s+)?(?:hierarchy|structures)\s*:.*$|"
|
| 574 |
+
r"^\s*(?:\d+[.)]\s+)?rule\s*:.*$",
|
| 575 |
+
re.IGNORECASE,
|
| 576 |
+
)
|
| 577 |
+
|
| 578 |
+
|
| 579 |
+
def _strip_rubric_preface(text: str) -> str:
|
| 580 |
+
lines = text.splitlines()
|
| 581 |
+
cut = 0
|
| 582 |
+
for line in lines[:6]:
|
| 583 |
+
if not line.strip() or _RUBRIC_RE.match(line):
|
| 584 |
+
if line.strip():
|
| 585 |
+
cut += 1
|
| 586 |
+
else:
|
| 587 |
+
break
|
| 588 |
+
else:
|
| 589 |
+
break
|
| 590 |
+
if cut:
|
| 591 |
+
return "\n".join(lines[cut:]).strip()
|
| 592 |
+
return text
|
| 593 |
+
|
| 594 |
+
|
| 595 |
def _passthrough(text: str) -> str:
|
| 596 |
lines = [l.rstrip() for l in text.splitlines()]
|
| 597 |
out: list[str] = []
|
|
|
|
| 614 |
original = text
|
| 615 |
text = _unwrap_code_fence(text)
|
| 616 |
text = _strip_prompt_echo(text)
|
| 617 |
+
text = _strip_rubric_preface(text)
|
| 618 |
text = _cut_input_echo(text, input_text or "")
|
| 619 |
text = _dedup_leading_h1(text)
|
| 620 |
text = _repair_headings(text, input_text or "")
|
|
|
|
| 628 |
text = _cut_repeated_tail_ngram(text, input_text or "")
|
| 629 |
text = _cut_repeated_char_tail(text)
|
| 630 |
text = _strip_leading_dot(text)
|
| 631 |
+
text = _strip_hallucinated_links(text, input_text or "")
|
| 632 |
text = _strip_prompt_echo(text)
|
| 633 |
+
text = _strip_dangling_tail(text)
|
| 634 |
text = "\n".join(l.rstrip() for l in text.splitlines()).strip()
|
| 635 |
return text if text else original
|
| 636 |
|
|
|
|
| 1149 |
|
| 1150 |
|
| 1151 |
LONG_PROMPT_TOKENS = 512
|
| 1152 |
+
SHORT_INPUT_CHARS = 150
|
| 1153 |
|
| 1154 |
|
| 1155 |
def convert_to_markdown(
|
|
|
|
| 1165 |
) -> dict[str, Any]:
|
| 1166 |
if kind == "generic":
|
| 1167 |
kind = infer_kind(text)
|
| 1168 |
+
if kind == "generic" and len(text.strip()) < SHORT_INPUT_CHARS:
|
| 1169 |
+
chosen = _passthrough(text)
|
| 1170 |
+
metrics = score_text(text, chosen, must, kind)
|
| 1171 |
+
return {
|
| 1172 |
+
"markdown": chosen,
|
| 1173 |
+
"candidate_index": 0,
|
| 1174 |
+
"fallback_used": False,
|
| 1175 |
+
"route": "passthrough",
|
| 1176 |
+
"metrics": metrics,
|
| 1177 |
+
"candidate_metrics": [metrics],
|
| 1178 |
+
"candidates": [chosen],
|
| 1179 |
+
"raw_candidates": [],
|
| 1180 |
+
"prompt_tokens": 0,
|
| 1181 |
+
"max_new_tokens": 0,
|
| 1182 |
+
"seconds": 0.0,
|
| 1183 |
+
"strategy": {"mode": "passthrough-short", "best_index": 0, "structured_fallback": structured_fallback, "route": "passthrough"},
|
| 1184 |
+
}
|
| 1185 |
if kind == "generic" and skip_model_for_structured:
|
| 1186 |
prompt_len = len(tokenizer(build_prompt(text), add_special_tokens=True)["input_ids"])
|
| 1187 |
if prompt_len > LONG_PROMPT_TOKENS:
|