m-polignano commited on
Commit
4880c2f
·
verified ·
1 Parent(s): 61b728e

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +115 -3
README.md CHANGED
@@ -35,7 +35,7 @@ wants to provide Italian NLP researchers with an improved model for the Italian
35
 
36
  <hr>
37
 
38
- **Colab Demo:** [A100 - 40GB - Colab Notebook](https://chat.llamantino.it/)<br>
39
  The Model run on a single GPU, 19.56GB of VRAM used during inference by using a 4bit Quantization.
40
 
41
  <hr>
@@ -54,10 +54,8 @@ The Model run on a single GPU, 19.56GB of VRAM used during inference by using a
54
  <hr>
55
 
56
  ## Playground
57
-
58
  To use the model directly, there are many ways to get started, choose one of the following ways to experience it.
59
 
60
-
61
  ### Prompt Template
62
 
63
  ```
@@ -68,7 +66,121 @@ Scrivi il tuo flusso di pensiero (monologo interiore) tra i tag <think></think>.
68
  Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
69
  Se necessario, usa la notazione markdown per formattare la risposta.[/SYSTEM_PROMPT][INST]{ USER Prompt }[/INST]<think>{ ASSIST Thinking }</think>{ ASSIST Prompt }</s>
70
  ```
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
  ## Citation instructions
73
  ```bibtex
74
  @misc{polignano2024advanced,
 
35
 
36
  <hr>
37
 
38
+ **Colab Demo:** [A100 - 40GB - Colab Notebook](https://colab.research.google.com/drive/1mhZLAdpOr3TRq-ZTG4XD52J98orHj_na?usp=sharing)<br>
39
  The Model run on a single GPU, 19.56GB of VRAM used during inference by using a 4bit Quantization.
40
 
41
  <hr>
 
54
  <hr>
55
 
56
  ## Playground
 
57
  To use the model directly, there are many ways to get started, choose one of the following ways to experience it.
58
 
 
59
  ### Prompt Template
60
 
61
  ```
 
66
  Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
67
  Se necessario, usa la notazione markdown per formattare la risposta.[/SYSTEM_PROMPT][INST]{ USER Prompt }[/INST]<think>{ ASSIST Thinking }</think>{ ASSIST Prompt }</s>
68
  ```
69
+ ### Transformers
70
+
71
+ For direct use with `transformers`, you can easily get started with the following steps.
72
+
73
+ - Firstly, you need to install transformers via the command below with `pip`.
74
+
75
+ ```bash
76
+ pip install -U --no-deps bitsandbytes accelerate xformers==0.0.29.post3 peft trl triton cut_cross_entropy unsloth_zoo
77
+ pip install sentencepiece protobuf "datasets>=3.4.1,<4.0.0" "huggingface_hub>=0.34.0" hf_transfer
78
+ ```
79
+
80
+ - Right now, you can start using the model directly.
81
+
82
+ ```python
83
+ from transformers import AutoModelForCausalLM, AutoTokenizer
84
+ import torch
85
+ from transformers import BitsAndBytesConfig
86
+
87
+
88
+ nf4_config = BitsAndBytesConfig(
89
+ load_in_4bit=True,
90
+ bnb_4bit_quant_type="nf4",
91
+ bnb_4bit_use_double_quant=True,
92
+ bnb_4bit_compute_dtype=torch.bfloat16
93
+ )
94
+
95
+ model_dir = "m-polignano/ANITA-NEXT-24B-Magistral-2506-ITA"
96
+ tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=True)
97
+ model = AutoModelForCausalLM.from_pretrained(
98
+ model_dir,
99
+ quantization_config=nf4_config,
100
+ device_map="auto",
101
+ torch_dtype=torch.bfloat16,
102
+ )
103
+
104
+ #Method 1
105
+ sys = '''Sei un an assistente AI per la lingua italiana di nome ANITA-NEXT (Advanced Natural-based interaction for the ITAlian language Next Generation) creato dal ricercatore Marco Polignano, Università degli Studi di Bari Aldo Moro, Italia. Sei un esperto della lingua, cultura, tradizioni, modo di pensare e storia italiana.
106
+
107
+ L'utente ti chiederà di risolvere un compito o rispondere ad una domanda. Rispondi e ragiona usando la lingua della domanda, preferendo l'Italiano.
108
+ Scrivi il tuo flusso di pensiero (monologo interiore) tra i tag <think></think>. Ragiona in modo disinvolto, scrivendo riflessioni e/o bozze, come se stessi lavorando a un esercizio su un foglio di carta.
109
+ Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
110
+ Se necessario, usa la notazione markdown per formattare la risposta.'''
111
+ messages = [
112
+ {"role" : "system", "content" : sys},
113
+ {"role" : "user", "content" : "Chi è Carlo Magno?"}
114
+ ]
115
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
116
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
117
+ for k,v in inputs.items():
118
+ inputs[k] = v.cuda()
119
+ outputs = model.generate(**inputs, max_new_tokens=32786, do_sample=True, top_p=0.9, temperature=0.7)
120
+ results = tokenizer.batch_decode(outputs)[0]
121
+ print(results)
122
 
123
+ #Method 2
124
+ from transformers import AutoModelForCausalLM, AutoTokenizer, TextIteratorStreamer
125
+ from threading import Thread
126
+ import torch # Import torch to use .cuda() if needed
127
+ sys = '''Sei un an assistente AI per la lingua italiana di nome ANITA-NEXT (Advanced Natural-based interaction for the ITAlian language Next Generation) creato dal ricercatore Marco Polignano, Università degli Studi di Bari Aldo Moro, Italia. Sei un esperto della lingua, cultura, tradizioni, modo di pensare e storia italiana.
128
+
129
+ L'utente ti chiederà di risolvere un compito o rispondere ad una domanda. Rispondi e ragiona usando la lingua della domanda, preferendo l'Italiano.
130
+ Scrivi il tuo flusso di pensiero (monologo interiore) tra i tag <think></think>. Ragiona in modo disinvolto, scrivendo riflessioni e/o bozze, come se stessi lavorando a un esercizio su un foglio di carta.
131
+ Successivamente, scrivi la soluzione in modo chiaro, corretto, semplice ed esaustivo basandoti sul riassunto del tuo flusso di pensiero.
132
+ Se necessario, usa la notazione markdown per formattare la risposta.'''
133
+ messages = [
134
+ #{"role" : "system", "content" : sys},
135
+ {"role" : "user", "content" : "Chi è Marco Polo?"}
136
+ ]
137
+ prompt = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
138
+ inputs = tokenizer(prompt, return_tensors="pt", add_special_tokens=False)
139
+
140
+ # Move inputs to CUDA if your model is on CUDA
141
+ for k,v in inputs.items():
142
+ inputs[k] = v.cuda()
143
+
144
+ # --- 4. Create a TextIteratorStreamer ---
145
+ # skip_prompt=True: This ensures that the streamer only yields the newly generated tokens,
146
+ # not the initial prompt you fed to the model.
147
+ # skip_special_tokens=True: This removes special tokens (like <s>, </s>, <pad>) from the output.
148
+ streamer = TextIteratorStreamer(tokenizer, skip_prompt=True, skip_special_tokens=True)
149
+
150
+ # --- 5. Define generation arguments, including the streamer ---
151
+ generation_kwargs = dict(
152
+ inputs,
153
+ streamer=streamer, # This is the key part for streaming!
154
+ max_new_tokens=32786,
155
+ do_sample=True,
156
+ top_p=0.9,
157
+ temperature=0.7,
158
+ # Add any other generation arguments you need
159
+ )
160
+
161
+ # --- 6. Run model.generate in a separate thread ---
162
+ # This is crucial because model.generate is a blocking call.
163
+ # By running it in a thread, your main script can simultaneously
164
+ # iterate over the streamer to get tokens as they are generated.
165
+ thread = Thread(target=model.generate, kwargs=generation_kwargs)
166
+ thread.start()
167
+
168
+ # --- 7. Iterate over the streamer to print tokens as they arrive ---
169
+ print("Generated text (streaming token by token):")
170
+ for new_text in streamer:
171
+ if "\\boxed" in new_text:
172
+ break
173
+ print(new_text, end="") # `end=""` prevents newlines between tokens
174
+ # You can also send 'new_text' to a web socket, a GUI, or any other output medium
175
+
176
+ # Optional: Wait for the thread to complete if you need to do something after generation
177
+ thread.join()
178
+
179
+ ```
180
+
181
+ <hr>
182
+
183
+
184
  ## Citation instructions
185
  ```bibtex
186
  @misc{polignano2024advanced,