The problem
Doctors spend a painful amount of time reading EMR notes, discharge summaries, and lab findings. Fatigue is real, and it delays critical decisions. The obvious fix is to paste the text into ChatGPT and ask for a summary.
In a clinical setting, that fix is dangerous.
Generative LLMs paraphrase. If a model hallucinates a symptom, invents a timeline, or subtly changes a dosage, it could cost a life.
Extractive, not generative
The engine is a smart highlighter, not a writer. It scores every sentence by clinical salience and returns the most important ones verbatim. No new tokens are generated. What the doctor reads is exactly what was written, filtered to what matters.
Scoring is powered by fine-tuned SciBERT, pre-trained on biomedical literature, and served as a FastAPI inference API on Hugging Face Spaces.
Phase 1: the lazy baseline
An unsupervised K-Means pipeline on raw SciBERT embeddings, picking each cluster's centroid as a summary sentence. ROUGE-1: 0.2631. Fine for a baseline, useless for production.
K-Means groups by distance in embedding space, not clinical importance. A sentence about parking could cluster with a diagnosis just because their embeddings were close.
Phase 2: the labels didn't exist, so I built them
This is where most people give up.
The ccdv/pubmed-summarization dataset only had human-written abstracts, not sentence-level labels. So I wrote a greedy matching algorithm. For each record it measured how much ROUGE would rise if a source sentence were added to the candidate summary, found the combination that best reconstructed the abstract, and labeled each sentence 1 or 0. Run across 200 records, that produced a real supervised dataset.
Labeling strategy matters more than model architecture. Without greedy matching there was no training signal, and no amount of fine-tuning would have helped.
Phase 3: BERTsum and the head swap
Plain SciBERT gives one embedding per document, which is useless for sentence scoring. I implemented BERTsum: [CLS] and [SEP] tokens around every sentence, interval segment embeddings to separate neighbors, and a custom PyTorch classification head (BioExtractor) in place of the language-model head.
Fine-tuned on a Tesla T4 in Colab, ROUGE-1 went from 0.2631 to 0.3782, a 43.75% improvement. The model learned to prioritize diagnoses, outcomes, and dosages over filler.
Phase 4: the deployment nobody warns you about
A custom PyTorch architecture can't be loaded with AutoModel.from_pretrained(). I saved the fine-tuned weights as a .pt file, shipped it to the Space, and wrote a lazy loader that injects them into the SciBERT skeleton at boot. Get the loading order wrong and the model runs but outputs nonsense, the silent failure I dreaded most.
The one-line safety feature
Before returning the top sentences, I sort them back into their original order. In medicine the timeline is everything. A summary that puts treatment before diagnosis creates dangerous confusion. One sort() call turned out to be the most clinically important line in the project.
The most important engineering decisions aren't always the hardest. Sometimes safety is one line.
What I learned
Knowing when not to use generative AI is worth more than knowing how to prompt it. The extractive decision was the whole project. The architecture followed from it.
If your data isn't labeled for your actual task, you don't have a training problem, you have a labeling problem.
What I owned
- A K-Means baseline on 768-dimensional SciBERT embeddings (ROUGE-1 0.2631) showing why context-blind clustering fails clinically
- A greedy matching algorithm that generated extractive labels from an abstractive-only dataset
- BERTsum implementation with per-sentence [CLS]/[SEP] tokens, interval segment embeddings, and a custom BioExtractor head
- Fine-tuning on a Tesla T4 to ROUGE-1 0.3782, a 43.75% improvement
- A FastAPI inference API with lazy-loaded custom weights, deployed on Hugging Face Spaces
- Chronological re-sorting of output sentences to protect clinical timeline integrity