Highlight_Generator / generators.py
Akash212's picture
Upload generators.py
a121d73 verified
Raw History Blame Contribute Delete
9.65 kB
"""
Abstractive highlight generators for the demo, both guarded by a fact check.
LLMRewriter a small instruction-tuned LLM (default Qwen/Qwen2.5-1.5B-Instruct, Apache-2.0) rewrites the
abstract into short highlights, guided by the sentences the extractive model picked
FineTunedSummarizer a seq2seq model fine-tuned on MixSub (see SciHigh_2026_Abstractive_FineTune.ipynb),
loaded from the Hugging Face Hub once it has been trained
fact_check() rejects a generated highlight when it contains a number that is not in the abstract, or when
too many of its content words do not occur in the abstract. Rejected highlights are replaced by the
Concise (compressed extractive) highlight for the same position, so the output never contains an
unsupported number.
"""
import os, re, threading, time
import numpy as np
LLM_ID = os.environ.get("SCIHIGH_LLM", "Qwen/Qwen2.5-1.5B-Instruct")
FT_ID = os.environ.get("SCIHIGH_FINETUNED", "Akash212/scihigh-bart-base")
_STOP = set("""a about above after again against all also am among an and any are as at be because been before being
below between both but by can could did do does doing during each few for from further had has have having he her
here him his how i if in into is it its itself more most no nor not now of off on once only or other our out over
own same she should so some such than that the their them then there these they this those through to too under
until up very was we were what when where which while who whom why will with would you your study paper work
research results result findings approach method methods proposed propose novel new using used use based show
shows shown highlight highlights without within across among while whereas however therefore thus including
including across towards toward upon also both either neither like""".split())
_NUM = re.compile(r"\d+(?:\.\d+)?")
_W = re.compile(r"[a-z][a-z\-]+")
try:
from nltk.stem.porter import PorterStemmer
_PS = PorterStemmer()
def _stem(w):
return _PS.stem(w.lower().strip("-"))
except Exception: # crude fallback if nltk is missing
def _stem(w):
w = w.lower().strip("-")
for suf in ("ational", "ization", "isation", "ingly", "edly", "ments", "ment", "ness", "ities", "ity", "ings",
"ing", "ies", "ied", "ers", "er", "ed", "es", "s", "al", "ly", "y"):
if len(w) > len(suf) + 3 and w.endswith(suf):
return w[:-len(suf)]
return w
def fact_check(highlight, source, max_novel=0.34):
"""(ok, reason). A highlight fails if it has a number absent from the source, or if more than
`max_novel` of its content words (compared by stem, hyphenated parts included) are absent from the source."""
src = source.lower()
for n in _NUM.findall(highlight):
if n not in src:
return False, f"number {n} is not in the abstract"
src_stems = {_stem(p) for w in _W.findall(src) for p in [w] + w.split("-")}
# names, datasets and acronyms must come from the abstract (an acronym may abbreviate a phrase in it)
src_words = re.findall(r"[a-z0-9]+", src)
initials = {"".join(x[0] for x in src_words[i:i + n]) for n in range(2, 6) for i in range(len(src_words) - n + 1)}
for i, t in enumerate(re.findall(r"[A-Za-z][A-Za-z0-9\-]*", highlight)):
if i == 0 or not (t[0].isupper() or sum(c.isupper() for c in t) >= 2): continue
tl = t.lower()
if tl in src or _stem(tl) in src_stems: continue
acro = re.sub(r"s$", "", t) if t[:-1].isupper() else t
if acro.isupper() and acro.lower() in initials: continue
return False, f"name or acronym '{t}' is not in the abstract"
words = [w for w in _W.findall(highlight.lower()) if w not in _STOP and len(w) > 3]
if not words:
return True, ""
novel = [w for w in words if _stem(w) not in src_stems and not all(_stem(p) in src_stems for p in w.split("-") if p)]
if len(novel) / len(words) > max_novel:
return False, "words not in the abstract: " + ", ".join(novel[:5])
return True, ""
def parse_bullets(text, k):
out = []
for line in text.splitlines():
line = re.sub(r"^\s*(?:[-*•]|\d+[.)])\s*", "", line).strip()
line = re.sub(r"^(?:highlight\s*\d*\s*[:.-])\s*", "", line, flags=re.I).strip()
line = re.sub(r"^\*\*[^*]{1,40}\*\*\s*:?\s*", "", line).strip() # "**Findings:** ..."
line = re.sub(r"^(?:problem|background|method|methods|approach|objective|aim|findings?|results?|implications?|"
r"conclusions?|contribution)\s*:\s*", "", line, flags=re.I).strip()
line = line.replace("**", "").strip()
if len(line.split()) >= 4 and not line.lower().startswith(("here are", "highlights", "sure")):
out.append(line.rstrip(".") + ".")
return out[:k]
def guarded(generated, fallback, source):
"""Apply fact_check to each generated highlight; replace failures with the fallback highlight in that slot."""
rows = []
for i, h in enumerate(generated):
ok, why = fact_check(h, source)
if ok:
rows.append((h, "generated", ""))
else:
fb = fallback[i] if i < len(fallback) else None
if fb: rows.append((fb.rstrip(".") + ".", "fallback (concise)", why))
if not rows:
rows = [(f.rstrip(".") + ".", "fallback (concise)", "no usable generated highlight") for f in fallback]
return rows
class _Lazy:
"""Loads a model in a background thread so the app starts immediately."""
def __init__(self):
self.model = self.tok = None; self.error = None; self._t = None; self.load_seconds = None
def start(self):
if self._t is None:
self._t = threading.Thread(target=self._safe_load, daemon=True); self._t.start()
return self
def _safe_load(self):
t = time.time()
try: self._load()
except Exception as e: self.error = f"{type(e).__name__}: {e}"
self.load_seconds = round(time.time() - t, 1)
def ready(self, wait=0):
self.start()
if wait: self._t.join(timeout=wait)
return self.model is not None
class LLMRewriter(_Lazy):
def __init__(self, model_id=LLM_ID):
super().__init__(); self.model_id = model_id
def _load(self):
import torch
from transformers import AutoTokenizer, AutoModelForCausalLM
torch.set_num_threads(max(1, os.cpu_count() or 1))
self.tok = AutoTokenizer.from_pretrained(self.model_id)
self.model = AutoModelForCausalLM.from_pretrained(self.model_id, dtype=torch.float32).eval()
def generate(self, abstract, key_sentences, k=3, max_words=20):
import torch
keys = "\n".join(f"- {s}" for s in key_sentences)
prompt = (f"Abstract:\n{abstract.strip()}\n\nMost important sentences (chosen by an extractive model):\n{keys}\n\n"
f"Write exactly {k} research highlights for this abstract.\n"
f"Rules:\n- One highlight per line, starting with \"- \".\n"
f"- Each highlight is one short sentence of at most {max_words} words.\n"
"- Summarise and rephrase; do not copy whole sentences.\n"
"- Cover the problem, the method and the main finding or aim.\n"
"- Use only facts stated in the abstract. Do not add numbers, names, datasets or claims that are not in it.\n"
"- Keep the authors' certainty: if the abstract only aims, proposes or evaluates something, do not state it as a result.\n"
"- Plain sentences only: no title, no introduction, no labels, no bold text, no numbering.")
msgs = [{"role": "system", "content": "You write concise, faithful research highlights for scientific papers."},
{"role": "user", "content": prompt}]
ids = self.tok.apply_chat_template(msgs, add_generation_prompt=True, return_tensors="pt", return_dict=True)
with torch.no_grad():
out = self.model.generate(**ids, max_new_tokens=40 * k, do_sample=False, repetition_penalty=1.05,
pad_token_id=self.tok.eos_token_id)
text = self.tok.decode(out[0][ids["input_ids"].shape[-1]:], skip_special_tokens=True)
return parse_bullets(text, k), text
class FineTunedSummarizer(_Lazy):
def __init__(self, model_id=FT_ID):
super().__init__(); self.model_id = model_id
def _load(self):
import torch
from huggingface_hub import repo_exists
if not os.path.isdir(self.model_id) and not repo_exists(self.model_id):
raise FileNotFoundError(f"{self.model_id} does not exist yet. Train it with the fine-tuning notebook first.")
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
torch.set_num_threads(max(1, os.cpu_count() or 1))
self.tok = AutoTokenizer.from_pretrained(self.model_id)
self.model = AutoModelForSeq2SeqLM.from_pretrained(self.model_id).eval()
def generate(self, abstract, k=None):
import torch
ids = self.tok(abstract, return_tensors="pt", truncation=True, max_length=512)
with torch.no_grad():
out = self.model.generate(**ids, max_new_tokens=128, num_beams=4, no_repeat_ngram_size=3, early_stopping=True)
text = self.tok.decode(out[0], skip_special_tokens=True)
parts = [p.strip() for p in re.split(r"(?<=[a-z0-9\)%])\.\s+", text) if len(p.split()) >= 3]
return [p.rstrip(".") + "." for p in parts][: (k or len(parts))], text