Spaces:
Running on Zero
Running on Zero
File size: 15,735 Bytes
d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 d8d0f29 d7b7e82 d8d0f29 d7b7e82 d8d0f29 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 e945472 4d72966 e945472 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 4d72966 d7b7e82 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 | """
Gradio test UI for the SciHigh 2026 highlight generator.
Four output modes share one sentence-ranking model (5 classifiers + reciprocal rank fusion):
Concise the ranked sentences, each compressed with word-importance scores (fast, cannot invent facts)
LLM rewrite a small instruction LLM rewrites the abstract into short highlights, guided by the ranked
sentences; a fact check replaces unsupported highlights with the Concise ones
Fine-tuned a BART model fine-tuned on MixSub (available after running the fine-tuning notebook)
FIRE the tuned extractive output that was submitted (whole sentences)
Everything runs on CPU in the main process; no GPU is requested.
"""
import os, time, tempfile
import numpy as np, pandas as pd
import gradio as gr
from scihigh_model import SciHighExtractor, clean, toks
from generators import LLMRewriter, FineTunedSummarizer, guarded, LLM_ID, FT_ID
try:
# ZeroGPU hardware refuses to start unless one @spaces.GPU function exists. This placeholder satisfies
# that check; it is never called. On CPU hardware the `spaces` package is absent and this is skipped.
import spaces
@spaces.GPU(duration=10)
def _zerogpu_placeholder():
return None
except Exception:
pass
HERE = os.path.dirname(os.path.abspath(__file__))
MODEL = SciHighExtractor.from_pretrained(HERE, encoder=os.environ.get("SCIHIGH_ENCODER")) # optional local encoder path
MODEL.predict(["This warm-up sentence loads the encoder once. It keeps the first request fast. It has no other use."])
CFG, VAL = MODEL.config["decoder"], MODEL.config["validation_scores"]
LLM = LLMRewriter().start() if os.environ.get("SCIHIGH_NO_LLM") != "1" else None
FT = FineTunedSummarizer().start()
MAX_ROWS, MAX_LLM_ROWS = 1000, 20
try:
from rouge_score import rouge_scorer
RS = rouge_scorer.RougeScorer(["rouge1", "rouge2", "rougeL"], use_stemmer=True)
except Exception:
RS = None
M_CONCISE, M_LLM, M_FT, M_FIRE = ("Concise (compressed extractive)", "LLM rewrite (abstractive)",
"Fine-tuned model (abstractive)", "FIRE submission (whole sentences)")
MODES = [M_CONCISE, M_LLM, M_FT, M_FIRE]
EXAMPLES = [
"Large language models have demonstrated remarkable performance in high-resource languages, but their effectiveness in "
"low-resource Indian languages remains limited due to insufficient training data and linguistic diversity. This study "
"investigates parameter-efficient adaptation techniques for improving sentiment classification in low-resource languages "
"using limited annotated datasets. We evaluate cross-lingual transfer, adapter-based fine-tuning, and data augmentation "
"strategies to identify approaches that improve performance while reducing computational requirements.",
"Automated hate speech detection systems frequently struggle to provide reliable explanations for their predictions, "
"particularly when processing multilingual and code-mixed text. This study proposes an explainability-driven framework "
"that combines transformer-based classification with token-level attribution methods to analyze model decisions. The "
"research evaluates whether explanation consistency and faithfulness can be improved without compromising classification "
"performance.",
"Lithium-ion battery degradation limits the service life of electric vehicles . Accurate estimation of remaining useful life "
"remains difficult because degradation depends on temperature , load and charging habits . We propose a hybrid framework "
"that couples an equivalent circuit model with a temporal convolutional network . The physical model supplies interpretable "
"health indicators , while the network learns the residual degradation trend . Experiments on 124 commercial cells show that "
"the framework reduces prediction error by 27 % compared with purely data-driven baselines . The method also requires only "
"the first 100 cycles of data to reach stable predictions . The proposed approach can support early warranty decisions and "
"second-life battery grading .",
"Early detection of diabetic retinopathy can prevent vision loss , but specialist screening is scarce in rural clinics . "
"The aim of this study was to evaluate a lightweight deep learning model that runs on a smartphone . A total of 8,412 fundus "
"images from four hospitals were used for training and external validation . The model detected referable retinopathy with "
"a sensitivity of 93.1 % and a specificity of 90.4 % . Inference took less than one second per image on a mid-range phone "
"without internet access . Performance was consistent across camera types and patient age groups . Smartphone-based "
"screening may therefore extend retinopathy screening to underserved populations .",
]
def bullets(hs):
return "\n".join(f"- {h.rstrip('.')}." for h in hs)
def _wait(model, name, seconds):
if model is None:
raise gr.Error(f"{name} is switched off on this Space.")
if not model.ready(wait=seconds):
if model.error:
return model.error
raise gr.Error(f"The {name} is still loading (first start downloads it). Please try again in a minute.")
return None
def run_single(abstract, mode, k, words, budget, mmr):
if not abstract or len(abstract.split()) < 10:
raise gr.Error("Please paste an abstract of at least a few sentences.")
t0 = time.time()
k, words = int(k), int(words)
r = MODEL.analyse(abstract, target=words, maxs=k, budget=int(budget), mmr=float(mmr))
concise_hl, checks, note = r["concise"], None, ""
if mode == M_FIRE:
hs = [s.strip() for s in r["fire"].rstrip(".").split(". ") if s.strip()]
elif mode == M_CONCISE:
hs = concise_hl
elif mode == M_LLM:
err = _wait(LLM, "LLM", 240)
if err:
hs, note = concise_hl, f"LLM unavailable ({err}); showing Concise output."
else:
gen, _ = LLM.generate(abstract, [r["sentences"][i] for i in r["picks"]], k=k, max_words=words)
rows = guarded(gen, concise_hl, clean(abstract))
hs = [h for h, _, _ in rows]
checks = pd.DataFrame(rows, columns=["highlight", "source", "fact-check note"])
else:
err = _wait(FT, "fine-tuned model", 60)
if err:
hs, note = concise_hl, ("The fine-tuned model is not available yet: run SciHigh_2026_Abstractive_FineTune.ipynb "
f"on Kaggle (it publishes {FT_ID}). Showing Concise output instead.")
else:
gen, _ = FT.generate(clean(abstract), k=k)
rows = guarded(gen, concise_hl, clean(abstract))
hs = [h for h, _, _ in rows]
checks = pd.DataFrame(rows, columns=["highlight", "source", "fact-check note"])
text = " ".join(h.rstrip(".") + "." for h in hs)
aw = len(toks(clean(abstract))); ow = len(toks(text))
info = (f"Mode: {mode}. {len(r['sentences'])} sentences found, {len(hs)} highlights, {ow} words "
f"({100 * ow / max(aw, 1):.0f}% of the abstract's {aw}). Time: {time.time() - t0:.1f} s. {note}")
t = r["table"]
main = t[["#", "picked", "rank", "sentence", "summary", "keywords"]]
scores = t[["#", "rank", "RRF"] + MODEL.config["ensemble"]]
return (bullets(hs), text, info, main, scores,
gr.Dataframe(value=checks, visible=checks is not None))
def run_batch(file, mode, k, words, budget, mmr):
if file is None:
raise gr.Error("Upload a CSV file first.")
path = file if isinstance(file, str) else file.name
df = pd.read_csv(path)
col = next((c for c in df.columns if c.lower() == "abstract"), None)
if col is None:
raise gr.Error(f"No 'Abstract' column found. Columns in the file: {list(df.columns)}")
limit = MAX_LLM_ROWS if mode == M_LLM else MAX_ROWS
note = ""
if len(df) > limit:
note = f"Only the first {limit} of {len(df)} rows were processed in this mode. "
df = df.head(limit)
A = df[col].astype(str).tolist()
kw = dict(maxs=int(k), budget=int(budget), mmr=float(mmr))
if mode == M_FIRE:
preds = MODEL.predict(A, **kw)
elif mode == M_CONCISE:
preds = MODEL.concise(A, target=int(words), **kw)
else:
preds = []
for a in A:
text = run_single(a, mode, k, words, budget, mmr)[1]
preds.append(text)
out = pd.DataFrame({"Filename": df["Filename"] if "Filename" in df.columns else np.arange(len(df)), "Highlights": preds})
gold = next((c for c in df.columns if c.lower() == "highlights"), None)
aw = np.array([len(toks(clean(a))) for a in A]); ow = np.array([len(toks(p)) for p in preds])
note += f"Mode: {mode}. Output length: {ow.mean():.0f} words on average ({100 * np.mean(ow / np.maximum(aw, 1)):.0f}% of the abstract). "
if gold is not None and RS is not None:
sc = {k2: [] for k2 in ["rouge1", "rouge2", "rougeL"]}
for p, g in zip(preds, df[gold].astype(str).map(clean)):
s = RS.score(g, p)
for k2 in sc: sc[k2].append(s[k2].fmeasure)
note += "ROUGE against the file's Highlights column: " + ", ".join(f"{k2.upper()} {100 * np.mean(v):.2f}" for k2, v in sc.items()) + "."
tmp = os.path.join(tempfile.mkdtemp(), "highlights.csv")
out.to_csv(tmp, index=False)
return out.head(50), tmp, note
ABOUT = f"""
### Output modes
| Mode | What it does | Can it invent facts? | Validation ROUGE-L | Avg. words |
|---|---|---|---|---|
| FIRE submission | Top sentences under a {CFG["budget"]}-word budget, copied with reporting phrases removed | No | {VAL["rougeL"]} | 64 |
| Concise | Same sentences, each compressed to about 18 words using word importance | No | 24.28 | 50 |
| LLM rewrite | `{LLM_ID}` rewrites the abstract into short highlights, guided by the ranked sentences | Checked: unsupported highlights are replaced | 20.20 on a 20-abstract sample* | about 50 |
| Fine-tuned model | `{FT_ID}` (BART fine-tuned on the 10,000 MixSub pairs) writes the highlights | Checked, as above | see the notebook | about 50 |
Author highlights in MixSub average 52 words (about 4 highlights of 12 words each).
*On the same 20 random validation abstracts, FIRE mode scored 21.83 and Concise 21.24, so the LLM rewrite costs about 1.6 ROUGE-L points against copying: paraphrased wording overlaps less with the authors' own words even when the meaning is kept. The LLM takes about 15-20 s per abstract on the Space's CPU.
### How sentences are ranked
Each sentence gets 67 features: position, length, lexical density, discourse cues, TF-IDF centrality, TextRank and
`all-MiniLM-L6-v2` embedding features (including retrieval from the 10 most similar training abstracts). Five classifiers
({", ".join(MODEL.config["ensemble"])}) score every sentence and their rankings are merged by Reciprocal Rank Fusion.
### How a sentence is compressed (Concise mode and the "summary" column)
1. Reporting frames are removed: "This study investigates X" becomes "Investigates X"; "X have shown A, but their B remains limited" becomes "X' B remains limited".
2. The sentence is cut into clauses and phrases. Each word is weighted by how prominent it is in this abstract
(term frequency x IDF from 61,775 training sentences) plus a highlight lexicon learned from the training data
(words that appear more often in author highlights than in abstracts, such as "inhibits" or "enhances"). Numbers count extra.
3. The least important phrases are dropped until the highlight is about 18 words; the main clause and phrases with results are kept.
Extra word-importance features (TF-IDF keyword coverage, BM25, character n-grams, bigram centrality, highlight log-odds) were
also tested inside the ranking model: validation ROUGE-L 24.77-24.80 against 24.76, within noise, so the ranker was left unchanged.
### Fact check (LLM and fine-tuned modes)
A generated highlight is rejected if it contains a number, name or acronym that is not in the abstract, or if more than a third of its
content words do not occur in it. A rejected highlight is replaced by the Concise highlight for the same position; the table under the
output shows which ones were replaced and why.
"""
with gr.Blocks(title="SciHigh 2026 Highlight Generator") as demo:
gr.Markdown("# SciHigh 2026: Research Highlight Generator\n"
"FIRE 2026 SciHigh, Subtask 1. Paste a scientific abstract and choose how the highlights are written. "
"**Concise** compresses the key sentences (fast, no invented facts); **LLM rewrite** paraphrases them "
"(slower, fact-checked); **FIRE submission** returns whole sentences as scored in the shared task.")
with gr.Row():
mode = gr.Radio(MODES, value=M_CONCISE, label="Output mode")
with gr.Accordion("Settings", open=False):
with gr.Row():
k = gr.Slider(1, 7, value=3, step=1, label="Number of highlights")
words = gr.Slider(8, 30, value=18, step=1, label="Words per highlight (Concise / LLM)")
budget = gr.Slider(40, 250, value=CFG["budget"], step=10, label="Word budget for sentence choice")
mmr = gr.Slider(0.5, 1.0, value=CFG["mmr"], step=0.05, label="Redundancy threshold (1.0 = off)")
with gr.Tab("Single abstract"):
with gr.Row():
with gr.Column():
abstract = gr.Textbox(label="Abstract", lines=12, placeholder="Paste an abstract here ...")
with gr.Row():
clear = gr.Button("Clear")
go = gr.Button("Generate highlights", variant="primary")
gr.Examples(EXAMPLES, inputs=abstract, label="Example abstracts (written for this demo)")
with gr.Column():
gr.Markdown("### Highlights")
out_md = gr.Markdown()
out_txt = gr.Textbox(label="Highlights as one string (submission format)", lines=4)
info = gr.Markdown()
checks = gr.Dataframe(label="Fact check of the generated highlights", wrap=True, visible=False)
table = gr.Dataframe(label="Every sentence: fused rank (1 = best), a compressed summary and its most important words",
wrap=True, column_widths=["4%", "6%", "5%", "40%", "32%", "13%"])
with gr.Accordion("Model scores for each sentence (probabilities from the five classifiers, fused by RRF)", open=False):
scores = gr.Dataframe(wrap=True)
go.click(run_single, [abstract, mode, k, words, budget, mmr], [out_md, out_txt, info, table, scores, checks])
clear.click(lambda: ("", "", "", "", None, None, gr.Dataframe(value=None, visible=False)), None,
[abstract, out_md, out_txt, info, table, scores, checks])
with gr.Tab("Batch (CSV)"):
gr.Markdown(f"Upload a CSV with an `Abstract` column (for example the MixSub validation or test file). If it also has a "
f"`Highlights` column, ROUGE scores are computed. Up to {MAX_ROWS} rows ({MAX_LLM_ROWS} in LLM mode).")
with gr.Row():
csv_in = gr.File(label="CSV file", file_types=[".csv"])
with gr.Column():
run_b = gr.Button("Run on file", variant="primary")
note = gr.Markdown()
csv_out = gr.File(label="Download highlights.csv")
preview = gr.Dataframe(label="First 50 predictions", wrap=True)
run_b.click(run_batch, [csv_in, mode, k, words, budget, mmr], [preview, csv_out, note])
with gr.Tab("About"):
gr.Markdown(ABOUT)
if __name__ == "__main__":
demo.queue().launch()
|