Last active
April 12, 2021 11:43
-
-
Save kristjan-eljand/ca54faa269e9b2685480eee77badb443 to your computer and use it in GitHub Desktop.
Translate from Estonian to English by using Huggingface's Inference API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import requests | |
# We use "Helsinki-NLP/opus-mt-et-en" model for translation | |
API_URL = "https://api-inference.huggingface.co/models/Helsinki-NLP/opus-mt-et-en" | |
# Register an account in Hugging Face to get your API_TOKEN | |
# you'll find it under settings | |
headers = {"Authorization": f"Bearer {API_TOKEN}"} | |
# Function to run the post request to the API | |
def translate(payload): | |
data = json.dumps(payload) | |
response = requests.request("POST", API_URL, headers=headers, data=data) | |
return json.loads(response.content.decode("utf-8")) | |
# Estonian sentence that we would like to translate to English | |
text_to_translate = """ | |
E-Lab on Enefit'i infotehnoloogia osakonna uurimis- ja arendusüksus. | |
""" | |
# Run the query, by proding the text as an output | |
data = translate({"inputs": text_to_translate,}) | |
# Extract and print the translated text | |
translated_text = data[0]['translation_text'] | |
print(translated_text) | |
} | |
) | |
# Output: | |
# E-Lab is a research and development unit of Enefit's Information Technology Department. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment