From 11a49b0a04cb955fb443587c031a7b3956ee0b44 Mon Sep 17 00:00:00 2001 From: Mohamed Elzeni Date: Sun, 17 Nov 2024 11:39:07 +0300 Subject: [PATCH] Add LLM translator service --- src/translator.py | 81 ++++++++++++++++++++++++++++------------------- 1 file changed, 48 insertions(+), 33 deletions(-) diff --git a/src/translator.py b/src/translator.py index dc33953..e06945b 100644 --- a/src/translator.py +++ b/src/translator.py @@ -1,34 +1,49 @@ +import openai +import time +import os + +openai.api_key = os.environ.get("OPENAI_API_KEY") +openai.api_base = "https://elzeni-recitation-resource.openai.azure.com/" +openai.api_type = "azure" +openai.api_version = "2024-08-01-preview" +deployment_name = "elzeni-gpt-4" + + def translate_content(content: str) -> tuple[bool, str]: - if content == "这是一条中文消息": - return False, "This is a Chinese message" - if content == "Ceci est un message en français": - return False, "This is a French message" - if content == "Esta es un mensaje en español": - return False, "This is a Spanish message" - if content == "Esta é uma mensagem em português": - return False, "This is a Portuguese message" - if content == "これは日本語のメッセージです": - return False, "This is a Japanese message" - if content == "이것은 한국어 메시지입니다": - return False, "This is a Korean message" - if content == "Dies ist eine Nachricht auf Deutsch": - return False, "This is a German message" - if content == "Questo è un messaggio in italiano": - return False, "This is an Italian message" - if content == "Это сообщение на русском": - return False, "This is a Russian message" - if content == "هذه رسالة باللغة العربية": - return False, "This is an Arabic message" - if content == "यह हिंदी में संदेश है": - return False, "This is a Hindi message" - if content == "นี่คือข้อความภาษาไทย": - return False, "This is a Thai message" - if content == "Bu bir Türkçe mesajdır": - return False, "This is a Turkish message" - if content == "Đây là một tin nhắn bằng tiếng Việt": - return False, "This is a Vietnamese message" - if content == "Esto es un mensaje en catalán": - return False, "This is a Catalan message" - if content == "This is an English message": - return True, "This is an English message" - return True, content + context = """ + You are a helpful assistant. Your task is to: + 1. Identify the language of the following text. + 2. Translate the text to English if it is not in English. + 3. If the text is already in English, output the text as it is. + 4. If the text is unintelligible or malformed, output only an empty string. + Provide your response in the format: "Language: , Translation: " + """ + + retry_delay = 9 + + while True: + try: + # Call the OpenAI API + response = openai.ChatCompletion.create( + engine=deployment_name, + messages=[ + {"role": "system", "content": context}, + {"role": "user", "content": content}, + ], + ) + + # Extract and process the response + response_text = response.choices[0].message.content.strip() + language, translation = response_text.split(", Translation: ") + + # Determine if the original post was in English + is_english = language.lower() == "language: english" + + return (is_english, translation) + + except openai.error.RateLimitError: + time.sleep(retry_delay) + + except Exception as e: + print(f"An error occurred: {e}") + return (False, "")