2. Translate to multiple languages
This notebook is optimized to run in Google Colab.
In [ ]:
Copied!
!pip install fast-dash jupyter_dash
!pip install fast-dash jupyter_dash
In [1]:
Copied!
# This demo uses Hugging Face API inference. To use this, get your token from https://huggingface.co/settings/tokens and paste it below.
import os
os.environ["HF_TOKEN"] = "hf_xxxxxxxxxxxxxxxxxxxxxxxxxxx"
# This demo uses Hugging Face API inference. To use this, get your token from https://huggingface.co/settings/tokens and paste it below.
import os
os.environ["HF_TOKEN"] = "hf_xxxxxxxxxxxxxxxxxxxxxxxxxxx"
In [2]:
Copied!
from fast_dash import fastdash
import requests
API_URL = "https://api-inference.huggingface.co/models/t5-base"
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
from fast_dash import fastdash
import requests
API_URL = "https://api-inference.huggingface.co/models/t5-base"
headers = {"Authorization": f"Bearer {os.environ.get('HF_TOKEN')}"}
In [4]:
Copied!
@fastdash(mode='inline', port=5000)
def translate_to_multiple_languages(text: str = "Translate this text to another language", language: str = ["German", "French", "Romanian"]):
"Uses the T5 Base model from Hugging Face. May give incorrect results."
payload = {"inputs": f"translate English to {language}: {text}"}
response = requests.post(API_URL, headers=headers, json=payload)
translation = response.json()[0]['translation_text'] if 'translation_text' in response.json()[0] else str(response.json())
return translation
@fastdash(mode='inline', port=5000)
def translate_to_multiple_languages(text: str = "Translate this text to another language", language: str = ["German", "French", "Romanian"]):
"Uses the T5 Base model from Hugging Face. May give incorrect results."
payload = {"inputs": f"translate English to {language}: {text}"}
response = requests.post(API_URL, headers=headers, json=payload)
translation = response.json()[0]['translation_text'] if 'translation_text' in response.json()[0] else str(response.json())
return translation