In our interconnected global society, the need for seamless communication across diverse linguistic landscapes has never been more crucial. Language translation emerges as an indispensable tool in bridging these gaps, and one of the most widely recognized solutions in this realm is Google Translate.
Thanks to advancements in artificial intelligence (AI), the process of language translation has undergone a paradigm shift. AI algorithms now possess the capability to discern the input language and swiftly translate it into the desired target language with remarkable accuracy and efficiency. This technological marvel not only simplifies the translation process but also enhances our ability to engage with individuals and communities worldwide, fostering greater understanding and collaboration across boundaries.
In this article, I demonstrate the process of language translation to English using OpenAI APIs through a straightforward Python script. The script provided below allows users to input text in any language and seamlessly translates it into English. If you wish to translate to a different language, change the prompt on line 13
from openai import OpenAI | |
# Read OpenAI API Key from environment variable | |
api_key = os.environ.get("OPENAI_API_KEY") | |
client = OpenAI(api_key=api_key) | |
def translate_text(text): | |
model = "gpt-4-turbo-preview", | |
response = client.chat.completions.create( | |
model="gpt-3.5-turbo-0125", | |
messages=[ | |
{"role": "user", "content": f"Translate the following text to English: {text}"} | |
] | |
) | |
return response.choices[0].message.content |