Text Generation
Transformers
Safetensors
t5
text2text-generation
medical
radiology
chest-x-ray
fact-extraction
text-generation-inference
Instructions to use pamessina/T5FactExtractor with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use pamessina/T5FactExtractor with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="pamessina/T5FactExtractor")# Load model directly from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("pamessina/T5FactExtractor") model = AutoModelForSeq2SeqLM.from_pretrained("pamessina/T5FactExtractor", device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use pamessina/T5FactExtractor with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "pamessina/T5FactExtractor" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pamessina/T5FactExtractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/pamessina/T5FactExtractor
- SGLang
How to use pamessina/T5FactExtractor with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "pamessina/T5FactExtractor" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pamessina/T5FactExtractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "pamessina/T5FactExtractor" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "pamessina/T5FactExtractor", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use pamessina/T5FactExtractor with Docker Model Runner:
docker model run hf.co/pamessina/T5FactExtractor
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,3 +1,140 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
library_name: transformers
|
| 4 |
+
tags:
|
| 5 |
+
- medical
|
| 6 |
+
- radiology
|
| 7 |
+
- chest-x-ray
|
| 8 |
+
- text-generation
|
| 9 |
+
- t5
|
| 10 |
+
- fact-extraction
|
| 11 |
+
base_model: t5-small
|
| 12 |
+
pipeline_tag: text-generation
|
| 13 |
+
---
|
| 14 |
+
|
| 15 |
+
# T5FactExtractor — Radiology Fact Extractor
|
| 16 |
+
|
| 17 |
+
T5FactExtractor 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 |
+
|
| 19 |
+
It is stage 1 of the *Extracting and Encoding* framework from Findings of ACL 2024:
|
| 20 |
+
|
| 21 |
+
1. **Fact extraction** — this model (`pamessina/T5FactExtractor`)
|
| 22 |
+
2. **Fact encoding** — [`pamessina/CXRFE`](https://huggingface.co/pamessina/CXRFE)
|
| 23 |
+
|
| 24 |
+
Paper: [*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 details
|
| 27 |
+
|
| 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 format
|
| 37 |
+
|
| 38 |
+
The model generates a string containing a JSON array of fact strings, for example:
|
| 39 |
+
|
| 40 |
+
```text
|
| 41 |
+
["small right pleural effusion", "normal heart size"]
|
| 42 |
+
```
|
| 43 |
+
|
| 44 |
+
Downstream code (including [`cxrfescore`](https://pypi.org/project/cxrfescore/)) parses that array, deduplicates facts, and lightly cleans repeated words.
|
| 45 |
+
|
| 46 |
+
## How to use
|
| 47 |
+
|
| 48 |
+
### Standalone (Transformers)
|
| 49 |
+
|
| 50 |
+
```python
|
| 51 |
+
import re
|
| 52 |
+
import json
|
| 53 |
+
import torch
|
| 54 |
+
from transformers import T5ForConditionalGeneration, T5TokenizerFast
|
| 55 |
+
|
| 56 |
+
device = "cuda" if torch.cuda.is_available() else "cpu"
|
| 57 |
+
model_id = "pamessina/T5FactExtractor"
|
| 58 |
+
|
| 59 |
+
tokenizer = T5TokenizerFast.from_pretrained(model_id)
|
| 60 |
+
model = T5ForConditionalGeneration.from_pretrained(model_id).to(device)
|
| 61 |
+
model.eval()
|
| 62 |
+
|
| 63 |
+
sentence = "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).
|
| 65 |
+
inputs = tokenizer(sentence, padding="longest", return_tensors="pt")
|
| 66 |
+
input_ids = inputs["input_ids"].to(device)
|
| 67 |
+
attention_mask = inputs["attention_mask"].to(device)
|
| 68 |
+
|
| 69 |
+
with 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 |
+
|
| 77 |
+
raw = tokenizer.batch_decode(output_ids, skip_special_tokens=True)[0]
|
| 78 |
+
print("raw:", raw)
|
| 79 |
+
|
| 80 |
+
# Minimal parse (same idea as cxrfescore.text_utils.parse_facts)
|
| 81 |
+
match = re.search(r"\[.*", raw)
|
| 82 |
+
if 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: CXRFEScore
|
| 91 |
+
|
| 92 |
+
For full reports, the package sentence-splits, runs this extractor, aggregates unique facts, and (optionally) embeds them with CXRFE:
|
| 93 |
+
|
| 94 |
+
```bash
|
| 95 |
+
pip install cxrfescore
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
```python
|
| 99 |
+
from cxrfescore import CXRFEScore
|
| 100 |
+
|
| 101 |
+
metric = CXRFEScore(device="cuda")
|
| 102 |
+
reports = [
|
| 103 |
+
"There is a small right pleural effusion. The heart size is normal.",
|
| 104 |
+
]
|
| 105 |
+
facts_per_report = metric.extract_facts(reports)
|
| 106 |
+
print(facts_per_report[0])
|
| 107 |
+
```
|
| 108 |
+
|
| 109 |
+
Demo notebook: [CXR-Fact-Encoder / notebooks/cxrfescore_demo.ipynb](https://github.com/PabloMessina/CXR-Fact-Encoder/blob/main/notebooks/cxrfescore_demo.ipynb)
|
| 110 |
+
|
| 111 |
+
## Related resources
|
| 112 |
+
|
| 113 |
+
- Paper hub: https://github.com/PabloMessina/CXR-Fact-Encoder
|
| 114 |
+
- Metric package: https://github.com/PabloMessina/CXRFEScore · [PyPI](https://pypi.org/project/cxrfescore/)
|
| 115 |
+
- Companion fact encoder: https://huggingface.co/pamessina/CXRFE
|
| 116 |
+
- ACL Anthology: https://aclanthology.org/2024.findings-acl.236/
|
| 117 |
+
- arXiv: https://arxiv.org/abs/2407.01948
|
| 118 |
+
|
| 119 |
+
## Citation
|
| 120 |
+
|
| 121 |
+
If you use T5FactExtractor, please cite:
|
| 122 |
+
|
| 123 |
+
```bibtex
|
| 124 |
+
@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 and
|
| 127 |
+
Vidal, Rene and
|
| 128 |
+
Parra, Denis and
|
| 129 |
+
Soto, Alvaro and
|
| 130 |
+
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 |
+
```
|