File size: 1,655 Bytes
e67043b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
import os
import random
import requests
import hashlib
from ...tool import Tool


def build_tool(config) -> Tool:
    tool = Tool(
        "Translator Info",
        "Translate a given text from one language to another.",
        name_for_model="Translator",
        description_for_model="Plugin for translating text from one language to another.",
        logo_url="https://your-app-url.com/.well-known/logo.png",
        contact_email="[email protected]",
        legal_info_url="[email protected]",
    )
    subscription_key = os.getenv("BAIDU_TRANSLATE_KEY", None)
    if subscription_key is None:
        raise Exception("BAIDU_TRANSLATE_KEY is not set")
    secret_key = os.getenv("BAIDU_SECRET_KEY", None)
    if secret_key is None:
        raise Exception("BAIDU_SECRET_KEY is not set")
    endpoint = "https://fanyi-api.baidu.com/api/trans/vip/translate"
    fromLang = "auto"
    salt = random.randint(32768, 65536)
    header = {"Content-Type": "application/x-www-form-urlencoded"}

    @tool.get("/get_translation")
    def get_translation(text: str, tgt_lang: str) -> str:
        sign = subscription_key + text + str(salt) + secret_key
        md = hashlib.md5()
        md.update(sign.encode(encoding="utf-8"))
        sign = md.hexdigest()
        data = {
            "appid": subscription_key,
            "q": text,
            "from": fromLang,
            "to": tgt_lang,
            "salt": salt,
            "sign": sign,
        }
        response = requests.post(endpoint, params=data, headers=header)
        text = response.json()
        results = text["trans_result"][0]["dst"]
        return results

    return tool