Leonard Püttmann
commited on
Update README.md
Browse files
README.md
CHANGED
@@ -1,3 +1,72 @@
|
|
1 |
-
---
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
library_name: transformers
|
3 |
+
tags:
|
4 |
+
- seq2seq
|
5 |
+
license: apache-2.0
|
6 |
+
datasets:
|
7 |
+
- Helsinki-NLP/europarl
|
8 |
+
- Helsinki-NLP/opus-100
|
9 |
+
language:
|
10 |
+
- en
|
11 |
+
- it
|
12 |
+
base_model:
|
13 |
+
- google/t5-efficient-tiny
|
14 |
+
pipeline_tag: translation
|
15 |
+
metrics:
|
16 |
+
- bleu
|
17 |
+
---
|
18 |
+
|
19 |
+
## 🍃 Foglietta - A small model for Italian -> English translation
|
20 |
+
|
21 |
+
Foglietta is an encoder-decoder transformer model for English-Italian text translation based on `bigscience/mt0-small`. It was trained on the `en-it` section of `Helsinki-NLP/opus-100` and `Helsinki-NLP/europarl`.
|
22 |
+
|
23 |
+
Be advised: As the model is really small, it will make errors.
|
24 |
+
|
25 |
+
## Usage
|
26 |
+
|
27 |
+
```python
|
28 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
29 |
+
|
30 |
+
# Load model and tokenizer from checkpoint directory
|
31 |
+
tokenizer = AutoTokenizer.from_pretrained("LeonardPuettmann/Foglietta-mt-it-en")
|
32 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Foglietta-mt-it-en")
|
33 |
+
|
34 |
+
def generate_response(input_text):
|
35 |
+
input_ids = tokenizer("translate Italian to English:" + input_text, return_tensors="pt").input_ids
|
36 |
+
output = model.generate(input_ids, max_new_tokens=256)
|
37 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
38 |
+
|
39 |
+
text_to_translate = "Vorrei una tazza di tè nero, per favore."
|
40 |
+
response = generate_response(text_to_translate)
|
41 |
+
print(response)
|
42 |
+
```
|
43 |
+
As this model is trained on translating sentence pairs, it is best to split longer text into individual sentences, ideally using SpaCy. You can then translate the sentences and join the translations at the end like this:
|
44 |
+
```python
|
45 |
+
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
|
46 |
+
import spacy
|
47 |
+
# First, install spaCy and the Italian language model if you haven't already
|
48 |
+
# !pip install spacy
|
49 |
+
# !python -m spacy download it_core_news_sm
|
50 |
+
|
51 |
+
nlp = spacy.load("it_core_news_sm")
|
52 |
+
|
53 |
+
tokenizer = AutoTokenizer.from_pretrained("LeonardPuettmann/Foglietta-mt-it-en")
|
54 |
+
model = AutoModelForSeq2SeqLM.from_pretrained("LeonardPuettmann/Foglietta-mt-it-en")
|
55 |
+
|
56 |
+
def generate_response(input_text):
|
57 |
+
input_ids = tokenizer("translate Italian to English: " + input_text, return_tensors="pt").input_ids
|
58 |
+
output = model.generate(input_ids, max_new_tokens=256)
|
59 |
+
return tokenizer.decode(output[0], skip_special_tokens=True)
|
60 |
+
|
61 |
+
text = "Ciao, come stai? Oggi è una bella giornata. Spero che tu stia bene."
|
62 |
+
doc = nlp(text)
|
63 |
+
sentences = [sent.text for sent in doc.sents]
|
64 |
+
|
65 |
+
sentence_translations = []
|
66 |
+
for i, sentence in enumerate(sentences):
|
67 |
+
sentence_translation = generate_response(sentence)
|
68 |
+
sentence_translations.append(sentence_translation)
|
69 |
+
|
70 |
+
full_translation = " ".join(sentence_translations)
|
71 |
+
print(full_translation)
|
72 |
+
```
|