French to English Text Translation with Transformers
This code allows you to translate French text into English using the ybanas/autotrain-fr-en-translate-51410121895 model from the Transformers library. To use this code, follow the steps below:
from transformers import AutoTokenizer, AutoModelForSeq2SeqLM
# Load the tokenizer and the model
tokenizer = AutoTokenizer.from_pretrained("ybanas/autotrain-fr-en-translate-51410121895")
model = AutoModelForSeq2SeqLM.from_pretrained("ybanas/autotrain-fr-en-translate-51410121895")
def translate_text(french_text: str) -> str:
"""
Translate French text to English using the ybanas/autotrain-fr-en-translate-51410121895 model.
Args:
french_text (str): French text to translate.
Returns:
str: Translated English text.
"""
# Tokenize the French text
inputs = tokenizer(french_text, return_tensors="pt", padding=True, truncation=True)
# Generate the English translation
outputs = model.generate(**inputs)
# Decode the English translation
english_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
return english_text
if __name__ == "__main__":
french_text = "Les enfants aiment profiter des beaux jours"
english_text = translate_text(french_text)
print("French text:", french_text)
print("Translated English text:", english_text)
Usage
- Install the Transformers library by running
pip install transformers. - Copy the code above into a
.pyfile, for exampletranslation.py. - Replace the value of the
french_textvariable with the French text you want to translate. - Run the script with
python translation.py. The translated English text will be displayed on the screen.
This script uses the ybanas/autotrain-fr-en-translate-51410121895 model to translate French text into English. The model is loaded using the AutoTokenizer and AutoModelForSeq2SeqLM classes from the Transformers library. The translate_text function takes a French text as input and returns its translation in English.
Model Trained Using AutoTrain
- Problem type: Translation
- Model ID: 51410121895
- CO2 Emissions (in grams): 86.9058
Validation Metrics
- Loss: 1.455
- SacreBLEU: 15.999
- Gen len: 15.299