from transformers import MarianMTModel, MarianTokenizer

# Téléchargement du modèle et du tokenizer pour la traduction de l'anglais vers le français
modele_marian = MarianMTModel.from_pretrained("Helsinki-NLP/opus-mt-en-fr")
tokenizer_marian = MarianTokenizer.from_pretrained("Helsinki-NLP/opus-mt-en-fr")

# Texte en anglais
texte_anglais = '''
The subject of this eulogy was one of the most remarkable men in the annals of British science and literature—according to Prof. Tyndall, the greatest man of science that had appeared since Newton; and, as his biography has never been republished in this country, a brief sketch of his life will be fresh and instructive to many. Thomas Young was born in 1773, and died in 1829. He was the eldest son of ten children. His parents were both members of the Society of Friends, and strict observers of the principles of their sect, in which their children were carefully educated. Dr. Dalton, the eminent English chemist, was also of Quaker parentage and education; but, while he continued through life to retain his membership of the denomination and to conform to its principles, Dr. Young held the tenets and conformed to the observances of the Society only during his youth. He was a very precocious child. At two years of age he could read with fluency, and had read the Bible twice through before he was four years old. At six years of age he could repeat Goldsmith's "Deserted Village," and had previously begun his Latin grammar. At seven years of age he was sent to a miserable boarding-school, but the next year at the house of a friend he came across a "Dictionary of Arts and Sciences," which he perused with intense interest, and also got instruction in the use of some mathematical and philosophical instruments. When nine years old he was sent to another school, where he remained four years, and made great proficiency in classics, mathematics, and natural philosophy. He also learned the principles of drawing, the art of book-binding, the construction of microscopes, telescopes, and electrical machines, and the use of the lathe.
'''


# Tokenisation du texte anglais
tokens_anglais = tokenizer_marian.encode(texte_anglais, return_tensors="pt")

# Traduction avec le modèle
traduction_ids = modele_marian.generate(
    tokens_anglais,
    max_length=10000,
    num_beams=1,  # Réduire le nombre de faisceaux
    no_repeat_ngram_size=2,
    top_k=50,
    top_p=0.95,
    temperature=1.0,  # Augmenter la température
)

# Décodage de la traduction
texte_francais = tokenizer_marian.decode(traduction_ids[0], skip_special_tokens=True)

print("Texte source en anglais : ", texte_anglais)
print("Texte traduit en français : ", texte_francais)