File size: 5,191 Bytes
b9e89c0
 
 
 
 
 
 
 
 
 
 
27e0cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9e89c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
012f4e9
 
b9e89c0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
---
license: mit
language:
- it
base_model:
- unsloth/Qwen2.5-7B-Instruct-bnb-4bit
pipeline_tag: text-generation
library_name: transformers
tags:
- legal
---
# sempl-it-lex-bnb
SEMPL-IT aims to simplify Italian administrative texts using a progressive approach based on multiple specialized models. 
Each model addresses a specific aspect of the text transformation process, ensuring a step-by-step refinement.

## Simplification Pipeline
The complete text simplification pipeline follows eight sequential steps:
1. [proofreading](https://huggingface.co/VerbACxSS/sempl-it-proofreading-bnb)
2. [lex](https://huggingface.co/VerbACxSS/sempl-it-lex-bnb)  (this)
3. [connectives](https://huggingface.co/VerbACxSS/sempl-it-connectives-bnb)
4. [expressions](https://huggingface.co/VerbACxSS/sempl-it-expressions-bnb)
5. [sentence-splitter](https://huggingface.co/VerbACxSS/sempl-it-sentence-splitter-bnb)
6. [nominalizations](https://huggingface.co/VerbACxSS/sempl-it-nominalizations-bnb)
7. [verbs](https://huggingface.co/VerbACxSS/sempl-it-verbs-bnb)
8. [sentence-reorganizer](https://huggingface.co/VerbACxSS/sempl-it-sentence-reorganizer-bnb)

## Web App
To integrate this model into the full system, check out:
- Frontend: [GitHub Repository](https://github.com/VerbACxSS/sempl-it-frontend)
- Backend: [GitHub Repository](https://github.com/VerbACxSS/sempl-it-backend)
- Inference: [GitHub Repository](https://github.com/VerbACxSS/sempl-it-inference)

## Usage
Install the following dependencies:
```sh
pip install transformers==4.49.0
pip install bitsandbytes==0.45.3
pip install peft==0.15.0
```

Define the system prompt and the text to simplify:
```py
PROMPT = """Sei un esperto redattore di documenti istituzionali italiani.

Migliora la leggibilità dei riferimenti normativi presenti in un documento istituzionale. **Non alterare il contenuto e lo stile del testo originale**.

# Steps
1. Leggi attentamente il testo.
2. Individua tutti i riferimenti normativi presenti nel testo.
3. Normalizza tutti i riferimenti normativi nella forma "art. [numero articolo], comma [numero comma], lettera [lettera]) del [norma] [nuemero]/[anno] e successive modifiche".
4. Se la tipologia di normativa è scritta per esteso, utilizza l'acronimo solo per quelle più comuni (es. D.Lgs, D.P.R., L.R., C.C, ...).

# Output Format
Il testo corretto con l'originale formattazione e suddivisione in sezioni e paragrafi.

# Examples
- **Input**: D.Lgs. 08.08.1994, n. 490
  **Output**: D.Lgs. 490/1994
- **Input**: art. 43 D.P.R. 28.12.2000, n. 445
  **Output**: art. 43 D.P.R 445/2000
- **Input**: articolo 16 del d.P.R. 30 dicembre 1982, n. 955 s.m.i.
  **Output**: art. 16 D.P.R 955/1982 e successive modifiche
- **Input**: articolo 16, comma 2, lettera c) L.R. n. 18/08
  **Output**: art. 16, comma 2, lettera c) della L.R. 18/08
- **Input**: articoli 19, 46 e 47 del decreto del Presidente della Repubblica n. 445/2000
  **Output**: artt. 19, 46 e 47 del D.P.R 445/2000

# Notes
- Il testo fornito può essere complesso e richiede attenzione ai dettagli.
- Esegui solamente le operazioni descritte, **non eliminare e non modificare altri contenuti**.
- Assicurati che le implicazioni giuridiche e legali del documento siano mantenute."""

TEXT_TO_SIMPLIFY = """Il documento individua le esigenze di sviluppo necessarie per assicurare che i principi delineati dalla Legge Regionale 23 dicembre 2004, n. 29 e dai successivi atti normativi, sulla essenziale funzione della ricerca e innovazione nelle Aziende Sanitarie della Regione Emilia-Romagna, si traducano in azioni concrete nel Servizio Sanitario Regionale. 

Alla luce delle evidenze della letteratura internazionale, delle indicazioni della normativa nazionale e della valutazione di quanto già attuato a livello regionale negli anni passati, vengono individuati gli obiettivi di sviluppo e le linee per il raggiungimento dei suddetti obiettivi."""
```

Load SEMPL-IT model and tokenizer:
```py
from transformers import AutoModelForCausalLM, AutoTokenizer

tokenizer = AutoTokenizer.from_pretrained("VerbACxSS/sempl-it-lex-bnb")
model = AutoModelForCausalLM.from_pretrained("VerbACxSS/sempl-it-lex-bnb").to("cuda")
```

Define and apply chat template:
```py
chat = [
  {"role": "system", "content": PROMPT},
  {"role": "assistant", "content": TEXT_TO_SIMPLIFY},
]

formatted_chat = tokenizer.apply_chat_template(
    chat,
    tokenize=False,
    add_generation_prompt=True
)
model_inputs = tokenizer([formatted_chat], return_tensors="pt").to("cuda")
```

Use SEMPL_IT model with following sampling parameters to generate `simplified_text`:
```py
generated_ids = model.generate(
    **model_inputs,
    max_new_tokens=4096,
    temperature=0.1,
    top_p=0.2
)
simplified_text = tokenizer.decode(generated_ids[0][len(model_inputs.input_ids[0]):], skip_special_tokens=True)
print(simplified_text)
```

## Acknowledgements
This contribution is a result of the research conducted within the framework of the PRIN 2020 (Progetti di Rilevante Interesse Nazionale) "VerbACxSS: on analytic verbs, complexity, synthetic verbs, and simplification. For accessibility" (Prot. 2020BJKB9M), funded by the Italian Ministero dell'Università e della Ricerca.