File size: 1,771 Bytes
ea99abb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
from llms import LLM
from utils.remote_client import execute_remote_task

def text_translation(text: str, model: str, src_lang: str, tgt_lang: str, custom_instructions: str = "", use_llm: bool = True) -> str:
    """
    Translate the input text using either LLM or traditional (Modal API) method.
    """
    if not text.strip():
        return ""
    if use_llm:
        return _translation_with_llm(text, model, src_lang, tgt_lang, custom_instructions)
    else:
        return _translation_with_traditional(text, model, src_lang, tgt_lang)

def _translation_with_llm(text: str, model: str, src_lang: str, tgt_lang: str, custom_instructions: str = "") -> str:
    try:
        llm = LLM(model=model)
        prompt = (
            f"Translate the following text from {src_lang} to {tgt_lang}.\n" +
            (f"{custom_instructions}\n" if custom_instructions else "") +
            f"Text: {text}\nTranslation:"
        )
        translation = llm.generate(prompt)
        return translation.strip()
    except Exception as e:
        print(f"Error in LLM translation: {str(e)}")
        return "Oops! Something went wrong. Please try again later."

def _translation_with_traditional(text: str, model: str, src_lang: str, tgt_lang: str) -> str:
    try:
        payload = {
            "text": text,
            "model": model,
            "src_lang": src_lang,
            "tgt_lang": tgt_lang,
        }
        resp = execute_remote_task("translation", payload)
        if "error" in resp:
            return "Oops! Something went wrong. Please try again later."
        return resp.get("translation", "")
    except Exception as e:
        print(f"Error in traditional translation: {str(e)}")
        return "Oops! Something went wrong. Please try again later."