You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Unify model loading and REST APIs and improve CLI (#241)
* unify model loading
* rework args
* rework cli
* consolidate rest apis
* update tests
* update examples
* remove unused kwargs
* typo
* fix import
* fix inconsistencies in readme and CLI docs
* log hyperlink to interactive docs on REST startup
* temporarily disable tlinkx check in temporal test
* allow disjoint labels in generated testing datasets
* fix issue loading config model type
* convert encoder_layer arg when loading legacy models
* re-enable test
* update readme
* fix typo in example notebook
* save tokenizer with model and fall back to encoder tokenizer when loading for REST apps
Copy file name to clipboardExpand all lines: README.md
+33-85Lines changed: 33 additions & 85 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -103,7 +103,7 @@ To use the library for fine-tuning, you'll need to take the following steps:
103
103
104
104
Instance labels should be formatted the same way as in the csv/tsv example above, see specifically the formats for tagging and relations. The'metadata' field can either be included in the train/dev/test files or as a separate metadata.json file.
105
105
106
-
2. Run train_system.pywith a ```--task_name``` from your data files and the ```--data-dir``` argument from Step 1. If no ```--task_name``` is provided, all tasks will be trained.
106
+
2. Run train_system.pywith a `--model-type` (one of`cnn`, `lstm`, `hier`, or `proj`), and a `--data-dir` (path to the folder you created in step 1). Optionally specify one or more `--task` names to train on. By default all tasks will be trained.
107
107
108
108
### Step-by-step finetuning examples
109
109
@@ -115,113 +115,61 @@ We provided the following step-by-step examples how to finetune in clinical NLP
115
115
116
116
### Fine-tuning options
117
117
118
-
Run `cnlpt train -h` to see all the available options. In addition to inherited Huggingface Transformers options, there are options to do the following:
118
+
Run `cnlpt train --help` to see all the available options. In addition to inherited Huggingface Transformers options, there are options to do the following:
119
119
120
120
* Select different models:`--model hier` uses a hierarchical transformer layer on top of a specified encoder model. We recommend using a very small encoder:`--encoder microsoft/xtremedistil-l6-h256-uncased` so that the full model fits into memory.
121
-
* Run simple baselines (use ``--model cnn|lstm --tokenizer_name roberta-base``-- since there is no HF model then you must specify the tokenizer explicitly)
121
+
* Run simple baselines (use `--model cnn|lstm --tokenizer roberta-base`-- since there is no HF model then you must specify the tokenizer explicitly)
122
122
* Use a different layer's CLS token for the classification (e.g., `--layer 10`)
123
123
* Probabilistically freeze weights of the encoder (leaving classifier weights all unfrozen) (`--freeze` alone freezes all encoder weights, `--freeze <float>` when given a parameter between 0 and 1, freezes that percentage of encoder weights)
124
124
* Classify based on a token embedding instead of the CLS embedding (`--token` -- applies to the event/entity classification setting only, and requires the input to have xml-style tags (`<e>`, `</e>`) around the tokens representing the event/entity)
125
125
* Use class-weighted loss function (`--class_weights`)
126
126
127
127
## Running REST APIs
128
128
129
-
There are existing REST APIs in the `src/cnlpt/api` folder for a few important clinical NLP tasks:
129
+
This library supports serving a REST API for your model with a single `/process` endpoint to process text and generate predictions, via the `cnlpt rest` command.
130
130
131
-
1. Negation detection
132
-
2. Time expression tagging (spans + time classes)
133
-
3. Event detection (spans + document creation time relation)
Run `cnlpt rest --help` to see available options. The only required option is `--model`, which must be either a HuggingFace repository or a local directory containing your model. By default, the model will be served at [http://localhost:8000](http://localhost:8000).
135
132
136
-
### Negation API
133
+
For example, to run our negation detection model from HuggingFace:
137
134
138
-
To demo the negation API:
139
-
140
-
1. Install the `cnlp-transformers` package.
141
-
2. Run `cnlpt rest --model-type negation [-p PORT]`.
142
-
3. Open a python console and run the following commands:
143
-
144
-
#### Setup variables for negation
145
-
146
-
```ipython
147
-
>>> import requests
148
-
>>> process_url = 'http://hostname:8000/negation/process' ## Replace hostname with your host name
149
-
```
150
-
151
-
#### Prepare the document
152
-
153
-
```ipython
154
-
>>> sent ='The patient has a sore knee and headache but denies nausea and has no anosmia.'
The model correctly classifies both nausea and anosmia as negated.
169
-
170
-
### Temporal API (End-to-end temporal information extraction)
171
-
172
-
To demo the temporal API:
173
-
174
-
1. Install the `cnlp-transformers` package.
175
-
2. Run `cnlpt rest --model-type temporal [-p PORT]`
176
-
3. Open a python console and run the following commands to test:
177
-
178
-
#### Setup variables for temporal
139
+
Once the application is running, you can either interact with it via web interface at [http://localhost:8000/docs](http://localhost:8000/docs), or manually send requests to the `/process` endpoint:
179
140
180
141
```ipython
181
142
>>> import requests
182
143
>>> from pprint import pprint
183
-
>>> process_url ='http://hostname:8000/temporal/process_sentence' ## Replace hostname with your host name
144
+
>>> sent = "The patient has a sore knee and headache but denies nausea and has no anosmia."
This output indicates the token spans of events and timexes, and relations between events and timexes, where the suffixes are indices into the respective arrays (e.g., TIMEX-0 in a relation refers to the 0th time expression found, which begins at token 6 and ends at token 9 -- ["March 3, 2010"])
224
-
225
173
## Citing cnlp_transformers
226
174
227
175
Please use the following bibtex to cite cnlp_transformers if you use it in a publication:
### Fine-tuning for classification: End-to-end example
1
+
#Drug Review Sentiment Classification
2
2
3
-
1. Download data from [Drug Reviews (Druglib.com) Data Set](https://archive.ics.uci.edu/dataset/461/drug+review+dataset+druglib+com) to `data` folder and extract. Pay attention to their terms:
4
-
1. only use the data for research purposes
5
-
2. don't use the data for any commerical purposes
6
-
3. don't distribute the data to anyone else
7
-
4. cite us
3
+
## Jupyter notebook example
8
4
9
-
2. Run ```python examples/uci_drug/transform_uci_drug.py <raw dir> <processed dir>``` to preprocess the data from the extract directory into a new directory. This will create {train,dev,test}.tsv in the processed directory specified, where the sentiment ratings have been collapsed into 3 categories.
5
+
See the [example notebook](./uci_drug.ipynb) for a step-by-step walkthrough of
6
+
how to use CNLPT to train a model for sentiment classification of drug reviews.
If you run the above command with the `--error_analysis` flag, you can obtain the `dev` instances for which the model made an erroneous
38
-
prediction, organized by their original index in `dev` split, in the `eval_predictions...tsv` file in the `--output_dir` argument.
39
-
For us the first line of this file (after the header) is:
40
-
41
-
```
42
-
text sentiment
43
-
2 Benefits: <cr> helped aleviate whip lash symptoms <cr> Side effects: <cr> none that i noticed <cr> Overall comments: <cr> i took the medications for the prescribed time and symptoms improved, however, I still have some symptoms which are being treated through physical therapy since the accident was only in December Ground: Medium Predicted: High
44
-
45
-
```
46
-
47
-
The number at the beginning of the line, 2, is the index of the instance in the `dev` split. The `text` column contains the text of the erroneous instances and the following columns are the tasks provided to the model, in this case, just `sentiment`. `Ground: Medium Predicted: High` indicates that the provided ground truth label for the instance sentiment is `Medium` but the model predicted `High`.
48
-
49
-
#### Human Readable Predictions for Classification
10
+
If you prefer, you can instead use the CLI to train the model:
50
11
51
-
Similarly if you run the above command with `--do_predict` you can obtain human readable predictions for the `test` split, in the `test_predictions...tsv` file. For us the first line of this file (after the header) is:
52
-
53
-
```
54
-
0 Benefits: <cr> The antibiotic may have destroyed bacteria causing my sinus infection. But it may also have been caused by a virus, so its hard to say. <cr> Side effects: <cr> Some back pain, some nauseau. <cr> Overall comments: <cr> Took the antibiotics for 14 days. Sinus infection was gone after the 6th day. Low
55
-
56
-
```
57
-
58
-
##### Prediction Probability Outputs for Classification
59
-
60
-
(Currently only supported for classification tasks), if you run the above command with the `--output_prob` flag, you can see the model's softmax-obtained probability for the predicted classification label. The first error analysis sample from `dev` would now looks like:
61
-
62
-
```
63
-
text sentiment
64
-
2 Benefits: <cr> helped aleviate whip lash symptoms <cr> Side effects: <cr> none that i noticed <cr> Overall comments: <cr> i took the medications for the prescribed time and symptoms improved, however, I still have some symptoms which are being treated through physical therapy since the accident was only in December Ground: Medium Predicted: High , Probability 0.613825
12
+
### Download and preprocess the data
65
13
14
+
Use the [`prepare_data.py`](./prepare_data.py) script to download the data and convert it to CNLPT's data format:
66
15
16
+
```bash
17
+
uv run prepare_data.py
67
18
```
68
19
69
-
And the first prediction sample from `test` now looks like:
[the RoBERTa base model](https://huggingface.co/FacebookAI/roberta-base)
50
+
with an added projection layer for classification:
70
51
71
-
```
72
-
text sentiment
73
-
0 Benefits: <cr> The antibiotic may have destroyed bacteria causing my sinus infection. But it may also have been caused by a virus, so its hard to say. <cr> Side effects: <cr> Some back pain, some nauseau. <cr> Overall comments: <cr> Took the antibiotics for 14 days. Sinus infection was gone after the 6th day. Low , Probability 0.370522
0 commit comments