Skip to content

Commit

Permalink
Add LLM translator service
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-elzeni committed Nov 17, 2024
1 parent 7346aa8 commit 11a49b0
Showing 1 changed file with 48 additions and 33 deletions.
81 changes: 48 additions & 33 deletions src/translator.py
Original file line number Diff line number Diff line change
@@ -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: <language>, Translation: <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, "")

0 comments on commit 11a49b0

Please sign in to comment.