2. Translate to multiple languages
This notebook is optimized to run in Google Colab.
!pip install fast-dash jupyter_dash
# 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"
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')}"}
@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