Upload translator.py
Browse files- translator.py +18 -0
translator.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import pipeline
|
| 2 |
+
|
| 3 |
+
# Load the translation pipeline for English to French
|
| 4 |
+
translator = pipeline("translation_en_to_fr")
|
| 5 |
+
|
| 6 |
+
# Define the English text you want to translate
|
| 7 |
+
english_texts = [
|
| 8 |
+
"Hello, how are you?",
|
| 9 |
+
"I love learning new languages!",
|
| 10 |
+
"Transformers make NLP tasks so easy."
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
# Translate each sentence
|
| 14 |
+
for text in english_texts:
|
| 15 |
+
translated = translator(text)[0]['translation_text']
|
| 16 |
+
print(f"English: {text}")
|
| 17 |
+
print(f"French: {translated}\n")
|
| 18 |
+
|