Skip to contents

Translate text from one language to another using a translation model via the Hugging Face Inference Providers API.

Usage

hf_translate(
  text,
  model = hf_default_model("translate"),
  source = NULL,
  target = NULL,
  token = NULL,
  endpoint_url = NULL,
  ...
)

Arguments

text

Character vector of text(s) to translate.

model

Character string. Model ID from the Hugging Face Hub. Append `":provider"` to select an inference provider. Default: "Helsinki-NLP/opus-mt-en-fr" (English to French).

source

Character string or NULL. Source language code (model-specific; ignored by `opus-mt-*` language-pair models).

target

Character string or NULL. Target language code (model-specific; ignored by `opus-mt-*` language-pair models).

token

Character string or NULL. API token for authentication.

endpoint_url

Character string or NULL. A custom Inference Endpoint URL.

...

Additional arguments (currently unused).

Value

A tibble with columns: text, translation.

Details

The default model, `Helsinki-NLP/opus-mt-en-fr`, translates English to French and is chosen for easy onboarding: it is small, fast, broadly known, and encodes the translation direction in the model ID, so `hf_translate("Hello")` works with no extra arguments. To translate a different language pair, swap in another Helsinki-NLP `opus-mt-*` model (for example `"Helsinki-NLP/opus-mt-en-es"` for English to Spanish).

Translation models vary in how they expect languages to be specified. Language-pair models such as the Helsinki-NLP `opus-mt-*` family encode the direction in the model ID and ignore `source`/`target`. Multilingual models such as NLLB (`facebook/nllb-200-distilled-600M`) instead require `source` and `target` to be set to FLORES-200 codes (for example "eng_Latn", "fra_Latn").

Examples

if (FALSE) { # \dontrun{
# Simplest call: English to French with the default model
hf_translate("Hello, how are you?")

# A different language pair (English to Spanish)
hf_translate("Hello, how are you?", model = "Helsinki-NLP/opus-mt-en-es")

# Multilingual model (FLORES-200 codes)
hf_translate(
  "Hello, how are you?",
  model = "facebook/nllb-200-distilled-600M",
  source = "eng_Latn",
  target = "fra_Latn"
)
} # }