Translate text between languages

Automatically translate content into multiple languages using LLMs.

Text · Notebook · Updated

Colab runs it in your browser. If it calls a model provider, it asks for that provider's API key in a cell.

How it works

You add computed columns for each target language. Translations are generated automatically when you insert new content and cached for future queries.

What it covers

  • Translate text using OpenAI models
  • Create multiple language columns from one source
  • Handle batch translation efficiently

What it uses

Install
terminal
pip install pixeltable openai
Environment variables it names
OPENAI_API_KEY
Pixeltable calls
openai.chat_completions()pxt.create_table()add_computed_column()insert()

A look at the code

text-translate.ipynb
# Add Spanish translation column
spanish_prompt = (
'Translate the following text to Spanish. Return only the translation, no explanations:\n\n'
+ content.text_en
)
content.add_computed_column(
response_es=chat_completions(
messages=[{'role': 'user', 'content': spanish_prompt}],
model='gpt-4o-mini',
)
)
content.add_computed_column(
text_es=content.response_es.choices[0].message.content
)

One cell of the notebook, copied as it is. Open it in Colab for the rest.

Browse all 103