pamessina commited on
Commit
32696f4
·
verified ·
1 Parent(s): e6e332b

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +140 -3
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
+ ```