Skip to content

Commit

Permalink
updated translator.py with implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
Tin Chi Lo committed Nov 15, 2024
1 parent 142c5d5 commit 2f06e18
Showing 1 changed file with 59 additions and 39 deletions.
98 changes: 59 additions & 39 deletions src/translator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,48 +10,68 @@
endpoint="https://translator-service-hunan-hunters.azurewebsites.net/"
)

def english_translation(post:str) -> str:
context = "Translate the following to English"
def get_language(post: str) -> str:
context = "The team is implementing a translation feature for NodeBB, a forum that allows instructors and students to make posts. Your task is to classify the language of inputed strings, returning the one-word name of the language in English. All inputs will be consistent in their language throughout, but they can be non-english or in english dialects."
response = client.chat.completions.create(
model = 'gpt-4o-mini',
messages = [
{
"role": "system",
"content": context
},
{
"role": "user",
"content": post
}
]
model="gpt-4o-mini", # This should match your deployment name in Azure
messages=[
{
"role": "system",
"content": context
},
{
"role": "user",
"content": post
}
]
)

translation = response.choices[0].message.content
return translation
return response.choices[0].message.content

def query_llm_robust(post: str) -> tuple[bool, str]:
'''
TODO: Implement this
'''
translation_context = "The team is implementing a translation feature for NodeBB, a forum that allows instructors and students to make posts. Your task is to translate the content of English and non-English posts into English. For inputs that are non-english strings, you will translate into college-level English. If inputs are in English, return the input but with spelling and grammar corrections."
translation_response = client.chat.completions.create(
model="gpt-4o-mini", # This should match your deployment name in Azure
messages=[
{
"role": "system",
"content": translation_context
},
{
"role": "user",
"content": post
}
]
)

def language(post:str) -> str:
context = "Determine if the text is written in English or not. Respond with 'True' or 'False' only."
response = client.chat.completions.create(
model='gpt-4o-mini',
messages=[
{
"role": "system",
"content": context
},
{
"role": "user",
"content": post
}
]
detection_context = "The team is implementing a translation feature for NodeBB, a forum that allows instructors and students to make posts. Your task is to classify the language of inputed strings, returning the one-word name of the language in English. All inputs will be consistent in their language throughout, but they can be non-english or in english dialects."
detection_response = client.chat.completions.create(
model="gpt-4o-mini", # This should match your deployment name in Azure
messages=[
{
"role": "system",
"content": detection_context
},
{
"role": "user",
"content": post
}
]
)
return response.choices[0].message.content

def translate_content(content:str) -> tuple[bool, str]:
is_english = language(content).strip()
try:
if is_english:
return (True, content)
else:
return (False, english_translation(content))
except Exception as e:
return (False, "error occured")
translation = translation_response.choices[0].message.content
language = detection_response.choices[0].message.content
is_english = language == "English"
translation_language = get_language(translation)

if language is None or len(language) == 0 or translation is None or len(translation) == 0:
return (False, "Sorry, we are unable to provide a translation at this moment. Please try again later!")
elif "i don't understand your request" in language.lower() or "i don't understand your request" in translation.lower():
return (False, "Sorry, we are unable to understand the post.")
elif translation_language is not None and translation_language != "English":
return (False, "Sorry, we are unable to provide an English Translation.")
else:
return (is_english, translation)

0 comments on commit 2f06e18

Please sign in to comment.