Update README.md
Browse files
README.md
CHANGED
|
@@ -7,4 +7,56 @@ language:
|
|
| 7 |
- en
|
| 8 |
base_model:
|
| 9 |
- google-t5/t5-base
|
| 10 |
-
---
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
- en
|
| 8 |
base_model:
|
| 9 |
- google-t5/t5-base
|
| 10 |
+
---
|
| 11 |
+
from transformers import T5Tokenizer, T5ForConditionalGeneration
|
| 12 |
+
|
| 13 |
+
# Load the pre-trained model and tokenizer
|
| 14 |
+
model_name = "SynapseQAI/T5-base-wmt14"
|
| 15 |
+
tokenizer = T5Tokenizer.from_pretrained(model_name)
|
| 16 |
+
model = T5ForConditionalGeneration.from_pretrained(model_name)
|
| 17 |
+
|
| 18 |
+
# Function to translate using a different generation strategy
|
| 19 |
+
def translate(sentence, strategy='beam_search'):
|
| 20 |
+
# Prepare the input for the model
|
| 21 |
+
input_text = f"translate French to English: {sentence}"
|
| 22 |
+
input_ids = tokenizer(input_text, return_tensors="pt").input_ids
|
| 23 |
+
|
| 24 |
+
# Choose generation strategy
|
| 25 |
+
if strategy == 'beam_search':
|
| 26 |
+
outputs = model.generate(input_ids, num_beams=5, max_length=50, early_stopping=True)
|
| 27 |
+
elif strategy == 'top_k_sampling':
|
| 28 |
+
outputs = model.generate(input_ids, do_sample=True, top_k=50, max_length=50)
|
| 29 |
+
elif strategy == 'top_p_sampling':
|
| 30 |
+
outputs = model.generate(input_ids, do_sample=True, top_p=0.92, max_length=50)
|
| 31 |
+
elif strategy == 'temperature_sampling':
|
| 32 |
+
outputs = model.generate(input_ids, do_sample=True, temperature=0.7, max_length=50)
|
| 33 |
+
else:
|
| 34 |
+
# Default to greedy decoding
|
| 35 |
+
outputs = model.generate(input_ids, max_length=50)
|
| 36 |
+
|
| 37 |
+
# Decode the generated translation
|
| 38 |
+
translation = tokenizer.decode(outputs[0], skip_special_tokens=True)
|
| 39 |
+
return translation
|
| 40 |
+
|
| 41 |
+
# French sentences from easy to advanced
|
| 42 |
+
sentences = [
|
| 43 |
+
"Il fait beau aujourd'hui.",
|
| 44 |
+
"J'aime lire des livres et regarder des films pendant mon temps libre.",
|
| 45 |
+
"Si j'avais su que tu venais, j'aurais préparé quelque chose de spécial pour le dîner.",
|
| 46 |
+
"Même si les avancées technologiques apportent de nombreux avantages, elles posent également des défis éthiques considérables qu'il nous faut relever."
|
| 47 |
+
]
|
| 48 |
+
|
| 49 |
+
# Translate each sentence with different strategies
|
| 50 |
+
for sentence in sentences:
|
| 51 |
+
translated_sentence = translate(sentence, strategy='beam_search') # You can try 'top_k_sampling', 'top_p_sampling', 'temperature_sampling'
|
| 52 |
+
print(f"French: {sentence}\nEnglish (Beam Search): {translated_sentence}\n")
|
| 53 |
+
|
| 54 |
+
translated_sentence = translate(sentence, strategy='top_k_sampling')
|
| 55 |
+
print(f"English (Top-k Sampling): {translated_sentence}\n")
|
| 56 |
+
|
| 57 |
+
translated_sentence = translate(sentence, strategy='top_p_sampling')
|
| 58 |
+
print(f"English (Top-p Sampling): {translated_sentence}\n")
|
| 59 |
+
|
| 60 |
+
translated_sentence = translate(sentence, strategy='temperature_sampling')
|
| 61 |
+
print(f"English (Temperature Sampling): {translated_sentence}\n")
|
| 62 |
+
|