pacovalentino commited on
Commit
f451486
·
verified ·
1 Parent(s): 95b30c0

Upload 6 files

Browse files

## Usage example
This example shows how to load the model and tokenizer, apply the NER pipeline to a sample text, and print the extracted entities.

```python
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline

path_model = "./Text2NER"

tokenizer = AutoTokenizer.from_pretrained(path_model)
model = AutoModelForTokenClassification.from_pretrained(path_model)
model.eval()

ner_pipeline = pipeline(
"ner",
model=model,
tokenizer=tokenizer,
aggregation_strategy="simple",
device=0 if torch.cuda.is_available() else -1
)

text = """
In Via Verdi a Parma, il paziente Mario Rossi, maschio, 58 anni, presentava dolore toracico con SpO₂ 91%,
PA 160/95 mmHg, FC 112 bpm; codice uscita Rosso, rientro 2, sul posto la Croce Rossa Italiana di Parma, autista Bianchi Luca,
medico Dott. Verdi Andrea.
"""

results = ner_pipeline(text)

print(f"{'ENTITÀ':<40} | {'LABEL'}")
print("-" * 60)
for r in results:
entity = r["word"]
label = r["entity_group"]
print(f"{entity:<40} | {label}")

```

## Expected output

When running the code above, the model extracts the following entities:

ENTITY | LABEL
------------------------------------------------------------
Via Verdi in Parma | LUOGO_INTERVENTO
Mario Rossi | NOME_COGNOME
male | SESSO
58 years old | DATA_NASCITA
SpO₂ 91% | SpO2
PA 160/95 mmHg | PA_MMHG
FC 112 bpm | FC_BPM
Red | CODICE_USCITA
2 | CODICE_RIENTRO
Italian Red Cross of Parma | CRI
Bianchi Luca | AUTISTA
Dr. Verdi Andrea | MEDICO

## Description

The code shows step by step how to initialize the tokenizer and model, create the NER pipeline with aggregation, apply it to a text example, and print the entities in a clear tabular format. The table represents the entities automatically recognized by the model with their corresponding labels, suitable for structured analysis of emergency medical records.


## Esempio di utilizzo

Questo esempio mostra come caricare il modello e il tokenizer, applicare la pipeline NER a un testo di esempio e stampare le entità estratte.

```python
import torch
from transformers import AutoTokenizer, AutoModelForTokenClassification, pipeline

path_model = "./Text2NER"

tokenizer = AutoTokenizer.from_pretrained(path_model)
model = AutoModelForTokenClassification.from_pretrained(path_model)
model.eval()

ner_pipeline = pipeline(
"ner",
model=model,
tokenizer=tokenizer,
aggregation_strategy="simple",
device=0 if torch.cuda.is_available() else -1
)

text = """
In Via Verdi a Parma, il paziente Mario Rossi, maschio, 58 anni, presentava dolore toracico con SpO₂ 91%,
PA 160/95 mmHg, FC 112 bpm; codice uscita Rosso, rientro 2, sul posto la Croce Rossa Italiana di Parma, autista Bianchi Luca,
medico Dott. Verdi Andrea.
"""

results = ner_pipeline(text)

print(f"{'ENTITÀ':<40} | {'LABEL'}")
print("-" * 60)
for r in results:
entity = r["word"]
label = r["entity_group"]
print(f"{entity:<40} | {label}")
```
## Output atteso

Eseguendo il codice sopra, il modello estrae le seguenti entità:

ENTITÀ | LABEL

Via Verdi a Parma | LUOGO_INTERVENTO
Mario Rossi | NOME_COGNOME
maschio | SESSO
58 anni | DATA_NASCITA
SpO₂ 91% | SpO2
PA 160/95 mmHg | PA_MMHG
FC 112 bpm | FC_BPM
Rosso | CODICE_USCITA
2 | CODICE_RIENTRO
Croce Rossa Italiana di Parma | CRI
Bianchi Luca | AUTISTA
Dott. Verdi Andrea | MEDICO

## Descrizione

Il codice mostra passo passo come inizializzare il tokenizer e il modello, creare la pipeline NER con aggregazione, applicarla a un testo di esempio e stampare le entità in formato tabellare chiaro. La tabella rappresenta le entità automaticamente riconosciute dal modello con le rispettive label, utile per analisi strutturate delle schede emergenziali del servizio 118.

config.json ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "architectures": [
3
+ "BertForTokenClassification"
4
+ ],
5
+ "attention_probs_dropout_prob": 0.1,
6
+ "classifier_dropout": null,
7
+ "hidden_act": "gelu",
8
+ "hidden_dropout_prob": 0.1,
9
+ "hidden_size": 768,
10
+ "id2label": {
11
+ "0": "O",
12
+ "1": "AUTISTA",
13
+ "10": "CUTE",
14
+ "11": "DATA_CHIAMATA",
15
+ "12": "DATA_NASCITA",
16
+ "13": "DECESSO",
17
+ "14": "DICHIARANTE_DATI",
18
+ "15": "FC_BPM",
19
+ "16": "GCS_APERTURA_OCCHI",
20
+ "17": "GCS_RISPOSTA_MOTORIA",
21
+ "18": "GCS_RISPOSTA_VERBALE",
22
+ "19": "GLICEMIA",
23
+ "2": "AUTORITA_PRESENTE",
24
+ "20": "INFERMIERE_PROFESSIONALE",
25
+ "21": "LESIONE",
26
+ "22": "LUOGO_INTERVENTO",
27
+ "23": "LUOGO_NASCITA",
28
+ "24": "MEDICINA",
29
+ "25": "MEDICO",
30
+ "26": "NOME_COGNOME",
31
+ "27": "NUMERO_RESIDENZA",
32
+ "28": "NUMERO_TELEFONO",
33
+ "29": "ORA_CHIAMATA",
34
+ "3": "CAUSA_TRASPORTO_NON_EFFETTUATO",
35
+ "30": "ORA_DECESSO",
36
+ "31": "ORA_IN_PRONTO_SOCCORSO",
37
+ "32": "ORA_LIBERO_OPERATIVO",
38
+ "33": "ORA_PARTENZA",
39
+ "34": "ORA_PARTENZA_POSTO",
40
+ "35": "ORA_SUL_POSTO",
41
+ "36": "PARTE_CORPO",
42
+ "37": "PA_MMHG",
43
+ "38": "PROVINCIA_RESIDENZA",
44
+ "39": "PROVVEDIMENTI_ALTRO",
45
+ "4": "CODICE_RIENTRO",
46
+ "40": "PROVVEDIMENTI_CIRCOLO",
47
+ "41": "PROVVEDIMENTI_IMMOBILIZZAZIONE",
48
+ "42": "PROVVEDIMENTI_RESPIRO",
49
+ "43": "PUPILLE_REATTIVITA",
50
+ "44": "PUPILLE_TIPO_DX",
51
+ "45": "PUPILLE_TIPO_SX",
52
+ "46": "RESPIRO",
53
+ "47": "RIFIUTO",
54
+ "48": "SEL",
55
+ "49": "SESSO",
56
+ "5": "CODICE_USCITA",
57
+ "50": "SOCCORRITORE_1",
58
+ "51": "SOCCORRITORE_2",
59
+ "52": "SOCCORRITORE_3",
60
+ "53": "SpO2",
61
+ "54": "TEMPERATURA",
62
+ "55": "VIA_RESIDENZA",
63
+ "6": "COMUNE_RESIDENZA",
64
+ "7": "CONDIZIONE_RIFERITA",
65
+ "8": "COSCIENZA",
66
+ "9": "CRI"
67
+ },
68
+ "initializer_range": 0.02,
69
+ "intermediate_size": 3072,
70
+ "label2id": {
71
+ "AUTISTA": 1,
72
+ "AUTORITA_PRESENTE": 2,
73
+ "CAUSA_TRASPORTO_NON_EFFETTUATO": 3,
74
+ "CODICE_RIENTRO": 4,
75
+ "CODICE_USCITA": 5,
76
+ "COMUNE_RESIDENZA": 6,
77
+ "CONDIZIONE_RIFERITA": 7,
78
+ "COSCIENZA": 8,
79
+ "CRI": 9,
80
+ "CUTE": 10,
81
+ "DATA_CHIAMATA": 11,
82
+ "DATA_NASCITA": 12,
83
+ "DECESSO": 13,
84
+ "DICHIARANTE_DATI": 14,
85
+ "FC_BPM": 15,
86
+ "GCS_APERTURA_OCCHI": 16,
87
+ "GCS_RISPOSTA_MOTORIA": 17,
88
+ "GCS_RISPOSTA_VERBALE": 18,
89
+ "GLICEMIA": 19,
90
+ "INFERMIERE_PROFESSIONALE": 20,
91
+ "LESIONE": 21,
92
+ "LUOGO_INTERVENTO": 22,
93
+ "LUOGO_NASCITA": 23,
94
+ "MEDICINA": 24,
95
+ "MEDICO": 25,
96
+ "NOME_COGNOME": 26,
97
+ "NUMERO_RESIDENZA": 27,
98
+ "NUMERO_TELEFONO": 28,
99
+ "O": 0,
100
+ "ORA_CHIAMATA": 29,
101
+ "ORA_DECESSO": 30,
102
+ "ORA_IN_PRONTO_SOCCORSO": 31,
103
+ "ORA_LIBERO_OPERATIVO": 32,
104
+ "ORA_PARTENZA": 33,
105
+ "ORA_PARTENZA_POSTO": 34,
106
+ "ORA_SUL_POSTO": 35,
107
+ "PARTE_CORPO": 36,
108
+ "PA_MMHG": 37,
109
+ "PROVINCIA_RESIDENZA": 38,
110
+ "PROVVEDIMENTI_ALTRO": 39,
111
+ "PROVVEDIMENTI_CIRCOLO": 40,
112
+ "PROVVEDIMENTI_IMMOBILIZZAZIONE": 41,
113
+ "PROVVEDIMENTI_RESPIRO": 42,
114
+ "PUPILLE_REATTIVITA": 43,
115
+ "PUPILLE_TIPO_DX": 44,
116
+ "PUPILLE_TIPO_SX": 45,
117
+ "RESPIRO": 46,
118
+ "RIFIUTO": 47,
119
+ "SEL": 48,
120
+ "SESSO": 49,
121
+ "SOCCORRITORE_1": 50,
122
+ "SOCCORRITORE_2": 51,
123
+ "SOCCORRITORE_3": 52,
124
+ "SpO2": 53,
125
+ "TEMPERATURA": 54,
126
+ "VIA_RESIDENZA": 55
127
+ },
128
+ "layer_norm_eps": 1e-12,
129
+ "max_position_embeddings": 512,
130
+ "model_type": "bert",
131
+ "num_attention_heads": 12,
132
+ "num_hidden_layers": 12,
133
+ "pad_token_id": 0,
134
+ "position_embedding_type": "absolute",
135
+ "torch_dtype": "float32",
136
+ "transformers_version": "4.51.3",
137
+ "type_vocab_size": 2,
138
+ "use_cache": true,
139
+ "vocab_size": 31102
140
+ }
model.safetensors ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:9480495b4bf8a6d797ff74d92a0fdde41e36e85f115b4e262d3b2a70f2509cb1
3
+ size 437543952
special_tokens_map.json ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cls_token": {
3
+ "content": "[CLS]",
4
+ "lstrip": false,
5
+ "normalized": false,
6
+ "rstrip": false,
7
+ "single_word": false
8
+ },
9
+ "mask_token": {
10
+ "content": "[MASK]",
11
+ "lstrip": false,
12
+ "normalized": false,
13
+ "rstrip": false,
14
+ "single_word": false
15
+ },
16
+ "pad_token": {
17
+ "content": "[PAD]",
18
+ "lstrip": false,
19
+ "normalized": false,
20
+ "rstrip": false,
21
+ "single_word": false
22
+ },
23
+ "sep_token": {
24
+ "content": "[SEP]",
25
+ "lstrip": false,
26
+ "normalized": false,
27
+ "rstrip": false,
28
+ "single_word": false
29
+ },
30
+ "unk_token": {
31
+ "content": "[UNK]",
32
+ "lstrip": false,
33
+ "normalized": false,
34
+ "rstrip": false,
35
+ "single_word": false
36
+ }
37
+ }
tokenizer.json ADDED
The diff for this file is too large to render. See raw diff
 
tokenizer_config.json ADDED
@@ -0,0 +1,67 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "added_tokens_decoder": {
3
+ "0": {
4
+ "content": "[PAD]",
5
+ "lstrip": false,
6
+ "normalized": false,
7
+ "rstrip": false,
8
+ "single_word": false,
9
+ "special": true
10
+ },
11
+ "101": {
12
+ "content": "[UNK]",
13
+ "lstrip": false,
14
+ "normalized": false,
15
+ "rstrip": false,
16
+ "single_word": false,
17
+ "special": true
18
+ },
19
+ "102": {
20
+ "content": "[CLS]",
21
+ "lstrip": false,
22
+ "normalized": false,
23
+ "rstrip": false,
24
+ "single_word": false,
25
+ "special": true
26
+ },
27
+ "103": {
28
+ "content": "[SEP]",
29
+ "lstrip": false,
30
+ "normalized": false,
31
+ "rstrip": false,
32
+ "single_word": false,
33
+ "special": true
34
+ },
35
+ "104": {
36
+ "content": "[MASK]",
37
+ "lstrip": false,
38
+ "normalized": false,
39
+ "rstrip": false,
40
+ "single_word": false,
41
+ "special": true
42
+ }
43
+ },
44
+ "clean_up_tokenization_spaces": false,
45
+ "cls_token": "[CLS]",
46
+ "do_basic_tokenize": true,
47
+ "do_lower_case": false,
48
+ "extra_special_tokens": {},
49
+ "mask_token": "[MASK]",
50
+ "max_len": 512,
51
+ "max_length": 128,
52
+ "model_max_length": 512,
53
+ "never_split": null,
54
+ "pad_to_multiple_of": null,
55
+ "pad_token": "[PAD]",
56
+ "pad_token_type_id": 0,
57
+ "padding_side": "right",
58
+ "sep_token": "[SEP]",
59
+ "stride": 0,
60
+ "strip_accents": null,
61
+ "tokenize_chinese_chars": true,
62
+ "tokenizer_class": "BertTokenizer",
63
+ "truncation": true,
64
+ "truncation_side": "right",
65
+ "truncation_strategy": "longest_first",
66
+ "unk_token": "[UNK]"
67
+ }
vocab.txt ADDED
The diff for this file is too large to render. See raw diff