pamessina/T5FactExtractor
0423
1---2license: apache-2.03library_name: transformers4tags:5 - medical6 - radiology7 - chest-x-ray8 - text-generation9 - t510 - fact-extraction11base_model: t5-small12pipeline_tag: text-generation13---14 15# T5FactExtractor — Radiology Fact Extractor16 17T5FactExtractor is a **T5-small** sequence-to-sequence model that extracts factual statements from chest X-ray radiology report sentences. Given a sentence, it generates a JSON-like list of short clinical facts that can be embedded, compared, or used in metrics such as CXRFEScore.18 19It is stage 1 of the *Extracting and Encoding* framework from Findings of ACL 2024:20 211. **Fact extraction** — this model (`pamessina/T5FactExtractor`)222. **Fact encoding** — [`pamessina/CXRFE`](https://huggingface.co/pamessina/CXRFE)23 24Paper: [*Extracting and Encoding: Leveraging Large Language Models and Medical Knowledge to Enhance Radiological Text Representation*](https://aclanthology.org/2024.findings-acl.236/)25 26## Model details27 28| | |29|---|---|30| **Architecture** | `T5ForConditionalGeneration` |31| **Base model** | [`t5-small`](https://huggingface.co/t5-small) |32| **Task** | Sentence → list of radiology facts |33| **Typical use** | Preprocess report sentences before encoding with CXRFE |34| **License** | Apache 2.0 |35 36## Output format37 38The model generates a string containing a JSON array of fact strings, for example:39 40```text41["small right pleural effusion", "normal heart size"]42```43 44Downstream code (including [`cxrfescore`](https://pypi.org/project/cxrfescore/)) parses that array, deduplicates facts, and lightly cleans repeated words.45 46## How to use47 48### Standalone (Transformers)49 50```python51import re52import json53import torch54from transformers import T5ForConditionalGeneration, T5TokenizerFast55 56device = "cuda" if torch.cuda.is_available() else "cpu"57model_id = "pamessina/T5FactExtractor"58 59tokenizer = T5TokenizerFast.from_pretrained(model_id)60model = T5ForConditionalGeneration.from_pretrained(model_id).to(device)61model.eval()62 63sentence = "There is a small right pleural effusion. The heart size is normal."64# Prefer one sentence at a time (reports are usually sentence-split first).65inputs = tokenizer(sentence, padding="longest", return_tensors="pt")66input_ids = inputs["input_ids"].to(device)67attention_mask = inputs["attention_mask"].to(device)68 69with torch.no_grad():70 output_ids = model.generate(71 input_ids=input_ids,72 attention_mask=attention_mask,73 max_new_tokens=input_ids.shape[1] * 4,74 num_beams=1,75 )76 77raw = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]78print("raw:", raw)79 80# Minimal parse (same idea as cxrfescore.text_utils.parse_facts)81match = re.search(r"\[.*", raw)82if match:83 facts_str = match.group()84 if not facts_str.endswith("]"):85 facts_str += "]"86 facts = json.loads(facts_str)87 print("facts:", facts)88```89 90### Easiest path: CXRFEScore91 92For full reports, the package sentence-splits, runs this extractor, aggregates unique facts, and (optionally) embeds them with CXRFE:93 94```bash95pip install cxrfescore96```97 98```python99from cxrfescore import CXRFEScore100 101metric = CXRFEScore(device="cuda")102reports = [103 "There is a small right pleural effusion. The heart size is normal.",104]105facts_per_report = metric.extract_facts(reports)106print(facts_per_report[0])107```108 109Demo notebook: [CXR-Fact-Encoder / notebooks/cxrfescore_demo.ipynb](https://github.com/PabloMessina/CXR-Fact-Encoder/blob/main/notebooks/cxrfescore_demo.ipynb)110 111## Related resources112 113- Paper hub: https://github.com/PabloMessina/CXR-Fact-Encoder114- Metric package: https://github.com/PabloMessina/CXRFEScore · [PyPI](https://pypi.org/project/cxrfescore/)115- Companion fact encoder: https://huggingface.co/pamessina/CXRFE116- ACL Anthology: https://aclanthology.org/2024.findings-acl.236/117- arXiv: https://arxiv.org/abs/2407.01948118 119## Citation120 121If you use T5FactExtractor, please cite:122 123```bibtex124@inproceedings{messina-etal-2024-extracting,125 title = "Extracting and Encoding: Leveraging Large Language Models and Medical Knowledge to Enhance Radiological Text Representation",126 author = "Messina, Pablo and127 Vidal, Rene and128 Parra, Denis and129 Soto, Alvaro and130 Araujo, Vladimir",131 booktitle = "Findings of the Association for Computational Linguistics: ACL 2024",132 month = aug,133 year = "2024",134 address = "Bangkok, Thailand",135 publisher = "Association for Computational Linguistics",136 url = "https://aclanthology.org/2024.findings-acl.236/",137 doi = "10.18653/v1/2024.findings-acl.236",138 pages = "3955--3986"139}140```141 