Spaces:
Running on Zero
Running on Zero
Download app.py from Akash212/Highlight_Generator: direct link, hf CLI and curl.
- Browser
- Download file 15.7 kB
-
https://huggingface.co/spaces/Akash212/Highlight_Generator/resolve/main/app.py
- Command line
-
hf download hf://spaces/Akash212/Highlight_Generator/app.py
-
curl -L -o app.py https://huggingface.co/spaces/Akash212/Highlight_Generator/resolve/main/app.py
15.7 kB
| """ | |
| 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 | |
| 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() | |