Update README.md
Browse files
README.md
CHANGED
|
@@ -79,6 +79,52 @@ Addestrato su **Mattimax/DATA-AI_Conversation_ITA**, un dataset italiano di dial
|
|
| 79 |
|
| 80 |
---
|
| 81 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 82 |
## Referenze
|
| 83 |
|
| 84 |
* Dataset: [Mattimax/DATA-AI_Conversation_ITA](https://huggingface.co/datasets/Mattimax/DATA-AI_Conversation_ITA)
|
|
|
|
| 79 |
|
| 80 |
---
|
| 81 |
|
| 82 |
+
## Codice per inferenza di esempio
|
| 83 |
+
|
| 84 |
+
```python
|
| 85 |
+
from transformers import AutoTokenizer, AutoModelForCausalLM
|
| 86 |
+
import torch
|
| 87 |
+
|
| 88 |
+
# 1. Carica modello e tokenizer addestrati
|
| 89 |
+
model_path = "Mattimax/DACMini-IT"
|
| 90 |
+
tokenizer = AutoTokenizer.from_pretrained(model_path)
|
| 91 |
+
model = AutoModelForCausalLM.from_pretrained(model_path)
|
| 92 |
+
model.eval()
|
| 93 |
+
|
| 94 |
+
# 2. Funzione di generazione
|
| 95 |
+
def chat_inference(prompt, max_new_tokens=150, temperature=0.7, top_p=0.9):
|
| 96 |
+
# Costruisci input nel formato usato in training
|
| 97 |
+
formatted_prompt = f"<|user|> {prompt.strip()} <|assistant|>"
|
| 98 |
+
|
| 99 |
+
# Tokenizza
|
| 100 |
+
inputs = tokenizer(formatted_prompt, return_tensors="pt")
|
| 101 |
+
|
| 102 |
+
# Genera risposta
|
| 103 |
+
with torch.no_grad():
|
| 104 |
+
output = model.generate(
|
| 105 |
+
**inputs,
|
| 106 |
+
max_new_tokens=max_new_tokens,
|
| 107 |
+
temperature=temperature,
|
| 108 |
+
top_p=top_p,
|
| 109 |
+
do_sample=True,
|
| 110 |
+
pad_token_id=tokenizer.pad_token_id or tokenizer.eos_token_id
|
| 111 |
+
)
|
| 112 |
+
|
| 113 |
+
# Decodifica e rimuovi prompt iniziale
|
| 114 |
+
generated_text = tokenizer.decode(output[0], skip_special_tokens=True)
|
| 115 |
+
response = generated_text.split("<|assistant|>")[-1].strip()
|
| 116 |
+
return response
|
| 117 |
+
|
| 118 |
+
# 3. Esempio d’uso
|
| 119 |
+
if __name__ == "__main__":
|
| 120 |
+
while True:
|
| 121 |
+
user_input = input("👤 Utente: ")
|
| 122 |
+
if user_input.lower() in ["exit", "quit"]:
|
| 123 |
+
break
|
| 124 |
+
response = chat_inference(user_input)
|
| 125 |
+
print(f"🤖 Assistant: {response}\n")
|
| 126 |
+
````
|
| 127 |
+
|
| 128 |
## Referenze
|
| 129 |
|
| 130 |
* Dataset: [Mattimax/DATA-AI_Conversation_ITA](https://huggingface.co/datasets/Mattimax/DATA-AI_Conversation_ITA)
|