Update README.md
Browse files
README.md
CHANGED
|
@@ -3,4 +3,44 @@ license: mit
|
|
| 3 |
language:
|
| 4 |
- it
|
| 5 |
pipeline_tag: sentence-similarity
|
| 6 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3 |
language:
|
| 4 |
- it
|
| 5 |
pipeline_tag: sentence-similarity
|
| 6 |
+
---
|
| 7 |
+
|
| 8 |
+
## How to use
|
| 9 |
+
```python
|
| 10 |
+
from transformers import AutoTokenizer, AutoModel
|
| 11 |
+
import torch
|
| 12 |
+
from sklearn.metrics.pairwise import cosine_similarity
|
| 13 |
+
from sklearn.preprocessing import normalize
|
| 14 |
+
|
| 15 |
+
# Load model and tokenizer
|
| 16 |
+
model_name = "DeepMount00/Anita"
|
| 17 |
+
tokenizer = AutoTokenizer.from_pretrained(model_name)
|
| 18 |
+
model = AutoModel.from_pretrained(model_name)
|
| 19 |
+
|
| 20 |
+
# Prepare sentences
|
| 21 |
+
sentences = [
|
| 22 |
+
"Cosa faceva ogni sera Luca?",
|
| 23 |
+
"Un cane felice corre nel parco, la coda ondeggiante al vento. Ogni erba, ogni farfalla, un'avventura. Occhi scintillanti, lingua penzolante, esplora gioiosamente, amato e coccolato dal suo fedele compagno umano. Insieme, condividono un legame indissolubile, tessuto di corse, giochi e affetto incondizionato.",
|
| 24 |
+
"In un piccolo paesino circondato da colline verdeggianti e campi fioriti viveva una persona il cui sorriso era capace di illuminare la giornata più grigia. Questa persona, di nome Luca, aveva trovato la chiave della felicità nelle piccole gioie quotidiane: il profumo del caffè al mattino, il suono ridente dei bambini che giocavano in piazza, il tramonto che dipingeva il cielo di arancione e viola ogni sera."
|
| 25 |
+
]
|
| 26 |
+
|
| 27 |
+
# Tokenize, encode, and compute embeddings
|
| 28 |
+
embeddings = []
|
| 29 |
+
with torch.no_grad():
|
| 30 |
+
for sentence in sentences:
|
| 31 |
+
encoded_input = tokenizer(sentence, return_tensors='pt', padding=True, truncation=True, max_length=512)
|
| 32 |
+
embedding = model(**encoded_input).pooler_output
|
| 33 |
+
embeddings.append(embedding)
|
| 34 |
+
|
| 35 |
+
# Convert embeddings to numpy and normalize
|
| 36 |
+
embeddings = torch.cat(embeddings, dim=0).numpy()
|
| 37 |
+
embeddings_normalized = normalize(embeddings)
|
| 38 |
+
|
| 39 |
+
# Calculate cosine similarity
|
| 40 |
+
similarity_matrix = cosine_similarity(embeddings_normalized)
|
| 41 |
+
|
| 42 |
+
# Print similarity scores
|
| 43 |
+
print("Similarità tra la sentenza 1 e 2:", similarity_matrix[0, 1])
|
| 44 |
+
print("Similarità tra la sentenza 1 e 3:", similarity_matrix[0, 2])
|
| 45 |
+
print("Similarità tra la sentenza 2 e 3:", similarity_matrix[1, 2])
|
| 46 |
+
```
|